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

File ResetPasswordTest.java

 

Code metrics

16
76
8
1
222
143
18
0.24
9.5
8
2.25

Classes

Class Line # Actions
ResetPasswordTest 54 76 0% 18 7
0.9393%
 

Contributing tests

This file is covered by 1 test. .

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.administration.test.ui;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24    import java.util.regex.Matcher;
25    import java.util.regex.Pattern;
26   
27    import javax.mail.Address;
28    import javax.mail.BodyPart;
29    import javax.mail.Multipart;
30    import javax.mail.internet.MimeMessage;
31   
32    import org.apache.commons.io.IOUtils;
33    import org.apache.commons.lang3.RandomStringUtils;
34    import org.junit.After;
35    import org.junit.Assert;
36    import org.junit.Before;
37    import org.junit.Rule;
38    import org.junit.Test;
39    import org.xwiki.administration.test.po.ResetPasswordCompletePage;
40    import org.xwiki.administration.test.po.ResetPasswordPage;
41    import org.xwiki.test.ui.AbstractTest;
42    import org.xwiki.test.ui.SuperAdminAuthenticationRule;
43    import org.xwiki.test.ui.po.LoginPage;
44   
45    import com.icegreen.greenmail.util.GreenMail;
46    import com.icegreen.greenmail.util.ServerSetupTest;
47   
48    /**
49    * Verify the Reset Password feature.
50    *
51    * @version $Id: efe676760bedbd1ea274b96f0db04bf48ce9798e $
52    * @since 7.0M2
53    */
 
