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.localisation.Language;
16 import org.cacert.gigi.pages.account.mail.MailOverview;
17 import org.cacert.gigi.testUtils.ManagedTest;
18 import org.junit.Test;
20 public class TestMailManagement extends ManagedTest {
22 private User u = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
24 private String cookie;
26 private String path = MailOverview.DEFAULT_PATH;
28 public TestMailManagement() throws IOException {
29 cookie = login(u.getEmail(), TEST_PASSWORD);
30 assertTrue(isLoggedin(cookie));
34 public void testMailAddInternal() throws InterruptedException, GigiApiException {
35 createVerifiedEmail(u);
39 public void testMailAddInternalFaulty() {
41 new EmailAddress(u, "kurti ");
43 } catch (IllegalArgumentException e) {
49 public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
50 String newMail = createUniqueName() + "uni@example.org";
51 assertNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
52 EmailAddress[] addrs = u.getEmails();
53 for (int i = 0; i < addrs.length; i++) {
54 if (addrs[i].getAddress().equals(newMail)) {
62 public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
63 String newMail = createUniqueName() + "uniexample.org";
64 assertNotNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
65 EmailAddress[] addrs = u.getEmails();
66 for (int i = 0; i < addrs.length; i++) {
67 if (addrs[i].getAddress().equals(newMail)) {
74 public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
75 EmailAddress adrr = createVerifiedEmail(u);
76 assertNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
77 ObjectCache.clearAllCaches();
78 assertEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
82 public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
83 EmailAddress adrr = new EmailAddress(u, createUniqueName() + "test@test.tld");
84 adrr.insert(Language.getInstance(Locale.ENGLISH));
85 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
86 assertNotEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
87 getMailReciever().clearMails();
91 public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
92 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
94 EmailAddress[] emails = u2.getEmails();
95 for (int i = 0; i < emails.length; i++) {
96 if (emails[i].getAddress().equals(u2.getEmail())) {
97 id = emails[i].getId();
100 assertNotEquals(id, -1);
101 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + id));
102 assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
103 getMailReciever().clearMails();
107 public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, 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());
118 public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
119 EmailAddress[] addr = new EmailAddress[] {
120 createVerifiedEmail(u), createVerifiedEmail(u)
122 assertNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + addr[0].getId() + "&delid[]=" + addr[1].getId(), 0));
123 User u = User.getById(this.u.getId());
124 EmailAddress[] addresses = u.getEmails();
125 for (int i = 0; i < addresses.length; i++) {
126 assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
127 assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
132 public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
133 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
134 EmailAddress em = u2.getEmails()[0];
135 assertNotNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + em.getId(), 0));
136 u2 = User.getById(u2.getId());
137 assertNotEquals(u2.getEmails().length, 0);