1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.ui.invitation

File InvitationTest.java

 

Code metrics

18
330
21
1
792
568
32
0.1
15.71
21
1.52

Classes

Class Line # Actions
InvitationTest 58 330 0% 32 14
0.962059696.2%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.test.ui.invitation;
21   
22    import java.util.HashMap;
23    import java.util.List;
24    import java.util.Map;
25   
26    import javax.mail.Address;
27    import javax.mail.BodyPart;
28    import javax.mail.Multipart;
29    import javax.mail.internet.MimeMessage;
30   
31    import org.junit.Assert;
32    import org.junit.Before;
33    import org.junit.Test;
34    import org.openqa.selenium.By;
35    import org.openqa.selenium.WebElement;
36    import org.xwiki.administration.test.po.AdministrationSectionPage;
37    import org.xwiki.invitation.test.po.InspectInvitationsPage;
38    import org.xwiki.invitation.test.po.InvitationActionConfirmationElement;
39    import org.xwiki.invitation.test.po.InvitationGuestActionsPage;
40    import org.xwiki.invitation.test.po.InvitationMessageDisplayElement;
41    import org.xwiki.invitation.test.po.InvitationSenderPage;
42    import org.xwiki.test.ui.AbstractTest;
43    import org.xwiki.test.ui.TestUtils;
44    import org.xwiki.test.ui.browser.IgnoreBrowser;
45    import org.xwiki.test.ui.browser.IgnoreBrowsers;
46    import org.xwiki.test.ui.po.RegistrationPage;
47    import org.xwiki.test.ui.po.TableElement;
48    import org.xwiki.test.ui.po.editor.ObjectEditPage;
49   
50    import com.icegreen.greenmail.util.GreenMail;
51   
52    /**
53    * Tests invitation application.
54    *
55    * @version $Id: 61a42f4e0908e8d66da2c13ed8fbd0df50d8a74d $
56    * @since 2.4M2
57    */
 
