]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/TestMain.java
70a71a5dc50c6f7672ca699f24ea5aefd9095253
[gigi.git] / tests / club / wpia / gigi / pages / TestMain.java
1 package club.wpia.gigi.pages;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
6 import java.io.IOException;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.URLConnection;
11 import java.security.GeneralSecurityException;
12
13 import org.junit.Test;
14
15 import club.wpia.gigi.GigiApiException;
16 import club.wpia.gigi.dbObjects.CATS.CATSType;
17 import club.wpia.gigi.dbObjects.Country;
18 import club.wpia.gigi.dbObjects.Country.CountryCodeType;
19 import club.wpia.gigi.dbObjects.Group;
20 import club.wpia.gigi.dbObjects.Organisation;
21 import club.wpia.gigi.dbObjects.User;
22 import club.wpia.gigi.testUtils.ClientTest;
23 import club.wpia.gigi.testUtils.IOUtils;
24
25 public class TestMain extends ClientTest {
26
27     private User orgAdmin;
28
29     @Test
30     public void testPasswordLogin() throws MalformedURLException, IOException, GigiApiException {
31         URLConnection uc = new URL("https://" + getServerName()).openConnection();
32         uc.addRequestProperty("Cookie", cookie);
33         String content = IOUtils.readURL(uc);
34
35         assertThat(content, not(containsString("via certificate")));
36
37         makeAgent(u.getId());
38         uc = new URL("https://" + getServerName()).openConnection();
39         uc.addRequestProperty("Cookie", cookie);
40         content = IOUtils.readURL(uc);
41         assertThat(content, containsString("For some actions, e.g. add verification, support, you need to be authenticated via certificate."));
42
43     }
44
45     @Test
46     public void testCertLogin() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
47         cookie = cookieWithCertificateLogin(u);
48
49         URLConnection uc = new URL("https://" + getSecureServerName()).openConnection();
50         authenticate((HttpURLConnection) uc);
51         String content = IOUtils.readURL(uc);
52         assertThat(content, not(containsString("via certificate")));
53
54         makeAgent(u.getId());
55         uc = new URL("https://" + getSecureServerName()).openConnection();
56         authenticate((HttpURLConnection) uc);
57         content = IOUtils.readURL(uc);
58         assertThat(content, containsString("You are authenticated via certificate, so you will be able to perform all actions."));
59     }
60
61     @Test
62     public void testPasswordLoginOrgAdmin() throws MalformedURLException, IOException, GigiApiException {
63         URLConnection uc = new URL("https://" + getServerName()).openConnection();
64         addOrgAdmin();
65         cookie = login(orgAdmin.getEmail(), TEST_PASSWORD);
66         loginCertificate = null;
67         uc.addRequestProperty("Cookie", cookie);
68         String content = IOUtils.readURL(uc);
69         assertThat(content, containsString("You need to be logged in via certificate to get access to the organisations."));
70         assertThat(content, containsString("For some actions, e.g. add verification, support, you need to be authenticated via certificate."));
71
72     }
73
74     @Test
75     public void testCertLoginOrgAdmin() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
76         cookie = cookieWithCertificateLogin(u);
77         addOrgAdmin();
78         cookie = cookieWithCertificateLogin(orgAdmin);
79
80         URLConnection uc = new URL("https://" + getSecureServerName()).openConnection();
81         authenticate((HttpURLConnection) uc);
82         String content = IOUtils.readURL(uc);
83
84         assertThat(content, containsString("change to organisation administrator context"));
85         assertThat(content, containsString("You are authenticated via certificate, so you will be able to perform all actions."));
86     }
87
88     private void addOrgAdmin() throws GigiApiException, IOException {
89         makeAgent(u.getId());
90         u.grantGroup(getSupporter(), Group.ORG_AGENT);
91         clearCaches();
92         Organisation o = new Organisation(createUniqueName(), Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), "pr", "city", "test@example.com", "", "", u);
93         orgAdmin = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
94         makeAgent(orgAdmin.getId());
95         o.addAdmin(orgAdmin, u, true);
96     }
97
98     @Test
99     public void testValidChallenges() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
100         cookie = cookieWithCertificateLogin(u);
101
102         // test RA Agent challenge
103         URLConnection uc = new URL("https://" + getSecureServerName()).openConnection();
104         authenticate((HttpURLConnection) uc);
105         String content = IOUtils.readURL(uc);
106         assertThat(content, not(containsString("you need to pass the RA Agent Challenge")));
107
108         add100Points(u.getId());
109         addChallengeInPast(u.getId(), CATSType.AGENT_CHALLENGE);
110         uc = new URL("https://" + getSecureServerName()).openConnection();
111         authenticate((HttpURLConnection) uc);
112         content = IOUtils.readURL(uc);
113         assertThat(content, containsString("you need to pass the RA Agent Challenge"));
114
115         addChallenge(u.getId(), CATSType.AGENT_CHALLENGE);
116         uc = new URL("https://" + getSecureServerName()).openConnection();
117         authenticate((HttpURLConnection) uc);
118         content = IOUtils.readURL(uc);
119         assertThat(content, not(containsString("you need to pass the RA Agent Challenge")));
120
121         // test Support challenge
122         uc = new URL("https://" + getSecureServerName()).openConnection();
123         authenticate((HttpURLConnection) uc);
124         content = IOUtils.readURL(uc);
125         assertThat(content, not(containsString("you need to pass the Support Challenge")));
126
127         grant(u, Group.SUPPORTER);
128         cookie = login(loginPrivateKey, loginCertificate.cert());
129         uc = new URL("https://" + getSecureServerName()).openConnection();
130         authenticate((HttpURLConnection) uc);
131         content = IOUtils.readURL(uc);
132         assertThat(content, containsString("you need to pass the Support Challenge"));
133
134         addChallengeInPast(u.getId(), CATSType.SUPPORT_DP_CHALLENGE_NAME);
135         uc = new URL("https://" + getSecureServerName()).openConnection();
136         authenticate((HttpURLConnection) uc);
137         content = IOUtils.readURL(uc);
138         assertThat(content, containsString("you need to pass the Support Challenge"));
139
140         addChallenge(u.getId(), CATSType.SUPPORT_DP_CHALLENGE_NAME);
141         uc = new URL("https://" + getSecureServerName()).openConnection();
142         authenticate((HttpURLConnection) uc);
143         content = IOUtils.readURL(uc);
144         assertThat(content, not(containsString("you need to pass the Support Challenge")));
145
146         // test Org Agent challenge
147         uc = new URL("https://" + getSecureServerName()).openConnection();
148         authenticate((HttpURLConnection) uc);
149         content = IOUtils.readURL(uc);
150         assertThat(content, not(containsString("you need to pass the Organisation Agent Challenge")));
151
152         grant(u, Group.ORG_AGENT);
153         cookie = login(loginPrivateKey, loginCertificate.cert());
154         uc = new URL("https://" + getSecureServerName()).openConnection();
155         authenticate((HttpURLConnection) uc);
156         content = IOUtils.readURL(uc);
157         assertThat(content, containsString("you need to pass the Organisation Agent Challenge"));
158
159         addChallengeInPast(u.getId(), CATSType.ORG_AGENT_CHALLENGE);
160         uc = new URL("https://" + getSecureServerName()).openConnection();
161         authenticate((HttpURLConnection) uc);
162         content = IOUtils.readURL(uc);
163         assertThat(content, containsString("you need to pass the Organisation Agent Challenge"));
164
165         addChallenge(u.getId(), CATSType.ORG_AGENT_CHALLENGE);
166         uc = new URL("https://" + getSecureServerName()).openConnection();
167         authenticate((HttpURLConnection) uc);
168         content = IOUtils.readURL(uc);
169         assertThat(content, not(containsString("you need to pass the Organisation Agent Challenge")));
170     }
171 }