]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/orga/TestOrgaManagement.java
(hopefully) stabelize the Orga test
[gigi.git] / tests / org / cacert / gigi / pages / orga / TestOrgaManagement.java
1 package org.cacert.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.HttpURLConnection;
8 import java.net.URL;
9 import java.net.URLConnection;
10 import java.net.URLEncoder;
11 import java.sql.SQLException;
12 import java.util.List;
13
14 import org.cacert.gigi.GigiApiException;
15 import org.cacert.gigi.dbObjects.Group;
16 import org.cacert.gigi.dbObjects.Organisation;
17 import org.cacert.gigi.dbObjects.Organisation.Affiliation;
18 import org.cacert.gigi.dbObjects.User;
19 import org.cacert.gigi.pages.account.MyDetails;
20 import org.cacert.gigi.testUtils.ClientTest;
21 import org.cacert.gigi.testUtils.IOUtils;
22 import org.junit.After;
23 import org.junit.Test;
24
25 public class TestOrgaManagement extends ClientTest {
26
27     public TestOrgaManagement() throws IOException {
28         u.grantGroup(u, Group.ORGASSURER);
29         makeAssurer(u.getId());
30         clearCaches();
31         cookie = login(email, TEST_PASSWORD);
32     }
33
34     @After
35     public void purgeDbAfterTest() throws SQLException, IOException {
36         purgeDatabase();
37     }
38
39     @Test
40     public void testAdd() throws IOException {
41         for (Organisation i : Organisation.getOrganisations(0, 30)) {
42             i.delete();
43         }
44         executeBasicWebInteraction(cookie, CreateOrgPage.DEFAULT_PATH, "O=name&contact=mail&L=K%C3%B6ln&ST=%C3%9C%C3%96%C3%84%C3%9F&C=DE&comments=jkl%C3%B6loiuzfdfgjlh%C3%B6", 0);
45         Organisation[] orgs = Organisation.getOrganisations(0, 30);
46         assertEquals(1, orgs.length);
47         assertEquals("mail", orgs[0].getContactEmail());
48         assertEquals("name", orgs[0].getName());
49         assertEquals("Köln", orgs[0].getCity());
50         assertEquals("ÜÖÄß", orgs[0].getProvince());
51
52         User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
53         executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1);
54         List<Affiliation> allAdmins = orgs[0].getAllAdmins();
55         assertEquals(1, allAdmins.size());
56         Affiliation affiliation = allAdmins.get(0);
57         assertSame(u2, affiliation.getTarget());
58         assertTrue(affiliation.isMaster());
59
60         executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&do_affiliate=y", 1);
61         allAdmins = orgs[0].getAllAdmins();
62         assertEquals(2, allAdmins.size());
63         Affiliation affiliation2 = allAdmins.get(0);
64         if (affiliation2.getTarget().getId() == u2.getId()) {
65             affiliation2 = allAdmins.get(1);
66         }
67         assertEquals(u.getId(), affiliation2.getTarget().getId());
68         assertFalse(affiliation2.isMaster());
69
70         executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
71         assertEquals(1, orgs[0].getAllAdmins().size());
72
73         executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
74         assertEquals(0, orgs[0].getAllAdmins().size());
75
76         executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "O=name1&contact=&L=K%C3%B6ln&ST=%C3%9C%C3%96%C3%84%C3%9F&C=DE&comments=jkl%C3%B6loiuzfdfgjlh%C3%B6", 0);
77         clearCaches();
78         orgs = Organisation.getOrganisations(0, 30);
79         assertEquals("name1", orgs[0].getName());
80     }
81
82     @Test
83     public void testNonAssurerSeeOnlyOwn() throws IOException, GigiApiException {
84         User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
85         Organisation o1 = new Organisation("name21", "DE", "sder", "Rostov", "email", u);
86         Organisation o2 = new Organisation("name12", "DE", "sder", "Rostov", "email", u);
87         o1.addAdmin(u2, u, false);
88         String session2 = login(u2.getEmail(), TEST_PASSWORD);
89
90         URLConnection uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
91         uc.addRequestProperty("Cookie", session2);
92         assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
93
94         uc = new URL("https://" + getServerName() + MyDetails.PATH).openConnection();
95         uc.addRequestProperty("Cookie", session2);
96         String content = IOUtils.readURL(uc);
97         assertThat(content, containsString("name21"));
98         assertThat(content, not(containsString("name12")));
99         uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), session2);
100         assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
101         uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), session2);
102         assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
103
104         uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
105         uc.addRequestProperty("Cookie", cookie);
106         content = IOUtils.readURL(uc);
107         assertThat(content, containsString("name21"));
108         assertThat(content, containsString("name12"));
109         uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), cookie);
110         assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
111         uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), cookie);
112         assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
113         o1.delete();
114         o2.delete();
115     }
116
117     @Test
118     public void testAffiliationRights() throws IOException, GigiApiException {
119         User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
120         User u3 = User.getById(createAssuranceUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
121         User u4_dummy = User.getById(createVerifiedUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
122         Organisation o1 = new Organisation("name21", "DE", "sder", "Rostov", "email", u);
123         o1.addAdmin(u3, u, true);
124         try {
125             // must fail because u4 is no assurer
126             o1.addAdmin(u4_dummy, u3, false);
127             fail("No exception!");
128         } catch (GigiApiException e) {
129         }
130         o1.addAdmin(u2, u3, false);
131         try {
132             // must fail because u2 may not add admins
133             o1.addAdmin(u3, u2, false);
134             fail("No exception!");
135         } catch (GigiApiException e) {
136         }
137         try {
138             // must fail because u4 is no assurer
139             o1.addAdmin(u4_dummy, u, false);
140             fail("No exception!");
141         } catch (GigiApiException e) {
142         }
143         o1.removeAdmin(u2, u3);
144         o1.removeAdmin(u3, u3);
145         assertEquals(0, o1.getAllAdmins().size());
146         o1.delete();
147     }
148 }