]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/orga/TestOrgManagement.java
760ca198c5ba7c199eb410df307abb4933b3bb42
[gigi.git] / tests / org / cacert / gigi / pages / orga / TestOrgManagement.java
1 package org.cacert.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.cacert.gigi.GigiApiException;
16 import org.cacert.gigi.dbObjects.Country;
17 import org.cacert.gigi.dbObjects.Country.CountryCodeType;
18 import org.cacert.gigi.dbObjects.Organisation;
19 import org.cacert.gigi.dbObjects.Organisation.Affiliation;
20 import org.cacert.gigi.dbObjects.User;
21 import org.cacert.gigi.pages.account.MyDetails;
22 import org.cacert.gigi.testUtils.IOUtils;
23 import org.cacert.gigi.testUtils.OrgTest;
24 import org.junit.After;
25 import org.junit.Test;
26
27 public class TestOrgManagement extends OrgTest {
28
29     public TestOrgManagement() throws IOException {
30
31     }
32
33     @After
34     public void purgeDbAfterTest() throws SQLException, IOException {
35         purgeDatabase();
36     }
37
38     @Test
39     public void testAdd() throws IOException {
40         for (Organisation i : Organisation.getOrganisations(0, 30)) {
41             i.delete();
42         }
43         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));
44         Organisation[] orgs = Organisation.getOrganisations(0, 30);
45         assertEquals(1, orgs.length);
46         assertEquals("mail@serv.tld", orgs[0].getContactEmail());
47         assertEquals("name", orgs[0].getName());
48         assertEquals("Köln", orgs[0].getCity());
49         assertEquals(DIFFICULT_CHARS, orgs[0].getProvince());
50         assertEquals("opname", orgs[0].getOptionalName());
51         assertEquals("postaladdress", orgs[0].getPostalAddress());
52
53         User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
54         assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
55         List<Affiliation> allAdmins = orgs[0].getAllAdmins();
56         assertEquals(1, allAdmins.size());
57         Affiliation affiliation = allAdmins.get(0);
58         assertSame(u2, affiliation.getTarget());
59         assertTrue(affiliation.isMaster());
60
61         assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&do_affiliate=y", 1));
62         allAdmins = orgs[0].getAllAdmins();
63         assertEquals(2, allAdmins.size());
64         Affiliation affiliation2 = allAdmins.get(0);
65         if (affiliation2.getTarget().getId() == u2.getId()) {
66             affiliation2 = allAdmins.get(1);
67         }
68         assertEquals(u.getId(), affiliation2.getTarget().getId());
69         assertFalse(affiliation2.isMaster());
70
71         assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
72         assertEquals(1, orgs[0].getAllAdmins().size());
73
74         assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1));
75         assertEquals(0, orgs[0].getAllAdmins().size());
76
77         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));
78         clearCaches();
79         orgs = Organisation.getOrganisations(0, 30);
80         assertEquals("name1", orgs[0].getName());
81     }
82
83     @Test
84     public void testNonAssurerSeeOnlyOwn() throws IOException, GigiApiException {
85         User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
86         Organisation o1 = createUniqueOrg();
87         Organisation o2 = createUniqueOrg();
88         o1.addAdmin(u2, u, false);
89         String session2 = login(u2.getEmail(), TEST_PASSWORD);
90
91         URLConnection uc = get(session2, ViewOrgPage.DEFAULT_PATH);
92         assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
93
94         uc = get(session2, MyDetails.PATH);
95         String content = IOUtils.readURL(uc);
96         assertThat(content, containsString(o1.getName()));
97         assertThat(content, not(containsString(o2.getName())));
98         uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
99         assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
100         uc = get(session2, ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
101         assertEquals(403, ((HttpURLConnection) uc).getResponseCode());
102
103         uc = get(ViewOrgPage.DEFAULT_PATH);
104         content = IOUtils.readURL(uc);
105         assertThat(content, containsString(o1.getName()));
106         assertThat(content, containsString(o2.getName()));
107         uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o1.getId());
108         assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
109         uc = get(ViewOrgPage.DEFAULT_PATH + "/" + o2.getId());
110         assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
111         o1.delete();
112         o2.delete();
113     }
114
115     @Test
116     public void testAffiliationRights() throws IOException, GigiApiException {
117         User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
118         User u3 = User.getById(createAssuranceUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
119         User u4_dummy = User.getById(createVerifiedUser("testmaster", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
120         Organisation o1 = createUniqueOrg();
121         o1.addAdmin(u3, u, true);
122         try {
123             // must fail because u4 is no RA-Agent
124             o1.addAdmin(u4_dummy, u3, false);
125             fail("No exception!");
126         } catch (GigiApiException e) {
127         }
128         o1.addAdmin(u2, u3, false);
129         try {
130             // must fail because u2 may not add admins
131             o1.addAdmin(u3, u2, false);
132             fail("No exception!");
133         } catch (GigiApiException e) {
134         }
135         try {
136             // must fail because u4 is no RA-Agent
137             o1.addAdmin(u4_dummy, u, false);
138             fail("No exception!");
139         } catch (GigiApiException e) {
140         }
141         o1.removeAdmin(u2, u3);
142         o1.removeAdmin(u3, u3);
143         assertEquals(0, o1.getAllAdmins().size());
144         o1.delete();
145     }
146
147     @Test
148     public void testUpdateOrgCertData() throws IOException, GigiApiException {
149         Organisation o1 = createUniqueOrg();
150         o1.updateCertData("name", Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), DIFFICULT_CHARS, "Köln");
151         assertEquals("name", o1.getName());
152         assertEquals("DE", o1.getState().getCode());
153         assertEquals(DIFFICULT_CHARS, o1.getProvince());
154         assertEquals("Köln", o1.getCity());
155         o1.delete();
156     }
157
158     @Test
159     public void testUpdateOrgData() throws IOException, GigiApiException {
160         Organisation o1 = createUniqueOrg();
161         o1.updateOrgData("mail", "opname", "Köln" + DIFFICULT_CHARS);
162         assertEquals("mail", o1.getContactEmail());
163         assertEquals("opname", o1.getOptionalName());
164         assertEquals("Köln" + DIFFICULT_CHARS, o1.getPostalAddress());
165         o1.delete();
166     }
167
168     /**
169      * Tests various contraints on organisation fields.
170      */
171     @Test
172     public void testLengthConstraint() throws IOException, GigiApiException {
173         Organisation o1 = createUniqueOrg();
174         String str128 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-_";
175         String se = "";
176         String s64 = str128.substring(0, 64);
177         String s65 = str128.substring(0, 65);
178
179         String s128 = str128;
180         String s129 = str128 + "a";
181
182         assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), o1.getCity()));
183
184         // test organisation name
185         assertNotNull(upCertData(o1, "", null, o1.getProvince(), o1.getCity()));
186         assertNull(upCertData(o1, "A", null, o1.getProvince(), o1.getCity()));
187         assertNull(upCertData(o1, s64, null, o1.getProvince(), o1.getCity()));
188         assertNotNull(upCertData(o1, s65, null, o1.getProvince(), o1.getCity()));
189
190         // test state
191         assertNotNull(upCertData(o1, o1.getName(), null, se, o1.getCity()));
192         assertNull(upCertData(o1, o1.getName(), null, "A", o1.getCity()));
193         assertNull(upCertData(o1, o1.getName(), null, s128, o1.getCity()));
194         assertNotNull(upCertData(o1, o1.getName(), null, s129, o1.getCity()));
195
196         // test town
197         assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), se));
198         assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), "A"));
199         assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s128));
200         assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s129));
201
202         // test country
203         assertNotNull(upCertData(o1, o1.getName(), "", o1.getProvince(), o1.getCity()));
204         assertNotNull(upCertData(o1, o1.getName(), "D", o1.getProvince(), o1.getCity()));
205         assertNull(upCertData(o1, o1.getName(), "DE", o1.getProvince(), o1.getCity()));
206         assertNotNull(upCertData(o1, o1.getName(), "DES", o1.getProvince(), o1.getCity()));
207         // country code does not exist
208         assertNotNull(upCertData(o1, o1.getName(), "DD", o1.getProvince(), o1.getCity()));
209         // 3-letter country code should not be accepted
210         assertNotNull(upCertData(o1, o1.getName(), "DEU", o1.getProvince(), o1.getCity()));
211
212         // test contact mail
213         assertNull(upOptData(o1, o1.getContactEmail()));
214         assertNotNull(upOptData(o1, "_mail@domail"));
215
216     }
217
218     /**
219      * Updates Organisation optional data via web interface.
220      * 
221      * @param o1
222      *            Organisation to update.
223      * @param email
224      *            the new contact email
225      * @return an error message or <code>null</code>
226      */
227     private String upOptData(Organisation o1, String email) throws IOException, MalformedURLException, UnsupportedEncodingException {
228         return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateOrganisationData&contact=" + email + "&optionalName=" + o1.getOptionalName() + "&postalAddress=" + o1.getPostalAddress(), 0);
229     }
230
231     /**
232      * Updates Organisation certificate data via web interface.
233      * 
234      * @param o1
235      *            Organisation to update.
236      * @param o
237      *            the new name
238      * @param c
239      *            the new country or <code>null</code> to keep the current
240      *            country.
241      * @param province
242      *            the new "province/state"
243      * @param ct
244      *            the new city or "locality"
245      * @return an error message or <code>null</code>
246      */
247     private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
248         if (c == null) {
249             c = o1.getState().getCode();
250         }
251         return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);
252     }
253
254 }