]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/orga/TestOrgDomain.java
7d4123cba769d6b6f79c110ed9bdfff6fba66b31
[gigi.git] / tests / club / wpia / gigi / pages / orga / TestOrgDomain.java
1 package club.wpia.gigi.pages.orga;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
6 import java.io.IOException;
7 import java.net.URLConnection;
8 import java.net.URLEncoder;
9
10 import org.junit.Test;
11
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;
19
20 public class TestOrgDomain extends OrgTest {
21
22     public TestOrgDomain() throws IOException, GigiApiException {
23
24     }
25
26     @Test
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());
34     }
35
36     @Test
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);
44     }
45
46     @Test
47     public void testBusinessAddWhileUser() throws IOException, GigiApiException {
48         Organisation o1 = createUniqueOrg();
49         String dom = createUniqueName() + ".de";
50         new Domain(u, u, dom);
51         try {
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());
56             // expected
57         }
58         assertEquals(0, o1.getDomains().length);
59         assertEquals(1, u.getDomains().length);
60     }
61
62     @Test
63     public void testBusinessAddWhileOtherOrg() throws IOException, GigiApiException {
64         Organisation o1 = createUniqueOrg();
65         Organisation o2 = createUniqueOrg();
66
67         String dom = createUniqueName() + ".de";
68         new Domain(u, o1, dom);
69         try {
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());
74             // expected
75         }
76         assertEquals(1, o1.getDomains().length);
77         assertEquals(0, o2.getDomains().length);
78         assertEquals(0, u.getDomains().length);
79     }
80
81     @Test
82     public void testBusinessAddInvalid() throws IOException, GigiApiException {
83         Organisation o1 = createUniqueOrg();
84         String dom = createUniqueName() + ".invalid-tld";
85         try {
86             new Domain(u, o1, dom);
87             fail("Was able to add invalid domain.");
88         } catch (GigiApiException e) {
89             // expected
90         }
91         assertEquals(0, o1.getDomains().length);
92         assertEquals(0, u.getDomains().length);
93     }
94
95     @Test
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         loginCertificate = null;
104         assertNull(executeBasicWebInteraction(adminCookie, SwitchOrganisation.PATH, "org:" + o.getId() + "=y", 0));
105
106         // test that delete button is not displayed
107         URLConnection uc = get(adminCookie, DomainOverview.PATH);
108         uc.setDoOutput(true);
109         String res = IOUtils.readURL(uc);
110         assertThat(res, not(containsString("Delete")));
111
112         // test that domain cannot be deleted by organisation administrator
113         assertNull(executeBasicWebInteraction(adminCookie, SwitchOrganisation.PATH, "org:" + o.getId() + "=y", 0));
114         uc = post(adminCookie, DomainOverview.PATH, "delete=" + d.getId(), 0);
115         res = IOUtils.readURL(uc);
116         assertThat(res, containsString("You are not allowed to delete a domain."));
117
118         // verify that domain still belongs to organisation
119         assertEquals(1, o.getDomains().length);
120
121     }
122 }