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