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.CATS.CATSType;
14 import club.wpia.gigi.dbObjects.Domain;
15 import club.wpia.gigi.dbObjects.Organisation;
16 import club.wpia.gigi.dbObjects.User;
17 import club.wpia.gigi.pages.account.domain.DomainOverview;
18 import club.wpia.gigi.testUtils.IOUtils;
19 import club.wpia.gigi.testUtils.OrgTest;
21 public class TestOrgDomain extends OrgTest {
23 public TestOrgDomain() throws IOException, GigiApiException {
28 public void testAdd() throws IOException, GigiApiException {
29 Organisation o1 = createUniqueOrg();
30 String dom = createUniqueName() + ".de";
31 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "addDomain&domain=" + URLEncoder.encode(dom, "UTF-8"), 3));
32 Domain[] d = o1.getDomains();
33 assertEquals(1, d.length);
34 assertEquals(dom, d[0].getSuffix());
38 public void testDel() throws IOException, GigiApiException {
39 Organisation o1 = createUniqueOrg();
40 String dom = createUniqueName() + ".de";
41 Domain d = new Domain(u, o1, dom);
42 assertEquals(1, o1.getDomains().length);
43 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "delete=" + d.getId(), 2));
44 assertEquals(0, o1.getDomains().length);
48 public void testBusinessAddWhileUser() throws IOException, GigiApiException {
49 Organisation o1 = createUniqueOrg();
50 String dom = createUniqueName() + ".de";
51 new Domain(u, u, dom);
53 new Domain(u, o1, dom);
54 fail("Was able to add domain twice.");
55 } catch (GigiApiException e) {
56 assertEquals("Domain could not be inserted. Domain is already known to the system.", e.getMessage());
59 assertEquals(0, o1.getDomains().length);
60 assertEquals(1, u.getDomains().length);
64 public void testBusinessAddWhileOtherOrg() throws IOException, GigiApiException {
65 Organisation o1 = createUniqueOrg();
66 Organisation o2 = createUniqueOrg();
68 String dom = createUniqueName() + ".de";
69 new Domain(u, o1, dom);
71 new Domain(u, o2, dom);
72 fail("Was able to add domain twice.");
73 } catch (GigiApiException e) {
74 assertEquals("Domain could not be inserted. Domain is already known to the system.", e.getMessage());
77 assertEquals(1, o1.getDomains().length);
78 assertEquals(0, o2.getDomains().length);
79 assertEquals(0, u.getDomains().length);
83 public void testBusinessAddInvalid() throws IOException, GigiApiException {
84 Organisation o1 = createUniqueOrg();
85 String dom = createUniqueName() + ".invalid-tld";
87 new Domain(u, o1, dom);
88 fail("Was able to add invalid domain.");
89 } catch (GigiApiException e) {
92 assertEquals(0, o1.getDomains().length);
93 assertEquals(0, u.getDomains().length);
97 public void testDelAsAdmin() throws IOException, GigiApiException {
98 Organisation o = createUniqueOrg();
99 String dom = createUniqueName() + ".de";
100 Domain d = new Domain(u, o, dom);
101 assertEquals(1, o.getDomains().length);
102 User admin = createOrgAdmin(o);
103 addChallenge(admin.getId(), CATSType.ORG_ADMIN_DP_CHALLENGE_NAME);
104 String adminCookie = cookieWithCertificateLogin(admin);
105 assertNull(executeBasicWebInteraction(adminCookie, SwitchOrganisation.PATH, "org:" + o.getId() + "=y", 0));
107 // test that delete button is not displayed
108 URLConnection uc = get(adminCookie, DomainOverview.PATH);
109 uc.setDoOutput(true);
110 String res = IOUtils.readURL(uc);
111 assertThat(res, not(containsString("Delete")));
113 // test that domain cannot be deleted by organisation administrator
114 assertNull(executeBasicWebInteraction(adminCookie, SwitchOrganisation.PATH, "org:" + o.getId() + "=y", 0));
115 uc = post(adminCookie, DomainOverview.PATH, "delete=" + d.getId(), 0);
116 res = IOUtils.readURL(uc);
117 assertThat(res, containsString("You are not allowed to delete a domain."));
119 // verify that domain still belongs to organisation
120 assertEquals(1, o.getDomains().length);