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