]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/wot/TestTTPAdmin.java
b1cbf9aba6625e1349eaf435bc2ed51fa58648fe
[gigi.git] / tests / org / cacert / gigi / pages / wot / TestTTPAdmin.java
1 package org.cacert.gigi.pages.wot;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9
10 import org.cacert.gigi.dbObjects.Group;
11 import org.cacert.gigi.dbObjects.User;
12 import org.cacert.gigi.pages.admin.TTPAdminPage;
13 import org.cacert.gigi.testUtils.ManagedTest;
14 import org.junit.Test;
15
16 public class TestTTPAdmin extends ManagedTest {
17
18     User us;
19
20     String cookie;
21
22     User us2;
23
24     public TestTTPAdmin() throws IOException {
25         String email = uniq + "@example.com";
26         us = User.getById(createVerifiedUser("fn", "ln", email, TEST_PASSWORD));
27         cookie = login(email, TEST_PASSWORD);
28         us2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@example.com", TEST_PASSWORD));
29     }
30
31     @Test
32     public void testHasRight() throws IOException {
33         testTTPAdmin(true);
34     }
35
36     @Test
37     public void testHasNoRight() throws IOException {
38         testTTPAdmin(false);
39     }
40
41     public void testTTPAdmin(boolean hasRight) throws IOException {
42         if (hasRight) {
43             grant(us.getEmail(), Group.getByString("ttp-assurer"));
44         }
45         grant(us.getEmail(), TTPAdminPage.TTP_APPLICANT);
46         cookie = login(us.getEmail(), TEST_PASSWORD);
47
48         assertEquals( !hasRight ? 403 : 200, fetchStatusCode("https://" + getServerName() + TTPAdminPage.PATH));
49         assertEquals( !hasRight ? 403 : 200, fetchStatusCode("https://" + getServerName() + TTPAdminPage.PATH + "/"));
50         assertEquals( !hasRight ? 403 : 200, fetchStatusCode("https://" + getServerName() + TTPAdminPage.PATH + "/" + us.getId()));
51         assertEquals( !hasRight ? 403 : 404, fetchStatusCode("https://" + getServerName() + TTPAdminPage.PATH + "/" + us2.getId()));
52         assertEquals( !hasRight ? 403 : 404, fetchStatusCode("https://" + getServerName() + TTPAdminPage.PATH + "/" + 100));
53     }
54
55     private int fetchStatusCode(String path) throws MalformedURLException, IOException {
56         URL u = new URL(path);
57         return ((HttpURLConnection) cookie(u.openConnection(), cookie)).getResponseCode();
58     }
59 }