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.io.UnsupportedEncodingException;
8 import java.net.HttpURLConnection;
9 import java.net.MalformedURLException;
10 import java.net.URLConnection;
11 import java.net.URLEncoder;
12 import java.sql.SQLException;
13 import java.util.List;
15 import org.cacert.gigi.GigiApiException;
16 import org.cacert.gigi.dbObjects.Country;
17 import org.cacert.gigi.dbObjects.Country.CountryCodeType;
18 import org.cacert.gigi.dbObjects.Organisation;
19 import org.cacert.gigi.dbObjects.Organisation.Affiliation;
20 import org.cacert.gigi.dbObjects.User;
21 import org.cacert.gigi.pages.account.MyDetails;
22 import org.cacert.gigi.testUtils.IOUtils;
23 import org.cacert.gigi.testUtils.OrgTest;
24 import org.junit.After;
25 import org.junit.Test;
27 public class TestOrgManagement extends OrgTest {
29 public TestOrgManagement() throws IOException, GigiApiException {
34 public void purgeDbAfterTest() throws SQLException, IOException {
39 public void testAdd() throws IOException {
40 for (Organisation i : Organisation.getOrganisations(0, 30)) {
43 assertNull(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));
44 Organisation[] orgs = Organisation.getOrganisations(0, 30);
45 assertEquals(1, orgs.length);
46 assertEquals("mail@serv.tld", orgs[0].getContactEmail());
47 assertEquals("name", orgs[0].getName());
48 assertEquals("Köln", orgs[0].getCity());
49 assertEquals(DIFFICULT_CHARS, orgs[0].getProvince());
50 assertEquals("opname", orgs[0].getOptionalName());
51 assertEquals("postaladdress", orgs[0].getPostalAddress());
53 User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
54 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
55 List<Affiliation> allAdmins = orgs[0].getAllAdmins();
56 assertEquals(1, allAdmins.size());
57 Affiliation affiliation = allAdmins.get(0);
58 assertSame(u2, affiliation.getTarget());
59 assertTrue(affiliation.isMaster());
61 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&do_affiliate=y", 1));
62 allAdmins = orgs[0].getAllAdmins();
63 assertEquals(2, allAdmins.size());
64 Affiliation affiliation2 = allAdmins.get(0);
65 if (affiliation2.getTarget().getId() == u2.getId()) {
66 affiliation2 = allAdmins.get(1);
68 assertEquals(u.getId(), affiliation2.getTarget().getId());
69 assertFalse(affiliation2.isMaster());
71 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
72 assertEquals(1, orgs[0].getAllAdmins().size());
74 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
75 assertEquals(0, orgs[0].getAllAdmins().size());
77 assertNull(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));
79 orgs = Organisation.getOrganisations(0, 30);
80 assertEquals("name1", orgs[0].getName());
84 public void testNonAssurerSeeOnlyOwn() throws IOException, GigiApiException {
85 User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
86 Organisation o1 = createUniqueOrg();
87 Organisation o2 = createUniqueOrg();
88 o1.addAdmin(u2, u, false);
89 String session2 = login(u2.getEmail(), TEST_PASSWORD);
91 URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH);
92 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
94 uc = get(session2, MyDetails.PATH);
95 String content = IOUtils.readURL(uc);
96 assertThat(content, containsString(o1.getName()));
97 assertThat(content, not(containsString(o2.getName())));
98 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
99 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
100 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
101 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
103 uc = get(ViewOrgPage.DEFAULT_PATH);
104 content = IOUtils.readURL(uc);
105 assertThat(content, containsString(o1.getName()));
106 assertThat(content, containsString(o2.getName()));
107 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
108 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
109 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
110 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
116 public void testAffiliationRights() throws IOException, GigiApiException {
117 User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
118 User u3 = User.getById(createAssuranceUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
119 User u4_dummy = User.getById(createVerifiedUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
120 Organisation o1 = createUniqueOrg();
121 o1.addAdmin(u3, u, true);
123 // must fail because u4 is no RA-Agent
124 o1.addAdmin(u4_dummy, u3, false);
125 fail("No exception!");
126 } catch (GigiApiException e) {
128 o1.addAdmin(u2, u3, false);
130 // must fail because u2 may not add admins
131 o1.addAdmin(u3, u2, false);
132 fail("No exception!");
133 } catch (GigiApiException e) {
136 // must fail because u4 is no RA-Agent
137 o1.addAdmin(u4_dummy, u, false);
138 fail("No exception!");
139 } catch (GigiApiException e) {
141 o1.removeAdmin(u2, u3);
142 o1.removeAdmin(u3, u3);
143 assertEquals(0, o1.getAllAdmins().size());
148 public void testUpdateOrgCertData() throws IOException, GigiApiException {
149 Organisation o1 = createUniqueOrg();
150 o1.updateCertData("name", Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), DIFFICULT_CHARS, "Köln");
151 assertEquals("name", o1.getName());
152 assertEquals("DE", o1.getState().getCode());
153 assertEquals(DIFFICULT_CHARS, o1.getProvince());
154 assertEquals("Köln", o1.getCity());
159 public void testUpdateOrgData() throws IOException, GigiApiException {
160 Organisation o1 = createUniqueOrg();
161 o1.updateOrgData("mail", "opname", "Köln" + DIFFICULT_CHARS);
162 assertEquals("mail", o1.getContactEmail());
163 assertEquals("opname", o1.getOptionalName());
164 assertEquals("Köln" + DIFFICULT_CHARS, o1.getPostalAddress());
169 * Tests various contraints on organisation fields.
172 public void testLengthConstraint() throws IOException, GigiApiException {
173 Organisation o1 = createUniqueOrg();
174 String str128 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_";
176 String s64 = str128.substring(0, 64);
177 String s65 = str128.substring(0, 65);
179 String s128 = str128;
180 String s129 = str128 + "a";
182 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), o1.getCity()));
184 // test organisation name
185 assertNotNull(upCertData(o1, "", null, o1.getProvince(), o1.getCity()));
186 assertNull(upCertData(o1, "A", null, o1.getProvince(), o1.getCity()));
187 assertNull(upCertData(o1, s64, null, o1.getProvince(), o1.getCity()));
188 assertNotNull(upCertData(o1, s65, null, o1.getProvince(), o1.getCity()));
191 assertNotNull(upCertData(o1, o1.getName(), null, se, o1.getCity()));
192 assertNull(upCertData(o1, o1.getName(), null, "A", o1.getCity()));
193 assertNull(upCertData(o1, o1.getName(), null, s128, o1.getCity()));
194 assertNotNull(upCertData(o1, o1.getName(), null, s129, o1.getCity()));
197 assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), se));
198 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), "A"));
199 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s128));
200 assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s129));
203 assertNotNull(upCertData(o1, o1.getName(), "", o1.getProvince(), o1.getCity()));
204 assertNotNull(upCertData(o1, o1.getName(), "D", o1.getProvince(), o1.getCity()));
205 assertNull(upCertData(o1, o1.getName(), "DE", o1.getProvince(), o1.getCity()));
206 assertNotNull(upCertData(o1, o1.getName(), "DES", o1.getProvince(), o1.getCity()));
207 // country code does not exist
208 assertNotNull(upCertData(o1, o1.getName(), "DD", o1.getProvince(), o1.getCity()));
209 // 3-letter country code should not be accepted
210 assertNotNull(upCertData(o1, o1.getName(), "DEU", o1.getProvince(), o1.getCity()));
213 assertNull(upOptData(o1, o1.getContactEmail()));
214 assertNotNull(upOptData(o1, "_mail@domail"));
219 * Updates Organisation optional data via web interface.
222 * Organisation to update.
224 * the new contact email
225 * @return an error message or <code>null</code>
227 private String upOptData(Organisation o1, String email) throws IOException, MalformedURLException, UnsupportedEncodingException {
228 return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateOrganisationData&contact=" + email + "&optionalName=" + o1.getOptionalName() + "&postalAddress=" + o1.getPostalAddress(), 0);
232 * Updates Organisation certificate data via web interface.
235 * Organisation to update.
239 * the new country or <code>null</code> to keep the current
242 * the new "province/state"
244 * the new city or "locality"
245 * @return an error message or <code>null</code>
247 private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
249 c = o1.getState().getCode();
251 return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);