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.ClientTest;
18 import org.cacert.gigi.testUtils.IOUtils;
19 import org.junit.Test;
21 public class TestOrgaManagement extends ClientTest {
23 public TestOrgaManagement() throws IOException {
24 u.grantGroup(u, Group.getByString("orgassurer"));
25 makeAssurer(u.getId());
27 cookie = login(email, TEST_PASSWORD);
31 public void testAdd() throws IOException {
32 executeBasicWebInteraction(cookie, 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);
33 Organisation[] orgs = Organisation.getOrganisations(0, 30);
34 assertEquals(1, orgs.length);
35 assertEquals("mail", orgs[0].getContactEmail());
36 assertEquals("name", orgs[0].getName());
37 assertEquals("Köln", orgs[0].getCity());
38 assertEquals("ÜÖÄß", orgs[0].getProvince());
40 User u2 = User.getById(createAssuranceUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
41 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1);
42 List<Affiliation> allAdmins = orgs[0].getAllAdmins();
43 assertEquals(1, allAdmins.size());
44 Affiliation affiliation = allAdmins.get(0);
45 assertSame(u2, affiliation.getTarget());
46 assertTrue(affiliation.isMaster());
48 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&do_affiliate=y", 1);
49 allAdmins = orgs[0].getAllAdmins();
50 assertEquals(2, allAdmins.size());
51 Affiliation affiliation2 = allAdmins.get(0);
52 if (affiliation2.getTarget().getId() == u2.getId()) {
53 affiliation2 = allAdmins.get(1);
55 assertSame(u.getId(), affiliation2.getTarget().getId());
56 assertFalse(affiliation2.isMaster());
58 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
59 assertEquals(1, orgs[0].getAllAdmins().size());
61 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
62 assertEquals(0, orgs[0].getAllAdmins().size());
64 executeBasicWebInteraction(cookie, 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);
66 orgs = Organisation.getOrganisations(0, 30);
67 assertEquals("name1", orgs[0].getName());
71 public void testNonAssurerSeeOnlyOwn() throws IOException {
72 User u2 = User.getById(createVerifiedUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
73 Organisation o1 = new Organisation("name21", "DE", "sder", "Rostov", "email", u);
74 Organisation o2 = new Organisation("name12", "DE", "sder", "Rostov", "email", u);
75 o1.addAdmin(u2, u2, false);
76 String session2 = login(u2.getEmail(), TEST_PASSWORD);
78 URLConnection uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
79 uc.addRequestProperty("Cookie", session2);
80 String content = IOUtils.readURL(uc);
81 assertThat(content, containsString("name21"));
82 assertThat(content, not(containsString("name12")));
83 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), session2);
84 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
85 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), session2);
86 assertEquals(404, ((HttpURLConnection) uc).getResponseCode());
88 uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
89 uc.addRequestProperty("Cookie", cookie);
90 content = IOUtils.readURL(uc);
91 assertThat(content, containsString("name21"));
92 assertThat(content, containsString("name12"));
93 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), cookie);
94 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
95 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), cookie);
96 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());