]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/admin/TestSEAdminPageDetails.java
upd: rename package name and all references to it
[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.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;
28
29 public class TestSEAdminPageDetails extends ClientTest {
30
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());
35     }
36
37     @Test
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 + "/");
44         uc.setDoOutput(true);
45         String res = IOUtils.readURL(uc);
46         assertThat(res, containsString(fname));
47         assertThat(res, containsString(lname));
48         assertThat(res, containsString(email));
49     }
50
51     @Test
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());
64
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)));
68
69         User.getById(id).updateDefaultEmail(ea);
70         clearCaches();
71         res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id + "/"));
72         assertEquals(1, countRegex(res, Pattern.quote(email)));
73         assertEquals(2, countRegex(res, Pattern.quote(email2)));
74     }
75
76     @Test
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);
83
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));
90
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));
95
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));
100
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));
105
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));
110
111     }
112
113     @Test
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);
120
121         // try to open mypoints as user
122         HttpURLConnection uc = get(clientCookie, SupportUserDetailsPage.PATH + id + "/points");
123
124         assertEquals(403, uc.getResponseCode());
125
126         // enter verification and open mypoints as supporter
127
128         makeAssurer(this.id);
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());
133             ps.setInt(3, 10);
134             ps.setString(4, location);
135             ps.setString(5, "2010-01-01");
136             ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
137             ps.execute();
138         }
139
140         uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
141
142         String res = IOUtils.readURL(uc);
143         assertThat(res, containsString("Support User Points"));
144         assertThat(res, containsString(location));
145
146         // remove ticket number and try to access mypoints from supporter
147         // account
148         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "deleteTicket=action", 0).getResponseCode());
149
150         uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
151
152         assertEquals(403, uc.getResponseCode());
153
154     }
155
156     private int logCountAdmin(int id) throws IOException {
157         return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
158     }
159
160     private int logCountUser(String cookie) throws IOException {
161         return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
162     }
163
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);
169         int i = 0;
170         int c = -1;
171         while (i != -1) {
172             i = logs.indexOf("<tr>", i + 1);
173             c++;
174         }
175         return c;
176     }
177
178 }