]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/orga/TestOrgManagement.java
2bb91f12c87dad6df21e67ba3d5b19b0f8f0c574
[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.sql.SQLException;
13 import java.util.List;
14
15 import org.junit.After;
16 import org.junit.Test;
17
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;
27
28 public class TestOrgManagement extends OrgTest {
29
30     public TestOrgManagement() throws IOException, GigiApiException {
31
32     }
33
34     @After
35     public void purgeDbAfterTest() throws SQLException, IOException {
36         purgeDatabase();
37     }
38
39     @Test
40     public void testAdd() throws IOException {
41         for (Organisation i : Organisation.getOrganisations(0, 30)) {
42             i.delete();
43         }
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());
53
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());
61
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);
69         }
70         assertEquals(u3.getId(), affiliation2.getTarget().getId());
71         assertFalse(affiliation2.isMaster());
72
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());
75
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());
78
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));
80         clearCaches();
81         orgs = Organisation.getOrganisations(0, 30);
82         assertEquals("name1", orgs[0].getName());
83     }
84
85     @Test
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);
92
93         URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH);
94         assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
95
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());
104
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());
113         o1.delete();
114         o2.delete();
115     }
116
117     @Test
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);
124         try {
125             // must fail because u4 is no RA-Agent
126             o1.addAdmin(u4_dummy, u3, false);
127             fail("No exception!");
128         } catch (GigiApiException e) {
129         }
130         o1.addAdmin(u2, u3, false);
131         try {
132             // must fail because u2 may not add admins
133             o1.addAdmin(u3, u2, false);
134             fail("No exception!");
135         } catch (GigiApiException e) {
136         }
137         try {
138             // must fail because u4 is no RA-Agent
139             o1.addAdmin(u4_dummy, u, false);
140             fail("No exception!");
141         } catch (GigiApiException e) {
142         }
143         o1.removeAdmin(u2, u3);
144         o1.removeAdmin(u3, u3);
145         assertEquals(0, o1.getAllAdmins().size());
146         try {
147             // must fail because one may not add oneself
148             o1.addAdmin(u3, u3, false);
149             fail("No exception!");
150         } catch (GigiApiException e) {
151         }
152         assertEquals(0, o1.getAllAdmins().size());
153         try {
154             // must fail because one may not add oneself
155             o1.addAdmin(u3, u3, true);
156             fail("No exception!");
157         } catch (GigiApiException e) {
158         }
159         assertEquals(0, o1.getAllAdmins().size());
160         o1.delete();
161     }
162
163     @Test
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());
171         o1.delete();
172     }
173
174     @Test
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());
181         o1.delete();
182     }
183
184     /**
185      * Tests various contraints on organisation fields.
186      */
187     @Test
188     public void testLengthConstraint() throws IOException, GigiApiException {
189         Organisation o1 = createUniqueOrg();
190         String str128 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_";
191         String se = "";
192         String s64 = str128.substring(0, 64);
193         String s65 = str128.substring(0, 65);
194
195         String s128 = str128;
196         String s129 = str128 + "a";
197
198         assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), o1.getCity()));
199
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()));
205
206         // test state
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()));
211
212         // test town
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));
217
218         // test country
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()));
227
228         // test contact mail
229         assertNull(upOptData(o1, o1.getContactEmail()));
230         assertNotNull(upOptData(o1, "_mail@domail"));
231
232     }
233
234     /**
235      * Updates Organisation optional data via web interface.
236      * 
237      * @param o1
238      *            Organisation to update.
239      * @param email
240      *            the new contact email
241      * @return an error message or <code>null</code>
242      */
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);
245     }
246
247     /**
248      * Updates Organisation certificate data via web interface.
249      * 
250      * @param o1
251      *            Organisation to update.
252      * @param o
253      *            the new name
254      * @param c
255      *            the new country or <code>null</code> to keep the current
256      *            country.
257      * @param province
258      *            the new "province/state"
259      * @param ct
260      *            the new city or "locality"
261      * @return an error message or <code>null</code>
262      */
263     private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
264         if (c == null) {
265             c = o1.getCountry().getCode();
266         }
267         return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);
268     }
269
270 }