58    public class InvitationTest extends AbstractTest
59    {
60    private static boolean initialized;
61   
62    private InvitationSenderPage senderPage;
63   
64    private GreenMail greenMail;
65   
 
66  11 toggle @Before
67    public void setUp() throws Exception
68    {
69    // Login as admin and delete existing messages.
70  11 getDriver().get(getUtil().getURLToLoginAsAdminAndGotoPage(getUtil().getURLToNonExistentPage()));
71  11 getUtil().recacheSecretToken();
72  11 getUtil().setDefaultCredentials(TestUtils.ADMIN_CREDENTIALS);
73  11 getUtil().rest().deletePage("Invitation", "InvitationMessages");
74   
75  11 if (!initialized) {
76    // We have to go to sender page before any config shows up.
77  1 InvitationSenderPage.gotoPage();
78   
79  1 AdministrationSectionPage config = AdministrationSectionPage.gotoPage("Invitation");
80    // Set port to 3025
81  1 config.getForm().setFieldValue(By.id("Invitation.InvitationConfig_Invitation.WebHome_0_smtp_port"),
82    "3025");
83    // Make sure that by default we don't allow non admin to send emails to multiple addresses
84  1 config.getForm().setFieldValue(By.id("Invitation.InvitationConfig_Invitation.WebHome_0_"
85    + "usersMaySendToMultiple"), "false");
86  1 config.clickSave();
87   
88    // Make sure the users we're registering in testAcceptInvitation and testAcceptInvitationToCloseWiki don't
89    // exist.
90    // TODO: Fix this whole mess of having try/finally blocks in tests below which is an anti pattern. Instead
91    // we need to separate tests by fixture.
92  1 getUtil().rest().deletePage("XWiki", "InvitedMember");
93  1 getUtil().rest().deletePage("XWiki", "AnotherInvitedMember");
94   
95  1 initialized = true;
96    }
97   
98  11 setSenderPage(InvitationSenderPage.gotoPage());
99  11 getSenderPage().fillInDefaultValues();
100    }
101   
 
102  1 toggle @Test
103    public void testGuestActionsOnNonexistantMessage() throws Exception
104    {
105  1 TestUtils.Session s = getUtil().getSession();
106  1 try {
107  1 getUtil().forceGuestUser();
108   
109    // Try to accept nonexistent message.
110  1 getUtil().gotoPage("Invitation", "InvitationGuestActions", "view", "doAction_accept&messageID=12345");
111  1 InvitationGuestActionsPage guestPage = new InvitationGuestActionsPage();
112  1 Assert.assertNotNull("Guests able to accept nonexistent invitation", guestPage.getMessage());
113  1 Assert.assertEquals("No message was found by the given ID. It might have been deleted "
114    + "or maybe the system is experiencing difficulties.", guestPage.getMessage());
115   
116    // Try to decline nonexistent message.
117  1 getUtil().gotoPage("Invitation", "InvitationGuestActions", "view", "doAction_decline&messageID=12345");
118  1 Assert.assertNotNull("Guests able to decline nonexistent invitation", guestPage.getMessage());
119  1 Assert.assertEquals("No invitation was found by the given ID. It might have been deleted or "
120    + "maybe the system is experiencing difficulties.", guestPage.getMessage());
121   
122    // Try to report nonexistent message.
123  1 getUtil().gotoPage("Invitation", "InvitationGuestActions", "view", "doAction_report&messageID=12345");
124  1 Assert.assertNotNull("Guests able to report nonexistent invitation as spam", guestPage.getMessage());
125  1 Assert.assertEquals("There was no message found by the given ID. Maybe an administrator "
126    + "deleted the message from our system.", guestPage.getMessage());
127    } finally {
128  1 getUtil().setSession(s);
129    }
130    }
131   
 
132  1 toggle @Test
133    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146")
134    public void testSendMailToTwoAddresses() throws Exception
135    {
136  1 try {
137  1 startGreenMail();
138  1 getSenderPage().fillForm("user@localhost.localdomain anotheruser@localhost.localdomain", null, null);
139  1 InvitationSenderPage.InvitationSentPage sent = getSenderPage().send();
140  1 getGreenMail().waitForIncomingEmail(10000, 2);
141  1 MimeMessage[] messages = getGreenMail().getReceivedMessages();
142   
143  1 Assert.assertTrue("wrong number of messages", messages.length == 2);
144   
145    // Correspond to messages a and b
146  1 int a = 1, b = 2;
147   
148  1 Map<String, String> messageA = getMessageContent(messages[0]);
149  1 Map<String, String> messageB = getMessageContent(messages[1]);
150   
151  1 Assert.assertFalse("Both messages are going to the same recipient",
152    messageA.get("recipient").equals(messageB.get("recipient")));
153   
154    // No guarentee which message will come in first.
155  1 if (messageA.get("recipient").contains("anotheruser@localhost.localdomain")) {
156  0 Map<String, String> temp = messageB;
157  0 messageB = messageA;
158  0 messageA = temp;
159  0 b = 1;
160  0 a = 2;
161    }
162   
163  1 Assert.assertTrue("Wrong recipient name.\nExpecting:user@localhost.localdomain\n Got:"
164    + messageA.get("recipient"),
165    messageA.get("recipient").contains("user@localhost.localdomain"));
166   
167  1 Assert.assertTrue("Wrong recipient name.\nExpecting:anotheruser@localhost.localdomain\n Got:"
168    + messageB.get("recipient"),
169    messageB.get("recipient").contains("anotheruser@localhost.localdomain"));
170   
171  1 assertMessageValid(messageA);
172  1 assertMessageValid(messageB);
173   
174    // Check that the page has the table and the messages.
175  1 Assert.assertTrue(sent.getMessageBoxContent().contains("Your message has been sent."));
176  1 TableElement table = sent.getTable();
177  1 Assert.assertTrue(table.numberOfRows() == 3);
178  1 Assert.assertTrue(table.numberOfColumns() == 3);
179  1 Assert.assertTrue(table.getRow(a).get(1).getText().contains("user@localhost.localdomain"));
180  1 Assert.assertTrue(table.getRow(a).get(2).getText().contains("Pending"));
181  1 Assert.assertTrue(table.getRow(b).get(1).getText().contains("anotheruser@localhost.localdomain"));
182  1 Assert.assertTrue(table.getRow(b).get(2).getText().contains("Pending"));
183    } finally {
184  1 stopGreenMail();
185    }
186    }
187   
 
188  1 toggle @Test
189    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146")
190    public void testPreviewMessage()
191    {
192  1 InvitationMessageDisplayElement preview = getSenderPage().preview();
193  1 Assert.assertTrue(preview.getSubjectLine().contains("has invited you to join"));
194  1 Assert.assertTrue(preview.getMessageBody().contains("If this message looks like abuse of our system"));
195  1 Assert.assertTrue(preview.getValidRecipients().get(0).getText().contains("user@localhost.localdomain"));
196    }
197   
 
198  1 toggle @Test
199    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146")
200    public void testNonAdminCanSend() throws Exception
201    {
202  1 TestUtils.Session s = getUtil().getSession();
203  1 try {
204  1 getUtil().forceGuestUser();
205  1 getUtil().createUserAndLogin("NonMailAdminUser", "WeakPassword");
206  1 setSenderPage(InvitationSenderPage.gotoPage());
207  1 startGreenMail();
208  1 getSenderPage().fillForm("user@localhost.localdomain", null, null);
209  1 InvitationSenderPage.InvitationSentPage sent = getSenderPage().send();
210   
211    // Prove that the message was sent.
212  1 getGreenMail().waitForIncomingEmail(10000, 1);
213  1 MimeMessage[] messages = getGreenMail().getReceivedMessages();
214  1 Map<String, String> message = getMessageContent(messages[0]);
215  1 Assert.assertTrue(message.get("recipient").contains("user@localhost.localdomain"));
216  1 assertMessageValid(message);
217   
218    // Check that the page has the table and the message.
219  1 Assert.assertTrue(sent.getMessageBoxContent().contains("Your message has been sent."));
220  1 TableElement table = sent.getTable();
221  1 Assert.assertTrue(table.numberOfRows() == 2);
222  1 Assert.assertTrue(table.numberOfColumns() == 3);
223  1 Assert.assertTrue(table.getRow(1).get(1).getText().contains("user@localhost.localdomain"));
224  1 Assert.assertTrue(table.getRow(1).get(2).getText().contains("Pending"));
225    } finally {
226  1 stopGreenMail();
227  1 getUtil().setSession(s);
228  1 getUtil().rest().deletePage("XWiki", "NonMailAdminUser");
229    }
230    }
231   
232    /**
233    * This test proves that: 1. Non administrators trying to send to multiple email addresses without permission will
234    * get an error message. and said mail will not be sent. 2. After permission is granted sending to multiple users
235    * will work and message will say mail was sent.
236    */
 
237  1 toggle @Test
238    @IgnoreBrowsers({
239    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
240    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
241    })
242    public void testUnpermittedUserCannotSendToMultipleAddresses() throws Exception
243    {
244  1 TestUtils.Session admin = getUtil().getSession();
245   
246    // Make sure users don't have the right to send to multiple.
247  1 AdministrationSectionPage config = AdministrationSectionPage.gotoPage("Invitation");
248  1 config.getForm().setFieldValue(By.id("Invitation.InvitationConfig_Invitation.WebHome_0_"
249    + "usersMaySendToMultiple"), "false");
250  1 config.clickSave();
251   
252  1 try {
253  1 getUtil().forceGuestUser();
254  1 getUtil().createUserAndLogin("NonMailAdminUser", "WeakPassword");
255  1 setSenderPage(InvitationSenderPage.gotoPage());
256  1 startGreenMail();
257  1 getSenderPage().fillForm("user@localhost.localdomain anotheruser@localhost.localdomain", null, null);
258  1 InvitationSenderPage.InvitationSentPage sent = getSenderPage().send();
259  1 getGreenMail().waitForIncomingEmail(2000, 2);
260  1 MimeMessage[] messages = getGreenMail().getReceivedMessages();
261  1 Assert.assertTrue("Messages were received when they shouldn't have been sent!", messages.length == 0);
262  1 Assert.assertTrue("User was not shown the correct error message.",
263    sent.getMessageBoxContent().equals("Your message couldn't be sent because there were no valid email "
264    + "addresses to send to."));
265  1 stopGreenMail();
266   
267    // Become admin and allow users to send to multiple.
268  1 TestUtils.Session nonAdmin = getUtil().getSession();
269  1 getUtil().setSession(admin);
270  1 config = AdministrationSectionPage.gotoPage("Invitation");
271  1 config.getForm().setFieldValue(By.id("Invitation.InvitationConfig_Invitation.WebHome_0_"
272    + "usersMaySendToMultiple"), "true");
273  1 config.clickSave();
274  1 getUtil().setSession(nonAdmin);
275   
276    // Prove that the user can now send to multiple recipients.
277  1 startGreenMail();
278  1 setSenderPage(InvitationSenderPage.gotoPage());
279  1 getSenderPage().fillForm("user@localhost.localdomain anotheruser@localhost.localdomain", null, null);
280  1 sent = getSenderPage().send();
281  1 getGreenMail().waitForIncomingEmail(10000, 2);
282  1 messages = getGreenMail().getReceivedMessages();
283  1 Assert.assertTrue("Non admins cannot send mail to even with permission", messages.length == 2);
284  1 Assert.assertTrue("User was not given the message that their mail was sent.",
285    sent.getMessageBoxContent().equals("Your message has been sent."));
286    } finally {
287  1 stopGreenMail();
288  1 getUtil().setSession(admin);
289  1 getUtil().rest().deletePage("XWiki", "NonMailAdminUser");
290    }
291    }
292   
293    /**
294    * This test proves that:
295    * 1. Guests (mail recipients) can report spam.
296    * 2. After a spam report, a user's mail privilege is suspended.
297    * 3. An admin will see a message telling him that a spam report was made.
298    * 4. After an admin marks the message as not spam, the sender can again send mail.
299    */
 
300  1 toggle @Test
301    @IgnoreBrowsers({
302    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
303    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
304    })
305    public void testSpamReporting() throws Exception
306    {
307  1 TestUtils.Session admin = getUtil().getSession();
308  1 try {
309  1 getUtil().forceGuestUser();
310  1 getUtil().createUserAndLogin("spam", "andEggs");
311  1 setSenderPage(InvitationSenderPage.gotoPage());
312  1 startGreenMail();
313  1 getSenderPage().fillForm("undisclosed-recipients@localhost.localdomain", null,
314    "You have won the email lottery!");
315  1 getSenderPage().send();
316  1 getGreenMail().waitForIncomingEmail(10000, 1);
317  1 MimeMessage[] messages = getGreenMail().getReceivedMessages();
318  1 String htmlMessage = getMessageContent(messages[0]).get("htmlPart");
319   
320    // Restare greenmail to clear message
321  1 stopGreenMail();
322  1 startGreenMail();
323   
324    // Now switch to guest.
325  1 TestUtils.Session spammer = getUtil().getSession();
326  1 getUtil().forceGuestUser();
327   
328  1 InvitationGuestActionsPage guestPage =
329    InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.REPORT);
330  1 guestPage.setMemo("It's the email lottery, they have taken over your server!");
331  1 guestPage.confirm();
332  1 Assert.assertTrue("Failed to report spam",
333    guestPage.getMessage().contains("Your report has been logged and the situation"));
334   
335    // Prove that a reported message cannot be accepted (which would clear the "reported" status)
336  1 guestPage = InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.ACCEPT);
337  1 Assert.assertTrue("After a message is reported a user can accept it, clearing the spam report",
338    guestPage.getMessage().equals("This invitation has been reported as spam and is no longer valid."));
339    // Prove that a reported message cannot be declined
340  1 guestPage = InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.DECLINE);
341  1 Assert.assertTrue("After a message is reported a user can decline it, clearing the spam report",
342    guestPage.getMessage().equals("This invitation has already been reported as "
343    + "spam and thus cannot be declined."));
344    // Switch to admin
345  1 getUtil().setSession(admin);
346    // Go to invitation sender.
347  1 setSenderPage(InvitationSenderPage.gotoPage());
348    // Switch back to spammer.
349  1 getUtil().setSession(spammer);
350  1 getSenderPage().send();
351  1 getGreenMail().waitForIncomingEmail(2000, 1);
352  1 Assert.assertTrue("Reported spammers can send mail!", getGreenMail().getReceivedMessages().length == 0);
353  1 Assert.assertTrue("No message telling user he's reported spammer.", getSenderPage().userIsSpammer());
354   
355    // Switch to admin.
356  1 getUtil().setSession(admin);
357  1 setSenderPage(InvitationSenderPage.gotoPage());
358  1 Assert.assertTrue("No warning in footer that a message is reported as spam",
359    getSenderPage().getFooter().spamReports() == 1);
360    // View spam message.
361  1 InspectInvitationsPage inspectPage = getSenderPage().getFooter().inspectAllInvitations();
362  1 InspectInvitationsPage.OneMessage inspect =
363    inspectPage.getMessageWhere("Subject", "spam has invited you to join localhost");
364    // Prove that the memo left by spam reported is shown.
365  1 String expectedMessage = "Reported as spam with message: It's the email lottery, they have taken over "
366    + "your server!";
367  1 Assert.assertTrue("The message by the spam reporter is not shown to the admin.\nExpecting:"
368    + expectedMessage + "\n Got:" + inspect.getStatusAndMemo(),
369    inspect.getStatusAndMemo().equals(expectedMessage));
370   
371  1 String memo = "Actually the email lottery is quite legitimate.";
372  1 String expectedSuccessMessage = "Invitation successfully marked as not spam. Log entry: " + memo;
373    // Return their sending privilege.
374  1 String successMessage = inspect.notSpam("Actually the email lottery is quite legitimate.");
375   
376    // Make sure the output is correct.
377  1 Assert.assertTrue("Admin got incorrect message after marking invitation as not spam\nExpecting:"
378    + expectedSuccessMessage + "\n Got:" + successMessage,
379    expectedSuccessMessage.equals(successMessage));
380    // Switch back to spammer
381  1 getUtil().setSession(spammer);
382  1 setSenderPage(InvitationSenderPage.gotoPage());
383  1 Assert.assertFalse("User permission to send not returned by admin action.",
384    getSenderPage().userIsSpammer());
385    } finally {
386  1 stopGreenMail();
387  1 getUtil().setSession(admin);
388  1 getUtil().rest().deletePage("XWiki", "spam");
389    }
390    }
391   
392    /**
393    * This test proves that:
394    * 1. A guest can decline an invitation.
395    * 2. The message status changes and the footer reflects this.
396    * 3. The sender can see the info box seeing the guest's reason for declining.
397    * 4. The message history table shows the decline properly.
398    * 5. A guest cannot accept a message which has already been declined.
399    */
 
