]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/account/TestMailManagement.java
531a2fdc4b76c72c1331fa2ec473c29060b649f7
[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         private User u = User
19                 .getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
20         private String cookie;
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() {
35                 try {
36                         new EmailAddress("kurti ", u);
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"),
47                         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,
61                         "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,
72                 InterruptedException, GigiApiException {
73                 EmailAddress adrr = createVerifiedEmail(u);
74                 assertNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
75                 assertEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
76         }
77
78         @Test
79         public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException,
80                 IOException, InterruptedException, GigiApiException {
81                 EmailAddress adrr = new EmailAddress(createUniqueName() + "test@test.tld", u);
82                 adrr.insert(Language.getInstance("en"));
83                 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
84                 assertNotEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
85                 getMailReciever().clearMails();
86         }
87
88         @Test
89         public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException,
90                 IOException, InterruptedException, GigiApiException {
91                 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
92                 int id = -1;
93                 EmailAddress[] emails = u2.getEmails();
94                 for (int i = 0; i < emails.length; i++) {
95                         if (emails[i].getAddress().equals(u2.getEmail())) {
96                                 id = emails[i].getId();
97                         }
98                 }
99                 assertNotEquals(id, -1);
100                 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + id));
101                 assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
102                 getMailReciever().clearMails();
103         }
104
105         @Test
106         public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException,
107                 UnsupportedEncodingException, IOException {
108                 EmailAddress addr = createVerifiedEmail(u);
109                 assertNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + addr.getId(), 0));
110                 User u = User.getById(this.u.getId());
111                 EmailAddress[] addresses = u.getEmails();
112                 for (int i = 0; i < addresses.length; i++) {
113                         assertNotEquals(addresses[i].getAddress(), addr.getAddress());
114                 }
115         }
116
117         @Test
118         public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException,
119                 UnsupportedEncodingException, IOException {
120                 EmailAddress[] addr = new EmailAddress[] { createVerifiedEmail(u), createVerifiedEmail(u) };
121                 assertNull(executeBasicWebInteraction(cookie, path,
122                         "delete&delid[]=" + addr[0].getId() + "&delid[]="
123                         + addr[1].getId(), 0));
124                 User u = User.getById(this.u.getId());
125                 EmailAddress[] addresses = u.getEmails();
126                 for (int i = 0; i < addresses.length; i++) {
127                         assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
128                         assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
129                 }
130         }
131 }