]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/account/TestPasswordResetExternal.java
add: implement password change log
[gigi.git] / tests / club / wpia / gigi / pages / account / TestPasswordResetExternal.java
1 package club.wpia.gigi.pages.account;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
6 import java.io.IOException;
7 import java.io.OutputStream;
8 import java.io.UnsupportedEncodingException;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11 import java.net.URLConnection;
12 import java.net.URLEncoder;
13 import java.security.GeneralSecurityException;
14
15 import org.hamcrest.CoreMatchers;
16 import org.junit.Test;
17
18 import club.wpia.gigi.GigiApiException;
19 import club.wpia.gigi.dbObjects.User;
20 import club.wpia.gigi.pages.PasswordResetPage;
21 import club.wpia.gigi.pages.wot.TestVerification;
22 import club.wpia.gigi.testUtils.ArrayContains;
23 import club.wpia.gigi.testUtils.ClientTest;
24 import club.wpia.gigi.testUtils.IOUtils;
25 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
26 import club.wpia.gigi.util.RandomToken;
27
28 public class TestPasswordResetExternal extends ClientTest {
29
30     @Test
31     public void testByVerification() throws IOException, GeneralSecurityException, GigiApiException, InterruptedException {
32         User u = User.getById(createVerificationUser("fn", "ln", createUniqueName() + "@example.com", TEST_PASSWORD));
33         String cookie2 = cookieWithCertificateLogin(u);
34         URLConnection uc = TestVerification.buildupVerifyFormConnection(cookie2, email, true);
35         String avalue = RandomToken.generateToken(32);
36         uc.getOutputStream().write(("verifiedName=" + this.u.getPreferredName().getId() + "&date=" + TestVerification.validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10&passwordReset=1&passwordResetValue=" + URLEncoder.encode(avalue, "UTF-8")).getBytes("UTF-8"));
37         uc.getOutputStream().flush();
38         String error = fetchStartErrorMessage(IOUtils.readURL(uc));
39         assertNull(error);
40
41         TestMail mail = getMailReceiver().receive(this.u.getEmail());
42         assertThat(mail.getSubject(), containsString("Verification"));
43         mail = getMailReceiver().receive(this.u.getEmail());
44         String link = mail.extractLink();
45         String npw = TEST_PASSWORD + "'";
46         assertNotNull(toPasswordReset(avalue, link, npw, npw + "'"));
47         assertNotNull(toPasswordReset(avalue + "'", link, npw, npw));
48         assertNotNull(toPasswordReset(avalue, link, "a", "a"));
49         assertNull(toPasswordReset(avalue, link, npw, npw));
50         assertNotNull(login(email, npw));
51
52         String[] result = this.u.getAdminLog();
53         assertThat(result, ArrayContains.contains(CoreMatchers.equalTo("RA Agent triggered password reset")));
54         assertThat(result, ArrayContains.contains(CoreMatchers.equalTo("User token based password reset")));
55     }
56
57     private String toPasswordReset(String avalue, String link, String npw, String npw2) throws IOException, MalformedURLException, UnsupportedEncodingException {
58         URLConnection uc2 = new URL(link).openConnection();
59         String csrf = getCSRF(uc2);
60         String headerField = uc2.getHeaderField("Set-Cookie");
61         assertNotNull(headerField);
62         String cookie3 = stripCookie(headerField);
63         uc2 = new URL("https://" + getServerName() + PasswordResetPage.PATH).openConnection();
64         cookie(uc2, cookie3);
65         uc2.setDoOutput(true);
66         OutputStream o = uc2.getOutputStream();
67         o.write(("csrf=" + csrf + "&pword1=" + URLEncoder.encode(npw, "UTF-8") + "&pword2=" + URLEncoder.encode(npw2, "UTF-8") + "&private_token=" + URLEncoder.encode(avalue, "UTF-8")).getBytes("UTF-8"));
68         String readURL = IOUtils.readURL(uc2);
69         return fetchStartErrorMessage(readURL);
70     }
71 }