Search in sources :

Example 11 with DocumentAccessBridge

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);
}
Also used : Expectations(org.jmock.Expectations) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Test(org.junit.Test)

Example 12 with DocumentAccessBridge

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);
}
Also used : Expectations(org.jmock.Expectations) ServletRequest(org.xwiki.container.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) DefaultCSRFToken(org.xwiki.csrf.internal.DefaultCSRFToken) HttpSession(javax.servlet.http.HttpSession) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Logger(org.slf4j.Logger) HttpServletRequest(javax.servlet.http.HttpServletRequest) Container(org.xwiki.container.Container) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 13 with DocumentAccessBridge

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());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) HashMap(java.util.HashMap) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) Properties(java.util.Properties) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) EntityReference(org.xwiki.model.reference.EntityReference) ComponentManager(org.xwiki.component.manager.ComponentManager) BaseObject(com.xpn.xwiki.objects.BaseObject) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session) Test(org.junit.Test)

Example 14 with DocumentAccessBridge

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());
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) MessagingException(javax.mail.MessagingException) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 15 with DocumentAccessBridge

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");
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)56 DocumentReference (org.xwiki.model.reference.DocumentReference)39 Test (org.junit.Test)33 Expectations (org.jmock.Expectations)17 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)15 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)11 Before (org.junit.Before)10 XDOM (org.xwiki.rendering.block.XDOM)10 Execution (org.xwiki.context.Execution)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 MacroBlock (org.xwiki.rendering.block.MacroBlock)6 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)6 HashMap (java.util.HashMap)5 VelocityContext (org.apache.velocity.VelocityContext)5 Event (org.xwiki.eventstream.Event)5 DefaultEvent (org.xwiki.eventstream.internal.DefaultEvent)5 VelocityEngine (org.xwiki.velocity.VelocityEngine)5 VelocityManager (org.xwiki.velocity.VelocityManager)5