]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/TestMain.java
dfc6579e07139d780b5c35fc167f488f7e1cccc0
[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
101         // test RA Agent challenge
102         cookie = cookieWithCertificateLogin(u);
103         testChallengeText("you need to pass the RA Agent Challenge", false);
104
105         add100Points(u.getId());
106         addChallengeInPast(u.getId(), CATSType.AGENT_CHALLENGE);
107         testChallengeText("you need to pass the RA Agent Challenge", true);
108
109         addChallenge(u.getId(), CATSType.AGENT_CHALLENGE);
110         testChallengeText("you need to pass the RA Agent Challenge", false);
111
112         // test Support challenge
113         testChallengeText("you need to pass the Support Challenge", false);
114
115         grant(u, Group.SUPPORTER);
116         cookie = login(loginPrivateKey, loginCertificate.cert());
117         testChallengeText("you need to pass the Support Challenge", true);
118
119         addChallengeInPast(u.getId(), CATSType.SUPPORT_DP_CHALLENGE_NAME);
120         testChallengeText("you need to pass the Support Challenge", true);
121
122         addChallenge(u.getId(), CATSType.SUPPORT_DP_CHALLENGE_NAME);
123         testChallengeText("you need to pass the Support Challenge", false);
124
125         // test Org Agent challenge
126         testChallengeText("you need to pass the Organisation Agent Challenge", false);
127
128         grant(u, Group.ORG_AGENT);
129         cookie = login(loginPrivateKey, loginCertificate.cert());
130         testChallengeText("you need to pass the Organisation Agent Challenge", true);
131
132         addChallengeInPast(u.getId(), CATSType.ORG_AGENT_CHALLENGE);
133         testChallengeText("you need to pass the Organisation Agent Challenge", true);
134
135         addChallenge(u.getId(), CATSType.ORG_AGENT_CHALLENGE);
136         testChallengeText("you need to pass the Organisation Agent Challenge", false);
137
138         // test TTP Agent challenge
139         testChallengeText("you need to pass the TTP RA Agent Challenge", false);
140
141         grant(u, Group.TTP_AGENT);
142         cookie = login(loginPrivateKey, loginCertificate.cert());
143         testChallengeText("you need to pass the TTP RA Agent Challenge", true);
144
145         addChallengeInPast(u.getId(), CATSType.TTP_AGENT_CHALLENGE);
146         testChallengeText("you need to pass the TTP RA Agent Challenge", true);
147
148         addChallenge(u.getId(), CATSType.TTP_AGENT_CHALLENGE);
149         testChallengeText("you need to pass the TTP RA Agent Challenge", false);
150     }
151
152     private void testChallengeText(String contentText, boolean contains) throws IOException, MalformedURLException {
153         URLConnection uc = new URL("https://" + getSecureServerName()).openConnection();
154         authenticate((HttpURLConnection) uc);
155         String content = IOUtils.readURL(uc);
156         if (contains) {
157             assertThat(content, containsString(contentText));
158         } else {
159             assertThat(content, not(containsString(contentText)));
160         }
161     }
162 }