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.admin.support.SupportEnterTicketPage;
20 import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage;
21 import org.cacert.gigi.testUtils.ClientTest;
22 import org.cacert.gigi.testUtils.IOUtils;
23 import org.junit.Test;
25 public class TestSEAdminPageDetails extends ClientTest {
27 public TestSEAdminPageDetails() throws IOException {
28 grant(email, Group.SUPPORTER);
29 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
33 public void testUserDetailsDisplay() throws MalformedURLException, IOException {
34 String email = createUniqueName() + "@example.com";
35 String fname = "Först";
36 String lname = "Secönd";
37 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
38 URLConnection uc = get(SupportUserDetailsPage.PATH + id);
40 String res = IOUtils.readURL(uc);
41 assertThat(res, containsString(fname));
42 assertThat(res, containsString(lname));
43 assertThat(res, containsString(email));
47 public void testUserDetailsEmail() throws MalformedURLException, IOException, GigiApiException {
48 String email = createUniqueName() + "@example.com";
49 String fname = "Först";
50 String lname = "Secönd";
51 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
52 String email2 = createUniqueName() + "@example.com";
53 EmailAddress ea = new EmailAddress(User.getById(id), email2, Locale.ENGLISH);
54 getMailReceiver().receive().verify();
55 // Refresh email Object
56 ObjectCache.clearAllCaches();
57 ea = EmailAddress.getById(ea.getId());
58 assertTrue(ea.isVerified());
60 String res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id));
61 assertEquals(2, countRegex(res, Pattern.quote(email)));
62 assertEquals(1, countRegex(res, Pattern.quote(email2)));
64 User.getById(id).updateDefaultEmail(ea);
66 res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id));
67 assertEquals(1, countRegex(res, Pattern.quote(email)));
68 assertEquals(2, countRegex(res, Pattern.quote(email2)));
72 public void testUserDetailsEditToLog() throws MalformedURLException, IOException {
73 String email = createUniqueName() + "@example.com";
74 String fname = "Först";
75 String lname = "Secönd";
76 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
77 String clientCookie = login(email, TEST_PASSWORD);
79 assertEquals(0, logCountAdmin(id));
80 assertEquals(0, logCountUser(clientCookie));
81 // chaniging both leads to 2 entries
82 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "dobd=1&dobm=2&doby=2000&detailupdate", 0));
83 assertEquals(1, logCountAdmin(id));
84 assertEquals(1, logCountUser(clientCookie));
86 // Sending same data keeps same
87 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "dobd=1&dobm=2&doby=2000&detailupdate", 0));
88 assertEquals(1, logCountAdmin(id));
89 assertEquals(1, logCountUser(clientCookie));
91 // changing one leads to one entry
92 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "dobd=1&dobm=3&doby=2000&detailupdate", 0));
93 assertEquals(2, logCountAdmin(id));
94 assertEquals(2, logCountUser(clientCookie));
96 // changing one leads to one entry
97 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "dobd=2&dobm=3&doby=2000&detailupdate", 0));
98 assertEquals(3, logCountAdmin(id));
99 assertEquals(3, logCountUser(clientCookie));
101 // changing none -> no entry
102 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "dobd=2&dobm=3&doby=2000&detailupdate", 0));
103 assertEquals(3, logCountAdmin(id));
104 assertEquals(3, logCountUser(clientCookie));
108 private int logCountAdmin(int id) throws IOException {
109 return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
112 private int logCountUser(String cookie) throws IOException {
113 return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
116 private int getLogEntryCount(String readURL) {
117 String s = "<tr><th>Support actions";
118 int start = readURL.indexOf(s);
119 int end = readURL.indexOf("</table>", start);
120 String logs = readURL.substring(start + s.length(), end);
124 i = logs.indexOf("<tr>", i + 1);
130 private String getFname(String res) {
131 Pattern p = Pattern.compile("<span class='fname'>([^<]*)</span>");
132 Matcher m = p.matcher(res);