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));
53 public void testMailAddWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
54 String newMail = createUniqueName() + "uniexample.org";
55 assertNotNull(addMail(newMail));
56 assertFalse(existsEmail(newMail));
60 public void testMailAddWebMultiple() throws MalformedURLException, UnsupportedEncodingException, IOException {
61 String u = createUniqueName();
62 String newMail = u + "uni@eXample.org";
63 assertNull(addMail(newMail));
64 assertTrue(existsEmail(newMail.toLowerCase()));
66 String newMail2 = u + "uni@eXamPlE.org";
67 assertNotNull(addMail(newMail2));
68 assertTrue(existsEmail(newMail2.toLowerCase()));
70 String newMail3 = u + "-buni@eXamPlE.org";
71 assertNull(addMail(newMail3));
72 assertTrue(existsEmail(newMail.toLowerCase()));
73 assertTrue(existsEmail(newMail3.toLowerCase()));
76 private String addMail(String newMail) throws IOException, MalformedURLException, UnsupportedEncodingException {
77 return executeBasicWebInteraction(cookie, path, "addmail&newemail=" + URLEncoder.encode(newMail, "UTF-8"), 1);
80 private boolean existsEmail(String newMail) {
81 EmailAddress[] addrs = u.getEmails();
82 for (int i = 0; i < addrs.length; i++) {
83 if (addrs[i].getAddress().equals(newMail)) {
91 public void testMailSetDefaultWeb() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
92 EmailAddress addr = createVerifiedEmail(u);
93 assertNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
94 ObjectCache.clearAllCaches();
95 assertEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
99 public void testMailSetDefaultWebUnverified() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
100 EmailAddress addr = new EmailAddress(u, createUniqueName() + "test@test.tld", Locale.ENGLISH);
101 assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + addr.getId()));
102 assertNotEquals(User.getById(u.getId()).getEmail(), addr.getAddress());
103 getMailReceiver().clearMails();
107 public void testMailSetDefaultWebInvalidID() throws MalformedURLException, UnsupportedEncodingException, IOException, InterruptedException, GigiApiException {
108 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@example.org", TEST_PASSWORD));
110 EmailAddress[] emails = u2.getEmails();
111 for (int i = 0; i < emails.length; i++) {
112 if (emails[i].getAddress().equals(u2.getEmail())) {
113 id = emails[i].getId();
116 assertNotEquals(id, -1);
117 assertNotNull(executeBasicWebInteraction(cookie, path, "default=" + id));
118 assertNotEquals(User.getById(u.getId()).getEmail(), u2.getEmail());
119 getMailReceiver().clearMails();
123 public void testMailDeleteWeb() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
124 EmailAddress addr = createVerifiedEmail(u);
125 assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr.getId(), 0));
126 User u = User.getById(this.u.getId());
127 EmailAddress[] addresses = u.getEmails();
128 for (int i = 0; i < addresses.length; i++) {
129 assertNotEquals(addresses[i].getAddress(), addr.getAddress());
134 public void testMailDeleteWebMulti() throws InterruptedException, GigiApiException, MalformedURLException, UnsupportedEncodingException, IOException {
135 EmailAddress[] addr = new EmailAddress[] {
136 createVerifiedEmail(u), createVerifiedEmail(u)
138 assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[0].getId(), 0));
139 assertNull(executeBasicWebInteraction(cookie, path, "delete=" + addr[1].getId(), 0));
140 User u = User.getById(this.u.getId());
141 EmailAddress[] addresses = u.getEmails();
142 for (int i = 0; i < addresses.length; i++) {
143 assertNotEquals(addresses[i].getAddress(), addr[0].getAddress());
144 assertNotEquals(addresses[i].getAddress(), addr[1].getAddress());
149 public void testMailDeleteWebFaulty() throws MalformedURLException, UnsupportedEncodingException, IOException {
150 User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "uni@test.tld", TEST_PASSWORD));
151 EmailAddress em = u2.getEmails()[0];
152 assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + em.getId(), 0));
153 u2 = User.getById(u2.getId());
154 assertNotEquals(u2.getEmails().length, 0);
158 public void testMailDeleteWebPrimary() throws MalformedURLException, UnsupportedEncodingException, IOException {
159 assertNotNull(executeBasicWebInteraction(cookie, path, "delete=" + u.getEmails()[0].getId(), 0));
160 assertNotEquals(u.getEmails().length, 0);