4

I have a web app which use Wicket Auth/Roles to login user and assign roles. (http://wicket.apache.org/learn/projects/authroles.html)

You can refer to this example too: http://www.wicket-library.com/wicket-examples-6.0.x/authentication3/

I have many web pages and I want to login in my application before testing my pages. My test page extends WebAppTestBase.

Here is my code for WebAppTestBase:

public class WebAppTestBase {

  protected WicketTester wicketTester;

  private MyAuthenticatedWebApplication myAuthenticatedWebApplication = new MyAuthenticatedWebApplication();

  @Before
  public void setUp() {
    wicketTester = new WicketTester(myAuthenticatedWebApplication);

  }

  @After
  public void tearDown() {
    wicketTester.destroy();
  }
}

So how I could setUp AuthenticatedWebSession to authenticate my user, so I'll be able to test the other page.

Regards,

abuteau
  • 6,963
  • 4
  • 16
  • 20

1 Answers1

0

This might be an old question, but I stumbled upon this myself and found a reasonable solution that might work for you.

It is quite obvious, but it took me a while to realise that this was the way to do it.

public class MyPageTest  {

    private static WicketTester tester;

    public static final String VALID_ADMIN_USERNAME = "admin";
    public static final String VALID_ADMIN_PASSWORD = "1234";

    @BeforeClass
    public static void beforeTesting() {
        tester = new WicketTester(new MyTestApplication());

        /*
         * Setup your AuthenticatedWebSession to be able to resolve any objects 
         * it might be depening on. In my case this was an AuthChecker instance that 
         * I get from Guice dependency injection but this might be very different 
         * for you.
         */
        ((MyAuthenticatedWebSession)tester.getSession())
                .configureAuthChecker(MyTestApplication.testInjector()
                        .getInstance(AuthChecker.class));
    }

    @AfterClass
    public static void afterTesting() {
        tester.destroy();
    }

    @Test
    public void testAdminOptions() {
        // You could consider doing this in a separate @Before annotated method.
        // This is basically where the magic happens and the user is logged in.
        ((AuthenticatedWebSession)tester.getSession()).signIn(
                VALID_ADMIN_USERNAME, 
                VALID_ADMIN_PASSWORD);

        // When the user is logged in, you can just start from a page that normally
        // requires authentication.
        tester.startPage(OverviewPage.class);
        tester.assertVisible("myPanel");
    }

}
StanB123
  • 477
  • 5
  • 14