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.regex.Matcher;
11 import java.util.regex.Pattern;
13 import org.cacert.gigi.dbObjects.Group;
14 import org.cacert.gigi.pages.account.MyDetails;
15 import org.cacert.gigi.pages.account.UserHistory;
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;
22 public class TestSEAdminPageDetails extends ClientTest {
24 public TestSEAdminPageDetails() throws IOException {
25 grant(email, Group.SUPPORTER);
26 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
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);
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));
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);
51 String userCookie = login(email, TEST_PASSWORD);
52 assertEquals("Först", getFname(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0))));
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, 0))));
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, 0))));
61 assertEquals("Kurti", getFnamePlain(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0))));
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, 0))));
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);
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));
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));
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));
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));
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));
106 private int logCountAdmin(int id) throws IOException {
107 return getLogEntryCount(IOUtils.readURL(get(UserHistory.SUPPORT_PATH.replace("*", Integer.toString(id)), 0)));
110 private int logCountUser(String cookie) throws IOException {
111 return getLogEntryCount(IOUtils.readURL(get(cookie, UserHistory.PATH, 0)));
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);
122 i = logs.indexOf("<tr>", i + 1);
128 private String getFname(String res) {
129 Pattern p = Pattern.compile("<input type=\"text\" name=\"fname\" value=\"([^\"]*)\">");
130 Matcher m = p.matcher(res);
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);