]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/admin/TestSEAdminPageDetails.java
9b92fcbc08dfb20dc52e1d83dc1a8472a242b3c1
[gigi.git] / tests / club / wpia / gigi / pages / admin / TestSEAdminPageDetails.java
1 package club.wpia.gigi.pages.admin;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
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;
13
14 import org.junit.Test;
15
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;
27
28 public class TestSEAdminPageDetails extends SEClientTest {
29
30     public TestSEAdminPageDetails() throws IOException, GigiApiException {}
31
32     @Test
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 + "/");
39         uc.setDoOutput(true);
40         String res = IOUtils.readURL(uc);
41         assertThat(res, containsString(fname));
42         assertThat(res, containsString(lname));
43         assertThat(res, containsString(email));
44     }
45
46     @Test
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());
59
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)));
63
64         User.getById(id).updateDefaultEmail(ea);
65         clearCaches();
66         res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id + "/"));
67         assertEquals(1, countRegex(res, Pattern.quote(email)));
68         assertEquals(2, countRegex(res, Pattern.quote(email2)));
69     }
70
71     @Test
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);
78
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));
85
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));
90
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));
95
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));
100
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));
105
106         // discarding all 6 mails
107         for (int i = 0; i < 6; i++) {
108             getMailReceiver().receive(null);
109         }
110     }
111
112     @Test
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);
119
120         // try to open mypoints as user
121         HttpURLConnection uc = get(clientCookie, SupportUserDetailsPage.PATH + id + "/points");
122
123         assertEquals(403, uc.getResponseCode());
124
125         // enter verification and open mypoints as supporter
126
127         makeAgent(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());
132             ps.setInt(3, 10);
133             ps.setString(4, location);
134             ps.setString(5, "2010-01-01");
135             ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
136             ps.execute();
137         }
138
139         uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
140
141         String res = IOUtils.readURL(uc);
142         assertThat(res, containsString("Support User Points"));
143         assertThat(res, containsString(location));
144
145         // remove ticket number and try to access mypoints from supporter
146         // account
147         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "deleteTicket=action", 0).getResponseCode());
148
149         uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
150
151         assertEquals(403, uc.getResponseCode());
152
153     }
154
155     private int logCountAdmin(int id) throws IOException {
156         return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
157     }
158
159     private int logCountUser(String cookie) throws IOException {
160         return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
161     }
162
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);
168         int i = 0;
169         int c = -1;
170         while (i != -1) {
171             i = logs.indexOf("<tr>", i + 1);
172             c++;
173         }
174         return c;
175     }
176
177 }