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 cookie = login(u.getEmail(), TEST_PASSWORD);
25 assertTrue(isLoggedin(cookie));
29 public void testMailAddInternal() throws InterruptedException, GigiApiException {
30 createVerifiedEmail(u);
34 public void testMailAddInternalFaulty() throws GigiApiException {
36 new EmailAddress(u, "kurti ", Locale.ENGLISH);
38 } catch (IllegalArgumentException e) {
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"), 1));
47 EmailAddress[] addrs = u.getEmails();
48 for (int i = 0; i < addrs.length; i++) {
49 if (addrs[i].getAddress().equals(newMail)) {
57 public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
58 String newMail = createUniqueName() + "uniexample.org";
59 assertNotNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
60 EmailAddress[] addrs = u.getEmails();
61 for (int i = 0; i < addrs.length; i++) {
62 if (addrs[i].getAddress().equals(newMail)) {
69 public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
70 EmailAddress adrr = createVerifiedEmail(u);
71 assertNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
72 ObjectCache.clearAllCaches();
73 assertEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
77 public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
78 EmailAddress adrr = new EmailAddress(u, createUniqueName() + "test@test.tld", Locale.ENGLISH);
79 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
80 assertNotEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
81 getMailReciever().clearMails();
85 public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
86 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
88 EmailAddress[] emails = u2.getEmails();
89 for (int i = 0; i < emails.length; i++) {
90 if (emails[i].getAddress().equals(u2.getEmail())) {
91 id = emails[i].getId();
94 assertNotEquals(id, -1);
95 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + id));
96 assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
97 getMailReciever().clearMails();
101 public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
102 EmailAddress addr = createVerifiedEmail(u);
103 assertNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + addr.getId(), 0));
104 User u = User.getById(this.u.getId());
105 EmailAddress[] addresses = u.getEmails();
106 for (int i = 0; i < addresses.length; i++) {
107 assertNotEquals(addresses[i].getAddress(), addr.getAddress());
112 public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
113 EmailAddress[] addr = new EmailAddress[] {
114 createVerifiedEmail(u), createVerifiedEmail(u)
116 assertNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + addr[0].getId() + "&delid[]=" + addr[1].getId(), 0));
117 User u = User.getById(this.u.getId());
118 EmailAddress[] addresses = u.getEmails();
119 for (int i = 0; i < addresses.length; i++) {
120 assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
121 assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
126 public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
127 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
128 EmailAddress em = u2.getEmails()[0];
129 assertNotNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + em.getId(), 0));
130 u2 = User.getById(u2.getId());
131 assertNotEquals(u2.getEmails().length, 0);
135 public void testMailDeleteWebPrimary() throws MalformedURLException, UnsupportedEncodingException, IOException {
136 assertNotNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + u.getEmails()[0].getId(), 0));
137 assertNotEquals(u.getEmails().length, 0);