Wicket Auth / Roles user authentication before testing a web page

I have a web application that uses Wicket Auth / Roles to log in a user and assign roles. ( http://wicket.apache.org/learn/projects/authroles.html )

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

I have many web pages and I want to log in to 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 can I set AuthenticatedWebSession authentication to authenticate my user, so I can check another page.

Hi,

+4
1

, , .

, , , .

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");
    }

}
0

All Articles