1 package club.wpia.gigi.pages;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
6 import java.io.IOException;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
10 import java.net.URLConnection;
11 import java.security.GeneralSecurityException;
13 import org.junit.Test;
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;
25 public class TestMain extends ClientTest {
27 private User orgAdmin;
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);
35 assertThat(content, not(containsString("via certificate")));
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."));
46 public void testCertLogin() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
47 cookie = cookieWithCertificateLogin(u);
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")));
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."));
62 public void testPasswordLoginOrgAdmin() throws MalformedURLException, IOException, GigiApiException {
63 URLConnection uc = new URL("https://" + getServerName()).openConnection();
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."));
75 public void testCertLoginOrgAdmin() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
76 cookie = cookieWithCertificateLogin(u);
78 cookie = cookieWithCertificateLogin(orgAdmin);
80 URLConnection uc = new URL("https://" + getSecureServerName()).openConnection();
81 authenticate((HttpURLConnection) uc);
82 String content = IOUtils.readURL(uc);
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."));
88 private void addOrgAdmin() throws GigiApiException, IOException {
90 u.grantGroup(getSupporter(), Group.ORG_AGENT);
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);
99 public void testValidChallenges() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
100 cookie = cookieWithCertificateLogin(u);
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")));
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"));
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")));
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")));
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"));
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"));
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")));
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")));
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"));
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"));
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")));