400  1 toggle @Test
401    @IgnoreBrowsers({
402    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
403    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
404    })
405    public void testDeclineInvitation() throws Exception
406    {
407  1 TestUtils.Session admin = getUtil().getSession();
408  1 try {
409  1 startGreenMail();
410  1 getSenderPage().send();
411  1 getGreenMail().waitForIncomingEmail(10000, 1);
412  1 MimeMessage[] messages = getGreenMail().getReceivedMessages();
413  1 String htmlMessage = getMessageContent(messages[0]).get("htmlPart");
414  1 Assert.assertTrue("New invitation is not listed as pending in the footer.",
415    getSenderPage().getFooter().myPendingInvitations() == 1);
416    // Now switch to guest.
417  1 getUtil().forceGuestUser();
418   
419  1 InvitationGuestActionsPage guestPage =
420    InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.DECLINE);
421  1 guestPage.setMemo("I'm not interested thank you.");
422  1 guestPage.confirm();
423  1 Assert.assertTrue("Failed to decline invitation",
424    getDriver().getPageSource().contains("This invitation has successfully been declined."));
425    // Switch to admin
426  1 getUtil().setSession(admin);
427    // Go to invitation sender.
428  1 setSenderPage(InvitationSenderPage.gotoPage());
429  1 Assert.assertTrue("Declined invitation is still listed as pending in the footer.",
430    getSenderPage().getFooter().spamReports() == 0);
431   
432    // View declined invitation.
433  1 InspectInvitationsPage inspectPage = getSenderPage().getFooter().inspectMyInvitations();
434  1 InspectInvitationsPage.OneMessage inspect = inspectPage.getMessageWhere("Status", "Declined");
435   
436  1 Assert.assertTrue("Not showing message box to say the invitation has been declined",
437    inspect.getStatusAndMemo().equals("Declined with message: I'm not interested thank you."));
438   
439    // Insure the message history table is correct.
440  1 TableElement messageHistoryTable = inspect.clickMessageHistory();
441  1 List<WebElement> row2 = messageHistoryTable.getRow(2);
442  1 Assert.assertTrue("Message history table not showing correctly.",
443    row2.get(0).getText().equals("Declined"));
444  1 Assert.assertTrue("Message history table not showing correctly.",
445    row2.get(2).getText().equals("I'm not interested thank you."));
446   
447    // Make sure a guest can't accept the invitation now.
448  1 getUtil().forceGuestUser();
449  1 guestPage = InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.ACCEPT);
450  1 Assert.assertTrue("After a message is declined a user can still accept it!",
451    guestPage.getMessage().equals("This invitation has been declined and cannot be accepted now."));
452    // Try to decline the invitation.
453  1 guestPage = InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.DECLINE);
454  1 Assert.assertTrue("User was allowed to decline an invitation twice.",
455    guestPage.getMessage().equals("This invitation has already been declined and "
456    + "cannot be declined again."));
457    // Prove that the message can still be reported as spam
458  1 guestPage = InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.REPORT);
459  1 Assert.assertTrue("After the invitation was declined it now cannot be reported as spam.",
460    guestPage.getMessage().equals(""));
461    } finally {
462  1 stopGreenMail();
463  1 getUtil().setSession(admin);
464    }
465    }
466   
467    /**
468    * This test proves that:
469    * 1. The accept invitation link sent in the email will work.
470    * 2. A user can accept an invitation and be directed to the registration form and can register and login.
471    * 3. An invitation once accepted cannot be accepted again nor declined.
472    * 4. An invitation once accepted can still be reported as spam.
473    */
 
