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.ObjectCache;
20 import club.wpia.gigi.dbObjects.User;
21 import club.wpia.gigi.pages.account.History;
22 import club.wpia.gigi.pages.admin.support.SupportEnterTicketPage;
23 import club.wpia.gigi.pages.admin.support.SupportUserDetailsPage;
24 import club.wpia.gigi.testUtils.IOUtils;
25 import club.wpia.gigi.testUtils.SEClientTest;
26 import club.wpia.gigi.util.DayDate;
28 public class TestSEAdminPageDetails extends SEClientTest {
30 public TestSEAdminPageDetails() throws IOException, GigiApiException {}
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(email2).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 // changing 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));
106 // discarding all 6 mails
107 for (int i = 0; i < 6; i++) {
108 getMailReceiver().receive(null);
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
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);