]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java
fix: let's say CATS assurer's challange is id #1
[gigi.git] / tests / org / cacert / gigi / pages / admin / TestSEAdminPageDetails.java
1 package org.cacert.gigi.pages.admin;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
6 import java.io.IOException;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.net.URLConnection;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12
13 import org.cacert.gigi.dbObjects.Group;
14 import org.cacert.gigi.pages.account.MyDetails;
15 import org.cacert.gigi.pages.admin.support.SupportEnterTicketPage;
16 import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage;
17 import org.cacert.gigi.testUtils.ClientTest;
18 import org.cacert.gigi.testUtils.IOUtils;
19 import org.junit.Test;
20
21 public class TestSEAdminPageDetails extends ClientTest {
22
23     public TestSEAdminPageDetails() throws IOException {
24         grant(email, Group.SUPPORTER);
25         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
26     }
27
28     @Test
29     public void testUserDetailsDisplay() throws MalformedURLException, IOException {
30         String email = createUniqueName() + "@example.com";
31         String fname = "Först";
32         String lname = "Secönd";
33         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
34         URLConnection uc = new URL("https://" + getServerName() + SupportUserDetailsPage.PATH + id).openConnection();
35         uc.addRequestProperty("Cookie", cookie);
36         uc.setDoOutput(true);
37         String res = IOUtils.readURL(uc);
38         assertThat(res, containsString("<input type=\"text\" value=\"" + fname + "\" name=\"fname\">"));
39         assertThat(res, containsString("<input type=\"text\" value=\"" + lname + "\" name=\"lname\">"));
40         assertThat(res, containsString(email));
41     }
42
43     @Test
44     public void testUserDetailsEdit() throws MalformedURLException, IOException {
45         String email = createUniqueName() + "@example.com";
46         String fname = "Först";
47         String lname = "Secönd";
48         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
49
50         String userCookie = login(email, TEST_PASSWORD);
51         assertEquals("Först", getFname(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0))));
52         // User can change his name
53         assertNull(executeBasicWebInteraction(userCookie, MyDetails.PATH, "fname=Kurti&lname=Hansel&mname=&suffix=&day=1&month=1&year=2000&processDetails", 0));
54         assertEquals("Kurti", getFname(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0))));
55         // But when assurer
56         makeAssurer(id);
57         // User cannot change his name, and the form changed
58         assertNotNull(executeBasicWebInteraction(userCookie, MyDetails.PATH, "fname=Kurti2&lname=Hansel&mname=&suffix=&day=1&month=1&year=2000&processDetails", 0));
59         assertNull(getFname(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0))));
60         assertEquals("Kurti", getFnamePlain(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0))));
61
62         // but support still can
63         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "fname=Kurti3&lname=Hansel&mname=&suffix=&dobd=1&dobm=2&doby=2000&detailupdate", 0));
64         assertEquals("Kurti3", getFnamePlain(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0))));
65
66     }
67
68     private String getFname(String res) {
69         Pattern p = Pattern.compile("<input type=\"text\" name=\"fname\" value=\"([^\"]*)\">");
70         Matcher m = p.matcher(res);
71         if (m.find()) {
72             return m.group(1);
73         }
74         return null;
75     }
76
77     private String getFnamePlain(String res) {
78         Pattern p = Pattern.compile("\\s*<td width=\"125\">First Name: </td>\\s*<td width=\"125\">([^<]*)</td>");
79         Matcher m = p.matcher(res);
80         if (m.find()) {
81             return m.group(1);
82         }
83         return null;
84     }
85 }