474  1 toggle @Test
475    @IgnoreBrowsers({
476    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
477    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
478    })
479    public void testAcceptInvitation() throws Exception
480    {
481  1 TestUtils.Session admin = getUtil().getSession();
482  1 try {
483  1 startGreenMail();
484  1 getSenderPage().send();
485  1 getGreenMail().waitForIncomingEmail(10000, 1);
486  1 MimeMessage[] messages = getGreenMail().getReceivedMessages();
487  1 String htmlMessage = getMessageContent(messages[0]).get("htmlPart");
488  1 Assert.assertTrue("New invitation is not listed as pending in the footer.",
489    getSenderPage().getFooter().myPendingInvitations() == 1);
490    // Now switch to guest.
491  1 getUtil().forceGuestUser();
492   
493  1 InvitationGuestActionsPage guestPage =
494    InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.ACCEPT);
495  1 Assert.assertTrue("There was an error message when accepting the invitation message:\n"
496    + guestPage.getMessage(),
497    guestPage.getMessage().equals(""));
498    // Register a new user.
499  1 RegistrationPage rp = new RegistrationPage();
500  1 rp.fillRegisterForm(null, null, "InvitedMember", "WeakPassword", "WeakPassword", null);
501  1 rp.clickRegister();
502  1 Assert.assertTrue("There were failure messages when registering.",
503    rp.getValidationFailureMessages().isEmpty());
504  1 getDriver().get(getUtil().getURLToLoginAs("InvitedMember", "WeakPassword"));
505   
506  1 Assert.assertTrue("Failed to log user in after registering from invitation.", rp.isAuthenticated());
507   
508    // Now switch to guest again and try to accept the invitation again.
509  1 getUtil().forceGuestUser();
510  1 guestPage = InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.ACCEPT);
511  1 Assert.assertTrue("After the invitation was accepted a user was allowed to accept it again.",
512    guestPage.getMessage().equals("This invitation has already been accepted and the "
513    + "offer is no longer valid."));
514    // Try to decline the invitation.
515  1 guestPage = InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.DECLINE);
516  1 Assert.assertTrue("After the invitation was accepted a user was allowed to decline it.",
517    guestPage.getMessage().equals("This invitation has already been accepted and "
518    + "now cannot be declined."));
519    // Prove that the message can still be reported as spam
520  1 guestPage = InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.REPORT);
521  1 Assert.assertTrue("After the invitation was accepted it now cannot be reported as spam.",
522    guestPage.getMessage().equals(""));
523    } finally {
524  1 stopGreenMail();
525  1 getUtil().setSession(admin);
526    }
527    }
528   
529    /**
530    * This test proves that:
531    * 1. A guest cannot register if register permission is removed from XWikiPreferences.
532    * 2. Upon recieving an email invitation the guest can register even without register premission.
533    */
 
