1 package club.wpia.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.junit.Test;
16 import club.wpia.gigi.GigiApiException;
17 import club.wpia.gigi.database.GigiPreparedStatement;
18 import club.wpia.gigi.dbObjects.EmailAddress;
19 import club.wpia.gigi.dbObjects.Group;
20 import club.wpia.gigi.dbObjects.ObjectCache;
21 import club.wpia.gigi.dbObjects.User;
22 import club.wpia.gigi.pages.account.History;
23 import club.wpia.gigi.pages.admin.support.SupportEnterTicketPage;
24 import club.wpia.gigi.pages.admin.support.SupportUserDetailsPage;
25 import club.wpia.gigi.testUtils.ClientTest;
26 import club.wpia.gigi.testUtils.IOUtils;
27 import club.wpia.gigi.util.DayDate;
29 public class TestSEAdminPageDetails extends ClientTest {
31 public TestSEAdminPageDetails() throws IOException, GigiApiException {
32 grant(u, Group.SUPPORTER);
33 cookie = login(email, TEST_PASSWORD);
34 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
38 public void testUserDetailsDisplay() throws MalformedURLException, IOException {
39 String email = createUniqueName() + "@example.com";
40 String fname = "Först";
41 String lname = "Secönd";
42 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
43 URLConnection uc = get(SupportUserDetailsPage.PATH + id + "/");
45 String res = IOUtils.readURL(uc);
46 assertThat(res, containsString(fname));
47 assertThat(res, containsString(lname));
48 assertThat(res, containsString(email));
52 public void testUserDetailsEmail() throws MalformedURLException, IOException, GigiApiException {
53 String email = createUniqueName() + "@example.com";
54 String fname = "Först";
55 String lname = "Secönd";
56 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
57 String email2 = createUniqueName() + "@example.com";
58 EmailAddress ea = new EmailAddress(User.getById(id), email2, Locale.ENGLISH);
59 getMailReceiver().receive().verify();
60 // Refresh email Object
61 ObjectCache.clearAllCaches();
62 ea = EmailAddress.getById(ea.getId());
63 assertTrue(ea.isVerified());
65 String res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id + "/"));
66 assertEquals(2, countRegex(res, Pattern.quote(email)));
67 assertEquals(1, countRegex(res, Pattern.quote(email2)));
69 User.getById(id).updateDefaultEmail(ea);
71 res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id + "/"));
72 assertEquals(1, countRegex(res, Pattern.quote(email)));
73 assertEquals(2, countRegex(res, Pattern.quote(email2)));
77 public void testUserDetailsEditToLog() throws MalformedURLException, IOException {
78 String email = createUniqueName() + "@example.com";
79 String fname = "Först";
80 String lname = "Secönd";
81 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
82 String clientCookie = login(email, TEST_PASSWORD);
84 assertEquals(0, logCountAdmin(id));
85 assertEquals(0, logCountUser(clientCookie));
86 // changing both leads to 2 entries
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 // Sending same data keeps same
92 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0));
93 assertEquals(1, logCountAdmin(id));
94 assertEquals(1, logCountUser(clientCookie));
96 // changing one leads to one entry
97 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=3&doby=2000&detailupdate", 0));
98 assertEquals(2, logCountAdmin(id));
99 assertEquals(2, logCountUser(clientCookie));
101 // changing one leads to one 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));
106 // changing none -> no entry
107 assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=2&dobm=3&doby=2000&detailupdate", 0));
108 assertEquals(3, logCountAdmin(id));
109 assertEquals(3, logCountUser(clientCookie));
114 public void testUserDetailsMyPoints() throws MalformedURLException, IOException {
115 String email = createUniqueName() + "@example.com";
116 String fname = "Först";
117 String lname = "Secönd";
118 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
119 String clientCookie = login(email, TEST_PASSWORD);
121 // try to open mypoints as user
122 HttpURLConnection uc = get(clientCookie, SupportUserDetailsPage.PATH + id + "/points");
124 assertEquals(403, uc.getResponseCode());
126 // enter verification and open mypoints as supporter
129 String location = createUniqueName();
130 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
131 ps.setInt(1, this.id);
132 ps.setInt(2, User.getById(id).getPreferredName().getId());
134 ps.setString(4, location);
135 ps.setString(5, "2010-01-01");
136 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
140 uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
142 String res = IOUtils.readURL(uc);
143 assertThat(res, containsString("Support User Points"));
144 assertThat(res, containsString(location));
146 // remove ticket number and try to access mypoints from supporter
148 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "deleteTicket=action", 0).getResponseCode());
150 uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
152 assertEquals(403, uc.getResponseCode());
156 private int logCountAdmin(int id) throws IOException {
157 return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
160 private int logCountUser(String cookie) throws IOException {
161 return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
164 private int getLogEntryCount(String readURL) {
165 String s = "<tr><th>Support actions";
166 int start = readURL.indexOf(s);
167 int end = readURL.indexOf("</table>", start);
168 String logs = readURL.substring(start + s.length(), end);
172 i = logs.indexOf("<tr>", i + 1);