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.Certificate;
19 import club.wpia.gigi.dbObjects.EmailAddress;
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.IOUtils;
26 import club.wpia.gigi.testUtils.SEClientTest;
27 import club.wpia.gigi.util.DayDate;
29 public class TestSEAdminPageDetails extends SEClientTest {
31 private Certificate cs;
33 public TestSEAdminPageDetails() throws IOException, GigiApiException {
34 cs = loginCertificate;
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(email2).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));
111 // discarding all 6 mails
112 for (int i = 0; i < 6; i++) {
113 getMailReceiver().receive(null);
118 public void testUserDetailsMyPoints() throws MalformedURLException, IOException {
119 String email = createUniqueName() + "@example.com";
120 String fname = "Först";
121 String lname = "Secönd";
122 int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
123 String clientCookie = login(email, TEST_PASSWORD);
125 // try to open mypoints as user
126 loginCertificate = null;
127 HttpURLConnection uc = get(clientCookie, SupportUserDetailsPage.PATH + id + "/points");
129 assertEquals(403, uc.getResponseCode());
131 // enter verification and open mypoints as supporter
132 loginCertificate = cs;
134 String location = createUniqueName();
135 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
136 ps.setInt(1, this.id);
137 ps.setInt(2, User.getById(id).getPreferredName().getId());
139 ps.setString(4, location);
140 ps.setString(5, "2010-01-01");
141 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
145 uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
147 String res = IOUtils.readURL(uc);
148 assertThat(res, containsString("Support User Points"));
149 assertThat(res, containsString(location));
151 // remove ticket number and try to access mypoints from supporter
153 assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "deleteTicket=action", 0).getResponseCode());
155 uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
157 assertEquals(403, uc.getResponseCode());
161 private int logCountAdmin(int id) throws IOException {
162 return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
165 private int logCountUser(String cookie) throws IOException {
166 loginCertificate = null;
167 int count = getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
168 loginCertificate = cs;
172 private int getLogEntryCount(String readURL) {
173 String s = "<tr><th>Actions in user account";
174 int start = readURL.indexOf(s);
175 int end = readURL.indexOf("</table>", start);
176 String logs = readURL.substring(start + s.length(), end);
180 i = logs.indexOf("<tr>", i + 1);
187 public void testUserDetailsRAAgent() throws IOException, GigiApiException {
188 User u0 = User.getById(createVerifiedUser("Kurti", "Hansel", createUniqueName() + "@email.com", TEST_PASSWORD));
190 HttpURLConnection uc = get(cookie, SupportUserDetailsPage.PATH + u0.getId() + "/");
191 String res = IOUtils.readURL(uc);
192 assertThat(res, containsString("No (RA Agent Contract signed: No)"));
195 uc = get(cookie, SupportUserDetailsPage.PATH + u0.getId() + "/");
196 res = IOUtils.readURL(uc);
197 assertThat(res, containsString("No (RA Agent Contract signed: Yes)"));
199 insertPassedTest(u0.getId());
200 uc = get(cookie, SupportUserDetailsPage.PATH + u0.getId() + "/");
201 res = IOUtils.readURL(uc);
202 assertThat(res, containsString("No (RA Agent Contract signed: Yes)"));
204 insertVerificationPoints(u0.getId());
205 uc = get(cookie, SupportUserDetailsPage.PATH + u0.getId() + "/");
206 res = IOUtils.readURL(uc);
207 assertThat(res, not(containsString("RA Agent Contract signed:")));