Wicket Auth /在测试网页之前角色用户身份验证

时间:2014-02-25 14:21:53

标签: java authentication wicket-6 wicket-tester

我有一个网络应用程序,它使用Wicket Auth / Roles登录用户并分配角色。 (http://wicket.apache.org/learn/projects/authroles.html

您也可以参考此示例:http://www.wicket-library.com/wicket-examples-6.0.x/authentication3/

我有很多网页,我想在测试我的网页之前登录我的应用程序。 我的测试页面扩展了WebAppTestBase。

这是我的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();
  }
}

所以我如何设置AuthenticatedWebSession来验证我的用户,这样我就可以测试另一页了。

此致

1 个答案:

答案 0 :(得分:0)

这可能是个老问题,但是我自己偶然发现了一个合理的解决方案,可能对您有用。

这很明显,但是我花了一段时间才意识到这是做到这一点的方法。

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

}