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 insertRAContract(u.getId());
101 // test RA Agent challenge
102 cookie = cookieWithCertificateLogin(u);
104 testChallengeText("you need to pass the RA Agent Challenge", false);
106 add100Points(u.getId());
107 addChallengeInPast(u.getId(), CATSType.AGENT_CHALLENGE);
108 testChallengeText("you need to pass the RA Agent Challenge", true);
110 addChallenge(u.getId(), CATSType.AGENT_CHALLENGE);
111 testChallengeText("you need to pass the RA Agent Challenge", false);
113 // test Support challenge
114 testChallengeText("you need to pass the Support Challenge", false);
116 grant(u, Group.SUPPORTER);
117 cookie = login(loginPrivateKey, loginCertificate.cert());
118 testChallengeText("you need to pass the Support Challenge", true);
120 addChallengeInPast(u.getId(), CATSType.SUPPORT_DP_CHALLENGE_NAME);
121 testChallengeText("you need to pass the Support Challenge", true);
123 addChallenge(u.getId(), CATSType.SUPPORT_DP_CHALLENGE_NAME);
124 testChallengeText("you need to pass the Support Challenge", false);
126 // test Org Agent challenge
127 testChallengeText("you need to pass the Organisation Agent Challenge", false);
129 grant(u, Group.ORG_AGENT);
130 cookie = login(loginPrivateKey, loginCertificate.cert());
131 testChallengeText("you need to pass the Organisation Agent Challenge", true);
133 addChallengeInPast(u.getId(), CATSType.ORG_AGENT_CHALLENGE);
134 testChallengeText("you need to pass the Organisation Agent Challenge", true);
136 addChallenge(u.getId(), CATSType.ORG_AGENT_CHALLENGE);
137 testChallengeText("you need to pass the Organisation Agent Challenge", false);
139 // test TTP Agent challenge
140 testChallengeText("you need to pass the TTP RA Agent Challenge", false);
142 grant(u, Group.TTP_AGENT);
143 cookie = login(loginPrivateKey, loginCertificate.cert());
144 testChallengeText("you need to pass the TTP RA Agent Challenge", true);
146 addChallengeInPast(u.getId(), CATSType.TTP_AGENT_CHALLENGE);
147 testChallengeText("you need to pass the TTP RA Agent Challenge", true);
149 addChallenge(u.getId(), CATSType.TTP_AGENT_CHALLENGE);
150 testChallengeText("you need to pass the TTP RA Agent Challenge", false);
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));
156 loginCertificate = null;
157 cookie = cookieWithCertificateLogin(admin);
158 testChallengeText("you need to pass the Organisation Administrator Challenge", false);
160 o.addAdmin(admin, u, true);
161 testChallengeText("you need to pass the Organisation Administrator Challenge", true);
163 addChallengeInPast(admin.getId(), CATSType.ORG_ADMIN_DP_CHALLENGE_NAME);
164 testChallengeText("you need to pass the Organisation Administrator Challenge", true);
166 addChallenge(admin.getId(), CATSType.ORG_ADMIN_DP_CHALLENGE_NAME);
167 testChallengeText("you need to pass the Organisation Administrator Challenge", false);
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);
175 assertThat(content, containsString(contentText));
177 assertThat(content, not(containsString(contentText)));