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