package ch.difty.sipamato;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SipamatoApplication.class)
@WebAppConfiguration
@DirtiesContext
public class SipamatoHomePageTest {

    private WicketTester tester;

    @Autowired
    private SipamatoApplication application;

    @Before
    public void setUp() {
        tester = new WicketTester(application);
    }

    @Test
    public void testRenderMyPage() {
        tester.startPage(SipamatoHomePage.class);
        tester.assertRenderedPage(SipamatoHomePage.class);
        tester.assertComponent("message", Label.class);
    }
}
package ch.difty.sipamato;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.test.ApplicationContextMock;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.util.ReflectionTestUtils;

import com.giffing.wicket.spring.boot.starter.configuration.extensions.external.spring.security.SecureWebSession;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SipamatoApplication.class)
@WebAppConfiguration
@DirtiesContext
public class SipamatoHomePageTest {

    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin";

    private WicketTester tester;

    @Autowired
    private SipamatoApplication application;

    private ApplicationContextMock applicationContextMock;

    @Before
    public void setUp() {
        applicationContextMock = new ApplicationContextMock();
        applicationContextMock.putBean("authenticationManager", new AuthenticationManager() {

            @Override
            public Authentication authenticate(Authentication arg0) throws AuthenticationException {
                return new TestingAuthenticationToken(USERNAME, PASSWORD, "USER", "ADMIN");
            }
        });
        ReflectionTestUtils.setField(application, "applicationContext", applicationContextMock);
        tester = new WicketTester(application);
        login(USERNAME, PASSWORD);
        tester = new WicketTester(application);
    }

    private void login(String username, String password) {
        SecureWebSession session = (SecureWebSession) tester.getSession();
        session.signOut();
        tester.startPage(LoginPage.class);
        FormTester formTester = tester.newFormTester("loginForm");
        formTester.setValue("username", username);
        formTester.setValue("password", password);
        formTester.submit();
        tester.assertNoErrorMessage();
        tester.assertRenderedPage(SipamatoHomePage.class);
    }

    public WicketTester getTester() {
        return tester;
    }

    public WebApplication getApplication() {
        return application;
    }

    public ApplicationContextMock getApplicationContextMock() {
        return applicationContextMock;
    }

    @Test
    public void testRenderMyPage() {
        tester.startPage(SipamatoHomePage.class);
        tester.assertRenderedPage(SipamatoHomePage.class);
        tester.assertComponent("message", Label.class);
    }
}
fix security test