1 package org.cacert.gigi.pages.admin;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
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;
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;
26 public class TestSEAdminPageDetails extends ClientTest {
28 public TestSEAdminPageDetails() throws IOException {
29 grant(email, Group.SUPPORTER);
30 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
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);
41 String res = IOUtils.readURL(uc);
42 assertThat(res, containsString("type=\"text\" value=\"" + fname + "\" name=\"fname\">"));
43 assertThat(res, containsString("type=\"text\" value=\"" + lname + "\" name=\"lname\">"));
44 assertThat(res, containsString(email));
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());
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)));
65 User.getById(id).updateDefaultEmail(ea);
67 res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id));
68 assertEquals(1, countRegex(res, Pattern.quote(email)));
69 assertEquals(2, countRegex(res, Pattern.quote(email2)));
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);
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))));
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))));
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))));
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);
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));
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));
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));
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));
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));
134 private int logCountAdmin(int id) throws IOException {
135 return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
138 private int logCountUser(String cookie) throws IOException {
139 return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
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);
150 i = logs.indexOf("<tr>", i + 1);
156 private String getFname(String res) {
157 Pattern p = Pattern.compile("type=\"text\" name=\"fname\" value=\"([^\"]*)\">");
158 Matcher m = p.matcher(res);
165 private String getFnamePlain(String res) {
166 Pattern p = Pattern.compile("\\s*<td[^>]*>First Name: </td>\\s*<td[^>]*>([^<]*)</td>");
167 Matcher m = p.matcher(res);