]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java
3cb78b9a10b232b91de888fc500739de040366b9
[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.account.History;
16 import org.cacert.gigi.pages.admin.support.SupportEnterTicketPage;
17 import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage;
18 import org.cacert.gigi.testUtils.ClientTest;
19 import org.cacert.gigi.testUtils.IOUtils;
20 import org.junit.Test;
21
22 public class TestSEAdminPageDetails extends ClientTest {
23
24     public TestSEAdminPageDetails() throws IOException {
25         grant(email, Group.SUPPORTER);
26         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
27     }
28
29     @Test
30     public void testUserDetailsDisplay() throws MalformedURLException, IOException {
31         String email = createUniqueName() + "@example.com";
32         String fname = "Först";
33         String lname = "Secönd";
34         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
35         URLConnection uc = new URL("https://" + getServerName() + SupportUserDetailsPage.PATH + id).openConnection();
36         uc.addRequestProperty("Cookie", cookie);
37         uc.setDoOutput(true);
38         String res = IOUtils.readURL(uc);
39         assertThat(res, containsString("<input type=\"text\" value=\"" + fname + "\" name=\"fname\">"));
40         assertThat(res, containsString("<input type=\"text\" value=\"" + lname + "\" name=\"lname\">"));
41         assertThat(res, containsString(email));
42     }
43
44     @Test
45     public void testUserDetailsEdit() throws MalformedURLException, IOException {
46         String email = createUniqueName() + "@example.com";
47         String fname = "Först";
48         String lname = "Secönd";
49         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
50
51         String userCookie = login(email, TEST_PASSWORD);
52         assertEquals("Först", getFname(IOUtils.readURL(get(userCookie, MyDetails.PATH))));
53         // User can change his name
54         assertNull(executeBasicWebInteraction(userCookie, MyDetails.PATH, "fname=Kurti&lname=Hansel&mname=&suffix=&day=1&month=1&year=2000&processDetails", 0));
55         assertEquals("Kurti", getFname(IOUtils.readURL(get(userCookie, MyDetails.PATH))));
56         // But when assurer
57         makeAssurer(id);
58         // User cannot change his name, and the form changed
59         assertNotNull(executeBasicWebInteraction(userCookie, MyDetails.PATH, "fname=Kurti2&lname=Hansel&mname=&suffix=&day=1&month=1&year=2000&processDetails", 0));
60         assertNull(getFname(IOUtils.readURL(get(userCookie, MyDetails.PATH))));
61         assertEquals("Kurti", getFnamePlain(IOUtils.readURL(get(userCookie, MyDetails.PATH))));
62
63         // but support still can
64         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "fname=Kurti3&lname=Hansel&mname=&suffix=&dobd=1&dobm=2&doby=2000&detailupdate", 0));
65         assertEquals("Kurti3", getFnamePlain(IOUtils.readURL(get(userCookie, MyDetails.PATH))));
66
67     }
68
69     @Test
70     public void testUserDetailsEditToLog() throws MalformedURLException, IOException {
71         String email = createUniqueName() + "@example.com";
72         String fname = "Först";
73         String lname = "Secönd";
74         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
75         String clientCookie = login(email, TEST_PASSWORD);
76
77         assertEquals(0, logCountAdmin(id));
78         assertEquals(0, logCountUser(clientCookie));
79         // chaniging both leads to 2 entries
80         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "fname=Kurti&lname=Hansel&mname=&suffix=&dobd=1&dobm=2&doby=2000&detailupdate", 0));
81         assertEquals(2, logCountAdmin(id));
82         assertEquals(2, logCountUser(clientCookie));
83
84         // Sending same data keeps same
85         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "fname=Kurti&lname=Hansel&mname=&suffix=&dobd=1&dobm=2&doby=2000&detailupdate", 0));
86         assertEquals(2, logCountAdmin(id));
87         assertEquals(2, logCountUser(clientCookie));
88
89         // changing one leads to one entry
90         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "fname=Kurti2&lname=Hansel&mname=&suffix=&dobd=1&dobm=2&doby=2000&detailupdate", 0));
91         assertEquals(3, logCountAdmin(id));
92         assertEquals(3, logCountUser(clientCookie));
93
94         // changing one leads to one entry
95         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "fname=Kurti2&lname=Hansel&mname=&suffix=&dobd=2&dobm=2&doby=2000&detailupdate", 0));
96         assertEquals(4, logCountAdmin(id));
97         assertEquals(4, logCountUser(clientCookie));
98
99         // changing none -> no entry
100         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "fname=Kurti2&lname=Hansel&mname=&suffix=&dobd=2&dobm=2&doby=2000&detailupdate", 0));
101         assertEquals(4, logCountAdmin(id));
102         assertEquals(4, logCountUser(clientCookie));
103
104     }
105
106     private int logCountAdmin(int id) throws IOException {
107         return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
108     }
109
110     private int logCountUser(String cookie) throws IOException {
111         return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
112     }
113
114     private int getLogEntryCount(String readURL) {
115         String s = "<tr><th>Support actions";
116         int start = readURL.indexOf(s);
117         int end = readURL.indexOf("</table>", start);
118         String logs = readURL.substring(start + s.length(), end);
119         int i = 0;
120         int c = -1;
121         while (i != -1) {
122             i = logs.indexOf("<tr>", i + 1);
123             c++;
124         }
125         return c;
126     }
127
128     private String getFname(String res) {
129         Pattern p = Pattern.compile("<input type=\"text\" name=\"fname\" value=\"([^\"]*)\">");
130         Matcher m = p.matcher(res);
131         if (m.find()) {
132             return m.group(1);
133         }
134         return null;
135     }
136
137     private String getFnamePlain(String res) {
138         Pattern p = Pattern.compile("\\s*<td width=\"125\">First Name: </td>\\s*<td width=\"125\">([^<]*)</td>");
139         Matcher m = p.matcher(res);
140         if (m.find()) {
141             return m.group(1);
142         }
143         return null;
144     }
145 }