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;
9 import java.net.URLConnection;
10 import java.util.Locale;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
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;
27 public class TestSEAdminPageDetails extends ClientTest {
29 public TestSEAdminPageDetails() throws IOException {
30 grant(email, Group.SUPPORTER);
31 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
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);
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));
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());
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)));
67 User.getById(id).updateDefaultEmail(ea);
69 res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id));
70 assertEquals(1, countRegex(res, Pattern.quote(email)));
71 assertEquals(2, countRegex(res, Pattern.quote(email2)));
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);
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))));
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))));
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))));
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);
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));
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));
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));
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));
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));
136 private int logCountAdmin(int id) throws IOException {
137 return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
140 private int logCountUser(String cookie) throws IOException {
141 return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
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);
152 i = logs.indexOf("<tr>", i + 1);
158 private String getFname(String res) {
159 Pattern p = Pattern.compile("<input type=\"text\" name=\"fname\" value=\"([^\"]*)\">");
160 Matcher m = p.matcher(res);
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);