]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/account/TestMailManagement.java
Format code according do BenBE's formatter.
[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
10 import org.cacert.gigi.EmailAddress;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.Language;
13 import org.cacert.gigi.User;
14 import org.cacert.gigi.testUtils.ManagedTest;
15 import org.junit.Test;
16
17 public class TestMailManagement extends ManagedTest {
18
19     private User u = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
20
21     private String cookie;
22
23     private String path = MailOverview.DEFAULT_PATH;
24
25     public TestMailManagement() throws IOException {
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() {
37         try {
38             new EmailAddress("kurti ", u);
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(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
49         EmailAddress[] addrs = u.getEmails();
50         for (int i = 0; i < addrs.length; i++) {
51             if (addrs[i].getAddress().equals(newMail)) {
52                 return;
53             }
54         }
55         fail();
56     }
57
58     @Test
59     public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
60         String newMail = createUniqueName() + "uniexample.org";
61         assertNotNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
62         EmailAddress[] addrs = u.getEmails();
63         for (int i = 0; i < addrs.length; i++) {
64             if (addrs[i].getAddress().equals(newMail)) {
65                 fail();
66             }
67         }
68     }
69
70     @Test
71     public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
72         EmailAddress adrr = createVerifiedEmail(u);
73         assertNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
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(createUniqueName() + "test@test.tld", u);
80         adrr.insert(Language.getInstance("en"));
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 }