add: notify board if a support role is granted or removed
[gigi.git] / tests / org / cacert / gigi / pages / admin / TestSEAdminNotificationMail.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.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.cacert.gigi.GigiApiException;
15 import org.cacert.gigi.dbObjects.Group;
16 import org.cacert.gigi.dbObjects.User;
17 import org.cacert.gigi.localisation.Language;
18 import org.cacert.gigi.pages.admin.support.SupportEnterTicketPage;
19 import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage;
20 import org.cacert.gigi.testUtils.ClientTest;
21 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
22 import org.cacert.gigi.util.ServerConstants;
23 import org.junit.Test;
24
25 public class TestSEAdminNotificationMail extends ClientTest {
26
27     private int targetID;
28
29     public TestSEAdminNotificationMail() throws IOException, GigiApiException {
30         grant(u, Group.SUPPORTER);
31         cookie = login(email, TEST_PASSWORD);
32         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
33
34         String email = createUniqueName() + "@example.com";
35         String fname = "Först";
36         String lname = "Secönd";
37         targetID = createVerifiedUser(fname, lname, email, TEST_PASSWORD);
38     }
39
40     @Test
41     public void testChangeAccountData() throws MalformedURLException, IOException {
42
43         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "dobd=1&dobm=2&doby=2000&detailupdate", 0);
44
45         // mail to support
46         String message = getMailReceiver().receive().getMessage();
47         assertThat(message, containsString("The DoB was changed"));
48         assertThat(message, containsString("supporter " + u.getPreferredName().toString() + " triggered:"));
49         // mail to user
50         message = getMailReceiver().receive().getMessage();
51         assertThat(message, containsString("The DoB in your account was changed to 2000-02-01."));
52     }
53
54     @Test
55     public void testPasswordReset() throws MalformedURLException, IOException {
56         executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + targetID + "/", "aword=SecretWord&resetPass", 0);
57         TestMail tm;
58         String targetMail = ServerConstants.getSupportMailAddress();
59         do {
60             tm = getMailReceiver().receive();
61         } while ( !tm.getTo().equals(targetMail));
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.getDatabaseName(), "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().getMessage();
75         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted."));
76         // mail to user
77         message = getMailReceiver().receive().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.getDatabaseName(), "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().getMessage();
91         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked."));
92         // mail to user
93         message = getMailReceiver().receive().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.getDatabaseName(), "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().getMessage();
108         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted."));
109         // mail to user
110         message = getMailReceiver().receive().getMessage();
111         assertThat(message, containsString("The group permission '" + sw.toString() + "' was granted to your account."));
112         // mail to board
113         message = getMailReceiver().receive().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.getDatabaseName(), "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().getMessage();
128         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked."));
129         // mail to user
130         message = getMailReceiver().receive().getMessage();
131         assertThat(message, containsString("The group permission '" + sw.toString() + "' was revoked from your account."));
132         // mail to board
133         message = getMailReceiver().receive().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().getMessage();
144         assertThat(message, containsString("All certificates in the account " + user.getPreferredName().toString()));
145         // mail to user
146         message = getMailReceiver().receive().getMessage();
147         assertThat(message, containsString("All certificates in your account have been revoked."));
148     }
149 }