]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/orga/TestOrgaManagement.java
2623fa020ac85b3fa61f810087d8049a16c8489c
[gigi.git] / tests / org / cacert / gigi / pages / orga / TestOrgaManagement.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.net.HttpURLConnection;
8 import java.net.URL;
9 import java.net.URLConnection;
10 import java.net.URLEncoder;
11 import java.util.List;
12
13 import org.cacert.gigi.dbObjects.Group;
14 import org.cacert.gigi.dbObjects.Organisation;
15 import org.cacert.gigi.dbObjects.Organisation.Affiliation;
16 import org.cacert.gigi.dbObjects.User;
17 import org.cacert.gigi.testUtils.IOUtils;
18 import org.cacert.gigi.testUtils.ManagedTest;
19 import org.junit.Test;
20
21 public class TestOrgaManagement extends ManagedTest {
22
23     public User u = User.getById(createVerifiedUser("testuser", "testname", uniq + "@testdom.com", TEST_PASSWORD));
24
25     public String session;
26
27     public TestOrgaManagement() throws IOException {
28         u.grantGroup(u, Group.getByString("orgassurer"));
29         clearCaches();
30         session = login(uniq + "@testdom.com", TEST_PASSWORD);
31     }
32
33     @Test
34     public void testAdd() throws IOException {
35         executeBasicWebInteraction(session, CreateOrgPage.DEFAULT_PATH, "O=name&contact=mail&L=K%C3%B6ln&ST=%C3%9C%C3%96%C3%84%C3%9F&C=DE&comments=jkl%C3%B6loiuzfdfgjlh%C3%B6", 0);
36         Organisation[] orgs = Organisation.getOrganisations(0, 30);
37         assertEquals(1, orgs.length);
38         assertEquals("mail", orgs[0].getContactEmail());
39         assertEquals("name", orgs[0].getName());
40         assertEquals("Köln", orgs[0].getCity());
41         assertEquals("ÜÖÄß", orgs[0].getProvince());
42
43         User u2 = User.getById(createVerifiedUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
44         executeBasicWebInteraction(session, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1);
45         List<Affiliation> allAdmins = orgs[0].getAllAdmins();
46         assertEquals(1, allAdmins.size());
47         Affiliation affiliation = allAdmins.get(0);
48         assertSame(u2, affiliation.getTarget());
49         assertTrue(affiliation.isMaster());
50
51         executeBasicWebInteraction(session, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&do_affiliate=y", 1);
52         allAdmins = orgs[0].getAllAdmins();
53         assertEquals(2, allAdmins.size());
54         Affiliation affiliation2 = allAdmins.get(0);
55         if (affiliation2.getTarget().getId() == u2.getId()) {
56             affiliation2 = allAdmins.get(1);
57         }
58         assertSame(u.getId(), affiliation2.getTarget().getId());
59         assertFalse(affiliation2.isMaster());
60
61         executeBasicWebInteraction(session, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
62         assertEquals(1, orgs[0].getAllAdmins().size());
63
64         executeBasicWebInteraction(session, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
65         assertEquals(0, orgs[0].getAllAdmins().size());
66
67         executeBasicWebInteraction(session, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "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);
68         clearCaches();
69         orgs = Organisation.getOrganisations(0, 30);
70         assertEquals("name1", orgs[0].getName());
71     }
72
73     @Test
74     public void testNonAssurerSeeOnlyOwn() throws IOException {
75         User u2 = User.getById(createVerifiedUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
76         Organisation o1 = new Organisation("name21", "DE", "sder", "Rostov", "email", u);
77         Organisation o2 = new Organisation("name12", "DE", "sder", "Rostov", "email", u);
78         o1.addAdmin(u2, u2, false);
79         String session2 = login(u2.getEmail(), TEST_PASSWORD);
80
81         URLConnection uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
82         uc.addRequestProperty("Cookie", session2);
83         String content = IOUtils.readURL(uc);
84         assertThat(content, containsString("name21"));
85         assertThat(content, not(containsString("name12")));
86         uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), session2);
87         assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
88         uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), session2);
89         assertEquals(404, ((HttpURLConnection) uc).getResponseCode());
90
91         uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
92         uc.addRequestProperty("Cookie", session);
93         content = IOUtils.readURL(uc);
94         assertThat(content, containsString("name21"));
95         assertThat(content, containsString("name12"));
96         uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), session);
97         assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
98         uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), session);
99         assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
100         o1.delete();
101         o2.delete();
102     }
103 }