]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/account/TestMailManagement.java
958954a36cfa0d7ab5a9c884f4846cc4182164b3
[gigi.git] / tests / org / cacert / gigi / pages / account / TestMailManagement.java
1 package org.cacert.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.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.dbObjects.EmailAddress;
13 import org.cacert.gigi.dbObjects.ObjectCache;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.pages.account.mail.MailOverview;
16 import org.cacert.gigi.testUtils.ClientTest;
17 import org.junit.Test;
18
19 public class TestMailManagement extends ClientTest {
20
21     private String path = MailOverview.DEFAULT_PATH;
22
23     public TestMailManagement() throws IOException {
24         clearCaches(); // and reset rate limits
25         cookie = login(u.getEmail(), TEST_PASSWORD);
26         assertTrue(isLoggedin(cookie));
27     }
28
29     @Test
30     public void testMailAddInternal() throws InterruptedException, GigiApiException {
31         createVerifiedEmail(u);
32     }
33
34     @Test
35     public void testMailAddInternalFaulty() throws GigiApiException {
36         try {
37             new EmailAddress(u, "kurti ", Locale.ENGLISH);
38             fail();
39         } catch (IllegalArgumentException e) {
40             // Intended.
41         }
42     }
43
44     @Test
45     public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
46         String newMail = createUniqueName() + "uni@example.org";
47         assertNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
48         EmailAddress[] addrs = u.getEmails();
49         for (int i = 0; i < addrs.length; i++) {
50             if (addrs[i].getAddress().equals(newMail)) {
51                 return;
52             }
53         }
54         fail();
55     }
56
57     @Test
58     public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
59         String newMail = createUniqueName() + "uniexample.org";
60         assertNotNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
61         EmailAddress[] addrs = u.getEmails();
62         for (int i = 0; i < addrs.length; i++) {
63             if (addrs[i].getAddress().equals(newMail)) {
64                 fail();
65             }
66         }
67     }
68
69     @Test
70     public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
71         EmailAddress addr = createVerifiedEmail(u);
72         assertNull(executeBasicWebInteraction(cookie, path, "default:" + addr.getId()));
73         ObjectCache.clearAllCaches();
74         assertEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
75     }
76
77     @Test
78     public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
79         EmailAddress addr = new EmailAddress(u, createUniqueName() + "test@test.tld", Locale.ENGLISH);
80         assertNotNull(executeBasicWebInteraction(cookie, path, "default:" + addr.getId()));
81         assertNotEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
82         getMailReciever().clearMails();
83     }
84
85     @Test
86     public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
87         User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
88         int id = -1;
89         EmailAddress[] emails = u2.getEmails();
90         for (int i = 0; i < emails.length; i++) {
91             if (emails[i].getAddress().equals(u2.getEmail())) {
92                 id = emails[i].getId();
93             }
94         }
95         assertNotEquals(id, -1);
96         assertNotNull(executeBasicWebInteraction(cookie, path, "default:" + id));
97         assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
98         getMailReciever().clearMails();
99     }
100
101     @Test
102     public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
103         EmailAddress addr = createVerifiedEmail(u);
104         assertNull(executeBasicWebInteraction(cookie, path, "delete:" + addr.getId(), 0));
105         User u = User.getById(this.u.getId());
106         EmailAddress[] addresses = u.getEmails();
107         for (int i = 0; i < addresses.length; i++) {
108             assertNotEquals(addresses[i].getAddress(), addr.getAddress());
109         }
110     }
111
112     @Test
113     public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
114         EmailAddress[] addr = new EmailAddress[] {
115                 createVerifiedEmail(u), createVerifiedEmail(u)
116         };
117         assertNull(executeBasicWebInteraction(cookie, path, "delete:" + addr[0].getId() + "&delete:" + addr[1].getId(), 0));
118         User u = User.getById(this.u.getId());
119         EmailAddress[] addresses = u.getEmails();
120         for (int i = 0; i < addresses.length; i++) {
121             assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
122             assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
123         }
124     }
125
126     @Test
127     public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
128         User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
129         EmailAddress em = u2.getEmails()[0];
130         assertNotNull(executeBasicWebInteraction(cookie, path, "delete:" + em.getId(), 0));
131         u2 = User.getById(u2.getId());
132         assertNotEquals(u2.getEmails().length, 0);
133     }
134
135     @Test
136     public void testMailDeleteWebPrimary() throws MalformedURLException, UnsupportedEncodingException, IOException {
137         assertNotNull(executeBasicWebInteraction(cookie, path, "delete:" + u.getEmails()[0].getId(), 0));
138         assertNotEquals(u.getEmails().length, 0);
139     }
140 }