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 {
31 grant(email, Group.SUPPORTER);
32 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
36 public void testUserDetailsDisplay() throws MalformedURLException, IOException {
37 String email = createUniqueName() + "@example.com";
38 String fname = "Först";
39 String lname = "Secönd";
40 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
41 URLConnection uc = get(SupportUserDetailsPage.PATH + id + "/");
43 String res = IOUtils.readURL(uc);
44 assertThat(res, containsString(fname));
45 assertThat(res, containsString(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 getMailReceiver().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 testUserDetailsEditToLog() 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);
80 String clientCookie = login(email, TEST_PASSWORD);
82 assertEquals(0, logCountAdmin(id));
83 assertEquals(0, logCountUser(clientCookie));
84 // changing both leads to 2 entries
85 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0));
86 assertEquals(1, logCountAdmin(id));
87 assertEquals(1, logCountUser(clientCookie));
89 // Sending same data keeps same
90 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0));
91 assertEquals(1, logCountAdmin(id));
92 assertEquals(1, logCountUser(clientCookie));
94 // changing one leads to one entry
95 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=3&doby=2000&detailupdate", 0));
96 assertEquals(2, logCountAdmin(id));
97 assertEquals(2, logCountUser(clientCookie));
99 // changing one leads to one entry
100 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=2&dobm=3&doby=2000&detailupdate", 0));
101 assertEquals(3, logCountAdmin(id));
102 assertEquals(3, logCountUser(clientCookie));
104 // changing none -> no entry
105 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=2&dobm=3&doby=2000&detailupdate", 0));
106 assertEquals(3, logCountAdmin(id));
107 assertEquals(3, logCountUser(clientCookie));
112 public void testUserDetailsMyPoints() throws MalformedURLException, IOException {
113 String email = createUniqueName() + "@example.com";
114 String fname = "Först";
115 String lname = "Secönd";
116 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
117 String clientCookie = login(email, TEST_PASSWORD);
119 // try to open mypoints as user
120 HttpURLConnection uc = get(clientCookie, SupportUserDetailsPage.PATH + id + "/points");
122 assertEquals(403, uc.getResponseCode());
124 // enter verification and open mypoints as supporter
126 makeAssurer(this.id);
127 String location = createUniqueName();
128 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
129 ps.setInt(1, this.id);
130 ps.setInt(2, User.getById(id).getPreferredName().getId());
132 ps.setString(4, location);
133 ps.setString(5, "2010-01-01");
134 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
138 uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
140 String res = IOUtils.readURL(uc);
141 assertThat(res, containsString("Support User Points"));
142 assertThat(res, containsString(location));
144 // remove ticket number and try to access mypoints from supporter
146 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "deleteTicket=action", 0).getResponseCode());
148 uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
150 assertEquals(403, uc.getResponseCode());
154 private int logCountAdmin(int id) throws IOException {
155 return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
158 private int logCountUser(String cookie) throws IOException {
159 return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
162 private int getLogEntryCount(String readURL) {
163 String s = "<tr><th>Support actions";
164 int start = readURL.indexOf(s);
165 int end = readURL.indexOf("</table>", start);
166 String logs = readURL.substring(start + s.length(), end);
170 i = logs.indexOf("<tr>", i + 1);