package org.wisdom.framework.csrf;

import org.junit.After;
import org.junit.Test;
import org.mockito.Matchers;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.dom.Element;
import org.thymeleaf.dom.Node;
import org.thymeleaf.processor.IProcessor;
import org.wisdom.api.configuration.Configuration;
import org.wisdom.api.crypto.Crypto;
import org.wisdom.api.crypto.Hash;
import org.wisdom.api.http.Context;
import org.wisdom.api.templates.TemplateEngine;
import org.wisdom.crypto.CryptoServiceSingleton;
import org.wisdom.framework.csrf.unit.CSRFServiceImplTest;
import org.wisdom.test.parents.FakeContext;

import java.util.Dictionary;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ThymeleafCsrfDialectTest {

    @After
    public void tearDown() {
        Context.CONTEXT.remove();
    }

    @Test
    public void testThatServiceIsRegisteredOnTheArrivalOfThymeleaf() {
        ThymeleafCsrfDialect component = new ThymeleafCsrfDialect();
        component.csrf = new CSRFServiceImpl();
        ((CSRFServiceImpl) component.csrf).crypto = new CryptoServiceSingleton(CSRFServiceImplTest.SECRET, Hash.MD5,
                128, Crypto.AES_CBC_ALGORITHM, 20);
        component.context = mock(BundleContext.class);
        when(component.context.registerService(any(Class.class), any(IDialect.class), Matchers.<Dictionary<String, Object>>any()))
                .thenReturn(mock(ServiceRegistration.class));

        assertThat(component.reg).isNull();

        TemplateEngine engine = mock(TemplateEngine.class);
        when(engine.name()).thenReturn("thymeleaf");
        component.bindTemplateEngine(engine);

        assertThat(component.reg).isNotNull();

        component.unbindTemplateEngine(engine);
        assertThat(component.reg).isNull();
    }

    @Test
    public void testTheCreatedProcessorWhenNoTokenCreated() {
        ThymeleafCsrfDialect component = new ThymeleafCsrfDialect();
        component.csrf = new CSRFServiceImpl();
        ((CSRFServiceImpl) component.csrf).crypto = new CryptoServiceSingleton(CSRFServiceImplTest.SECRET, Hash.MD5,
                128, Crypto.AES_CBC_ALGORITHM, 20);
        final Configuration configuration = mock(Configuration.class);
        when(configuration.getWithDefault("token.name", "csrfToken")).thenReturn("csrfToken");
        ((CSRFServiceImpl) component.csrf).configuration = configuration;

        FakeContext ctxt = new FakeContext();
        Context.CONTEXT.set(ctxt);

        IDialect dialect = component.createDialect();
        assertThat(dialect.getPrefix()).isEqualToIgnoringCase("csrf");
        assertThat(dialect.getProcessors()).hasSize(1);

        IProcessor processor = dialect.getProcessors().iterator().next();
        assertThat(processor).isInstanceOf(ThymeleafCsrfDialect.CSRFElementProcessor.class);

        List<Node> nodes = ((ThymeleafCsrfDialect.CSRFElementProcessor) processor).getMarkupSubstitutes(null, null);
        assertThat(nodes).hasSize(1);
        assertThat(nodes.get(0)).isInstanceOf(Element.class);
        Element element = (Element) nodes.get(0);
        assertThat(element.getNormalizedName()).isEqualTo("input");
        assertThat(element.getAttributeFromNormalizedName("type").getValue()).isEqualTo("hidden");
        assertThat(element.getAttributeFromNormalizedName("name").getValue()).isEqualTo("csrfToken");
        assertThat(element.getAttributeFromNormalizedName("value").getValue()).isEqualTo("invalid");
    }

    @Test
    public void testTheCreatedProcessorWithToken() {
        ThymeleafCsrfDialect component = new ThymeleafCsrfDialect();
        component.csrf = new CSRFServiceImpl();
        ((CSRFServiceImpl) component.csrf).crypto = new CryptoServiceSingleton(CSRFServiceImplTest.SECRET, Hash.MD5,
                128, Crypto.AES_CBC_ALGORITHM, 20);
        final Configuration configuration = mock(Configuration.class);
        when(configuration.getWithDefault("token.name", "csrfToken")).thenReturn("csrfToken");
        ((CSRFServiceImpl) component.csrf).configuration = configuration;

        FakeContext ctxt = new FakeContext();
        ctxt.getFakeRequest().data().put(CSRFServiceImpl.TOKEN_KEY, "token");
        Context.CONTEXT.set(ctxt);

        IDialect dialect = component.createDialect();
        assertThat(dialect.getPrefix()).isEqualToIgnoringCase("csrf");
        assertThat(dialect.getProcessors()).hasSize(1);

        IProcessor processor = dialect.getProcessors().iterator().next();
        assertThat(processor).isInstanceOf(ThymeleafCsrfDialect.CSRFElementProcessor.class);

        List<Node> nodes = ((ThymeleafCsrfDialect.CSRFElementProcessor) processor).getMarkupSubstitutes(null, null);
        assertThat(nodes).hasSize(1);
        assertThat(nodes.get(0)).isInstanceOf(Element.class);
        Element element = (Element) nodes.get(0);
        assertThat(element.getNormalizedName()).isEqualTo("input");
        assertThat(element.getAttributeFromNormalizedName("type").getValue()).isEqualTo("hidden");
        assertThat(element.getAttributeFromNormalizedName("name").getValue()).isEqualTo("csrfToken");
        assertThat(element.getAttributeFromNormalizedName("value").getValue()).isEqualTo("token");
    }

}
@@ -1,8 +1,665 @@
+/*%0A * #%25L%0A * Wisdom-Framework%0A * %25%25%0A * Copyright (C) 2013 - 2015 Wisdom Framework%0A * %25%25%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A * %0A *      http://www.apache.org/licenses/LICENSE-2.0%0A * %0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A * #L%25%0A */%0A
 package 

Add missing license header.