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.HttpURLConnection;
9 import java.net.URLConnection;
10 import java.net.URLEncoder;
11 import java.sql.SQLException;
13 import org.junit.After;
14 import org.junit.Test;
16 import club.wpia.gigi.GigiApiException;
17 import club.wpia.gigi.dbObjects.Organisation;
18 import club.wpia.gigi.dbObjects.User;
19 import club.wpia.gigi.testUtils.IOUtils;
20 import club.wpia.gigi.testUtils.OrgTest;
22 public class TestOrgSwitch extends OrgTest {
26 private Organisation org1 = createUniqueOrg();
28 private Organisation org2 = createUniqueOrg();
30 public TestOrgSwitch() throws IOException, GigiApiException {
32 assertEquals(403, get(SwitchOrganisation.PATH).getResponseCode());
34 String email = createUniqueName() + "@testdom.com";
35 u2 = User.getById(createVerificationUser("testworker", "testname", email, TEST_PASSWORD));
36 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + org1.getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
37 assertNull(executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + org2.getId(), "email=" + URLEncoder.encode(u2.getEmail(), "UTF-8") + "&do_affiliate=y&master=y", 1));
39 // login with new user u2
40 cookie = cookieWithCertificateLogin(u2);
44 public void purgeDbAfterTest() throws SQLException, IOException {
49 public void testSwitchToOrg() throws IOException, GigiApiException {
51 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
53 String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
54 assertThat(res, containsString("Logged in as " + u2.getPreferredName() + ", acting as " + org1.getName()));
59 public void testSwitchToNonOrg() throws IOException, GigiApiException {
61 String res = IOUtils.readURL(post(SwitchOrganisation.PATH, "org:5000=y"));
62 assertThat(res, containsString("Context switch failed"));
67 public void testSwitchToPersonal() throws IOException, GigiApiException {
69 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
71 String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
72 assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
74 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
76 res = IOUtils.readURL(get(SwitchOrganisation.PATH));
77 assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
82 public void testSwitchOrgToOrg() throws IOException, GigiApiException {
84 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
85 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org2.getId() + "=y", 0));
87 String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
88 assertThat(res, containsString("Logged in as " + u2.getPreferredName() + ", acting as " + org2.getName()));
93 public void testSwitchOrgToSameOrg() throws IOException, GigiApiException {
95 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
96 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
98 String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
99 assertThat(res, containsString("Logged in as " + u2.getPreferredName() + ", acting as " + org1.getName()));
104 public void testSwitchOrgToNonOrg() throws IOException, GigiApiException {
106 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
107 String res = IOUtils.readURL(post(SwitchOrganisation.PATH, "org:5000=y"));
108 assertThat(res, containsString("Context switch failed"));
113 public void testSwitchOrgToPersonal() throws IOException, GigiApiException {
115 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
116 assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
118 String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
119 assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
124 public void testSwitchOrgPasswordLogin() throws IOException, GigiApiException {
125 cookie = login(email, TEST_PASSWORD);
126 loginCertificate = null;
127 URLConnection uc = get(cookie, SwitchOrganisation.PATH);
128 assertEquals(403, ((HttpURLConnection) uc).getResponseCode());