add: ensure that for Org Administrator actions certificate login is used
[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.Country;
17 import club.wpia.gigi.dbObjects.Country.CountryCodeType;
18 import club.wpia.gigi.dbObjects.Group;
19 import club.wpia.gigi.dbObjects.Organisation;
20 import club.wpia.gigi.dbObjects.User;
21 import club.wpia.gigi.testUtils.ClientTest;
22 import club.wpia.gigi.testUtils.IOUtils;
23
24 public class TestMain extends ClientTest {
25
26     private User orgAdmin;
27
28     @Test
29     public void testPasswordLogin() throws MalformedURLException, IOException, GigiApiException {
30         URLConnection uc = new URL("https://" + getServerName()).openConnection();
31         uc.addRequestProperty("Cookie", cookie);
32         String content = IOUtils.readURL(uc);
33
34         assertThat(content, not(containsString("via certificate")));
35
36         makeAgent(u.getId());
37         uc = new URL("https://" + getServerName()).openConnection();
38         uc.addRequestProperty("Cookie", cookie);
39         content = IOUtils.readURL(uc);
40         assertThat(content, containsString("For some actions, e.g. add verification, support, you need to be authenticated via certificate."));
41
42     }
43
44     @Test
45     public void testCertLogin() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
46         cookie = cookieWithCertificateLogin(u);
47
48         URLConnection uc = new URL("https://" + getSecureServerName()).openConnection();
49         authenticate((HttpURLConnection) uc);
50         String content = IOUtils.readURL(uc);
51         assertThat(content, not(containsString("via certificate")));
52
53         makeAgent(u.getId());
54         uc = new URL("https://" + getSecureServerName()).openConnection();
55         authenticate((HttpURLConnection) uc);
56         content = IOUtils.readURL(uc);
57         assertThat(content, containsString("You are authenticated via certificate, so you will be able to perform all actions."));
58     }
59
60     @Test
61     public void testPasswordLoginOrgAdmin() throws MalformedURLException, IOException, GigiApiException {
62         URLConnection uc = new URL("https://" + getServerName()).openConnection();
63         addOrgAdmin();
64         cookie = login(orgAdmin.getEmail(), TEST_PASSWORD);
65         loginCertificate = null;
66         uc.addRequestProperty("Cookie", cookie);
67         String content = IOUtils.readURL(uc);
68         assertThat(content, containsString("You need to be logged in via certificate to get access to the organisations."));
69         assertThat(content, containsString("For some actions, e.g. add verification, support, you need to be authenticated via certificate."));
70
71     }
72
73     @Test
74     public void testCertLoginOrgAdmin() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
75         cookie = cookieWithCertificateLogin(u);
76         addOrgAdmin();
77         cookie = cookieWithCertificateLogin(orgAdmin);
78
79         URLConnection uc = new URL("https://" + getSecureServerName()).openConnection();
80         authenticate((HttpURLConnection) uc);
81         String content = IOUtils.readURL(uc);
82
83         assertThat(content, containsString("change to organisation administrator context"));
84         assertThat(content, containsString("You are authenticated via certificate, so you will be able to perform all actions."));
85     }
86
87     private void addOrgAdmin() throws GigiApiException, IOException {
88         makeAgent(u.getId());
89         u.grantGroup(getSupporter(), Group.ORG_AGENT);
90         clearCaches();
91         Organisation o = new Organisation(createUniqueName(), Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS), "pr", "city", "test@example.com", "", "", u);
92         orgAdmin = User.getById(createVerificationUser("testworker", "testname", createUniqueName() + "@testdom.com", TEST_PASSWORD));
93         makeAgent(orgAdmin.getId());
94         o.addAdmin(orgAdmin, u, true);
95     }
96 }