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