1 package org.cacert.gigi.pages.orga;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
6 import java.io.IOException;
7 import java.net.HttpURLConnection;
9 import java.net.URLConnection;
10 import java.net.URLEncoder;
11 import java.util.List;
13 import org.cacert.gigi.GigiApiException;
14 import org.cacert.gigi.dbObjects.Group;
15 import org.cacert.gigi.dbObjects.Organisation;
16 import org.cacert.gigi.dbObjects.Organisation.Affiliation;
17 import org.cacert.gigi.dbObjects.User;
18 import org.cacert.gigi.testUtils.ClientTest;
19 import org.cacert.gigi.testUtils.IOUtils;
20 import org.junit.Test;
22 public class TestOrgaManagement extends ClientTest {
24 public TestOrgaManagement() throws IOException {
25 u.grantGroup(u, Group.ORGASSURER);
26 makeAssurer(u.getId());
28 cookie = login(email, TEST_PASSWORD);
32 public void testAdd() throws IOException {
33 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);
34 Organisation[] orgs = Organisation.getOrganisations(0, 30);
35 assertEquals(1, orgs.length);
36 assertEquals("mail", orgs[0].getContactEmail());
37 assertEquals("name", orgs[0].getName());
38 assertEquals("Köln", orgs[0].getCity());
39 assertEquals("ÜÖÄß", orgs[0].getProvince());
41 User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
42 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1);
43 List<Affiliation> allAdmins = orgs[0].getAllAdmins();
44 assertEquals(1, allAdmins.size());
45 Affiliation affiliation = allAdmins.get(0);
46 assertSame(u2, affiliation.getTarget());
47 assertTrue(affiliation.isMaster());
49 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&do_affiliate=y", 1);
50 allAdmins = orgs[0].getAllAdmins();
51 assertEquals(2, allAdmins.size());
52 Affiliation affiliation2 = allAdmins.get(0);
53 if (affiliation2.getTarget().getId() == u2.getId()) {
54 affiliation2 = allAdmins.get(1);
56 assertEquals(u.getId(), affiliation2.getTarget().getId());
57 assertFalse(affiliation2.isMaster());
59 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
60 assertEquals(1, orgs[0].getAllAdmins().size());
62 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
63 assertEquals(0, orgs[0].getAllAdmins().size());
65 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);
67 orgs = Organisation.getOrganisations(0, 30);
68 assertEquals("name1", orgs[0].getName());
72 public void testNonAssurerSeeOnlyOwn() throws IOException, GigiApiException {
73 User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
74 Organisation o1 = new Organisation("name21", "DE", "sder", "Rostov", "email", u);
75 Organisation o2 = new Organisation("name12", "DE", "sder", "Rostov", "email", u);
76 o1.addAdmin(u2, u, false);
77 String session2 = login(u2.getEmail(), TEST_PASSWORD);
79 URLConnection uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
80 uc.addRequestProperty("Cookie", session2);
81 String content = IOUtils.readURL(uc);
82 assertThat(content, containsString("name21"));
83 assertThat(content, not(containsString("name12")));
84 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), session2);
85 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
86 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), session2);
87 assertEquals(404, ((HttpURLConnection) uc).getResponseCode());
89 uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
90 uc.addRequestProperty("Cookie", cookie);
91 content = IOUtils.readURL(uc);
92 assertThat(content, containsString("name21"));
93 assertThat(content, containsString("name12"));
94 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), cookie);
95 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
96 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), cookie);
97 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
103 public void testAffiliationRights() throws IOException, GigiApiException {
104 User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
105 User u3 = User.getById(createAssuranceUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
106 User u4_dummy = User.getById(createVerifiedUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
107 Organisation o1 = new Organisation("name21", "DE", "sder", "Rostov", "email", u);
108 o1.addAdmin(u3, u, true);
110 // must fail because u4 is no assurer
111 o1.addAdmin(u4_dummy, u3, false);
112 fail("No exception!");
113 } catch (GigiApiException e) {
115 o1.addAdmin(u2, u3, false);
117 // must fail because u2 may not add admins
118 o1.addAdmin(u3, u2, false);
119 fail("No exception!");
120 } catch (GigiApiException e) {
123 // must fail because u4 is no assurer
124 o1.addAdmin(u4_dummy, u, false);
125 fail("No exception!");
126 } catch (GigiApiException e) {
128 o1.removeAdmin(u2, u3);
129 o1.removeAdmin(u3, u3);
130 assertEquals(0, o1.getAllAdmins().size());