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