]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/orga/TestOrgSwitch.java
90362a9a8cac0be7a2e954e6258bc61715507a70
[gigi.git] / tests / club / wpia / gigi / pages / orga / TestOrgSwitch.java
1 package club.wpia.gigi.pages.orga;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.junit.Assert.*;
6
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;
12
13 import org.junit.After;
14 import org.junit.Test;
15
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;
21
22 public class TestOrgSwitch extends OrgTest {
23
24     private User u2;
25
26     private Organisation org1 = createUniqueOrg();
27
28     private Organisation org2 = createUniqueOrg();
29
30     public TestOrgSwitch() throws IOException, GigiApiException {
31
32         assertEquals(403, get(SwitchOrganisation.PATH).getResponseCode());
33
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));
38
39         // login with new user u2
40         cookie = cookieWithCertificateLogin(u2);
41     }
42
43     @After
44     public void purgeDbAfterTest() throws SQLException, IOException {
45         purgeDatabase();
46     }
47
48     @Test
49     public void testSwitchToOrg() throws IOException, GigiApiException {
50
51         assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
52
53         String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
54         assertThat(res, containsString("Logged in as " + u2.getPreferredName() + ", acting as " + org1.getName()));
55
56     }
57
58     @Test
59     public void testSwitchToNonOrg() throws IOException, GigiApiException {
60
61         String res = IOUtils.readURL(post(SwitchOrganisation.PATH, "org:5000=y"));
62         assertThat(res, containsString("Context switch failed"));
63
64     }
65
66     @Test
67     public void testSwitchToPersonal() throws IOException, GigiApiException {
68
69         assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
70
71         String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
72         assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
73
74         assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
75
76         res = IOUtils.readURL(get(SwitchOrganisation.PATH));
77         assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
78
79     }
80
81     @Test
82     public void testSwitchOrgToOrg() throws IOException, GigiApiException {
83
84         assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
85         assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org2.getId() + "=y", 0));
86
87         String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
88         assertThat(res, containsString("Logged in as " + u2.getPreferredName() + ", acting as " + org2.getName()));
89
90     }
91
92     @Test
93     public void testSwitchOrgToSameOrg() throws IOException, GigiApiException {
94
95         assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
96         assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
97
98         String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
99         assertThat(res, containsString("Logged in as " + u2.getPreferredName() + ", acting as " + org1.getName()));
100
101     }
102
103     @Test
104     public void testSwitchOrgToNonOrg() throws IOException, GigiApiException {
105
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"));
109
110     }
111
112     @Test
113     public void testSwitchOrgToPersonal() throws IOException, GigiApiException {
114
115         assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org:" + org1.getId() + "=y", 0));
116         assertNull(executeBasicWebInteraction(cookie, SwitchOrganisation.PATH, "org-leave=personal", 0));
117
118         String res = IOUtils.readURL(get(SwitchOrganisation.PATH));
119         assertThat(res, containsString("Logged in as " + u2.getPreferredName()));
120
121     }
122
123     @Test
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());
129     }
130 }