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