]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/account/TestMailManagement.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[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(addMail(newMail));
48         assertTrue(existsEmail(newMail));
49     }
50
51     @Test
52     public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
53         String newMail = createUniqueName() + "uniexample.org";
54         assertNotNull(addMail(newMail));
55         assertFalse(existsEmail(newMail));
56     }
57
58     @Test
59     public void testMailAddWebMultiple() throws MalformedURLException, UnsupportedEncodingException, IOException {
60         String u = createUniqueName();
61         String newMail = u + "uni@eXample.org";
62         assertNull(addMail(newMail));
63         assertTrue(existsEmail(newMail.toLowerCase()));
64
65         String newMail2 = u + "uni@eXamPlE.org";
66         assertNotNull(addMail(newMail2));
67         assertTrue(existsEmail(newMail2.toLowerCase()));
68
69         String newMail3 = u + "-buni@eXamPlE.org";
70         assertNull(addMail(newMail3));
71         assertTrue(existsEmail(newMail.toLowerCase()));
72         assertTrue(existsEmail(newMail3.toLowerCase()));
73     }
74
75     private String addMail(String newMail) throws IOException, MalformedURLException, UnsupportedEncodingException {
76         return executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1);
77     }
78
79     private boolean existsEmail(String newMail) {
80         EmailAddress[] addrs = u.getEmails();
81         for (int i = 0; i < addrs.length; i++) {
82             if (addrs[i].getAddress().equals(newMail)) {
83                 return true;
84             }
85         }
86         return false;
87     }
88
89     @Test
90     public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
91         EmailAddress addr = createVerifiedEmail(u);
92         assertNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
93         ObjectCache.clearAllCaches();
94         assertEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
95     }
96
97     @Test
98     public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
99         EmailAddress addr = new EmailAddress(u, createUniqueName() + "test@test.tld", Locale.ENGLISH);
100         assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
101         assertNotEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
102         getMailReceiver().clearMails();
103     }
104
105     @Test
106     public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
107         User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
108         int id = -1;
109         EmailAddress[] emails = u2.getEmails();
110         for (int i = 0; i < emails.length; i++) {
111             if (emails[i].getAddress().equals(u2.getEmail())) {
112                 id = emails[i].getId();
113             }
114         }
115         assertNotEquals(id, -1);
116         assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + id));
117         assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
118         getMailReceiver().clearMails();
119     }
120
121     @Test
122     public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
123         EmailAddress addr = createVerifiedEmail(u);
124         assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr.getId(), 0));
125         User u = User.getById(this.u.getId());
126         EmailAddress[] addresses = u.getEmails();
127         for (int i = 0; i < addresses.length; i++) {
128             assertNotEquals(addresses[i].getAddress(), addr.getAddress());
129         }
130     }
131
132     @Test
133     public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
134         EmailAddress[] addr = new EmailAddress[] {
135                 createVerifiedEmail(u), createVerifiedEmail(u)
136         };
137         assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[0].getId(), 0));
138         assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[1].getId(), 0));
139         User u = User.getById(this.u.getId());
140         EmailAddress[] addresses = u.getEmails();
141         for (int i = 0; i < addresses.length; i++) {
142             assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
143             assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
144         }
145     }
146
147     @Test
148     public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
149         User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
150         EmailAddress em = u2.getEmails()[0];
151         assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + em.getId(), 0));
152         u2 = User.getById(u2.getId());
153         assertNotEquals(u2.getEmails().length, 0);
154     }
155
156     @Test
157     public void testMailDeleteWebPrimary() throws MalformedURLException, UnsupportedEncodingException, IOException {
158         assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + u.getEmails()[0].getId(), 0));
159         assertNotEquals(u.getEmails().length, 0);
160     }
161 }