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.Organisation;
21 import club.wpia.gigi.dbObjects.User;
22 import club.wpia.gigi.dbObjects.Country.CountryCodeType;
23 import club.wpia.gigi.dbObjects.Organisation.Affiliation;
24 import club.wpia.gigi.pages.account.MyDetails;
25 import club.wpia.gigi.pages.orga.CreateOrgPage;
26 import club.wpia.gigi.pages.orga.ViewOrgPage;
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 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&do_affiliate=y", 1));
65 allAdmins = orgs[0].getAllAdmins();
66 assertEquals(2, allAdmins.size());
67 Affiliation affiliation2 = allAdmins.get(0);
68 if (affiliation2.getTarget().getId() == u2.getId()) {
69 affiliation2 = allAdmins.get(1);
71 assertEquals(u.getId(), affiliation2.getTarget().getId());
72 assertFalse(affiliation2.isMaster());
74 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
75 assertEquals(1, orgs[0].getAllAdmins().size());
77 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
78 assertEquals(0, orgs[0].getAllAdmins().size());
80 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));
82 orgs = Organisation.getOrganisations(0, 30);
83 assertEquals("name1", orgs[0].getName());
87 public void testNonAgentSeeOnlyOwn() throws IOException, GigiApiException {
88 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
89 Organisation o1 = createUniqueOrg();
90 Organisation o2 = createUniqueOrg();
91 o1.addAdmin(u2, u, false);
92 String session2 = login(u2.getEmail(), TEST_PASSWORD);
94 URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH);
95 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
97 uc = get(session2, MyDetails.PATH);
98 String content = IOUtils.readURL(uc);
99 assertThat(content, containsString(o1.getName()));
100 assertThat(content, not(containsString(o2.getName())));
101 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
102 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
103 uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
104 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
106 uc = get(ViewOrgPage.DEFAULT_PATH);
107 content = IOUtils.readURL(uc);
108 assertThat(content, containsString(o1.getName()));
109 assertThat(content, containsString(o2.getName()));
110 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
111 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
112 uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
113 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
119 public void testAffiliationRights() throws IOException, GigiApiException {
120 User u2 = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
121 User u3 = User.getById(createVerificationUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
122 User u4_dummy = User.getById(createVerifiedUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
123 Organisation o1 = createUniqueOrg();
124 o1.addAdmin(u3, u, true);
126 // must fail because u4 is no RA-Agent
127 o1.addAdmin(u4_dummy, u3, false);
128 fail("No exception!");
129 } catch (GigiApiException e) {
131 o1.addAdmin(u2, u3, false);
133 // must fail because u2 may not add admins
134 o1.addAdmin(u3, u2, false);
135 fail("No exception!");
136 } catch (GigiApiException e) {
139 // must fail because u4 is no RA-Agent
140 o1.addAdmin(u4_dummy, u, false);
141 fail("No exception!");
142 } catch (GigiApiException e) {
144 o1.removeAdmin(u2, u3);
145 o1.removeAdmin(u3, u3);
146 assertEquals(0, o1.getAllAdmins().size());
151 public void testUpdateOrgCertData() throws IOException, GigiApiException {
152 Organisation o1 = createUniqueOrg();
153 o1.updateCertData("name", Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), DIFFICULT_CHARS, "Köln");
154 assertEquals("name", o1.getName());
155 assertEquals("DE", o1.getCountry().getCode());
156 assertEquals(DIFFICULT_CHARS, o1.getProvince());
157 assertEquals("Köln", o1.getCity());
162 public void testUpdateOrgData() throws IOException, GigiApiException {
163 Organisation o1 = createUniqueOrg();
164 o1.updateOrgData("mail", "opname", "Köln" + DIFFICULT_CHARS);
165 assertEquals("mail", o1.getContactEmail());
166 assertEquals("opname", o1.getOptionalName());
167 assertEquals("Köln" + DIFFICULT_CHARS, o1.getPostalAddress());
172 * Tests various contraints on organisation fields.
175 public void testLengthConstraint() throws IOException, GigiApiException {
176 Organisation o1 = createUniqueOrg();
177 String str128 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_";
179 String s64 = str128.substring(0, 64);
180 String s65 = str128.substring(0, 65);
182 String s128 = str128;
183 String s129 = str128 + "a";
185 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), o1.getCity()));
187 // test organisation name
188 assertNotNull(upCertData(o1, "", null, o1.getProvince(), o1.getCity()));
189 assertNull(upCertData(o1, "A", null, o1.getProvince(), o1.getCity()));
190 assertNull(upCertData(o1, s64, null, o1.getProvince(), o1.getCity()));
191 assertNotNull(upCertData(o1, s65, null, o1.getProvince(), o1.getCity()));
194 assertNotNull(upCertData(o1, o1.getName(), null, se, o1.getCity()));
195 assertNull(upCertData(o1, o1.getName(), null, "A", o1.getCity()));
196 assertNull(upCertData(o1, o1.getName(), null, s128, o1.getCity()));
197 assertNotNull(upCertData(o1, o1.getName(), null, s129, o1.getCity()));
200 assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), se));
201 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), "A"));
202 assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s128));
203 assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s129));
206 assertNotNull(upCertData(o1, o1.getName(), "", o1.getProvince(), o1.getCity()));
207 assertNotNull(upCertData(o1, o1.getName(), "D", o1.getProvince(), o1.getCity()));
208 assertNull(upCertData(o1, o1.getName(), "DE", o1.getProvince(), o1.getCity()));
209 assertNotNull(upCertData(o1, o1.getName(), "DES", o1.getProvince(), o1.getCity()));
210 // country code does not exist
211 assertNotNull(upCertData(o1, o1.getName(), "DD", o1.getProvince(), o1.getCity()));
212 // 3-letter country code should not be accepted
213 assertNotNull(upCertData(o1, o1.getName(), "DEU", o1.getProvince(), o1.getCity()));
216 assertNull(upOptData(o1, o1.getContactEmail()));
217 assertNotNull(upOptData(o1, "_mail@domail"));
222 * Updates Organisation optional data via web interface.
225 * Organisation to update.
227 * the new contact email
228 * @return an error message or <code>null</code>
230 private String upOptData(Organisation o1, String email) throws IOException, MalformedURLException, UnsupportedEncodingException {
231 return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateOrganisationData&contact=" + email + "&optionalName=" + o1.getOptionalName() + "&postalAddress=" + o1.getPostalAddress(), 0);
235 * Updates Organisation certificate data via web interface.
238 * Organisation to update.
242 * the new country or <code>null</code> to keep the current
245 * the new "province/state"
247 * the new city or "locality"
248 * @return an error message or <code>null</code>
250 private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
252 c = o1.getCountry().getCode();
254 return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);