gpt4 book ai didi

java - 测试网页前的 Wicket Auth/Roles 用户认证

转载 作者:搜寻专家 更新时间:2023-11-01 03:52:26 25 4
gpt4 key购买 nike

我有一个 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");
}

}

关于java - 测试网页前的 Wicket Auth/Roles 用户认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22016953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com