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