簡體   English   中英

Wicket Auth /在測試網頁之前角色用戶身份驗證

[英]Wicket Auth/Roles user authentication before testing web Page

我有一個Web應用程序,它使用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來驗證我的用戶,所以我將能夠測試其他頁面。

問候,

這可能是一個老問題,但我自己偶然發現了這個問題,並找到了一個可能對你有用的合理解決方案。

這很明顯,但我花了一段時間才意識到這是實現它的方法。

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM