]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/account/TestPasswordResetExternal.java
add: ensure that for RA Agent actions certificate login is used
[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.junit.Test;
16
17 import club.wpia.gigi.GigiApiException;
18 import club.wpia.gigi.dbObjects.User;
19 import club.wpia.gigi.pages.PasswordResetPage;
20 import club.wpia.gigi.pages.wot.TestVerification;
21 import club.wpia.gigi.testUtils.ClientTest;
22 import club.wpia.gigi.testUtils.IOUtils;
23 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
24 import club.wpia.gigi.util.RandomToken;
25
26 public class TestPasswordResetExternal extends ClientTest {
27
28     @Test
29     public void testByVerification() throws IOException, GeneralSecurityException, GigiApiException, InterruptedException {
30         User u = User.getById(createVerificationUser("fn", "ln", createUniqueName() + "@example.com", TEST_PASSWORD));
31         String cookie2 = cookieWithCertificateLogin(u);
32         URLConnection uc = TestVerification.buildupVerifyFormConnection(cookie2, email, true);
33         String avalue = RandomToken.generateToken(32);
34         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"));
35         uc.getOutputStream().flush();
36         String error = fetchStartErrorMessage(IOUtils.readURL(uc));
37         assertNull(error);
38
39         TestMail mail = getMailReceiver().receive(this.u.getEmail());
40         assertThat(mail.getSubject(), containsString("Verification"));
41         mail = getMailReceiver().receive(this.u.getEmail());
42         String link = mail.extractLink();
43         String npw = TEST_PASSWORD + "'";
44         assertNotNull(toPasswordReset(avalue, link, npw, npw + "'"));
45         assertNotNull(toPasswordReset(avalue + "'", link, npw, npw));
46         assertNotNull(toPasswordReset(avalue, link, "a", "a"));
47         assertNull(toPasswordReset(avalue, link, npw, npw));
48         assertNotNull(login(email, npw));
49     }
50
51     private String toPasswordReset(String avalue, String link, String npw, String npw2) throws IOException, MalformedURLException, UnsupportedEncodingException {
52         URLConnection uc2 = new URL(link).openConnection();
53         String csrf = getCSRF(uc2);
54         String headerField = uc2.getHeaderField("Set-Cookie");
55         assertNotNull(headerField);
56         String cookie3 = stripCookie(headerField);
57         uc2 = new URL("https://" + getServerName() + PasswordResetPage.PATH).openConnection();
58         cookie(uc2, cookie3);
59         uc2.setDoOutput(true);
60         OutputStream o = uc2.getOutputStream();
61         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"));
62         String readURL = IOUtils.readURL(uc2);
63         return fetchStartErrorMessage(readURL);
64     }
65 }