]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/orga/TestOrgManagement.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[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.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;
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(createAssuranceUser("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         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);
70         }
71         assertEquals(u.getId(), affiliation2.getTarget().getId());
72         assertFalse(affiliation2.isMaster());
73
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());
76
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());
79
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));
81         clearCaches();
82         orgs = Organisation.getOrganisations(0, 30);
83         assertEquals("name1", orgs[0].getName());
84     }
85
86     @Test
87     public void testNonAssurerSeeOnlyOwn() throws IOException, GigiApiException {
88         User u2 = User.getById(createAssuranceUser("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);
93
94         URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH);
95         assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
96
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());
105
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());
114         o1.delete();
115         o2.delete();
116     }
117
118     @Test
119     public void testAffiliationRights() throws IOException, GigiApiException {
120         User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
121         User u3 = User.getById(createAssuranceUser("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);
125         try {
126             // must fail because u4 is no RA-Agent
127             o1.addAdmin(u4_dummy, u3, false);
128             fail("No exception!");
129         } catch (GigiApiException e) {
130         }
131         o1.addAdmin(u2, u3, false);
132         try {
133             // must fail because u2 may not add admins
134             o1.addAdmin(u3, u2, false);
135             fail("No exception!");
136         } catch (GigiApiException e) {
137         }
138         try {
139             // must fail because u4 is no RA-Agent
140             o1.addAdmin(u4_dummy, u, false);
141             fail("No exception!");
142         } catch (GigiApiException e) {
143         }
144         o1.removeAdmin(u2, u3);
145         o1.removeAdmin(u3, u3);
146         assertEquals(0, o1.getAllAdmins().size());
147         o1.delete();
148     }
149
150     @Test
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());
158         o1.delete();
159     }
160
161     @Test
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());
168         o1.delete();
169     }
170
171     /**
172      * Tests various contraints on organisation fields.
173      */
174     @Test
175     public void testLengthConstraint() throws IOException, GigiApiException {
176         Organisation o1 = createUniqueOrg();
177         String str128 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_";
178         String se = "";
179         String s64 = str128.substring(0, 64);
180         String s65 = str128.substring(0, 65);
181
182         String s128 = str128;
183         String s129 = str128 + "a";
184
185         assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), o1.getCity()));
186
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()));
192
193         // test state
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()));
198
199         // test town
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));
204
205         // test country
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()));
214
215         // test contact mail
216         assertNull(upOptData(o1, o1.getContactEmail()));
217         assertNotNull(upOptData(o1, "_mail@domail"));
218
219     }
220
221     /**
222      * Updates Organisation optional data via web interface.
223      * 
224      * @param o1
225      *            Organisation to update.
226      * @param email
227      *            the new contact email
228      * @return an error message or <code>null</code>
229      */
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);
232     }
233
234     /**
235      * Updates Organisation certificate data via web interface.
236      * 
237      * @param o1
238      *            Organisation to update.
239      * @param o
240      *            the new name
241      * @param c
242      *            the new country or <code>null</code> to keep the current
243      *            country.
244      * @param province
245      *            the new "province/state"
246      * @param ct
247      *            the new city or "locality"
248      * @return an error message or <code>null</code>
249      */
250     private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
251         if (c == null) {
252             c = o1.getCountry().getCode();
253         }
254         return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);
255     }
256
257 }