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.HttpURLConnection;
8 import java.net.MalformedURLException;
9 import java.net.URLConnection;
10 import java.sql.Timestamp;
11 import java.util.Locale;
12 import java.util.regex.Pattern;
14 import org.cacert.gigi.GigiApiException;
15 import org.cacert.gigi.database.GigiPreparedStatement;
16 import org.cacert.gigi.dbObjects.EmailAddress;
17 import org.cacert.gigi.dbObjects.Group;
18 import org.cacert.gigi.dbObjects.ObjectCache;
19 import org.cacert.gigi.dbObjects.User;
20 import org.cacert.gigi.pages.account.History;
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.cacert.gigi.util.DayDate;
26 import org.junit.Test;
28 public class TestSEAdminPageDetails extends ClientTest {
30 public TestSEAdminPageDetails() throws IOException, GigiApiException {
31 grant(u, Group.SUPPORTER);
32 cookie = login(email, TEST_PASSWORD);
33 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
37 public void testUserDetailsDisplay() throws MalformedURLException, IOException {
38 String email = createUniqueName() + "@example.com";
39 String fname = "Först";
40 String lname = "Secönd";
41 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
42 URLConnection uc = get(SupportUserDetailsPage.PATH + id + "/");
44 String res = IOUtils.readURL(uc);
45 assertThat(res, containsString(fname));
46 assertThat(res, containsString(lname));
47 assertThat(res, containsString(email));
51 public void testUserDetailsEmail() throws MalformedURLException, IOException, GigiApiException {
52 String email = createUniqueName() + "@example.com";
53 String fname = "Först";
54 String lname = "Secönd";
55 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
56 String email2 = createUniqueName() + "@example.com";
57 EmailAddress ea = new EmailAddress(User.getById(id), email2, Locale.ENGLISH);
58 getMailReceiver().receive().verify();
59 // Refresh email Object
60 ObjectCache.clearAllCaches();
61 ea = EmailAddress.getById(ea.getId());
62 assertTrue(ea.isVerified());
64 String res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id + "/"));
65 assertEquals(2, countRegex(res, Pattern.quote(email)));
66 assertEquals(1, countRegex(res, Pattern.quote(email2)));
68 User.getById(id).updateDefaultEmail(ea);
70 res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id + "/"));
71 assertEquals(1, countRegex(res, Pattern.quote(email)));
72 assertEquals(2, countRegex(res, Pattern.quote(email2)));
76 public void testUserDetailsEditToLog() throws MalformedURLException, IOException {
77 String email = createUniqueName() + "@example.com";
78 String fname = "Först";
79 String lname = "Secönd";
80 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
81 String clientCookie = login(email, TEST_PASSWORD);
83 assertEquals(0, logCountAdmin(id));
84 assertEquals(0, logCountUser(clientCookie));
85 // changing both leads to 2 entries
86 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0));
87 assertEquals(1, logCountAdmin(id));
88 assertEquals(1, logCountUser(clientCookie));
90 // Sending same data keeps same
91 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0));
92 assertEquals(1, logCountAdmin(id));
93 assertEquals(1, logCountUser(clientCookie));
95 // changing one leads to one entry
96 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=3&doby=2000&detailupdate", 0));
97 assertEquals(2, logCountAdmin(id));
98 assertEquals(2, logCountUser(clientCookie));
100 // changing one leads to one entry
101 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=2&dobm=3&doby=2000&detailupdate", 0));
102 assertEquals(3, logCountAdmin(id));
103 assertEquals(3, logCountUser(clientCookie));
105 // changing none -> no entry
106 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=2&dobm=3&doby=2000&detailupdate", 0));
107 assertEquals(3, logCountAdmin(id));
108 assertEquals(3, logCountUser(clientCookie));
113 public void testUserDetailsMyPoints() throws MalformedURLException, IOException {
114 String email = createUniqueName() + "@example.com";
115 String fname = "Först";
116 String lname = "Secönd";
117 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
118 String clientCookie = login(email, TEST_PASSWORD);
120 // try to open mypoints as user
121 HttpURLConnection uc = get(clientCookie, SupportUserDetailsPage.PATH + id + "/points");
123 assertEquals(403, uc.getResponseCode());
125 // enter verification and open mypoints as supporter
127 makeAssurer(this.id);
128 String location = createUniqueName();
129 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
130 ps.setInt(1, this.id);
131 ps.setInt(2, User.getById(id).getPreferredName().getId());
133 ps.setString(4, location);
134 ps.setString(5, "2010-01-01");
135 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
139 uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
141 String res = IOUtils.readURL(uc);
142 assertThat(res, containsString("Support User Points"));
143 assertThat(res, containsString(location));
145 // remove ticket number and try to access mypoints from supporter
147 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "deleteTicket=action", 0).getResponseCode());
149 uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
151 assertEquals(403, uc.getResponseCode());
155 private int logCountAdmin(int id) throws IOException {
156 return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
159 private int logCountUser(String cookie) throws IOException {
160 return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
163 private int getLogEntryCount(String readURL) {
164 String s = "<tr><th>Support actions";
165 int start = readURL.indexOf(s);
166 int end = readURL.indexOf("</table>", start);
167 String logs = readURL.substring(start + s.length(), end);
171 i = logs.indexOf("<tr>", i + 1);