]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/admin/TestSEAdminNotificationMail.java
add: ensure that for support actions certificate login is used
[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.URLConnection;
11 import java.net.URLEncoder;
12 import java.util.HashMap;
13 import java.util.Locale;
14
15 import org.junit.Test;
16
17 import club.wpia.gigi.GigiApiException;
18 import club.wpia.gigi.dbObjects.Group;
19 import club.wpia.gigi.dbObjects.User;
20 import club.wpia.gigi.localisation.Language;
21 import club.wpia.gigi.pages.account.MyDetails;
22 import club.wpia.gigi.pages.admin.support.SupportUserDetailsPage;
23 import club.wpia.gigi.testUtils.IOUtils;
24 import club.wpia.gigi.testUtils.SEClientTest;
25 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
26 import club.wpia.gigi.util.ServerConstants;
27 import club.wpia.gigi.util.ServerConstants.Host;
28
29 public class TestSEAdminNotificationMail extends SEClientTest {
30
31     private int targetID;
32
33     private String targetEmail;
34
35     public TestSEAdminNotificationMail() throws IOException, GigiApiException {
36         targetEmail = createUniqueName() + "@example.com";
37         String fname = "Först";
38         String lname = "Secönd";
39         targetID = createVerifiedUser(fname, lname, targetEmail, TEST_PASSWORD);
40     }
41
42     @Test
43     public void testChangeAccountData() throws MalformedURLException, IOException {
44
45         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0);
46
47         // mail to support
48         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
49         assertThat(message, containsString("The DoB was changed"));
50         assertThat(message, containsString("supporter " + u.getPreferredName().toString() + " triggered:"));
51         // mail to user
52         message = getMailReceiver().receive(targetEmail).getMessage();
53         assertThat(message, containsString("The DoB in your account was changed to 2000-02-01."));
54     }
55
56     @Test
57     public void testPasswordReset() throws MalformedURLException, IOException {
58         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "aword=SecretWord&resetPass", 0);
59         getMailReceiver().receive(targetEmail);
60         TestMail tm = getMailReceiver().receive(ServerConstants.getSupportMailAddress());
61         assertThat(tm.getMessage(), containsString("A password reset was triggered and an email was sent to user."));
62     }
63
64     @Test
65     public void testGrantUserGroup() throws MalformedURLException, IOException {
66         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "addGroup&groupToModify=" + URLEncoder.encode(Group.CODESIGNING.getDBName(), "UTF-8"), 0);
67
68         StringWriter sw = new StringWriter();
69         PrintWriter pw = new PrintWriter(sw);
70         Group.CODESIGNING.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
71
72         // mail to support
73         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
74         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted."));
75         // mail to user
76         message = getMailReceiver().receive(targetEmail).getMessage();
77         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted to your account."));
78     }
79
80     @Test
81     public void testRemoveUserGroup() throws MalformedURLException, IOException {
82         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "removeGroup&groupToModify=" + URLEncoder.encode(Group.CODESIGNING.getDBName(), "UTF-8"), 0);
83
84         StringWriter sw = new StringWriter();
85         PrintWriter pw = new PrintWriter(sw);
86         Group.CODESIGNING.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
87
88         // mail to support
89         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
90         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked."));
91         // mail to user
92         message = getMailReceiver().receive(targetEmail).getMessage();
93         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked from your account."));
94     }
95
96     @Test
97     public void testGrantSupporterGroup() throws MalformedURLException, IOException {
98         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "addGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDBName(), "UTF-8"), 0);
99
100         StringWriter sw = new StringWriter();
101         PrintWriter pw = new PrintWriter(sw);
102         Group.SUPPORTER.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
103         User target = User.getById(targetID);
104
105         // mail to support
106         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
107         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted."));
108         // mail to user
109         message = getMailReceiver().receive(targetEmail).getMessage();
110         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted to your account."));
111         // mail to board
112         message = getMailReceiver().receive(ServerConstants.getBoardMailAddress()).getMessage();
113         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted for '" + target.getPreferredName().toString() + "'."));
114     }
115
116     @Test
117     public void testRemoveSupporterGroup() throws MalformedURLException, IOException {
118         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "removeGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDBName(), "UTF-8"), 0);
119
120         StringWriter sw = new StringWriter();
121         PrintWriter pw = new PrintWriter(sw);
122         Group.SUPPORTER.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
123         User target = User.getById(targetID);
124
125         // mail to support
126         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
127         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked."));
128         // mail to user
129         message = getMailReceiver().receive(targetEmail).getMessage();
130         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked from your account."));
131         // mail to board
132         message = getMailReceiver().receive(ServerConstants.getBoardMailAddress()).getMessage();
133         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked for '" + target.getPreferredName().toString() + "'."));
134     }
135
136     @Test
137     public void testRevokeAllCertificates() throws MalformedURLException, IOException {
138         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "revokeall", 1);
139         User user = User.getById(targetID);
140
141         // mail to support
142         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
143         assertThat(message, containsString("All certificates in the account " + user.getPreferredName().toString()));
144         // mail to user
145         message = getMailReceiver().receive(targetEmail).getMessage();
146         assertThat(message, containsString("All certificates in your account have been revoked."));
147     }
148
149     @Test
150     public void testSupportSupporterGroup() throws MalformedURLException, IOException {
151         // supporter adds to his own groups
152         String s = IOUtils.readURL(post(SupportUserDetailsPage.PATH + u.getId() + "/", "addGroup&groupToModify=" + URLEncoder.encode(Group.ORG_AGENT.getDBName(), "UTF-8")));
153         assertThat(s, containsString("Supporter may not modify himself."));
154
155         // supporter removes from his own groups
156         s = IOUtils.readURL(post(SupportUserDetailsPage.PATH + u.getId() + "/", "removeGroup&groupToModify=" + URLEncoder.encode(Group.ORG_AGENT.getDBName(), "UTF-8")));
157         assertThat(s, containsString("Supporter may not modify himself."));
158
159         // supporter removes supporter flag
160         URLConnection uc = post(SupportUserDetailsPage.PATH + u.getId() + "/", "removeGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDBName(), "UTF-8"));
161         assertEquals("https://" + ServerConstants.getHostNamePortSecure(Host.SECURE) + MyDetails.PATH, uc.getHeaderField("Location"));
162         StringWriter sw = new StringWriter();
163         PrintWriter pw = new PrintWriter(sw);
164         Group.SUPPORTER.getName().output(pw, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
165         // mail to support
166         String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
167         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked."));
168         // mail to user
169         message = getMailReceiver().receive(u.getEmail()).getMessage();
170         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked from your account."));
171         // mail to board
172         message = getMailReceiver().receive(ServerConstants.getBoardMailAddress()).getMessage();
173         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked for '" + u.getPreferredName().toString() + "'."));
174         s = IOUtils.readURL(get(cookie, MyDetails.PATH));
175         assertThat(s, not(containsString("supporter")));
176     }
177
178 }