54    public class ResetPasswordTest extends AbstractTest
55    {
56    @Rule
57    public SuperAdminAuthenticationRule authenticationRule = new SuperAdminAuthenticationRule(getUtil());
58   
59    private GreenMail mail;
60   
 
61  1 toggle @Before
62    public void startMail()
63    {
64  1 this.mail = new GreenMail(ServerSetupTest.SMTP);
65  1 this.mail.start();
66   
67  1 configureEmail();
68    }
69   
 
70  1 toggle @After
71    public void stopMail()
72    {
73  1 if (this.mail != null) {
74  1 this.mail.stop();
75    }
76   
77  1 restoreSettings();
78    }
79   
 
80  1 toggle private void configureEmail()
81    {
82  1 getUtil().updateObject("Mail", "MailConfig", "Mail.SendMailConfigClass", 0, "host", "localhost", "port",
83    "3025", "sendWaitTime", "0");
84    }
85   
 
86  1 toggle private void restoreSettings()
87    {
88    // Make sure we can restore the settings, so we log back with superadmin to finish the work
89  1 getUtil().loginAsSuperAdmin();
90   
91    // Remove the previous version that the setup has created.
92  1 getUtil().deleteLatestVersion("Mail", "MailConfig");
93    }
94   
 
95  1 toggle @Test
96    public void resetForgottenPassword() throws Exception
97    {
98  1 String userName = "testUser" + RandomStringUtils.randomAlphanumeric(6);
99  1 String password = "password";
100  1 String newPassword = "newPassword";
101   
102    // Create a user
103  1 getUtil().createUser(userName, password, null);
104   
105    // Make sure we are not logged in and go to the reset password page
106  1 getUtil().forceGuestUser();
107  1 ResetPasswordPage resetPasswordPage = ResetPasswordPage.gotoPage();
108   
109    // Try to reset the password of a non existent user
110  1 resetPasswordPage.setUserName("SomeUserThatDoesNotExist");
111  1 resetPasswordPage.clickResetPassword();
112  1 Assert.assertFalse(resetPasswordPage.isResetPasswordSent());
113  1 Assert.assertTrue(resetPasswordPage.getMessage().contains("user does not exist"));
114   
115    // Try again
116  1 resetPasswordPage = resetPasswordPage.clickRetry();
117   
118    // Try to reset the password of our user, when he has no email set
119  1 resetPasswordPage.setUserName(userName);
120  1 resetPasswordPage.clickResetPassword();
121  1 Assert.assertFalse(resetPasswordPage.isResetPasswordSent());
122  1 Assert.assertTrue(resetPasswordPage.getMessage().contains("email address not provided"));
123   
124    // Try again. This time, set the user's email address in the profile
125  1 getUtil().loginAsSuperAdmin();
126  1 getUtil().updateObject("XWiki", userName, "XWiki.XWikiUsers", 0, "email", "foo@bar.com", "form_token",
127    getUtil().getSecretToken());
128  1 getUtil().forceGuestUser();
129   
130    // Actually reset the user's password
131  1 resetPasswordPage = ResetPasswordPage.gotoPage();
132  1 resetPasswordPage.setUserName(userName);
133  1 resetPasswordPage.clickResetPassword();
134   
135    // Check the result
136  1 Assert.assertTrue(resetPasswordPage.isResetPasswordSent());
137    // Check the emails received by the user
138  1 MimeMessage[] receivedEmails = this.mail.getReceivedMessages();
139  1 Assert.assertEquals(1, receivedEmails.length);
140  1 MimeMessage receivedEmail = receivedEmails[0];
141  1 Assert.assertEquals("Password reset request for " + userName, receivedEmail.getSubject());
142  1 String receivedMailContent = getMessageContent(receivedEmail).get("textPart");
143  1 String passwordResetLink = getResetLink(receivedMailContent, userName);
144  1 Assert.assertNotNull(passwordResetLink);
145   
146    // Use the password reset link
147  1 getUtil().gotoPage(passwordResetLink);
148    // We should now be on the ResetPasswordComplete page
149  1 ResetPasswordCompletePage resetPasswordCompletePage = new ResetPasswordCompletePage();
150    // Check that the link was valid
151  1 Assert.assertTrue(resetPasswordCompletePage.isResetLinkValid());
152  1 resetPasswordCompletePage.setPassword(newPassword);
153  1 resetPasswordCompletePage.setPasswordConfirmation(newPassword);
154  1 resetPasswordCompletePage = resetPasswordCompletePage.clickSave();
155   
156    // Check the result
157  1 Assert.assertTrue(resetPasswordCompletePage.isPasswordSuccessfullyReset());
158  1 LoginPage loginPage = resetPasswordCompletePage.clickLogin();
159   
160    // Check the new password
161  1 loginPage.loginAs(userName, newPassword);
162  1 Assert.assertEquals(userName, getUtil().getLoggedInUserName());
163    }
164   
 
165  1 toggle protected Map<String, String> getMessageContent(MimeMessage message) throws Exception
166    {
167  1 Map<String, String> messageMap = new HashMap<String, String>();
168   
169  1 Address[] addresses = message.getAllRecipients();
170  1 Assert.assertTrue(addresses.length == 1);
171  1 messageMap.put("recipient", addresses[0].toString());
172   
173  1 messageMap.put("subjectLine", message.getSubject());
174   
175  1 Multipart mp = (Multipart) message.getContent();
176   
177  1 BodyPart plain = getPart(mp, "text/plain");
178  1 if (plain != null) {
179  1 messageMap.put("textPart", IOUtils.toString(plain.getInputStream()));
180    }
181  1 BodyPart html = getPart(mp, "text/html");
182  1 if (html != null) {
183  1 messageMap.put("htmlPart", IOUtils.toString(html.getInputStream()));
184    }
185   
186  1 return messageMap;
187    }
188   
 
189  4 toggle protected BodyPart getPart(Multipart messageContent, String mimeType) throws Exception
190    {
191  5 for (int i = 0; i < messageContent.getCount(); i++) {
192  5 BodyPart part = messageContent.getBodyPart(i);
193   
194  5 if (part.isMimeType(mimeType)) {
195  2 return part;
196    }
197   
198  3 if (part.isMimeType("multipart/related") || part.isMimeType("multipart/alternative")
199    || part.isMimeType("multipart/mixed")) {
200  2 BodyPart out = getPart((Multipart) part.getContent(), mimeType);
201  2 if (out != null) {
202  2 return out;
203    }
204    }
205    }
206  0 return null;
207    }
208   
 
209  1 toggle private String getResetLink(String emailContent, String userName)
210    {
211  1 String result = null;
212   
213    // Use a regex to extract the password reset link
214  1 Pattern resetLinkPattern = Pattern.compile("http[^\\s]+?ResetPasswordComplete\\?u=" + userName + "\\&v=\\w+");
215  1 Matcher matcher = resetLinkPattern.matcher(emailContent);
216  1 if (matcher.find()) {
217  1 result = matcher.group();
218    }
219   
220  1 return result;
221    }
222    }