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.testUtils.ManagedTest;
17 import org.junit.Test;
19 public class TestMailManagement extends ManagedTest {
21 private User u = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
23 private String cookie;
25 private String path = MailOverview.DEFAULT_PATH;
27 public TestMailManagement() throws IOException {
28 cookie = login(u.getEmail(), TEST_PASSWORD);
29 assertTrue(isLoggedin(cookie));
33 public void testMailAddInternal() throws InterruptedException, GigiApiException {
34 createVerifiedEmail(u);
38 public void testMailAddInternalFaulty() {
40 new EmailAddress(u, "kurti ");
42 } catch (IllegalArgumentException e) {
48 public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
49 String newMail = createUniqueName() + "uni@example.org";
50 assertNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
51 EmailAddress[] addrs = u.getEmails();
52 for (int i = 0; i < addrs.length; i++) {
53 if (addrs[i].getAddress().equals(newMail)) {
61 public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
62 String newMail = createUniqueName() + "uniexample.org";
63 assertNotNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
64 EmailAddress[] addrs = u.getEmails();
65 for (int i = 0; i < addrs.length; i++) {
66 if (addrs[i].getAddress().equals(newMail)) {
73 public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
74 EmailAddress adrr = createVerifiedEmail(u);
75 assertNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
76 ObjectCache.clearAllCaches();
77 assertEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
81 public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
82 EmailAddress adrr = new EmailAddress(u, createUniqueName() + "test@test.tld");
83 adrr.insert(Language.getInstance(Locale.ENGLISH));
84 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
85 assertNotEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
86 getMailReciever().clearMails();
90 public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
91 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
93 EmailAddress[] emails = u2.getEmails();
94 for (int i = 0; i < emails.length; i++) {
95 if (emails[i].getAddress().equals(u2.getEmail())) {
96 id = emails[i].getId();
99 assertNotEquals(id, -1);
100 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + id));
101 assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
102 getMailReciever().clearMails();
106 public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
107 EmailAddress addr = createVerifiedEmail(u);
108 assertNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + addr.getId(), 0));
109 User u = User.getById(this.u.getId());
110 EmailAddress[] addresses = u.getEmails();
111 for (int i = 0; i < addresses.length; i++) {
112 assertNotEquals(addresses[i].getAddress(), addr.getAddress());
117 public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
118 EmailAddress[] addr = new EmailAddress[] {
119 createVerifiedEmail(u), createVerifiedEmail(u)
121 assertNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + addr[0].getId() + "&delid[]=" + addr[1].getId(), 0));
122 User u = User.getById(this.u.getId());
123 EmailAddress[] addresses = u.getEmails();
124 for (int i = 0; i < addresses.length; i++) {
125 assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
126 assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
131 public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
132 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
133 EmailAddress em = u2.getEmails()[0];
134 assertNotNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + em.getId(), 0));
135 u2 = User.getById(u2.getId());
136 assertNotEquals(u2.getEmails().length, 0);