]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/TestMain.java
d0d03ec5b441f2321758ac2afe744ef9537b04a5
[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         insertRAContract(u.getId());
101         // test RA Agent challenge
102         cookie = cookieWithCertificateLogin(u);
103
104         testChallengeText("you need to pass the RA Agent Challenge", false);
105
106         add100Points(u.getId());
107         addChallengeInPast(u.getId(), CATSType.AGENT_CHALLENGE);
108         testChallengeText("you need to pass the RA Agent Challenge", true);
109
110         addChallenge(u.getId(), CATSType.AGENT_CHALLENGE);
111         testChallengeText("you need to pass the RA Agent Challenge", false);
112
113         // test Support challenge
114         testChallengeText("you need to pass the Support Challenge", false);
115
116         grant(u, Group.SUPPORTER);
117         cookie = login(loginPrivateKey, loginCertificate.cert());
118         testChallengeText("you need to pass the Support Challenge", true);
119
120         addChallengeInPast(u.getId(), CATSType.SUPPORT_DP_CHALLENGE_NAME);
121         testChallengeText("you need to pass the Support Challenge", true);
122
123         addChallenge(u.getId(), CATSType.SUPPORT_DP_CHALLENGE_NAME);
124         testChallengeText("you need to pass the Support Challenge", false);
125
126         // test Org Agent challenge
127         testChallengeText("you need to pass the Organisation Agent Challenge", false);
128
129         grant(u, Group.ORG_AGENT);
130         cookie = login(loginPrivateKey, loginCertificate.cert());
131         testChallengeText("you need to pass the Organisation Agent Challenge", true);
132
133         addChallengeInPast(u.getId(), CATSType.ORG_AGENT_CHALLENGE);
134         testChallengeText("you need to pass the Organisation Agent Challenge", true);
135
136         addChallenge(u.getId(), CATSType.ORG_AGENT_CHALLENGE);
137         testChallengeText("you need to pass the Organisation Agent Challenge", false);
138
139         // test TTP Agent challenge
140         testChallengeText("you need to pass the TTP RA Agent Challenge", false);
141
142         grant(u, Group.TTP_AGENT);
143         cookie = login(loginPrivateKey, loginCertificate.cert());
144         testChallengeText("you need to pass the TTP RA Agent Challenge", true);
145
146         addChallengeInPast(u.getId(), CATSType.TTP_AGENT_CHALLENGE);
147         testChallengeText("you need to pass the TTP RA Agent Challenge", true);
148
149         addChallenge(u.getId(), CATSType.TTP_AGENT_CHALLENGE);
150         testChallengeText("you need to pass the TTP RA Agent Challenge", false);
151
152         // test Org Admin Challenge
153         Organisation o = new Organisation(createUniqueName(), Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), "pr", "city", "test@example.com", "", "", u);
154         User admin = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
155
156         loginCertificate = null;
157         cookie = cookieWithCertificateLogin(admin);
158         testChallengeText("you need to pass the Organisation Administrator Challenge", false);
159
160         o.addAdmin(admin, u, true);
161         testChallengeText("you need to pass the Organisation Administrator Challenge", true);
162
163         addChallengeInPast(admin.getId(), CATSType.ORG_ADMIN_DP_CHALLENGE_NAME);
164         testChallengeText("you need to pass the Organisation Administrator Challenge", true);
165
166         addChallenge(admin.getId(), CATSType.ORG_ADMIN_DP_CHALLENGE_NAME);
167         testChallengeText("you need to pass the Organisation Administrator Challenge", false);
168     }
169
170     private void testChallengeText(String contentText, boolean contains) throws IOException, MalformedURLException, GigiApiException {
171         URLConnection uc = new URL("https://" + getSecureServerName()).openConnection();
172         authenticate((HttpURLConnection) uc);
173         String content = IOUtils.readURL(uc);
174         if (contains) {
175             assertThat(content, containsString(contentText));
176         } else {
177             assertThat(content, not(containsString(contentText)));
178         }
179     }
180 }