]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/orga/TestOrgManagement.java
Merge "upd: remove 'browser install'"
[gigi.git] / tests / club / wpia / gigi / pages / orga / TestOrgManagement.java
1 package club.wpia.gigi.pages.orga;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
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;
15
16 import org.junit.After;
17 import org.junit.Test;
18
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;
30
31 public class TestOrgManagement extends OrgTest {
32
33     public TestOrgManagement() throws IOException, GigiApiException {
34
35     }
36
37     @After
38     public void purgeDbAfterTest() throws SQLException, IOException {
39         purgeDatabase();
40     }
41
42     @Test
43     public void testAdd() throws IOException {
44         for (Organisation i : Organisation.getOrganisations(0, 30)) {
45             i.delete();
46         }
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());
56
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());
64
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);
72         }
73         assertEquals(u3.getId(), affiliation2.getTarget().getId());
74         assertFalse(affiliation2.isMaster());
75
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());
78
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());
81
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));
83         clearCaches();
84         orgs = Organisation.getOrganisations(0, 30);
85         assertEquals("name1", orgs[0].getName());
86     }
87
88     @Test
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);
95
96         Certificate c1 = loginCertificate;
97         PrivateKey pk1 = loginPrivateKey;
98         loginCertificate = null;
99
100         URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH);
101         assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
102
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());
112
113         loginCertificate = c1;
114         loginPrivateKey = pk1;
115
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());
124         o1.delete();
125         o2.delete();
126     }
127
128     @Test
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);
135         try {
136             // must fail because u4 is no RA-Agent
137             o1.addAdmin(u4_dummy, u3, false);
138             fail("No exception!");
139         } catch (GigiApiException e) {
140         }
141         o1.addAdmin(u2, u3, false);
142         try {
143             // must fail because u2 may not add admins
144             o1.addAdmin(u3, u2, false);
145             fail("No exception!");
146         } catch (GigiApiException e) {
147         }
148         try {
149             // must fail because u4 is no RA-Agent
150             o1.addAdmin(u4_dummy, u, false);
151             fail("No exception!");
152         } catch (GigiApiException e) {
153         }
154         o1.removeAdmin(u2, u3);
155         o1.removeAdmin(u3, u3);
156         assertEquals(0, o1.getAllAdmins().size());
157         try {
158             // must fail because one may not add oneself
159             o1.addAdmin(u3, u3, false);
160             fail("No exception!");
161         } catch (GigiApiException e) {
162         }
163         assertEquals(0, o1.getAllAdmins().size());
164         try {
165             // must fail because one may not add oneself
166             o1.addAdmin(u3, u3, true);
167             fail("No exception!");
168         } catch (GigiApiException e) {
169         }
170         assertEquals(0, o1.getAllAdmins().size());
171         o1.delete();
172     }
173
174     @Test
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());
182         o1.delete();
183     }
184
185     @Test
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());
192         o1.delete();
193     }
194
195     /**
196      * Tests various contraints on organisation fields.
197      */
198     @Test
199     public void testLengthConstraint() throws IOException, GigiApiException {
200         Organisation o1 = createUniqueOrg();
201         String str128 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_";
202         String se = "";
203         String s64 = str128.substring(0, 64);
204         String s65 = str128.substring(0, 65);
205
206         String s128 = str128;
207         String s129 = str128 + "a";
208
209         assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), o1.getCity()));
210
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()));
216
217         // test state
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()));
222
223         // test town
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));
228
229         // test country
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()));
238
239         // test contact mail
240         assertNull(upOptData(o1, o1.getContactEmail()));
241         assertNotNull(upOptData(o1, "_mail@domail"));
242
243     }
244
245     /**
246      * Updates Organisation optional data via web interface.
247      * 
248      * @param o1
249      *            Organisation to update.
250      * @param email
251      *            the new contact email
252      * @return an error message or <code>null</code>
253      */
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);
256     }
257
258     /**
259      * Updates Organisation certificate data via web interface.
260      * 
261      * @param o1
262      *            Organisation to update.
263      * @param o
264      *            the new name
265      * @param c
266      *            the new country or <code>null</code> to keep the current
267      *            country.
268      * @param province
269      *            the new "province/state"
270      * @param ct
271      *            the new city or "locality"
272      * @return an error message or <code>null</code>
273      */
274     private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
275         if (c == null) {
276             c = o1.getCountry().getCode();
277         }
278         return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);
279     }
280
281     @Test
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());
289     }
290
291     @Test
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());
301     }
302 }