]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/account/TestMailManagement.java
Merge "add: show more certificates on the "roots" page"
[gigi.git] / tests / club / wpia / gigi / pages / account / TestMailManagement.java
1 package club.wpia.gigi.pages.account;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.io.UnsupportedEncodingException;
7 import java.net.MalformedURLException;
8 import java.net.URLEncoder;
9 import java.util.Locale;
10
11 import org.junit.Test;
12
13 import club.wpia.gigi.GigiApiException;
14 import club.wpia.gigi.dbObjects.EmailAddress;
15 import club.wpia.gigi.dbObjects.ObjectCache;
16 import club.wpia.gigi.dbObjects.User;
17 import club.wpia.gigi.pages.account.mail.MailOverview;
18 import club.wpia.gigi.testUtils.ClientTest;
19
20 public class TestMailManagement extends ClientTest {
21
22     private String path = MailOverview.DEFAULT_PATH;
23
24     public TestMailManagement() throws IOException {
25         clearCaches(); // and reset rate limits
26         cookie = login(u.getEmail(), TEST_PASSWORD);
27         assertTrue(isLoggedin(cookie));
28     }
29
30     @Test
31     public void testMailAddInternal() throws InterruptedException, GigiApiException {
32         createVerifiedEmail(u);
33     }
34
35     @Test
36     public void testMailAddInternalFaulty() throws GigiApiException {
37         try {
38             new EmailAddress(u, "kurti ", Locale.ENGLISH);
39             fail();
40         } catch (IllegalArgumentException e) {
41             // Intended.
42         }
43     }
44
45     @Test
46     public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
47         String newMail = createUniqueName() + "uni@example.org";
48         assertNull(addMail(newMail));
49         assertTrue(existsEmail(newMail));
50         getMailReceiver().receive(newMail);
51     }
52
53     @Test
54     public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
55         String newMail = createUniqueName() + "uniexample.org";
56         assertNotNull(addMail(newMail));
57         assertFalse(existsEmail(newMail));
58     }
59
60     @Test
61     public void testMailAddWebMultiple() throws MalformedURLException, UnsupportedEncodingException, IOException {
62         String u = createUniqueName();
63         String newMail = u + "uni@eXample.org";
64         assertNull(addMail(newMail));
65         assertTrue(existsEmail(newMail.toLowerCase()));
66         getMailReceiver().receive(newMail.toLowerCase());
67
68         String newMail2 = u + "uni@eXamPlE.org";
69         assertNotNull(addMail(newMail2));
70         assertTrue(existsEmail(newMail2.toLowerCase()));
71
72         String newMail3 = u + "-buni@eXamPlE.org";
73         assertNull(addMail(newMail3));
74         assertTrue(existsEmail(newMail.toLowerCase()));
75         assertTrue(existsEmail(newMail3.toLowerCase()));
76         getMailReceiver().receive(newMail3.toLowerCase());
77     }
78
79     private String addMail(String newMail) throws IOException, MalformedURLException, UnsupportedEncodingException {
80         return executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1);
81     }
82
83     private boolean existsEmail(String newMail) {
84         EmailAddress[] addrs = u.getEmails();
85         for (int i = 0; i < addrs.length; i++) {
86             if (addrs[i].getAddress().equals(newMail)) {
87                 return true;
88             }
89         }
90         return false;
91     }
92
93     @Test
94     public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
95         EmailAddress addr = createVerifiedEmail(u);
96         assertNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
97         ObjectCache.clearAllCaches();
98         assertEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
99     }
100
101     @Test
102     public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
103         EmailAddress addr = new EmailAddress(u, createUniqueName() + "test@test.tld", Locale.ENGLISH);
104         assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
105         assertNotEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
106         getMailReceiver().receive(addr.getAddress());
107     }
108
109     @Test
110     public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
111         User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
112         int id = -1;
113         EmailAddress[] emails = u2.getEmails();
114         for (int i = 0; i < emails.length; i++) {
115             if (emails[i].getAddress().equals(u2.getEmail())) {
116                 id = emails[i].getId();
117             }
118         }
119         assertNotEquals(id, -1);
120         assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + id));
121         assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
122         getMailReceiver().assertEmpty();
123     }
124
125     @Test
126     public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
127         EmailAddress addr = createVerifiedEmail(u);
128         assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr.getId(), 0));
129         User u = User.getById(this.u.getId());
130         EmailAddress[] addresses = u.getEmails();
131         for (int i = 0; i < addresses.length; i++) {
132             assertNotEquals(addresses[i].getAddress(), addr.getAddress());
133         }
134     }
135
136     @Test
137     public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
138         EmailAddress[] addr = new EmailAddress[] {
139                 createVerifiedEmail(u), createVerifiedEmail(u)
140         };
141         assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[0].getId(), 0));
142         assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[1].getId(), 0));
143         User u = User.getById(this.u.getId());
144         EmailAddress[] addresses = u.getEmails();
145         for (int i = 0; i < addresses.length; i++) {
146             assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
147             assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
148         }
149     }
150
151     @Test
152     public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
153         User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
154         EmailAddress em = u2.getEmails()[0];
155         assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + em.getId(), 0));
156         u2 = User.getById(u2.getId());
157         assertNotEquals(u2.getEmails().length, 0);
158     }
159
160     @Test
161     public void testMailDeleteWebPrimary() throws MalformedURLException, UnsupportedEncodingException, IOException {
162         assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + u.getEmails()[0].getId(), 0));
163         assertNotEquals(u.getEmails().length, 0);
164     }
165 }