Merge "add: show more certificates on the "roots" page"
[gigi.git] / tests / club / wpia / gigi / pages / admin / TestSEAdminNotificationMail.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.io.PrintWriter;
8 import java.io.StringWriter;
9 import java.net.MalformedURLException;
10 import java.net.URLEncoder;
11 import java.util.HashMap;
12 import java.util.Locale;
13
14 import org.junit.Test;
15
16 import club.wpia.gigi.GigiApiException;
17 import club.wpia.gigi.dbObjects.Group;
18 import club.wpia.gigi.dbObjects.User;
19 import club.wpia.gigi.localisation.Language;
20 import club.wpia.gigi.pages.admin.support.SupportEnterTicketPage;
21 import club.wpia.gigi.pages.admin.support.SupportUserDetailsPage;
22 import club.wpia.gigi.testUtils.ClientTest;
23 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
24 import club.wpia.gigi.util.ServerConstants;
25
26 public class TestSEAdminNotificationMail extends ClientTest {
27
28     private int targetID;
29
30     private String targetEmail;
31
32     public TestSEAdminNotificationMail() throws IOException, GigiApiException {
33         grant(u, Group.SUPPORTER);
34         cookie = login(email, TEST_PASSWORD);
35         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
36
37         targetEmail = createUniqueName() + "@example.com";
38         String fname = "Först";
39         String lname = "Secönd";
40         targetID = createVerifiedUser(fname, lname, targetEmail, TEST_PASSWORD);
41     }
42
43     @Test
44     public void testChangeAccountData() throws MalformedURLException, IOException {
45
46         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0);
47
48         // mail to support
49         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
50         assertThat(message, containsString("The DoB was changed"));
51         assertThat(message, containsString("supporter " + u.getPreferredName().toString() + " triggered:"));
52         // mail to user
53         message = getMailReceiver().receive(targetEmail).getMessage();
54         assertThat(message, containsString("The DoB in your account was changed to 2000-02-01."));
55     }
56
57     @Test
58     public void testPasswordReset() throws MalformedURLException, IOException {
59         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "aword=SecretWord&resetPass", 0);
60         getMailReceiver().receive(targetEmail);
61         TestMail tm = getMailReceiver().receive(ServerConstants.getSupportMailAddress());
62         assertThat(tm.getMessage(), containsString("A password reset was triggered and an email was sent to user."));
63     }
64
65     @Test
66     public void testGrantUserGroup() throws MalformedURLException, IOException {
67         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "addGroup&groupToModify=" + URLEncoder.encode(Group.CODESIGNING.getDBName(), "UTF-8"), 0);
68
69         StringWriter sw = new StringWriter();
70         PrintWriter pw = new PrintWriter(sw);
71         Group.CODESIGNING.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
72
73         // mail to support
74         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
75         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted."));
76         // mail to user
77         message = getMailReceiver().receive(targetEmail).getMessage();
78         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted to your account."));
79     }
80
81     @Test
82     public void testRemoveUserGroup() throws MalformedURLException, IOException {
83         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "removeGroup&groupToModify=" + URLEncoder.encode(Group.CODESIGNING.getDBName(), "UTF-8"), 0);
84
85         StringWriter sw = new StringWriter();
86         PrintWriter pw = new PrintWriter(sw);
87         Group.CODESIGNING.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
88
89         // mail to support
90         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
91         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked."));
92         // mail to user
93         message = getMailReceiver().receive(targetEmail).getMessage();
94         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked from your account."));
95     }
96
97     @Test
98     public void testGrantSupporterGroup() throws MalformedURLException, IOException {
99         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "addGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDBName(), "UTF-8"), 0);
100
101         StringWriter sw = new StringWriter();
102         PrintWriter pw = new PrintWriter(sw);
103         Group.SUPPORTER.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
104         User target = User.getById(targetID);
105
106         // mail to support
107         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
108         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted."));
109         // mail to user
110         message = getMailReceiver().receive(targetEmail).getMessage();
111         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted to your account."));
112         // mail to board
113         message = getMailReceiver().receive(ServerConstants.getBoardMailAddress()).getMessage();
114         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted for '" + target.getPreferredName().toString() + "'."));
115     }
116
117     @Test
118     public void testRemoveSupporterGroup() throws MalformedURLException, IOException {
119         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "removeGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDBName(), "UTF-8"), 0);
120
121         StringWriter sw = new StringWriter();
122         PrintWriter pw = new PrintWriter(sw);
123         Group.SUPPORTER.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
124         User target = User.getById(targetID);
125
126         // mail to support
127         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
128         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked."));
129         // mail to user
130         message = getMailReceiver().receive(targetEmail).getMessage();
131         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked from your account."));
132         // mail to board
133         message = getMailReceiver().receive(ServerConstants.getBoardMailAddress()).getMessage();
134         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked for '" + target.getPreferredName().toString() + "'."));
135     }
136
137     @Test
138     public void testRevokeAllCertificates() throws MalformedURLException, IOException {
139         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "revokeall", 1);
140         User user = User.getById(targetID);
141
142         // mail to support
143         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
144         assertThat(message, containsString("All certificates in the account " + user.getPreferredName().toString()));
145         // mail to user
146         message = getMailReceiver().receive(targetEmail).getMessage();
147         assertThat(message, containsString("All certificates in your account have been revoked."));
148     }
149 }