/** * Test that SomeUser is correctly authenticated as XWiki.SomeUser when xwiki:SomeUser is entered * as username. */ public void testLoginWithWikiPrefix() throws Exception { // Setup a simple user profile document XWikiDocument userDoc = new XWikiDocument("XWiki", "SomeUser"); // Mock the XWikiUsers object, since a real objects requires more mocking on the XWiki object Mock mockUserObj = mock(BaseObject.class, new Class[] {}, new Object[] {}); mockUserObj.stubs().method("setDocumentReference"); mockUserObj.stubs().method("setNumber"); mockUserObj.stubs().method("getStringValue").with(eq("password")).will(returnValue("pass")); mockUserObj.stubs().method("setOwnerDocument"); userDoc.addObject("XWiki.XWikiUsers", (BaseObject) mockUserObj.proxy()); // Make a simple XWiki.XWikiUsers class that will contain a default password field BaseClass userClass = new BaseClass(); userClass.addPasswordField("password", "Password", 20); userClass.setClassName("XWiki.XWikiUsers"); // Prepare the XWiki mock this.mockXWiki .stubs() .method("getDocument") .with(eq("XWiki.SomeUser"), eq(this.getContext())) .will(returnValue(userDoc)); this.mockXWiki .stubs() .method("getClass") .with(eq("XWiki.XWikiUsers"), eq(this.getContext())) .will(returnValue(userClass)); this.mockXWiki.stubs().method("exists").will(returnValue(true)); // Finally run the test: Using xwiki:Admin should correctly authenticate the Admin user Principal principal = this.authService.authenticate("xwiki:SomeUser", "pass", this.getContext()); assertNotNull(principal); assertEquals("xwiki:XWiki.SomeUser", principal.getName()); }
@Override protected void setUp() throws Exception { super.setUp(); this.authService = new XWikiAuthServiceImpl(); this.mockXWiki = mock(XWiki.class); getContext().setWiki((XWiki) this.mockXWiki.proxy()); BaseClass userClass = new BaseClass(); userClass.setDocumentReference( new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiUsers")); userClass.addPasswordField("password", "Password", 10); this.mockXWiki.stubs().method("getUserClass").will(returnValue(userClass)); }