1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
|
|
| 93% |
Uncovered Elements: 7 (100) |
Complexity: 18 |
Complexity Density: 0.24 |
|
54 |
|
public class ResetPasswordTest extends AbstractTest |
55 |
|
{ |
56 |
|
@Rule |
57 |
|
public SuperAdminAuthenticationRule authenticationRule = new SuperAdminAuthenticationRule(getUtil()); |
58 |
|
|
59 |
|
private GreenMail mail; |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
61 |
1 |
@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 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
70 |
1 |
@After... |
71 |
|
public void stopMail() |
72 |
|
{ |
73 |
1 |
if (this.mail != null) { |
74 |
1 |
this.mail.stop(); |
75 |
|
} |
76 |
|
|
77 |
1 |
restoreSettings(); |
78 |
|
} |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
80 |
1 |
private void configureEmail()... |
81 |
|
{ |
82 |
1 |
getUtil().updateObject("Mail", "MailConfig", "Mail.SendMailConfigClass", 0, "host", "localhost", "port", |
83 |
|
"3025", "sendWaitTime", "0"); |
84 |
|
} |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
86 |
1 |
private void restoreSettings()... |
87 |
|
{ |
88 |
|
|
89 |
1 |
getUtil().loginAsSuperAdmin(); |
90 |
|
|
91 |
|
|
92 |
1 |
getUtil().deleteLatestVersion("Mail", "MailConfig"); |
93 |
|
} |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (39) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
95 |
1 |
@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 |
|
|
103 |
1 |
getUtil().createUser(userName, password, null); |
104 |
|
|
105 |
|
|
106 |
1 |
getUtil().forceGuestUser(); |
107 |
1 |
ResetPasswordPage resetPasswordPage = ResetPasswordPage.gotoPage(); |
108 |
|
|
109 |
|
|
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 |
|
|
116 |
1 |
resetPasswordPage = resetPasswordPage.clickRetry(); |
117 |
|
|
118 |
|
|
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 |
|
|
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 |
|
|
131 |
1 |
resetPasswordPage = ResetPasswordPage.gotoPage(); |
132 |
1 |
resetPasswordPage.setUserName(userName); |
133 |
1 |
resetPasswordPage.clickResetPassword(); |
134 |
|
|
135 |
|
|
136 |
1 |
Assert.assertTrue(resetPasswordPage.isResetPasswordSent()); |
137 |
|
|
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 |
|
|
147 |
1 |
getUtil().gotoPage(passwordResetLink); |
148 |
|
|
149 |
1 |
ResetPasswordCompletePage resetPasswordCompletePage = new ResetPasswordCompletePage(); |
150 |
|
|
151 |
1 |
Assert.assertTrue(resetPasswordCompletePage.isResetLinkValid()); |
152 |
1 |
resetPasswordCompletePage.setPassword(newPassword); |
153 |
1 |
resetPasswordCompletePage.setPasswordConfirmation(newPassword); |
154 |
1 |
resetPasswordCompletePage = resetPasswordCompletePage.clickSave(); |
155 |
|
|
156 |
|
|
157 |
1 |
Assert.assertTrue(resetPasswordCompletePage.isPasswordSuccessfullyReset()); |
158 |
1 |
LoginPage loginPage = resetPasswordCompletePage.clickLogin(); |
159 |
|
|
160 |
|
|
161 |
1 |
loginPage.loginAs(userName, newPassword); |
162 |
1 |
Assert.assertEquals(userName, getUtil().getLoggedInUserName()); |
163 |
|
} |
164 |
|
|
|
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
165 |
1 |
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 |
|
|
|
|
| 82.4% |
Uncovered Elements: 3 (17) |
Complexity: 7 |
Complexity Density: 0.78 |
|
189 |
4 |
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 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
209 |
1 |
private String getResetLink(String emailContent, String userName)... |
210 |
|
{ |
211 |
1 |
String result = null; |
212 |
|
|
213 |
|
|
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 |
|
} |