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.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.security.PrivateKey;
13 import java.sql.SQLException;
14 import java.util.List;
16 import org.junit.After;
17 import org.junit.Test;
19 import club.wpia.gigi.GigiApiException;
20 import club.wpia.gigi.dbObjects.Certificate;
21 import club.wpia.gigi.dbObjects.Country;
22 import club.wpia.gigi.dbObjects.Country.CountryCodeType;
23 import club.wpia.gigi.dbObjects.Organisation;
24 import club.wpia.gigi.dbObjects.Organisation.Affiliation;
25 import club.wpia.gigi.dbObjects.User;
26 import club.wpia.gigi.pages.account.MyDetails;
27 import club.wpia.gigi.testUtils.IOUtils;
28 import club.wpia.gigi.testUtils.OrgTest;
30 public class TestOrgManagement extends OrgTest {
32 public TestOrgManagement() throws IOException, GigiApiException {
37 public void purgeDbAfterTest() throws SQLException, IOException {
42 public void testAdd() throws IOException {
43 for (Organisation i : Organisation.getOrganisations(0, 30)) {
46 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));
47 Organisation[] orgs = Organisation.getOrganisations(0, 30);
48 assertEquals(1, orgs.length);
49 assertEquals("mail@serv.tld", orgs[0].getContactEmail());
50 assertEquals("name", orgs[0].getName());
51 assertEquals("Köln", orgs[0].getCity());
52 assertEquals(DIFFICULT_CHARS, orgs[0].getProvince());
53 assertEquals("opname", orgs[0].getOptionalName());
54 assertEquals("postaladdress", orgs[0].getPostalAddress());
56 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
57 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
58 List<Affiliation> allAdmins = orgs[0].getAllAdmins();
59 assertEquals(1, allAdmins.size());
60 Affiliation affiliation = allAdmins.get(0);
61 assertSame(u2, affiliation.getTarget());
62 assertTrue(affiliation.isMaster());
64 User u3 = User.getById(createVerificationUser("testworker2", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
65 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u3.getEmail(), "UTF-8") + "&do_affiliate=y", 1));
66 allAdmins = orgs[0].getAllAdmins();
67 assertEquals(2, allAdmins.size());
68 Affiliation affiliation2 = allAdmins.get(0);
69 if (affiliation2.getTarget().getId() == u2.getId()) {
70 affiliation2 = allAdmins.get(1);
72 assertEquals(u3.getId(), affiliation2.getTarget().getId());
73 assertFalse(affiliation2.isMaster());
75 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u3.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
76 assertEquals(1, orgs[0].getAllAdmins().size());
78 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
79 assertEquals(0, orgs[0].getAllAdmins().size());
81 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));
83 orgs = Organisation.getOrganisations(0, 30);
84 assertEquals("name1", orgs[0].getName());
88 public void testNonAgentSeeOnlyOwn() throws IOException, GigiApiException {
89 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
90 Organisation o1 = createUniqueOrg();
91 Organisation o2 = createUniqueOrg();
92 o1.addAdmin(u2, u, false);
93 String session2 = login(u2.getEmail(), TEST_PASSWORD);
95 Certificate c1 = loginCertificate;
96 PrivateKey pk1 = loginPrivateKey;
97 loginCertificate = null;
99 URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH);
100 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
102 session2 = cookieWithCertificateLogin(u2);
103 uc = get(session2, MyDetails.PATH);
104 String content = IOUtils.readURL(uc);
105 assertThat(content, containsString(o1.getName()));
106 assertThat(content, not(containsString(o2.getName())));
107 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
108 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
109 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
110 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
112 loginCertificate = c1;
113 loginPrivateKey = pk1;
115 uc = get(ViewOrgPage.DEFAULT_PATH);
116 content = IOUtils.readURL(uc);
117 assertThat(content, containsString(o1.getName()));
118 assertThat(content, containsString(o2.getName()));
119 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
120 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
121 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
122 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
128 public void testAffiliationRights() throws IOException, GigiApiException {
129 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
130 User u3 = User.getById(createVerificationUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
131 User u4_dummy = User.getById(createVerifiedUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
132 Organisation o1 = createUniqueOrg();
133 o1.addAdmin(u3, u, true);
135 // must fail because u4 is no RA-Agent
136 o1.addAdmin(u4_dummy, u3, false);
137 fail("No exception!");
138 } catch (GigiApiException e) {
140 o1.addAdmin(u2, u3, false);
142 // must fail because u2 may not add admins
143 o1.addAdmin(u3, u2, false);
144 fail("No exception!");
145 } catch (GigiApiException e) {
148 // must fail because u4 is no RA-Agent
149 o1.addAdmin(u4_dummy, u, false);
150 fail("No exception!");
151 } catch (GigiApiException e) {
153 o1.removeAdmin(u2, u3);
154 o1.removeAdmin(u3, u3);
155 assertEquals(0, o1.getAllAdmins().size());
157 // must fail because one may not add oneself
158 o1.addAdmin(u3, u3, false);
159 fail("No exception!");
160 } catch (GigiApiException e) {
162 assertEquals(0, o1.getAllAdmins().size());
164 // must fail because one may not add oneself
165 o1.addAdmin(u3, u3, true);
166 fail("No exception!");
167 } catch (GigiApiException e) {
169 assertEquals(0, o1.getAllAdmins().size());
174 public void testUpdateOrgCertData() throws IOException, GigiApiException {
175 Organisation o1 = createUniqueOrg();
176 o1.updateCertData("name", Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), DIFFICULT_CHARS, "Köln");
177 assertEquals("name", o1.getName());
178 assertEquals("DE", o1.getCountry().getCode());
179 assertEquals(DIFFICULT_CHARS, o1.getProvince());
180 assertEquals("Köln", o1.getCity());
185 public void testUpdateOrgData() throws IOException, GigiApiException {
186 Organisation o1 = createUniqueOrg();
187 o1.updateOrgData("mail", "opname", "Köln" + DIFFICULT_CHARS);
188 assertEquals("mail", o1.getContactEmail());
189 assertEquals("opname", o1.getOptionalName());
190 assertEquals("Köln" + DIFFICULT_CHARS, o1.getPostalAddress());
195 * Tests various contraints on organisation fields.
198 public void testLengthConstraint() throws IOException, GigiApiException {
199 Organisation o1 = createUniqueOrg();
200 String str128 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_";
202 String s64 = str128.substring(0, 64);
203 String s65 = str128.substring(0, 65);
205 String s128 = str128;
206 String s129 = str128 + "a";
208 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), o1.getCity()));
210 // test organisation name
211 assertNotNull(upCertData(o1, "", null, o1.getProvince(), o1.getCity()));
212 assertNull(upCertData(o1, "A", null, o1.getProvince(), o1.getCity()));
213 assertNull(upCertData(o1, s64, null, o1.getProvince(), o1.getCity()));
214 assertNotNull(upCertData(o1, s65, null, o1.getProvince(), o1.getCity()));
217 assertNotNull(upCertData(o1, o1.getName(), null, se, o1.getCity()));
218 assertNull(upCertData(o1, o1.getName(), null, "A", o1.getCity()));
219 assertNull(upCertData(o1, o1.getName(), null, s128, o1.getCity()));
220 assertNotNull(upCertData(o1, o1.getName(), null, s129, o1.getCity()));
223 assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), se));
224 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), "A"));
225 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s128));
226 assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s129));
229 assertNotNull(upCertData(o1, o1.getName(), "", o1.getProvince(), o1.getCity()));
230 assertNotNull(upCertData(o1, o1.getName(), "D", o1.getProvince(), o1.getCity()));
231 assertNull(upCertData(o1, o1.getName(), "DE", o1.getProvince(), o1.getCity()));
232 assertNotNull(upCertData(o1, o1.getName(), "DES", o1.getProvince(), o1.getCity()));
233 // country code does not exist
234 assertNotNull(upCertData(o1, o1.getName(), "DD", o1.getProvince(), o1.getCity()));
235 // 3-letter country code should not be accepted
236 assertNotNull(upCertData(o1, o1.getName(), "DEU", o1.getProvince(), o1.getCity()));
239 assertNull(upOptData(o1, o1.getContactEmail()));
240 assertNotNull(upOptData(o1, "_mail@domail"));
245 * Updates Organisation optional data via web interface.
248 * Organisation to update.
250 * the new contact email
251 * @return an error message or <code>null</code>
253 private String upOptData(Organisation o1, String email) throws IOException, MalformedURLException, UnsupportedEncodingException {
254 return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateOrganisationData&contact=" + email + "&optionalName=" + o1.getOptionalName() + "&postalAddress=" + o1.getPostalAddress(), 0);
258 * Updates Organisation certificate data via web interface.
261 * Organisation to update.
265 * the new country or <code>null</code> to keep the current
268 * the new "province/state"
270 * the new city or "locality"
271 * @return an error message or <code>null</code>
273 private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
275 c = o1.getCountry().getCode();
277 return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);
281 public void testAgentWithoutCertLogin() throws IOException, GigiApiException {
282 cookie = login(u.getEmail(), TEST_PASSWORD);
283 loginCertificate = null;
284 URLConnection uc = get(cookie, ViewOrgPage.DEFAULT_PATH);
285 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
286 uc = get(cookie, CreateOrgPage.DEFAULT_PATH);
287 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());