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.CATS.CATSType;
21 import club.wpia.gigi.dbObjects.Certificate;
22 import club.wpia.gigi.dbObjects.Country;
23 import club.wpia.gigi.dbObjects.Country.CountryCodeType;
24 import club.wpia.gigi.dbObjects.Organisation;
25 import club.wpia.gigi.dbObjects.Organisation.Affiliation;
26 import club.wpia.gigi.dbObjects.User;
27 import club.wpia.gigi.pages.account.MyDetails;
28 import club.wpia.gigi.testUtils.IOUtils;
29 import club.wpia.gigi.testUtils.OrgTest;
31 public class TestOrgManagement extends OrgTest {
33 public TestOrgManagement() throws IOException, GigiApiException {
38 public void purgeDbAfterTest() throws SQLException, IOException {
43 public void testAdd() throws IOException {
44 for (Organisation i : Organisation.getOrganisations(0, 30)) {
47 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));
48 Organisation[] orgs = Organisation.getOrganisations(0, 30);
49 assertEquals(1, orgs.length);
50 assertEquals("mail@serv.tld", orgs[0].getContactEmail());
51 assertEquals("name", orgs[0].getName());
52 assertEquals("Köln", orgs[0].getCity());
53 assertEquals(DIFFICULT_CHARS, orgs[0].getProvince());
54 assertEquals("opname", orgs[0].getOptionalName());
55 assertEquals("postaladdress", orgs[0].getPostalAddress());
57 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
58 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
59 List<Affiliation> allAdmins = orgs[0].getAllAdmins();
60 assertEquals(1, allAdmins.size());
61 Affiliation affiliation = allAdmins.get(0);
62 assertSame(u2, affiliation.getTarget());
63 assertTrue(affiliation.isMaster());
65 User u3 = User.getById(createVerificationUser("testworker2", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
66 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u3.getEmail(), "UTF-8") + "&do_affiliate=y", 1));
67 allAdmins = orgs[0].getAllAdmins();
68 assertEquals(2, allAdmins.size());
69 Affiliation affiliation2 = allAdmins.get(0);
70 if (affiliation2.getTarget().getId() == u2.getId()) {
71 affiliation2 = allAdmins.get(1);
73 assertEquals(u3.getId(), affiliation2.getTarget().getId());
74 assertFalse(affiliation2.isMaster());
76 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u3.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
77 assertEquals(1, orgs[0].getAllAdmins().size());
79 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
80 assertEquals(0, orgs[0].getAllAdmins().size());
82 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));
84 orgs = Organisation.getOrganisations(0, 30);
85 assertEquals("name1", orgs[0].getName());
89 public void testNonAgentSeeOnlyOwn() throws IOException, GigiApiException {
90 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
91 Organisation o1 = createUniqueOrg();
92 Organisation o2 = createUniqueOrg();
93 o1.addAdmin(u2, u, false);
94 String session2 = login(u2.getEmail(), TEST_PASSWORD);
96 Certificate c1 = loginCertificate;
97 PrivateKey pk1 = loginPrivateKey;
98 loginCertificate = null;
100 URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH);
101 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
103 session2 = cookieWithCertificateLogin(u2);
104 uc = get(session2, MyDetails.PATH);
105 String content = IOUtils.readURL(uc);
106 assertThat(content, containsString(o1.getName()));
107 assertThat(content, not(containsString(o2.getName())));
108 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
109 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
110 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
111 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
113 loginCertificate = c1;
114 loginPrivateKey = pk1;
116 uc = get(ViewOrgPage.DEFAULT_PATH);
117 content = IOUtils.readURL(uc);
118 assertThat(content, containsString(o1.getName()));
119 assertThat(content, containsString(o2.getName()));
120 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
121 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
122 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
123 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
129 public void testAffiliationRights() throws IOException, GigiApiException {
130 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
131 User u3 = User.getById(createVerificationUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
132 User u4_dummy = User.getById(createVerifiedUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
133 Organisation o1 = createUniqueOrg();
134 o1.addAdmin(u3, u, true);
136 // must fail because u4 is no RA-Agent
137 o1.addAdmin(u4_dummy, u3, false);
138 fail("No exception!");
139 } catch (GigiApiException e) {
141 o1.addAdmin(u2, u3, false);
143 // must fail because u2 may not add admins
144 o1.addAdmin(u3, u2, false);
145 fail("No exception!");
146 } catch (GigiApiException e) {
149 // must fail because u4 is no RA-Agent
150 o1.addAdmin(u4_dummy, u, false);
151 fail("No exception!");
152 } catch (GigiApiException e) {
154 o1.removeAdmin(u2, u3);
155 o1.removeAdmin(u3, u3);
156 assertEquals(0, o1.getAllAdmins().size());
158 // must fail because one may not add oneself
159 o1.addAdmin(u3, u3, false);
160 fail("No exception!");
161 } catch (GigiApiException e) {
163 assertEquals(0, o1.getAllAdmins().size());
165 // must fail because one may not add oneself
166 o1.addAdmin(u3, u3, true);
167 fail("No exception!");
168 } catch (GigiApiException e) {
170 assertEquals(0, o1.getAllAdmins().size());
175 public void testUpdateOrgCertData() throws IOException, GigiApiException {
176 Organisation o1 = createUniqueOrg();
177 o1.updateCertData("name", Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), DIFFICULT_CHARS, "Köln");
178 assertEquals("name", o1.getName());
179 assertEquals("DE", o1.getCountry().getCode());
180 assertEquals(DIFFICULT_CHARS, o1.getProvince());
181 assertEquals("Köln", o1.getCity());
186 public void testUpdateOrgData() throws IOException, GigiApiException {
187 Organisation o1 = createUniqueOrg();
188 o1.updateOrgData("mail", "opname", "Köln" + DIFFICULT_CHARS);
189 assertEquals("mail", o1.getContactEmail());
190 assertEquals("opname", o1.getOptionalName());
191 assertEquals("Köln" + DIFFICULT_CHARS, o1.getPostalAddress());
196 * Tests various contraints on organisation fields.
199 public void testLengthConstraint() throws IOException, GigiApiException {
200 Organisation o1 = createUniqueOrg();
201 String str128 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_";
203 String s64 = str128.substring(0, 64);
204 String s65 = str128.substring(0, 65);
206 String s128 = str128;
207 String s129 = str128 + "a";
209 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), o1.getCity()));
211 // test organisation name
212 assertNotNull(upCertData(o1, "", null, o1.getProvince(), o1.getCity()));
213 assertNull(upCertData(o1, "A", null, o1.getProvince(), o1.getCity()));
214 assertNull(upCertData(o1, s64, null, o1.getProvince(), o1.getCity()));
215 assertNotNull(upCertData(o1, s65, null, o1.getProvince(), o1.getCity()));
218 assertNotNull(upCertData(o1, o1.getName(), null, se, o1.getCity()));
219 assertNull(upCertData(o1, o1.getName(), null, "A", o1.getCity()));
220 assertNull(upCertData(o1, o1.getName(), null, s128, o1.getCity()));
221 assertNotNull(upCertData(o1, o1.getName(), null, s129, o1.getCity()));
224 assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), se));
225 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), "A"));
226 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s128));
227 assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s129));
230 assertNotNull(upCertData(o1, o1.getName(), "", o1.getProvince(), o1.getCity()));
231 assertNotNull(upCertData(o1, o1.getName(), "D", o1.getProvince(), o1.getCity()));
232 assertNull(upCertData(o1, o1.getName(), "DE", o1.getProvince(), o1.getCity()));
233 assertNotNull(upCertData(o1, o1.getName(), "DES", o1.getProvince(), o1.getCity()));
234 // country code does not exist
235 assertNotNull(upCertData(o1, o1.getName(), "DD", o1.getProvince(), o1.getCity()));
236 // 3-letter country code should not be accepted
237 assertNotNull(upCertData(o1, o1.getName(), "DEU", o1.getProvince(), o1.getCity()));
240 assertNull(upOptData(o1, o1.getContactEmail()));
241 assertNotNull(upOptData(o1, "_mail@domail"));
246 * Updates Organisation optional data via web interface.
249 * Organisation to update.
251 * the new contact email
252 * @return an error message or <code>null</code>
254 private String upOptData(Organisation o1, String email) throws IOException, MalformedURLException, UnsupportedEncodingException {
255 return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateOrganisationData&contact=" + email + "&optionalName=" + o1.getOptionalName() + "&postalAddress=" + o1.getPostalAddress(), 0);
259 * Updates Organisation certificate data via web interface.
262 * Organisation to update.
266 * the new country or <code>null</code> to keep the current
269 * the new "province/state"
271 * the new city or "locality"
272 * @return an error message or <code>null</code>
274 private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
276 c = o1.getCountry().getCode();
278 return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);
282 public void testAgentWithoutCertLogin() throws IOException, GigiApiException {
283 cookie = login(u.getEmail(), TEST_PASSWORD);
284 loginCertificate = null;
285 URLConnection uc = get(cookie, ViewOrgPage.DEFAULT_PATH);
286 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
287 uc = get(cookie, CreateOrgPage.DEFAULT_PATH);
288 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
292 public void testAgentWithoutValidChallenge() throws IOException, GigiApiException {
293 User agent = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
294 addChallenge(agent.getId(), CATSType.ORG_AGENT_CHALLENGE);
295 loginCertificate = null;
296 cookie = cookieWithCertificateLogin(agent);
297 URLConnection uc = get(cookie, ViewOrgPage.DEFAULT_PATH);
298 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
299 uc = get(cookie, CreateOrgPage.DEFAULT_PATH);
300 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());