534  1 toggle @Test
535    @IgnoreBrowsers({
536    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
537    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
538    })
539    public void testAcceptInvitationToClosedWiki() throws Exception
540    {
541  1 TestUtils.Session admin = getUtil().getSession();
542  1 try {
543    // First we ban anon from registering.
544  1 ObjectEditPage oep = ObjectEditPage.gotoPage("XWiki", "XWikiPreferences");
545   
546  1 oep.getObjectsOfClass("XWiki.XWikiGlobalRights").get(2)
547    .getSelectElement(By.name("XWiki.XWikiGlobalRights_2_levels")).unSelect("register");
548   
549  1 oep.clickSaveAndContinue();
550    // now prove anon cannot register
551  1 getUtil().forceGuestUser();
552  1 RegistrationPage.gotoPage();
553  1 getUtil().assertOnPage(getUtil().getURL("XWiki", "XWikiLogin", "login"));
554   
555    // Now we try sending and accepting an invitation.
556  1 getUtil().setSession(admin);
557  1 setSenderPage(InvitationSenderPage.gotoPage());
558  1 getSenderPage().fillInDefaultValues();
559   
560  1 startGreenMail();
561  1 getSenderPage().send();
562  1 getGreenMail().waitForIncomingEmail(10000, 1);
563  1 MimeMessage[] messages = getGreenMail().getReceivedMessages();
564  1 String htmlMessage = getMessageContent(messages[0]).get("htmlPart");
565  1 Assert.assertTrue("New invitation is not listed as pending in the footer.",
566    getSenderPage().getFooter().myPendingInvitations() == 1);
567    // Now switch to guest.
568  1 getUtil().forceGuestUser();
569   
570  1 InvitationGuestActionsPage guestPage =
571    InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.ACCEPT);
572  1 Assert.assertTrue("There was an error message when accepting the invitation message:\n"
573    + guestPage.getMessage(),
574    guestPage.getMessage().equals(""));
575    // Register a new user.
576  1 RegistrationPage rp = new RegistrationPage();
577  1 rp.fillRegisterForm(null, null, "AnotherInvitedMember", "WeakPassword", "WeakPassword", null);
578  1 rp.clickRegister();
579  1 Assert.assertTrue("There were failure messages when registering.",
580    rp.getValidationFailureMessages().isEmpty());
581  1 getDriver().get(getUtil().getURLToLoginAs("AnotherInvitedMember", "WeakPassword"));
582   
583  1 Assert.assertTrue("Failed to log user in after registering from invitation.", rp.isAuthenticated());
584    } finally {
585  1 stopGreenMail();
586  1 getUtil().setSession(admin);
587   
588    // Better open the wiki back up again.
589  1 ObjectEditPage oep = ObjectEditPage.gotoPage("XWiki", "XWikiPreferences");
590   
591  1 oep.getObjectsOfClass("XWiki.XWikiGlobalRights").get(2)
592    .getSelectElement(By.name("XWiki.XWikiGlobalRights_2_levels")).select("register");
593   
594  1 oep.clickSaveAndContinue();
595    }
596    }
597   
598    /**
599    * This test proves that:
600    * 1. A user can cancel an invitation after sending it, leaving a message for the recipient should they try to
601    * accept.
602    * 2. A canceled invitation cannot be accepted and the guest will see an explaination with the message left when
603    * the sender canceled.
604    * 3. A canceled invitation cannot be declined, the guest gets the sender's note.
605    * 4. A canceled invitation can still be reported as spam.
606    */
 
