1 package club.wpia.gigi.pages.orga;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.junit.Assert.*;
7 import java.io.IOException;
8 import java.net.URLEncoder;
9 import java.sql.SQLException;
11 import org.junit.After;
12 import org.junit.Test;
14 import club.wpia.gigi.GigiApiException;
15 import club.wpia.gigi.dbObjects.Organisation;
16 import club.wpia.gigi.dbObjects.User;
17 import club.wpia.gigi.testUtils.IOUtils;
18 import club.wpia.gigi.testUtils.OrgTest;
20 public class TestOrgSwitch extends OrgTest {
24 private Organisation org1 = createUniqueOrg();
26 private Organisation org2 = createUniqueOrg();
28 public TestOrgSwitch() throws IOException, GigiApiException {
30 assertEquals(403, get(SwitchOrganisation.PATH).getResponseCode());
32 String email = createUniqueName() + "@testdom.com";
33 u2 = User.getById(createVerificationUser("testworker", "testname", email, TEST_PASSWORD));
34 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + org1.getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
35 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + org2.getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
37 // login with new user u2
38 cookie = login(email, TEST_PASSWORD);
42 public void purgeDbAfterTest() throws SQLException, IOException {
47 public void testSwitchToOrg() throws IOException, GigiApiException {
49 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
51 String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
52 assertThat(res, containsString("Logged in as " + u2.getPreferredName() + ", acting as " + org1.getName()));
57 public void testSwitchToNonOrg() throws IOException, GigiApiException {
59 String res = IOUtils.readURL(post(SwitchOrganisation.PATH, "org:5000=y"));
60 assertThat(res, containsString("Context switch failed"));
65 public void testSwitchToPersonal() throws IOException, GigiApiException {
67 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
69 String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
70 assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
72 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
74 res = IOUtils.readURL(get(SwitchOrganisation.PATH));
75 assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
80 public void testSwitchOrgToOrg() throws IOException, GigiApiException {
82 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
83 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org2.getId() + "=y", 0));
85 String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
86 assertThat(res, containsString("Logged in as " + u2.getPreferredName() + ", acting as " + org2.getName()));
91 public void testSwitchOrgToSameOrg() throws IOException, GigiApiException {
93 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
94 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
96 String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
97 assertThat(res, containsString("Logged in as " + u2.getPreferredName() + ", acting as " + org1.getName()));
102 public void testSwitchOrgToNonOrg() throws IOException, GigiApiException {
104 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
105 String res = IOUtils.readURL(post(SwitchOrganisation.PATH, "org:5000=y"));
106 assertThat(res, containsString("Context switch failed"));
111 public void testSwitchOrgToPersonal() throws IOException, GigiApiException {
113 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
114 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
116 String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
117 assertThat(res, containsString("Logged in as " + u2.getPreferredName()));