]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java
08541decf47f3eab2102b125983263b2d0eb4fff
[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 {
31         grant(email, Group.SUPPORTER);
32         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
33     }
34
35     @Test
36     public void testUserDetailsDisplay() throws MalformedURLException, IOException {
37         String email = createUniqueName() + "@example.com";
38         String fname = "Först";
39         String lname = "Secönd";
40         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
41         URLConnection uc = get(SupportUserDetailsPage.PATH + id + "/");
42         uc.setDoOutput(true);
43         String res = IOUtils.readURL(uc);
44         assertThat(res, containsString(fname));
45         assertThat(res, containsString(lname));
46         assertThat(res, containsString(email));
47     }
48
49     @Test
50     public void testUserDetailsEmail() throws MalformedURLException, IOException, GigiApiException {
51         String email = createUniqueName() + "@example.com";
52         String fname = "Först";
53         String lname = "Secönd";
54         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
55         String email2 = createUniqueName() + "@example.com";
56         EmailAddress ea = new EmailAddress(User.getById(id), email2, Locale.ENGLISH);
57         getMailReceiver().receive().verify();
58         // Refresh email Object
59         ObjectCache.clearAllCaches();
60         ea = EmailAddress.getById(ea.getId());
61         assertTrue(ea.isVerified());
62
63         String res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id + "/"));
64         assertEquals(2, countRegex(res, Pattern.quote(email)));
65         assertEquals(1, countRegex(res, Pattern.quote(email2)));
66
67         User.getById(id).updateDefaultEmail(ea);
68         clearCaches();
69         res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id + "/"));
70         assertEquals(1, countRegex(res, Pattern.quote(email)));
71         assertEquals(2, countRegex(res, Pattern.quote(email2)));
72     }
73
74     @Test
75     public void testUserDetailsEditToLog() throws MalformedURLException, IOException {
76         String email = createUniqueName() + "@example.com";
77         String fname = "Först";
78         String lname = "Secönd";
79         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
80         String clientCookie = login(email, TEST_PASSWORD);
81
82         assertEquals(0, logCountAdmin(id));
83         assertEquals(0, logCountUser(clientCookie));
84         // changing both leads to 2 entries
85         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0));
86         assertEquals(1, logCountAdmin(id));
87         assertEquals(1, logCountUser(clientCookie));
88
89         // Sending same data keeps same
90         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0));
91         assertEquals(1, logCountAdmin(id));
92         assertEquals(1, logCountUser(clientCookie));
93
94         // changing one leads to one entry
95         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=1&dobm=3&doby=2000&detailupdate", 0));
96         assertEquals(2, logCountAdmin(id));
97         assertEquals(2, logCountUser(clientCookie));
98
99         // changing one leads to one entry
100         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=2&dobm=3&doby=2000&detailupdate", 0));
101         assertEquals(3, logCountAdmin(id));
102         assertEquals(3, logCountUser(clientCookie));
103
104         // changing none -> no entry
105         assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id + "/", "dobd=2&dobm=3&doby=2000&detailupdate", 0));
106         assertEquals(3, logCountAdmin(id));
107         assertEquals(3, logCountUser(clientCookie));
108
109     }
110
111     @Test
112     public void testUserDetailsMyPoints() throws MalformedURLException, IOException {
113         String email = createUniqueName() + "@example.com";
114         String fname = "Först";
115         String lname = "Secönd";
116         int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
117         String clientCookie = login(email, TEST_PASSWORD);
118
119         // try to open mypoints as user
120         HttpURLConnection uc = get(clientCookie, SupportUserDetailsPage.PATH + id + "/points");
121
122         assertEquals(403, uc.getResponseCode());
123
124         // enter verification and open mypoints as supporter
125
126         makeAssurer(this.id);
127         String location = createUniqueName();
128         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
129             ps.setInt(1, this.id);
130             ps.setInt(2, User.getById(id).getPreferredName().getId());
131             ps.setInt(3, 10);
132             ps.setString(4, location);
133             ps.setString(5, "2010-01-01");
134             ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
135             ps.execute();
136         }
137
138         uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
139
140         String res = IOUtils.readURL(uc);
141         assertThat(res, containsString("Support User Points"));
142         assertThat(res, containsString(location));
143
144         // remove ticket number and try to access mypoints from supporter
145         // account
146         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "deleteTicket=action", 0).getResponseCode());
147
148         uc = get(cookie, SupportUserDetailsPage.PATH + id + "/points");
149
150         assertEquals(403, uc.getResponseCode());
151
152     }
153
154     private int logCountAdmin(int id) throws IOException {
155         return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id)))));
156     }
157
158     private int logCountUser(String cookie) throws IOException {
159         return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH)));
160     }
161
162     private int getLogEntryCount(String readURL) {
163         String s = "<tr><th>Support actions";
164         int start = readURL.indexOf(s);
165         int end = readURL.indexOf("</table>", start);
166         String logs = readURL.substring(start + s.length(), end);
167         int i = 0;
168         int c = -1;
169         while (i != -1) {
170             i = logs.indexOf("<tr>", i + 1);
171             c++;
172         }
173         return c;
174     }
175
176 }