]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/account/TestMailManagement.java
UPD: Refactored faulty internal mail add test
[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("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         }
38
39         @Test
40         public void testMailAddInternalFaulty() {
41                 try {
42                         new EmailAddress("kurti ", u);
43                         fail();
44                 } catch (IllegalArgumentException e) {
45                         // Intended.
46                 }
47         }
48
49         @Test
50         public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
51                 String newMail = createUniqueName() + "uni@example.org";
52                 assertNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"),
53                         1));
54                 EmailAddress[] addrs = u.getEmails();
55                 for (int i = 0; i < addrs.length; i++) {
56                         if (addrs[i].getAddress().equals(newMail)) {
57                                 return;
58                         }
59                 }
60                 fail();
61         }
62
63         @Test
64         public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
65                 String newMail = createUniqueName() + "uniexample.org";
66                 assertNotNull(executeBasicWebInteraction(cookie, path,
67                         "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
68                 EmailAddress[] addrs = u.getEmails();
69                 for (int i = 0; i < addrs.length; i++) {
70                         if (addrs[i].getAddress().equals(newMail)) {
71                                 fail();
72                         }
73                 }
74         }
75 }