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.ClientTest;
18 import org.junit.Test;
20 public class TestMailManagement extends ClientTest {
22 private String path = MailOverview.DEFAULT_PATH;
24 public TestMailManagement() throws IOException {
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() {
37 new EmailAddress(u, "kurti ");
39 } catch (IllegalArgumentException e) {
45 public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
46 String newMail = createUniqueName() + "uni@example.org";
47 assertNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
48 EmailAddress[] addrs = u.getEmails();
49 for (int i = 0; i < addrs.length; i++) {
50 if (addrs[i].getAddress().equals(newMail)) {
58 public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
59 String newMail = createUniqueName() + "uniexample.org";
60 assertNotNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
61 EmailAddress[] addrs = u.getEmails();
62 for (int i = 0; i < addrs.length; i++) {
63 if (addrs[i].getAddress().equals(newMail)) {
70 public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
71 EmailAddress adrr = createVerifiedEmail(u);
72 assertNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
73 ObjectCache.clearAllCaches();
74 assertEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
78 public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
79 EmailAddress adrr = new EmailAddress(u, createUniqueName() + "test@test.tld");
80 adrr.insert(Language.getInstance(Locale.ENGLISH));
81 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
82 assertNotEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
83 getMailReciever().clearMails();
87 public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
88 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
90 EmailAddress[] emails = u2.getEmails();
91 for (int i = 0; i < emails.length; i++) {
92 if (emails[i].getAddress().equals(u2.getEmail())) {
93 id = emails[i].getId();
96 assertNotEquals(id, -1);
97 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + id));
98 assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
99 getMailReciever().clearMails();
103 public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
104 EmailAddress addr = createVerifiedEmail(u);
105 assertNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + addr.getId(), 0));
106 User u = User.getById(this.u.getId());
107 EmailAddress[] addresses = u.getEmails();
108 for (int i = 0; i < addresses.length; i++) {
109 assertNotEquals(addresses[i].getAddress(), addr.getAddress());
114 public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
115 EmailAddress[] addr = new EmailAddress[] {
116 createVerifiedEmail(u), createVerifiedEmail(u)
118 assertNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + addr[0].getId() + "&delid[]=" + addr[1].getId(), 0));
119 User u = User.getById(this.u.getId());
120 EmailAddress[] addresses = u.getEmails();
121 for (int i = 0; i < addresses.length; i++) {
122 assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
123 assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
128 public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
129 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
130 EmailAddress em = u2.getEmails()[0];
131 assertNotNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + em.getId(), 0));
132 u2 = User.getById(u2.getId());
133 assertNotEquals(u2.getEmails().length, 0);
137 public void testMailDeleteWebPrimary() throws MalformedURLException, UnsupportedEncodingException, IOException {
138 assertNotNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + u.getEmails()[0].getId(), 0));
139 assertNotEquals(u.getEmails().length, 0);