]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / tests / org / cacert / gigi / pages / admin / TestSEAdminPageDetails.java
1 package org.cacert.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.cacert.gigi.GigiApiException;
15 import org.cacert.gigi.database.GigiPreparedStatement;
16 import org.cacert.gigi.dbObjects.EmailAddress;
17 import org.cacert.gigi.dbObjects.Group;
18 import org.cacert.gigi.dbObjects.ObjectCache;
19 import org.cacert.gigi.dbObjects.User;
20 import org.cacert.gigi.pages.account.History;
21 import org.cacert.gigi.pages.admin.support.SupportEnterTicketPage;
22 import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage;
23 import org.cacert.gigi.testUtils.ClientTest;
24 import org.cacert.gigi.testUtils.IOUtils;
25 import org.cacert.gigi.util.DayDate;
26 import org.junit.Test;
27
28 public class TestSEAdminPageDetails extends ClientTest {
29
30     public TestSEAdminPageDetails() throws IOException, GigiApiException {
31         grant(u, Group.SUPPORTER);
32         cookie = login(email, TEST_PASSWORD);
33         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
34     }
35
36     @Test
37     public void testUserDetailsDisplay() throws MalformedURLException, IOException {
38         String email = createUniqueName() + "@example.com";
39         String fname = "Först";
40         String lname = "Secönd";
41         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
42         URLConnection uc = get(SupportUserDetailsPage.PATH + id + "/");
43         uc.setDoOutput(true);
44         String res = IOUtils.readURL(uc);
45         assertThat(res, containsString(fname));
46         assertThat(res, containsString(lname));
47         assertThat(res, containsString(email));
48     }
49
50     @Test
51     public void testUserDetailsEmail() throws MalformedURLException, IOException, GigiApiException {
52         String email = createUniqueName() + "@example.com";
53         String fname = "Först";
54         String lname = "Secönd";
55         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
56         String email2 = createUniqueName() + "@example.com";
57         EmailAddress ea = new EmailAddress(User.getById(id), email2, Locale.ENGLISH);
58         getMailReceiver().receive().verify();
59         // Refresh email Object
60         ObjectCache.clearAllCaches();
61         ea = EmailAddress.getById(ea.getId());
62         assertTrue(ea.isVerified());
63
64         String res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id + "/"));
65         assertEquals(2, countRegex(res, Pattern.quote(email)));
66         assertEquals(1, countRegex(res, Pattern.quote(email2)));
67
68         User.getById(id).updateDefaultEmail(ea);
69         clearCaches();
70         res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id + "/"));
71         assertEquals(1, countRegex(res, Pattern.quote(email)));
72         assertEquals(2, countRegex(res, Pattern.quote(email2)));
73     }
74
75     @Test
76     public void testUserDetailsEditToLog() throws MalformedURLException, IOException {
77         String email = createUniqueName() + "@example.com";
78         String fname = "Först";
79         String lname = "Secönd";
80         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
81         String clientCookie = login(email, TEST_PASSWORD);
82
83         assertEquals(0, logCountAdmin(id));
84         assertEquals(0, logCountUser(clientCookie));
85         // changing both leads to 2 entries
86         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0));
87         assertEquals(1, logCountAdmin(id));
88         assertEquals(1, logCountUser(clientCookie));
89
90         // Sending same data keeps same
91         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0));
92         assertEquals(1, logCountAdmin(id));
93         assertEquals(1, logCountUser(clientCookie));
94
95         // changing one leads to one entry
96         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=3&doby=2000&detailupdate", 0));
97         assertEquals(2, logCountAdmin(id));
98         assertEquals(2, logCountUser(clientCookie));
99
100         // changing one leads to one entry
101         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=2&dobm=3&doby=2000&detailupdate", 0));
102         assertEquals(3, logCountAdmin(id));
103         assertEquals(3, logCountUser(clientCookie));
104
105         // changing none -> no entry
106         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=2&dobm=3&doby=2000&detailupdate", 0));
107         assertEquals(3, logCountAdmin(id));
108         assertEquals(3, logCountUser(clientCookie));
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         makeAssurer(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 }