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