]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/account/TestMailManagement.java
ADD: email set default tests
[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.cacert.gigi.testUtils.TestEmailReciever.TestMail;
16 import org.junit.Test;
17
18 public class TestMailManagement extends ManagedTest {
19         private User u = User
20                 .getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
21         private String cookie;
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                 EmailAddress adrr = new EmailAddress(createUniqueName() + "test@test.tld", u);
32                 adrr.insert(Language.getInstance("en"));
33                 TestMail testMail = getMailReciever().recieve();
34                 assertTrue(adrr.getAddress().equals(testMail.getTo()));
35                 String hash = testMail.extractLink().substring(testMail.extractLink().lastIndexOf('=') + 1);
36                 adrr.verify(hash);
37                 getMailReciever().clearMails();
38         }
39
40         @Test
41         public void testMailAddInternalFaulty() {
42                 try {
43                         new EmailAddress("kurti ", u);
44                         fail();
45                 } catch (IllegalArgumentException e) {
46                         // Intended.
47                 }
48         }
49
50         @Test
51         public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
52                 String newMail = createUniqueName() + "uni@example.org";
53                 assertNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"),
54                         1));
55                 EmailAddress[] addrs = u.getEmails();
56                 for (int i = 0; i < addrs.length; i++) {
57                         if (addrs[i].getAddress().equals(newMail)) {
58                                 return;
59                         }
60                 }
61                 fail();
62         }
63
64         @Test
65         public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
66                 String newMail = createUniqueName() + "uniexample.org";
67                 assertNotNull(executeBasicWebInteraction(cookie, path,
68                         "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
69                 EmailAddress[] addrs = u.getEmails();
70                 for (int i = 0; i < addrs.length; i++) {
71                         if (addrs[i].getAddress().equals(newMail)) {
72                                 fail();
73                         }
74                 }
75         }
76
77         @Test
78         public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException,
79                 InterruptedException, GigiApiException {
80                 EmailAddress adrr = new EmailAddress(createUniqueName() + "test@test.tld", u);
81                 adrr.insert(Language.getInstance("en"));
82                 TestMail testMail = getMailReciever().recieve();
83                 assertTrue(adrr.getAddress().equals(testMail.getTo()));
84                 String hash = testMail.extractLink().substring(testMail.extractLink().lastIndexOf('=') + 1);
85                 adrr.verify(hash);
86                 assertNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
87                 assertEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
88                 getMailReciever().clearMails();
89         }
90
91         @Test
92         public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException,
93                 IOException, InterruptedException, GigiApiException {
94                 EmailAddress adrr = new EmailAddress(createUniqueName() + "test@test.tld", u);
95                 adrr.insert(Language.getInstance("en"));
96                 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
97                 assertNotEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
98                 getMailReciever().clearMails();
99         }
100
101         @Test
102         public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException,
103                 IOException, InterruptedException, GigiApiException {
104                 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
105                 int id = -1;
106                 EmailAddress[] emails = u2.getEmails();
107                 for (int i = 0; i < emails.length; i++) {
108                         if (emails[i].getAddress().equals(u2.getEmail())) {
109                                 id = emails[i].getId();
110                         }
111                 }
112                 assertNotEquals(id, -1);
113                 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + id));
114                 assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
115                 getMailReciever().clearMails();
116         }
117 }