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.sql.SQLException;
13 import java.util.List;
15 import org.junit.After;
16 import org.junit.Test;
18 import club.wpia.gigi.GigiApiException;
19 import club.wpia.gigi.dbObjects.Country;
20 import club.wpia.gigi.dbObjects.Country.CountryCodeType;
21 import club.wpia.gigi.dbObjects.Organisation;
22 import club.wpia.gigi.dbObjects.Organisation.Affiliation;
23 import club.wpia.gigi.dbObjects.User;
24 import club.wpia.gigi.pages.account.MyDetails;
25 import club.wpia.gigi.testUtils.IOUtils;
26 import club.wpia.gigi.testUtils.OrgTest;
28 public class TestOrgManagement extends OrgTest {
30 public TestOrgManagement() throws IOException, GigiApiException {
35 public void purgeDbAfterTest() throws SQLException, IOException {
40 public void testAdd() throws IOException {
41 for (Organisation i : Organisation.getOrganisations(0, 30)) {
44 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));
45 Organisation[] orgs = Organisation.getOrganisations(0, 30);
46 assertEquals(1, orgs.length);
47 assertEquals("mail@serv.tld", orgs[0].getContactEmail());
48 assertEquals("name", orgs[0].getName());
49 assertEquals("Köln", orgs[0].getCity());
50 assertEquals(DIFFICULT_CHARS, orgs[0].getProvince());
51 assertEquals("opname", orgs[0].getOptionalName());
52 assertEquals("postaladdress", orgs[0].getPostalAddress());
54 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
55 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
56 List<Affiliation> allAdmins = orgs[0].getAllAdmins();
57 assertEquals(1, allAdmins.size());
58 Affiliation affiliation = allAdmins.get(0);
59 assertSame(u2, affiliation.getTarget());
60 assertTrue(affiliation.isMaster());
62 User u3 = User.getById(createVerificationUser("testworker2", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
63 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u3.getEmail(), "UTF-8") + "&do_affiliate=y", 1));
64 allAdmins = orgs[0].getAllAdmins();
65 assertEquals(2, allAdmins.size());
66 Affiliation affiliation2 = allAdmins.get(0);
67 if (affiliation2.getTarget().getId() == u2.getId()) {
68 affiliation2 = allAdmins.get(1);
70 assertEquals(u3.getId(), affiliation2.getTarget().getId());
71 assertFalse(affiliation2.isMaster());
73 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u3.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
74 assertEquals(1, orgs[0].getAllAdmins().size());
76 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
77 assertEquals(0, orgs[0].getAllAdmins().size());
79 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));
81 orgs = Organisation.getOrganisations(0, 30);
82 assertEquals("name1", orgs[0].getName());
86 public void testNonAgentSeeOnlyOwn() throws IOException, GigiApiException {
87 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
88 Organisation o1 = createUniqueOrg();
89 Organisation o2 = createUniqueOrg();
90 o1.addAdmin(u2, u, false);
91 String session2 = login(u2.getEmail(), TEST_PASSWORD);
93 URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH);
94 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
96 uc = get(session2, MyDetails.PATH);
97 String content = IOUtils.readURL(uc);
98 assertThat(content, containsString(o1.getName()));
99 assertThat(content, not(containsString(o2.getName())));
100 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
101 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
102 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
103 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
105 uc = get(ViewOrgPage.DEFAULT_PATH);
106 content = IOUtils.readURL(uc);
107 assertThat(content, containsString(o1.getName()));
108 assertThat(content, containsString(o2.getName()));
109 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
110 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
111 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
112 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
118 public void testAffiliationRights() throws IOException, GigiApiException {
119 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
120 User u3 = User.getById(createVerificationUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
121 User u4_dummy = User.getById(createVerifiedUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
122 Organisation o1 = createUniqueOrg();
123 o1.addAdmin(u3, u, true);
125 // must fail because u4 is no RA-Agent
126 o1.addAdmin(u4_dummy, u3, false);
127 fail("No exception!");
128 } catch (GigiApiException e) {
130 o1.addAdmin(u2, u3, false);
132 // must fail because u2 may not add admins
133 o1.addAdmin(u3, u2, false);
134 fail("No exception!");
135 } catch (GigiApiException e) {
138 // must fail because u4 is no RA-Agent
139 o1.addAdmin(u4_dummy, u, false);
140 fail("No exception!");
141 } catch (GigiApiException e) {
143 o1.removeAdmin(u2, u3);
144 o1.removeAdmin(u3, u3);
145 assertEquals(0, o1.getAllAdmins().size());
147 // must fail because one may not add oneself
148 o1.addAdmin(u3, u3, false);
149 fail("No exception!");
150 } catch (GigiApiException e) {
152 assertEquals(0, o1.getAllAdmins().size());
154 // must fail because one may not add oneself
155 o1.addAdmin(u3, u3, true);
156 fail("No exception!");
157 } catch (GigiApiException e) {
159 assertEquals(0, o1.getAllAdmins().size());
164 public void testUpdateOrgCertData() throws IOException, GigiApiException {
165 Organisation o1 = createUniqueOrg();
166 o1.updateCertData("name", Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), DIFFICULT_CHARS, "Köln");
167 assertEquals("name", o1.getName());
168 assertEquals("DE", o1.getCountry().getCode());
169 assertEquals(DIFFICULT_CHARS, o1.getProvince());
170 assertEquals("Köln", o1.getCity());
175 public void testUpdateOrgData() throws IOException, GigiApiException {
176 Organisation o1 = createUniqueOrg();
177 o1.updateOrgData("mail", "opname", "Köln" + DIFFICULT_CHARS);
178 assertEquals("mail", o1.getContactEmail());
179 assertEquals("opname", o1.getOptionalName());
180 assertEquals("Köln" + DIFFICULT_CHARS, o1.getPostalAddress());
185 * Tests various contraints on organisation fields.
188 public void testLengthConstraint() throws IOException, GigiApiException {
189 Organisation o1 = createUniqueOrg();
190 String str128 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_";
192 String s64 = str128.substring(0, 64);
193 String s65 = str128.substring(0, 65);
195 String s128 = str128;
196 String s129 = str128 + "a";
198 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), o1.getCity()));
200 // test organisation name
201 assertNotNull(upCertData(o1, "", null, o1.getProvince(), o1.getCity()));
202 assertNull(upCertData(o1, "A", null, o1.getProvince(), o1.getCity()));
203 assertNull(upCertData(o1, s64, null, o1.getProvince(), o1.getCity()));
204 assertNotNull(upCertData(o1, s65, null, o1.getProvince(), o1.getCity()));
207 assertNotNull(upCertData(o1, o1.getName(), null, se, o1.getCity()));
208 assertNull(upCertData(o1, o1.getName(), null, "A", o1.getCity()));
209 assertNull(upCertData(o1, o1.getName(), null, s128, o1.getCity()));
210 assertNotNull(upCertData(o1, o1.getName(), null, s129, o1.getCity()));
213 assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), se));
214 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), "A"));
215 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s128));
216 assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s129));
219 assertNotNull(upCertData(o1, o1.getName(), "", o1.getProvince(), o1.getCity()));
220 assertNotNull(upCertData(o1, o1.getName(), "D", o1.getProvince(), o1.getCity()));
221 assertNull(upCertData(o1, o1.getName(), "DE", o1.getProvince(), o1.getCity()));
222 assertNotNull(upCertData(o1, o1.getName(), "DES", o1.getProvince(), o1.getCity()));
223 // country code does not exist
224 assertNotNull(upCertData(o1, o1.getName(), "DD", o1.getProvince(), o1.getCity()));
225 // 3-letter country code should not be accepted
226 assertNotNull(upCertData(o1, o1.getName(), "DEU", o1.getProvince(), o1.getCity()));
229 assertNull(upOptData(o1, o1.getContactEmail()));
230 assertNotNull(upOptData(o1, "_mail@domail"));
235 * Updates Organisation optional data via web interface.
238 * Organisation to update.
240 * the new contact email
241 * @return an error message or <code>null</code>
243 private String upOptData(Organisation o1, String email) throws IOException, MalformedURLException, UnsupportedEncodingException {
244 return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateOrganisationData&contact=" + email + "&optionalName=" + o1.getOptionalName() + "&postalAddress=" + o1.getPostalAddress(), 0);
248 * Updates Organisation certificate data via web interface.
251 * Organisation to update.
255 * the new country or <code>null</code> to keep the current
258 * the new "province/state"
260 * the new city or "locality"
261 * @return an error message or <code>null</code>
263 private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
265 c = o1.getCountry().getCode();
267 return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);