1 package org.cacert.gigi.pages.orga;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
6 import java.io.IOException;
7 import java.net.HttpURLConnection;
9 import java.net.URLConnection;
10 import java.net.URLEncoder;
11 import java.util.List;
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;
21 public class TestOrgaManagement extends ManagedTest {
23 public User u = User.getById(createVerifiedUser("testuser", "testname", uniq + "@testdom.com", TEST_PASSWORD));
25 public String session;
27 public TestOrgaManagement() throws IOException {
28 u.grantGroup(u, Group.getByString("orgassurer"));
30 session = login(uniq + "@testdom.com", TEST_PASSWORD);
34 public void testAdd() throws IOException {
35 executeBasicWebInteraction(session, CreateOrgPage.DEFAULT_PATH, "O=name&contact=&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("name", orgs[0].getName());
39 assertEquals("Köln", orgs[0].getCity());
40 assertEquals("ÜÖÄß", orgs[0].getProvince());
42 User u2 = User.getById(createVerifiedUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
43 executeBasicWebInteraction(session, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&affiliate=y&master=y", 1);
44 List<Affiliation> allAdmins = orgs[0].getAllAdmins();
45 assertEquals(1, allAdmins.size());
46 Affiliation affiliation = allAdmins.get(0);
47 assertSame(u2, affiliation.getTarget());
48 assertTrue(affiliation.isMaster());
50 executeBasicWebInteraction(session, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&affiliate=y", 1);
51 allAdmins = orgs[0].getAllAdmins();
52 assertEquals(2, allAdmins.size());
53 Affiliation affiliation2 = allAdmins.get(0);
54 if (affiliation2.getTarget().getId() == u2.getId()) {
55 affiliation2 = allAdmins.get(1);
57 assertSame(u.getId(), affiliation2.getTarget().getId());
58 assertFalse(affiliation2.isMaster());
60 executeBasicWebInteraction(session, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&email=&affiliate=y", 1);
61 assertEquals(1, orgs[0].getAllAdmins().size());
63 executeBasicWebInteraction(session, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&affiliate=y", 1);
64 assertEquals(0, orgs[0].getAllAdmins().size());
66 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 orgs = Organisation.getOrganisations(0, 30);
69 assertEquals("name1", orgs[0].getName());
73 public void testNonAssurerSeeOnlyOwn() throws IOException {
74 User u2 = User.getById(createVerifiedUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
75 Organisation o1 = new Organisation("name21", "DE", "sder", "Rostov", u);
76 Organisation o2 = new Organisation("name12", "DE", "sder", "Rostov", u);
77 o1.addAdmin(u2, u2, false);
78 String session2 = login(u2.getEmail(), TEST_PASSWORD);
80 URLConnection uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
81 uc.addRequestProperty("Cookie", session2);
82 String content = IOUtils.readURL(uc);
83 assertThat(content, containsString("name21"));
84 assertThat(content, not(containsString("name12")));
85 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), session2);
86 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
87 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), session2);
88 assertEquals(404, ((HttpURLConnection) uc).getResponseCode());
90 uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
91 uc.addRequestProperty("Cookie", session);
92 content = IOUtils.readURL(uc);
93 assertThat(content, containsString("name21"));
94 assertThat(content, containsString("name12"));
95 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), session);
96 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
97 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), session);
98 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());