607  1 toggle @Test
608    @IgnoreBrowsers({
609    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
610    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
611    })
612    public void testCancelInvitation() throws Exception
613    {
614  1 TestUtils.Session admin = getUtil().getSession();
615  1 try {
616  1 startGreenMail();
617  1 getSenderPage().send();
618  1 getGreenMail().waitForIncomingEmail(10000, 1);
619  1 MimeMessage[] messages = getGreenMail().getReceivedMessages();
620  1 String htmlMessage = getMessageContent(messages[0]).get("htmlPart");
621  1 Assert.assertTrue("New invitation is not listed as pending in the footer.",
622    getSenderPage().getFooter().myPendingInvitations() == 1);
623   
624  1 InspectInvitationsPage.OneMessage message = getSenderPage().getFooter().inspectMyInvitations()
625    .getMessageWhere("Subject", "Admin has invited you to join localhost This is a subject line.");
626   
627  1 InvitationActionConfirmationElement confirm = message.cancel();
628   
629  1 Assert.assertEquals("leave a message in case the invitee(s) try to register.",
630    confirm.getLabel().toLowerCase());
631   
632  1 confirm.setMemo("Sorry, wrong email address.");
633  1 Assert.assertEquals("Invitation successfully rescinded.", confirm.confirm());
634   
635    // Now switch to guest.
636  1 getUtil().forceGuestUser();
637   
638  1 String commonPart = "\nAdministrator left you this message when rescinding the invitation.\n"
639    + "Sorry, wrong email address.";
640   
641    // Prove that invitation cannot be accepted
642  1 InvitationGuestActionsPage guestPage =
643    InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.ACCEPT);
644  1 Assert.assertFalse("Guest was able to accept a message which had been canceled.",
645    guestPage.getMessage().equals(""));
646  1 Assert.assertEquals("We're sorry but this invitation has been rescinded." + commonPart,
647    guestPage.getMessage());
648   
649    // Prove that invitation cannot be declined
650  1 guestPage = InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.DECLINE);
651  1 Assert.assertFalse("Guest was able to decline a message which had been canceled.",
652    guestPage.getMessage().equals(""));
653  1 Assert.assertEquals("This invitation has been rescinded and thus cannot be declined." + commonPart,
654    guestPage.getMessage());
655   
656    // Prove that the message report spam page still shows up.
657  1 guestPage = InvitationGuestActionsPage.gotoPage(htmlMessage, InvitationGuestActionsPage.Action.REPORT);
658  1 Assert.assertTrue("Guest was not able to report canceled invitation as spam",
659    guestPage.getMessage().equals(""));
660  1 guestPage.setMemo("Canceled message is spam.");
661  1 Assert.assertEquals("Your report has been logged and the situation will "
662    + "be investigated as soon as possible, we apologize for the inconvenience.", guestPage.confirm());
663    } finally {
664  1 stopGreenMail();
665  1 getUtil().setSession(admin);
666    }
667    }
668   
669    /**
670    * This test proves that:
671    * 1. A user cannot send to the same address multiple (8000) times which would be very annoying for the recipient.
672    */
 
