1 package club.wpia.gigi.pages.orga;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
6 import java.io.IOException;
7 import java.net.URLConnection;
8 import java.net.URLEncoder;
10 import org.junit.Test;
12 import club.wpia.gigi.GigiApiException;
13 import club.wpia.gigi.dbObjects.Domain;
14 import club.wpia.gigi.dbObjects.Organisation;
15 import club.wpia.gigi.dbObjects.User;
16 import club.wpia.gigi.pages.account.domain.DomainOverview;
17 import club.wpia.gigi.testUtils.IOUtils;
18 import club.wpia.gigi.testUtils.OrgTest;
20 public class TestOrgDomain extends OrgTest {
22 public TestOrgDomain() throws IOException, GigiApiException {
27 public void testAdd() throws IOException, GigiApiException {
28 Organisation o1 = createUniqueOrg();
29 String dom = createUniqueName() + ".de";
30 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "addDomain&domain=" + URLEncoder.encode(dom, "UTF-8"), 3));
31 Domain[] d = o1.getDomains();
32 assertEquals(1, d.length);
33 assertEquals(dom, d[0].getSuffix());
37 public void testDel() throws IOException, GigiApiException {
38 Organisation o1 = createUniqueOrg();
39 String dom = createUniqueName() + ".de";
40 Domain d = new Domain(u, o1, dom);
41 assertEquals(1, o1.getDomains().length);
42 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "delete=" + d.getId(), 2));
43 assertEquals(0, o1.getDomains().length);
47 public void testBusinessAddWhileUser() throws IOException, GigiApiException {
48 Organisation o1 = createUniqueOrg();
49 String dom = createUniqueName() + ".de";
50 new Domain(u, u, dom);
52 new Domain(u, o1, dom);
53 fail("Was able to add domain twice.");
54 } catch (GigiApiException e) {
55 assertEquals("Domain could not be inserted. Domain is already known to the system.", e.getMessage());
58 assertEquals(0, o1.getDomains().length);
59 assertEquals(1, u.getDomains().length);
63 public void testBusinessAddWhileOtherOrg() throws IOException, GigiApiException {
64 Organisation o1 = createUniqueOrg();
65 Organisation o2 = createUniqueOrg();
67 String dom = createUniqueName() + ".de";
68 new Domain(u, o1, dom);
70 new Domain(u, o2, dom);
71 fail("Was able to add domain twice.");
72 } catch (GigiApiException e) {
73 assertEquals("Domain could not be inserted. Domain is already known to the system.", e.getMessage());
76 assertEquals(1, o1.getDomains().length);
77 assertEquals(0, o2.getDomains().length);
78 assertEquals(0, u.getDomains().length);
82 public void testBusinessAddInvalid() throws IOException, GigiApiException {
83 Organisation o1 = createUniqueOrg();
84 String dom = createUniqueName() + ".invalid-tld";
86 new Domain(u, o1, dom);
87 fail("Was able to add invalid domain.");
88 } catch (GigiApiException e) {
91 assertEquals(0, o1.getDomains().length);
92 assertEquals(0, u.getDomains().length);
96 public void testDelAsAdmin() throws IOException, GigiApiException {
97 Organisation o = createUniqueOrg();
98 String dom = createUniqueName() + ".de";
99 Domain d = new Domain(u, o, dom);
100 assertEquals(1, o.getDomains().length);
101 User admin = createOrgAdmin(o);
102 String adminCookie = login(admin.getEmail(), TEST_PASSWORD);
103 assertNull(executeBasicWebInteraction(adminCookie, SwitchOrganisation.PATH, "org:" + o.getId() + "=y", 0));
105 // test that delete button is not displayed
106 URLConnection uc = get(adminCookie, DomainOverview.PATH);
107 uc.setDoOutput(true);
108 String res = IOUtils.readURL(uc);
109 assertThat(res, not(containsString("Delete")));
111 // test that domain cannot be deleted by organisation administrator
112 assertNull(executeBasicWebInteraction(adminCookie, SwitchOrganisation.PATH, "org:" + o.getId() + "=y", 0));
113 uc = post(adminCookie, DomainOverview.PATH, "delete=" + d.getId(), 0);
114 res = IOUtils.readURL(uc);
115 assertThat(res, containsString("You are not allowed to delete a domain."));
117 // verify that domain still belongs to organisation
118 assertEquals(1, o.getDomains().length);