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.EmailAddress;
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.User;
14 import org.cacert.gigi.localisation.Language;
15 import org.cacert.gigi.testUtils.ManagedTest;
16 import org.junit.Test;
18 public class TestMailManagement extends ManagedTest {
20 private User u = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
22 private String cookie;
24 private String path = MailOverview.DEFAULT_PATH;
26 public TestMailManagement() throws IOException {
27 cookie = login(u.getEmail(), TEST_PASSWORD);
28 assertTrue(isLoggedin(cookie));
32 public void testMailAddInternal() throws InterruptedException, GigiApiException {
33 createVerifiedEmail(u);
37 public void testMailAddInternalFaulty() {
39 new EmailAddress("kurti ", u);
41 } catch (IllegalArgumentException e) {
47 public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
48 String newMail = createUniqueName() + "uni@example.org";
49 assertNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
50 EmailAddress[] addrs = u.getEmails();
51 for (int i = 0; i < addrs.length; i++) {
52 if (addrs[i].getAddress().equals(newMail)) {
60 public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
61 String newMail = createUniqueName() + "uniexample.org";
62 assertNotNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
63 EmailAddress[] addrs = u.getEmails();
64 for (int i = 0; i < addrs.length; i++) {
65 if (addrs[i].getAddress().equals(newMail)) {
72 public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
73 EmailAddress adrr = createVerifiedEmail(u);
74 assertNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
75 assertEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
79 public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
80 EmailAddress adrr = new EmailAddress(createUniqueName() + "test@test.tld", u);
81 adrr.insert(Language.getInstance(Locale.ENGLISH));
82 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
83 assertNotEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
84 getMailReciever().clearMails();
88 public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
89 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
91 EmailAddress[] emails = u2.getEmails();
92 for (int i = 0; i < emails.length; i++) {
93 if (emails[i].getAddress().equals(u2.getEmail())) {
94 id = emails[i].getId();
97 assertNotEquals(id, -1);
98 assertNotNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + id));
99 assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
100 getMailReciever().clearMails();
104 public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
105 EmailAddress addr = createVerifiedEmail(u);
106 assertNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + addr.getId(), 0));
107 User u = User.getById(this.u.getId());
108 EmailAddress[] addresses = u.getEmails();
109 for (int i = 0; i < addresses.length; i++) {
110 assertNotEquals(addresses[i].getAddress(), addr.getAddress());
115 public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
116 EmailAddress[] addr = new EmailAddress[] {
117 createVerifiedEmail(u), createVerifiedEmail(u)
119 assertNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + addr[0].getId() + "&delid[]=" + addr[1].getId(), 0));
120 User u = User.getById(this.u.getId());
121 EmailAddress[] addresses = u.getEmails();
122 for (int i = 0; i < addresses.length; i++) {
123 assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
124 assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
129 public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
130 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
131 EmailAddress em = u2.getEmails()[0];
132 assertNotNull(executeBasicWebInteraction(cookie, path, "delete&delid[]=" + em.getId(), 0));
133 u2 = User.getById(u2.getId());
134 assertNotEquals(u2.getEmails().length, 0);