673  1 toggle @Test
674    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
675    public void testSendManyToOneAddress() throws Exception
676    {
677  1 TestUtils.Session admin = getUtil().getSession();
678  1 try {
679    // Allow users to send to multiple.
680  1 AdministrationSectionPage config = AdministrationSectionPage.gotoPage("Invitation");
681  1 config.getForm().setFieldValue(By.id("Invitation.InvitationConfig_Invitation.WebHome_0_"
682    + "usersMaySendToMultiple"), "true");
683  1 config.clickSave();
684   
685    // Now switch to a wizeguy user
686  1 getUtil().forceGuestUser();
687  1 getUtil().createUserAndLogin("tr0ll", "StrongPassword");
688  1 setSenderPage(InvitationSenderPage.gotoPage());
689   
690  1 startGreenMail();
691  1 getSenderPage().fillForm("user@localhost.localdomain user@localhost.localdomain "
692    + "user@localhost.localdomain user@localhost.localdomain", null, null);
693  1 getSenderPage().send();
694  1 getGreenMail().waitForIncomingEmail(10000, 1);
695  1 MimeMessage[] messages = getGreenMail().getReceivedMessages();
696  1 Assert.assertTrue("One user is able to send multiple messages to the same poor recipient.",
697    messages.length == 1);
698    } finally {
699  1 stopGreenMail();
700  1 getUtil().setSession(admin);
701  1 getUtil().rest().deletePage("XWiki", "tr0ll");
702    }
703    }
704   
705    //-----------------------Helper methods--------------------------//
706   
707   
708    /** To put the page someplace else, subclass this class and change this method. */
 
