1 package club.wpia.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.junit.Test;
13 import club.wpia.gigi.GigiApiException;
14 import club.wpia.gigi.dbObjects.EmailAddress;
15 import club.wpia.gigi.dbObjects.ObjectCache;
16 import club.wpia.gigi.dbObjects.User;
17 import club.wpia.gigi.pages.account.mail.MailOverview;
18 import club.wpia.gigi.testUtils.ClientTest;
20 public class TestMailManagement extends ClientTest {
22 private String path = MailOverview.DEFAULT_PATH;
24 public TestMailManagement() throws IOException {
25 clearCaches(); // and reset rate limits
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() throws GigiApiException {
38 new EmailAddress(u, "kurti ", Locale.ENGLISH);
40 } catch (IllegalArgumentException e) {
46 public void testMailAddWeb() throws MalformedURLException, UnsupportedEncodingException, IOException {
47 String newMail = createUniqueName() + "uni@example.org";
48 assertNull(addMail(newMail));
49 assertTrue(existsEmail(newMail));
50 getMailReceiver().receive(newMail);
54 public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
55 String newMail = createUniqueName() + "uniexample.org";
56 assertNotNull(addMail(newMail));
57 assertFalse(existsEmail(newMail));
61 public void testMailAddWebMultiple() throws MalformedURLException, UnsupportedEncodingException, IOException {
62 String u = createUniqueName();
63 String newMail = u + "uni@eXample.org";
64 assertNull(addMail(newMail));
65 assertTrue(existsEmail(newMail.toLowerCase()));
66 getMailReceiver().receive(newMail.toLowerCase());
68 String newMail2 = u + "uni@eXamPlE.org";
69 assertNotNull(addMail(newMail2));
70 assertTrue(existsEmail(newMail2.toLowerCase()));
72 String newMail3 = u + "-buni@eXamPlE.org";
73 assertNull(addMail(newMail3));
74 assertTrue(existsEmail(newMail.toLowerCase()));
75 assertTrue(existsEmail(newMail3.toLowerCase()));
76 getMailReceiver().receive(newMail3.toLowerCase());
79 private String addMail(String newMail) throws IOException, MalformedURLException, UnsupportedEncodingException {
80 return executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1);
83 private boolean existsEmail(String newMail) {
84 EmailAddress[] addrs = u.getEmails();
85 for (int i = 0; i < addrs.length; i++) {
86 if (addrs[i].getAddress().equals(newMail)) {
94 public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
95 EmailAddress addr = createVerifiedEmail(u);
96 assertNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
97 ObjectCache.clearAllCaches();
98 assertEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
102 public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
103 EmailAddress addr = new EmailAddress(u, createUniqueName() + "test@test.tld", Locale.ENGLISH);
104 assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
105 assertNotEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
106 getMailReceiver().receive(addr.getAddress());
110 public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
111 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
113 EmailAddress[] emails = u2.getEmails();
114 for (int i = 0; i < emails.length; i++) {
115 if (emails[i].getAddress().equals(u2.getEmail())) {
116 id = emails[i].getId();
119 assertNotEquals(id, -1);
120 assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + id));
121 assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
122 getMailReceiver().assertEmpty();
126 public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
127 EmailAddress addr = createVerifiedEmail(u);
128 assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr.getId(), 0));
129 User u = User.getById(this.u.getId());
130 EmailAddress[] addresses = u.getEmails();
131 for (int i = 0; i < addresses.length; i++) {
132 assertNotEquals(addresses[i].getAddress(), addr.getAddress());
137 public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
138 EmailAddress[] addr = new EmailAddress[] {
139 createVerifiedEmail(u), createVerifiedEmail(u)
141 assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[0].getId(), 0));
142 assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[1].getId(), 0));
143 User u = User.getById(this.u.getId());
144 EmailAddress[] addresses = u.getEmails();
145 for (int i = 0; i < addresses.length; i++) {
146 assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
147 assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
152 public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
153 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
154 EmailAddress em = u2.getEmails()[0];
155 assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + em.getId(), 0));
156 u2 = User.getById(u2.getId());
157 assertNotEquals(u2.getEmails().length, 0);
161 public void testMailDeleteWebPrimary() throws MalformedURLException, UnsupportedEncodingException, IOException {
162 assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + u.getEmails()[0].getId(), 0));
163 assertNotEquals(u.getEmails().length, 0);