]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/account/TestMailManagement.java
upd: rename package name and all references to it
[gigi.git] / tests / club / wpia / gigi / pages / account / TestMailManagement.java
1 package club.wpia.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.junit.Test;
12
13 import club.wpia.gigi.GigiApiException;
14 import club.wpia.gigi.dbObjects.EmailAddress;
15 import club.wpia.gigi.dbObjects.ObjectCache;
16 import club.wpia.gigi.dbObjects.User;
17 import club.wpia.gigi.pages.account.mail.MailOverview;
18 import club.wpia.gigi.testUtils.ClientTest;
19
20 public class TestMailManagement extends ClientTest {
21
22     private String path = MailOverview.DEFAULT_PATH;
23
24     public TestMailManagement() throws IOException {
25         clearCaches(); // and reset rate limits
26         cookie = login(u.getEmail(), TEST_PASSWORD);
27         assertTrue(isLoggedin(cookie));
28     }
29
30     @Test
31     public void testMailAddInternal() throws InterruptedException, GigiApiException {
32         createVerifiedEmail(u);
33     }
34
35     @Test
36     public void testMailAddInternalFaulty() throws GigiApiException {
37         try {
38             new EmailAddress(u, "kurti ", Locale.ENGLISH);
39             fail();
40         } catch (IllegalArgumentException e) {
41             // Intended.
42         }
43     }
44
45     @Test
46     public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
47         String newMail = createUniqueName() + "uni@example.org";
48         assertNull(addMail(newMail));
49         assertTrue(existsEmail(newMail));
50     }
51
52     @Test
53     public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
54         String newMail = createUniqueName() + "uniexample.org";
55         assertNotNull(addMail(newMail));
56         assertFalse(existsEmail(newMail));
57     }
58
59     @Test
60     public void testMailAddWebMultiple() throws MalformedURLException, UnsupportedEncodingException, IOException {
61         String u = createUniqueName();
62         String newMail = u + "uni@eXample.org";
63         assertNull(addMail(newMail));
64         assertTrue(existsEmail(newMail.toLowerCase()));
65
66         String newMail2 = u + "uni@eXamPlE.org";
67         assertNotNull(addMail(newMail2));
68         assertTrue(existsEmail(newMail2.toLowerCase()));
69
70         String newMail3 = u + "-buni@eXamPlE.org";
71         assertNull(addMail(newMail3));
72         assertTrue(existsEmail(newMail.toLowerCase()));
73         assertTrue(existsEmail(newMail3.toLowerCase()));
74     }
75
76     private String addMail(String newMail) throws IOException, MalformedURLException, UnsupportedEncodingException {
77         return executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1);
78     }
79
80     private boolean existsEmail(String newMail) {
81         EmailAddress[] addrs = u.getEmails();
82         for (int i = 0; i < addrs.length; i++) {
83             if (addrs[i].getAddress().equals(newMail)) {
84                 return true;
85             }
86         }
87         return false;
88     }
89
90     @Test
91     public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
92         EmailAddress addr = createVerifiedEmail(u);
93         assertNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
94         ObjectCache.clearAllCaches();
95         assertEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
96     }
97
98     @Test
99     public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
100         EmailAddress addr = new EmailAddress(u, createUniqueName() + "test@test.tld", Locale.ENGLISH);
101         assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
102         assertNotEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
103         getMailReceiver().clearMails();
104     }
105
106     @Test
107     public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
108         User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
109         int id = -1;
110         EmailAddress[] emails = u2.getEmails();
111         for (int i = 0; i < emails.length; i++) {
112             if (emails[i].getAddress().equals(u2.getEmail())) {
113                 id = emails[i].getId();
114             }
115         }
116         assertNotEquals(id, -1);
117         assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + id));
118         assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
119         getMailReceiver().clearMails();
120     }
121
122     @Test
123     public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
124         EmailAddress addr = createVerifiedEmail(u);
125         assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr.getId(), 0));
126         User u = User.getById(this.u.getId());
127         EmailAddress[] addresses = u.getEmails();
128         for (int i = 0; i < addresses.length; i++) {
129             assertNotEquals(addresses[i].getAddress(), addr.getAddress());
130         }
131     }
132
133     @Test
134     public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
135         EmailAddress[] addr = new EmailAddress[] {
136                 createVerifiedEmail(u), createVerifiedEmail(u)
137         };
138         assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[0].getId(), 0));
139         assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[1].getId(), 0));
140         User u = User.getById(this.u.getId());
141         EmailAddress[] addresses = u.getEmails();
142         for (int i = 0; i < addresses.length; i++) {
143             assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
144             assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
145         }
146     }
147
148     @Test
149     public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
150         User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
151         EmailAddress em = u2.getEmails()[0];
152         assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + em.getId(), 0));
153         u2 = User.getById(u2.getId());
154         assertNotEquals(u2.getEmails().length, 0);
155     }
156
157     @Test
158     public void testMailDeleteWebPrimary() throws MalformedURLException, UnsupportedEncodingException, IOException {
159         assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + u.getEmails()[0].getId(), 0));
160         assertNotEquals(u.getEmails().length, 0);
161     }
162 }