1 package club.wpia.gigi.pages.admin;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
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;
15 import org.junit.Test;
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;
29 public class TestSEAdminNotificationMail extends SEClientTest {
33 private String targetEmail;
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);
43 public void testChangeAccountData() throws MalformedURLException, IOException {
45 executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0);
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:"));
52 message = getMailReceiver().receive(targetEmail).getMessage();
53 assertThat(message, containsString("The DoB in your account was changed to 2000-02-01."));
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."));
65 public void testGrantUserGroup() throws MalformedURLException, IOException {
66 executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "addGroup&groupToModify=" + URLEncoder.encode(Group.CODESIGNING.getDBName(), "UTF-8"), 0);
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>());
73 String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
74 assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted."));
76 message = getMailReceiver().receive(targetEmail).getMessage();
77 assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted to your account."));
81 public void testRemoveUserGroup() throws MalformedURLException, IOException {
82 executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "removeGroup&groupToModify=" + URLEncoder.encode(Group.CODESIGNING.getDBName(), "UTF-8"), 0);
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>());
89 String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
90 assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked."));
92 message = getMailReceiver().receive(targetEmail).getMessage();
93 assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked from your account."));
97 public void testGrantSupporterGroup() throws MalformedURLException, IOException {
98 executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "addGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDBName(), "UTF-8"), 0);
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);
106 String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
107 assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted."));
109 message = getMailReceiver().receive(targetEmail).getMessage();
110 assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted to your account."));
112 message = getMailReceiver().receive(ServerConstants.getBoardMailAddress()).getMessage();
113 assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted for '" + target.getPreferredName().toString() + "'."));
117 public void testRemoveSupporterGroup() throws MalformedURLException, IOException {
118 executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "removeGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDBName(), "UTF-8"), 0);
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);
126 String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
127 assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked."));
129 message = getMailReceiver().receive(targetEmail).getMessage();
130 assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked from your account."));
132 message = getMailReceiver().receive(ServerConstants.getBoardMailAddress()).getMessage();
133 assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked for '" + target.getPreferredName().toString() + "'."));
137 public void testRevokeAllCertificates() throws MalformedURLException, IOException {
138 executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "revokeall", 1);
139 User user = User.getById(targetID);
142 String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
143 assertThat(message, containsString("All certificates in the account " + user.getPreferredName().toString()));
145 message = getMailReceiver().receive(targetEmail).getMessage();
146 assertThat(message, containsString("All certificates in your account have been revoked."));
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."));
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."));
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.WWW) + 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>());
166 String message = getMailReceiver().receive(ServerConstants.getSupportMailAddress()).getMessage();
167 assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked."));
169 message = getMailReceiver().receive(u.getEmail()).getMessage();
170 assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked from your account."));
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")));