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"));
26 cookie = login(email, TEST_PASSWORD);
30 public void testAdd() throws IOException {
31 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);
32 Organisation[] orgs = Organisation.getOrganisations(0, 30);
33 assertEquals(1, orgs.length);
34 assertEquals("mail", orgs[0].getContactEmail());
35 assertEquals("name", orgs[0].getName());
36 assertEquals("Köln", orgs[0].getCity());
37 assertEquals("ÜÖÄß", orgs[0].getProvince());
39 User u2 = User.getById(createVerifiedUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
40 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1);
41 List<Affiliation> allAdmins = orgs[0].getAllAdmins();
42 assertEquals(1, allAdmins.size());
43 Affiliation affiliation = allAdmins.get(0);
44 assertSame(u2, affiliation.getTarget());
45 assertTrue(affiliation.isMaster());
47 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "email=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&do_affiliate=y", 1);
48 allAdmins = orgs[0].getAllAdmins();
49 assertEquals(2, allAdmins.size());
50 Affiliation affiliation2 = allAdmins.get(0);
51 if (affiliation2.getTarget().getId() == u2.getId()) {
52 affiliation2 = allAdmins.get(1);
54 assertSame(u.getId(), affiliation2.getTarget().getId());
55 assertFalse(affiliation2.isMaster());
57 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
58 assertEquals(1, orgs[0].getAllAdmins().size());
60 executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + orgs[0].getId(), "del=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&email=&do_affiliate=y", 1);
61 assertEquals(0, orgs[0].getAllAdmins().size());
63 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);
65 orgs = Organisation.getOrganisations(0, 30);
66 assertEquals("name1", orgs[0].getName());
70 public void testNonAssurerSeeOnlyOwn() throws IOException {
71 User u2 = User.getById(createVerifiedUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
72 Organisation o1 = new Organisation("name21", "DE", "sder", "Rostov", "email", u);
73 Organisation o2 = new Organisation("name12", "DE", "sder", "Rostov", "email", u);
74 o1.addAdmin(u2, u2, false);
75 String session2 = login(u2.getEmail(), TEST_PASSWORD);
77 URLConnection uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
78 uc.addRequestProperty("Cookie", session2);
79 String content = IOUtils.readURL(uc);
80 assertThat(content, containsString("name21"));
81 assertThat(content, not(containsString("name12")));
82 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), session2);
83 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
84 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), session2);
85 assertEquals(404, ((HttpURLConnection) uc).getResponseCode());
87 uc = new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH).openConnection();
88 uc.addRequestProperty("Cookie", cookie);
89 content = IOUtils.readURL(uc);
90 assertThat(content, containsString("name21"));
91 assertThat(content, containsString("name12"));
92 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o1.getId()).openConnection(), cookie);
93 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());
94 uc = cookie(new URL("https://" + getServerName() + ViewOrgPage.DEFAULT_PATH + "/" + o2.getId()).openConnection(), cookie);
95 assertEquals(200, ((HttpURLConnection) uc).getResponseCode());