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;
10 import org.cacert.gigi.EmailAddress;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.Language;
13 import org.cacert.gigi.User;
14 import org.cacert.gigi.testUtils.ManagedTest;
15 import org.junit.Test;
17 public class TestMailManagement extends ManagedTest {
19 private User u = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
21 private String cookie;
23 private String path = MailOverview.DEFAULT_PATH;
25 public TestMailManagement() throws IOException {
26 cookie = login(u.getEmail(), TEST_PASSWORD);
27 assertTrue(isLoggedin(cookie));
31 public void testMailAddInternal() throws InterruptedException, GigiApiException {
32 createVerifiedEmail(u);
36 public void testMailAddInternalFaulty() {
38 new EmailAddress("kurti ", u);
40 } catch (IllegalArgumentException e) {
46 public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
47 String newMail = createUniqueName() + "uni@example.org";
48 assertNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
49 EmailAddress[] addrs = u.getEmails();
50 for (int i = 0; i < addrs.length; i++) {
51 if (addrs[i].getAddress().equals(newMail)) {
59 public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
60 String newMail = createUniqueName() + "uniexample.org";
61 assertNotNull(executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1));
62 EmailAddress[] addrs = u.getEmails();
63 for (int i = 0; i < addrs.length; i++) {
64 if (addrs[i].getAddress().equals(newMail)) {
71 public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
72 EmailAddress adrr = createVerifiedEmail(u);
73 assertNull(executeBasicWebInteraction(cookie, path, "makedefault&emailid=" + adrr.getId()));
74 assertEquals(User.getById(u.getId()).getEmail(), adrr.getAddress());
78 public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
79 EmailAddress adrr = new EmailAddress(createUniqueName() + "test@test.tld", u);
80 adrr.insert(Language.getInstance("en"));
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);