use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class DefaultCSRFTokenTest method testTokenForGuestUser.
/**
* Test that the secret token is a non-empty string, even for guest user.
*/
@Test
public void testTokenForGuestUser() throws Exception {
// document access bridge
final DocumentAccessBridge mockDocumentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
getMockery().checking(new Expectations() {
{
allowing(mockDocumentAccessBridge).getCurrentUserReference();
will(returnValue(null));
}
});
String token = this.csrf.getToken();
Assert.assertNotNull("CSRF token is null", token);
Assert.assertNotSame("CSRF token is empty string", "", token);
Assert.assertTrue("CSRF token is too short: \"" + token + "\"", token.length() > 20);
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class DefaultCSRFTokenTest method configure.
@Before
public void configure() throws Exception {
// set up mocked dependencies
// document access bridge
final DocumentAccessBridge mockDocumentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
final CopyStringMatcher returnValue = new CopyStringMatcher(resubmitUrl + "?", "");
getMockery().checking(new Expectations() {
{
allowing(mockDocumentAccessBridge).getDocumentURL(with(aNonNull(DocumentReference.class)), with("view"), with(returnValue), with(aNull(String.class)));
will(returnValue);
allowing(mockDocumentAccessBridge).getDocumentURL(with(aNull(DocumentReference.class)), with("view"), with(aNull(String.class)), with(aNull(String.class)));
will(returnValue(mockDocumentUrl));
allowing(mockDocumentAccessBridge).getCurrentDocumentReference();
will(returnValue(null));
}
});
// configuration
final CSRFTokenConfiguration mockConfiguration = getComponentManager().getInstance(CSRFTokenConfiguration.class);
getMockery().checking(new Expectations() {
{
allowing(mockConfiguration).isEnabled();
will(returnValue(true));
}
});
// request
final HttpSession mockSession = getMockery().mock(HttpSession.class);
final HttpServletRequest httpRequest = getMockery().mock(HttpServletRequest.class);
final ServletRequest servletRequest = new ServletRequest(httpRequest);
getMockery().checking(new Expectations() {
{
allowing(httpRequest).getRequestURL();
will(returnValue(new StringBuffer(mockDocumentUrl)));
allowing(httpRequest).getRequestURI();
will(returnValue(mockDocumentUrl));
allowing(httpRequest).getParameterMap();
will(returnValue(new HashMap<String, String[]>()));
allowing(httpRequest).getSession();
will(returnValue(mockSession));
}
});
// session
getMockery().checking(new Expectations() {
{
allowing(mockSession).getAttribute(with(any(String.class)));
will(returnValue(new HashMap<String, Object>()));
}
});
// container
final Container mockContainer = getComponentManager().getInstance(Container.class);
getMockery().checking(new Expectations() {
{
allowing(mockContainer).getRequest();
will(returnValue(servletRequest));
}
});
// logging
getMockery().checking(new Expectations() {
{
// Ignore all calls to debug()
ignoring(any(Logger.class)).method("debug");
}
});
this.csrf = getComponentManager().getInstance(CSRFToken.class);
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class GroupMimeMessageIteratorTest method createMessage.
@Test
public void createMessage() throws Exception {
DocumentReference groupReference = new DocumentReference("xwiki", "XWiki", "Marketing");
DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
Session session = Session.getInstance(new Properties());
MimeMessageFactory<MimeMessage> factory = new MimeMessageFactory<MimeMessage>() {
@Override
public MimeMessage createMessage(Object source, Map parameters) throws MessagingException {
return new ExtendedMimeMessage();
}
};
Map<String, Object> parameters = new HashMap<>();
parameters.put("parameters", Collections.EMPTY_MAP);
parameters.put("session", session);
DocumentAccessBridge accessBridge = mock(DocumentAccessBridge.class);
when(accessBridge.getProperty(eq(groupReference), any(), eq(0), eq("member"))).thenReturn("XWiki.JohnDoe");
when(accessBridge.getProperty(eq(groupReference), any(), eq(1), eq("member"))).thenReturn("XWiki.JaneDoe");
when(accessBridge.getProperty(eq(groupReference), any(), eq(2), eq("member"))).thenReturn("XWiki.JonnieDoe");
when(accessBridge.getProperty(eq(userReference1), any(), eq("email"))).thenReturn("john@doe.com");
when(accessBridge.getProperty(eq(userReference2), any(), eq("email"))).thenReturn("jane@doe.com");
when(accessBridge.getProperty(eq(userReference3), any(), eq("email"))).thenReturn("jannie@doe.com");
Execution execution = mock(Execution.class);
ExecutionContext executionContext = mock(ExecutionContext.class);
when(execution.getContext()).thenReturn(executionContext);
XWikiContext xwikiContext = mock(XWikiContext.class);
when(executionContext.getProperty("xwikicontext")).thenReturn(xwikiContext);
XWiki xwiki = mock(XWiki.class);
when(xwikiContext.getWiki()).thenReturn(xwiki);
XWikiDocument document = mock(XWikiDocument.class);
when(xwiki.getDocument(eq(groupReference), eq(xwikiContext))).thenReturn(document);
BaseObject object = mock(BaseObject.class);
when(document.getXObjects(any(EntityReference.class))).thenReturn(Arrays.asList(object, object, object));
DocumentReferenceResolver<String> resolver = (DocumentReferenceResolver<String>) mock(DocumentReferenceResolver.class);
when(resolver.resolve("XWiki.JohnDoe")).thenReturn(userReference1);
when(resolver.resolve("XWiki.JaneDoe")).thenReturn(userReference2);
when(resolver.resolve("XWiki.JonnieDoe")).thenReturn(userReference3);
ComponentManager componentManager = mock(ComponentManager.class);
when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
when(componentManager.getInstance(eq(Execution.class))).thenReturn(execution);
when(componentManager.getInstance(eq(DocumentReferenceResolver.TYPE_STRING), eq("current"))).thenReturn(resolver);
GroupMimeMessageIterator iterator = new GroupMimeMessageIterator(groupReference, factory, parameters, componentManager);
assertTrue(iterator.hasNext());
MimeMessage message1 = iterator.next();
assertArrayEquals(message1.getRecipients(Message.RecipientType.TO), InternetAddress.parse("john@doe.com"));
assertTrue(iterator.hasNext());
MimeMessage message2 = iterator.next();
assertArrayEquals(message2.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jane@doe.com"));
assertTrue(iterator.hasNext());
MimeMessage message3 = iterator.next();
assertArrayEquals(message3.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jannie@doe.com"));
assertFalse(iterator.hasNext());
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class DefaultMailTemplateManagerTest method evaluateWithErrorNoObjectMatches.
@Test
public void evaluateWithErrorNoObjectMatches() throws Exception {
XWikiDocument document = mock(XWikiDocument.class);
when(this.xwiki.getDocument(any(DocumentReference.class), any())).thenReturn(document);
BaseObject object1 = mock(BaseObject.class);
BaseObject object2 = mock(BaseObject.class);
List<BaseObject> xobjects = Arrays.asList(object1, object2);
when(document.getXObjects(any())).thenReturn(xobjects);
DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
// First call with the passed language, return -1 (No XWiki.Mail xobject found)
when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(-1);
// Second call with the default language, return -1 (No XWiki.Mail xobject found)
when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("en"))).thenReturn(-1);
try {
this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap(), Locale.FRENCH);
fail("Should have thrown an exception here!");
} catch (MessagingException expected) {
assertEquals("No [Document XWiki.Mail] object matches the locale [fr] or the default locale [en] " + "in the Document [wiki:space.page]", expected.getMessage());
}
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class DefaultMailTemplateManagerTest method evaluateWithObjectNotFoundWithLanguagePassed.
@Test
public void evaluateWithObjectNotFoundWithLanguagePassed() throws Exception {
DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
// First call with the passed language, return -1 (No XWiki.Mail xobject found)
when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(-1);
// Second call with the default language (en), return one (Only XWiki.Mail xobject is found)
when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("en"))).thenReturn(1);
when(documentBridge.getProperty(any(DocumentReference.class), any(), eq(1), eq("html"))).thenReturn("Salut <b>${name}</b> <br />${email}");
VelocityEngine velocityEngine = mock(VelocityEngine.class);
VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
when(velocityEvaluator.evaluateVelocity(eq("Salut <b>${name}</b> <br />${email}"), any(), any(VelocityContext.class))).thenReturn("Salut <b>John Doe</b> <br />john@doe.com");
String result = this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap(), Locale.FRENCH);
verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("fr"));
verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("en"));
assertEquals(result, "Salut <b>John Doe</b> <br />john@doe.com");
}
Aggregations