709  0 toggle protected InvitationSenderPage newSenderPage()
710    {
711  0 return new InvitationSenderPage();
712    }
713   
 
714  3 toggle protected void assertMessageValid(Map<String, String> message)
715    {
716  3 Assert.assertTrue(message.get("htmlPart").contains("If this message looks like abuse of our system"));
717  3 Assert.assertTrue(message.get("subjectLine").contains("has invited you to join"));
718    }
719   
 
720  8 toggle protected Map<String, String> getMessageContent(MimeMessage message) throws Exception
721    {
722  8 Map<String, String> messageMap = new HashMap<String, String>();
723   
724  8 Address[] addresses = message.getAllRecipients();
725  8 Assert.assertTrue(addresses.length == 1);
726  8 messageMap.put("recipient", addresses[0].toString());
727   
728  8 messageMap.put("subjectLine", message.getSubject());
729   
730  8 Multipart mp = (Multipart) message.getContent();
731   
732  8 BodyPart plain = getPart(mp, "text/plain");
733  8 if (plain != null) {
734  8 messageMap.put("textPart", plain.getContent().toString());
735    }
736  8 BodyPart html = getPart(mp, "text/html");
737  8 if (html != null) {
738  8 messageMap.put("htmlPart", html.getContent().toString());
739    }
740   
741  8 return messageMap;
742    }
743   
 
744  40 toggle protected BodyPart getPart(Multipart messageContent, String mimeType) throws Exception
745    {
746  48 for (int i = 0; i < messageContent.getCount(); i++) {
747  48 BodyPart part = messageContent.getBodyPart(i);
748   
749  48 if (part.isMimeType(mimeType)) {
750  16 return part;
751    }
752   
753  32 if (part.isMimeType("multipart/related")
754    || part.isMimeType("multipart/alternative")
755    || part.isMimeType("multipart/mixed")) {
756  24 BodyPart out = getPart((Multipart) part.getContent(), mimeType);
757  24 if (out != null) {
758  24 return out;
759    }
760    }
761    }
762  0 return null;
763    }
764   
 
765  11 toggle protected void startGreenMail() throws Exception
766    {
767  11 this.greenMail = new GreenMail();
768  11 this.greenMail.start();
769    }
770   
 
771  11 toggle protected void stopGreenMail() throws Exception
772    {
773  11 if (getGreenMail() != null) {
774  11 getGreenMail().stop();
775    }
776    }
777   
 
778  44 toggle protected GreenMail getGreenMail()
779    {
780  44 return this.greenMail;
781    }
782   
 
783  41 toggle protected InvitationSenderPage getSenderPage()
784    {
785  41 return this.senderPage;
786    }
787   
 
788  21 toggle protected void setSenderPage(InvitationSenderPage senderPage)
789    {
790  21 this.senderPage = senderPage;
791    }
792    }