1 package org.cacert.gigi.pages.account;
3 import static org.junit.Assert.*;
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;
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.pages.account.mail.MailOverview;
16 import org.cacert.gigi.testUtils.ClientTest;
17 import org.junit.Test;
19 public class TestMailManagement extends ClientTest {
21 private String path = MailOverview.DEFAULT_PATH;
23 public TestMailManagement() throws IOException {
24 clearCaches(); // and reset rate limits
25 cookie = login(u.getEmail(), TEST_PASSWORD);
26 assertTrue(isLoggedin(cookie));
30 public void testMailAddInternal() throws InterruptedException, GigiApiException {
31 createVerifiedEmail(u);
35 public void testMailAddInternalFaulty() throws GigiApiException {
37 new EmailAddress(u, "kurti ", Locale.ENGLISH);
39 } catch (IllegalArgumentException e) {
45 public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
46 String newMail = createUniqueName() + "uni@example.org";
47 assertNull(addMail(newMail));
48 assertTrue(existsEmail(newMail));
52 public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
53 String newMail = createUniqueName() + "uniexample.org";
54 assertNotNull(addMail(newMail));
55 assertFalse(existsEmail(newMail));
59 public void testMailAddWebMultiple() throws MalformedURLException, UnsupportedEncodingException, IOException {
60 String u = createUniqueName();
61 String newMail = u + "uni@eXample.org";
62 assertNull(addMail(newMail));
63 assertTrue(existsEmail(newMail.toLowerCase()));
65 String newMail2 = u + "uni@eXamPlE.org";
66 assertNotNull(addMail(newMail2));
67 assertTrue(existsEmail(newMail2.toLowerCase()));
69 String newMail3 = u + "-buni@eXamPlE.org";
70 assertNull(addMail(newMail3));
71 assertTrue(existsEmail(newMail.toLowerCase()));
72 assertTrue(existsEmail(newMail3.toLowerCase()));
75 private String addMail(String newMail) throws IOException, MalformedURLException, UnsupportedEncodingException {
76 return executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1);
79 private boolean existsEmail(String newMail) {
80 EmailAddress[] addrs = u.getEmails();
81 for (int i = 0; i < addrs.length; i++) {
82 if (addrs[i].getAddress().equals(newMail)) {
90 public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
91 EmailAddress addr = createVerifiedEmail(u);
92 assertNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
93 ObjectCache.clearAllCaches();
94 assertEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
98 public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
99 EmailAddress addr = new EmailAddress(u, createUniqueName() + "test@test.tld", Locale.ENGLISH);
100 assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
101 assertNotEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
102 getMailReciever().clearMails();
106 public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
107 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
109 EmailAddress[] emails = u2.getEmails();
110 for (int i = 0; i < emails.length; i++) {
111 if (emails[i].getAddress().equals(u2.getEmail())) {
112 id = emails[i].getId();
115 assertNotEquals(id, -1);
116 assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + id));
117 assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
118 getMailReciever().clearMails();
122 public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
123 EmailAddress addr = createVerifiedEmail(u);
124 assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr.getId(), 0));
125 User u = User.getById(this.u.getId());
126 EmailAddress[] addresses = u.getEmails();
127 for (int i = 0; i < addresses.length; i++) {
128 assertNotEquals(addresses[i].getAddress(), addr.getAddress());
133 public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
134 EmailAddress[] addr = new EmailAddress[] {
135 createVerifiedEmail(u), createVerifiedEmail(u)
137 assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[0].getId(), 0));
138 assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[1].getId(), 0));
139 User u = User.getById(this.u.getId());
140 EmailAddress[] addresses = u.getEmails();
141 for (int i = 0; i < addresses.length; i++) {
142 assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
143 assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
148 public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
149 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
150 EmailAddress em = u2.getEmails()[0];
151 assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + em.getId(), 0));
152 u2 = User.getById(u2.getId());
153 assertNotEquals(u2.getEmails().length, 0);
157 public void testMailDeleteWebPrimary() throws MalformedURLException, UnsupportedEncodingException, IOException {
158 assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + u.getEmails()[0].getId(), 0));
159 assertNotEquals(u.getEmails().length, 0);