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;
8 import java.net.URLConnection;
9 import java.net.URLEncoder;
10 import java.sql.SQLException;
11 import java.util.List;
13 import org.cacert.gigi.GigiApiException;
14 import org.cacert.gigi.dbObjects.Organisation;
15 import org.cacert.gigi.dbObjects.Organisation.Affiliation;
16 import org.cacert.gigi.dbObjects.User;
17 import org.cacert.gigi.pages.account.MyDetails;
18 import org.cacert.gigi.testUtils.IOUtils;
19 import org.cacert.gigi.testUtils.OrgTest;
20 import org.junit.After;
21 import org.junit.Test;
23 public class TestOrgManagement extends OrgTest {
25 public TestOrgManagement() throws IOException {
30 public void purgeDbAfterTest() throws SQLException, IOException {
35 public void testAdd() throws IOException {
36 for (Organisation i : Organisation.getOrganisations(0, 30)) {
39 executeBasicWebInteraction(cookie, CreateOrgPage.DEFAULT_PATH, "action=new&O=name&contact=mail@serv.tld&L=K%C3%B6ln&ST=" + URLEncoder.encode(DIFFICULT_CHARS, "UTF-8") + "&C=DE&comments=jkl%C3%B6loiuzfdfgjlh%C3%B6&optionalName=opname&postalAddress=postaladdress", 0);
40 Organisation[] orgs = Organisation.getOrganisations(0, 30);
41 assertEquals(1, orgs.length);
42 assertEquals("mail@serv.tld", orgs[0].getContactEmail());
43 assertEquals("name", orgs[0].getName());
44 assertEquals("Köln", orgs[0].getCity());
45 assertEquals(DIFFICULT_CHARS, orgs[0].getProvince());
46 assertEquals("opname", orgs[0].getOptionalName());
47 assertEquals("postaladdress", orgs[0].getPostalAddress());
49 User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
50 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1);
51 List<Affiliation> allAdmins = orgs[0].getAllAdmins();
52 assertEquals(1, allAdmins.size());
53 Affiliation affiliation = allAdmins.get(0);
54 assertSame(u2, affiliation.getTarget());
55 assertTrue(affiliation.isMaster());
57 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&do_affiliate=y", 1);
58 allAdmins = orgs[0].getAllAdmins();
59 assertEquals(2, allAdmins.size());
60 Affiliation affiliation2 = allAdmins.get(0);
61 if (affiliation2.getTarget().getId() == u2.getId()) {
62 affiliation2 = allAdmins.get(1);
64 assertEquals(u.getId(), affiliation2.getTarget().getId());
65 assertFalse(affiliation2.isMaster());
67 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
68 assertEquals(1, orgs[0].getAllAdmins().size());
70 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
71 assertEquals(0, orgs[0].getAllAdmins().size());
73 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "action=updateCertificateData&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);
75 orgs = Organisation.getOrganisations(0, 30);
76 assertEquals("name1", orgs[0].getName());
80 public void testNonAssurerSeeOnlyOwn() throws IOException, GigiApiException {
81 User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
82 Organisation o1 = createUniqueOrg();
83 Organisation o2 = createUniqueOrg();
84 o1.addAdmin(u2, u, false);
85 String session2 = login(u2.getEmail(), TEST_PASSWORD);
87 URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH);
88 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
90 uc = get(session2, MyDetails.PATH);
91 String content = IOUtils.readURL(uc);
92 assertThat(content, containsString(o1.getName()));
93 assertThat(content, not(containsString(o2.getName())));
94 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
95 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
96 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
97 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
99 uc = get(ViewOrgPage.DEFAULT_PATH);
100 content = IOUtils.readURL(uc);
101 assertThat(content, containsString(o1.getName()));
102 assertThat(content, containsString(o2.getName()));
103 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
104 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
105 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
106 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
112 public void testAffiliationRights() throws IOException, GigiApiException {
113 User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
114 User u3 = User.getById(createAssuranceUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
115 User u4_dummy = User.getById(createVerifiedUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
116 Organisation o1 = createUniqueOrg();
117 o1.addAdmin(u3, u, true);
119 // must fail because u4 is no assurer
120 o1.addAdmin(u4_dummy, u3, false);
121 fail("No exception!");
122 } catch (GigiApiException e) {
124 o1.addAdmin(u2, u3, false);
126 // must fail because u2 may not add admins
127 o1.addAdmin(u3, u2, false);
128 fail("No exception!");
129 } catch (GigiApiException e) {
132 // must fail because u4 is no assurer
133 o1.addAdmin(u4_dummy, u, false);
134 fail("No exception!");
135 } catch (GigiApiException e) {
137 o1.removeAdmin(u2, u3);
138 o1.removeAdmin(u3, u3);
139 assertEquals(0, o1.getAllAdmins().size());
144 public void testUpdateOrgCertData() throws IOException, GigiApiException {
145 Organisation o1 = createUniqueOrg();
146 o1.updateCertData("name", "DE", DIFFICULT_CHARS, "Köln");
147 assertEquals("name", o1.getName());
148 assertEquals("DE", o1.getState());
149 assertEquals(DIFFICULT_CHARS, o1.getProvince());
150 assertEquals("Köln", o1.getCity());
155 public void testUpdateOrgData() throws IOException, GigiApiException {
156 Organisation o1 = createUniqueOrg();
157 o1.updateOrgData("mail", "opname", "Köln" + DIFFICULT_CHARS);
158 assertEquals("mail", o1.getContactEmail());
159 assertEquals("opname", o1.getOptionalName());
160 assertEquals("Köln" + DIFFICULT_CHARS, o1.getPostalAddress());