]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/api/TestFindAgent.java
Merge "add: show more certificates on the "roots" page"
[gigi.git] / tests / club / wpia / gigi / api / TestFindAgent.java
1 package club.wpia.gigi.api;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.net.HttpURLConnection;
9 import java.security.GeneralSecurityException;
10 import java.util.Arrays;
11
12 import org.json.JSONArray;
13 import org.json.JSONObject;
14 import org.json.JSONTokener;
15 import org.junit.Test;
16
17 import club.wpia.gigi.GigiApiException;
18 import club.wpia.gigi.dbObjects.Certificate;
19 import club.wpia.gigi.dbObjects.Certificate.CSRType;
20 import club.wpia.gigi.dbObjects.Certificate.SANType;
21 import club.wpia.gigi.dbObjects.CertificateProfile;
22 import club.wpia.gigi.dbObjects.Digest;
23 import club.wpia.gigi.dbObjects.Group;
24 import club.wpia.gigi.dbObjects.User;
25 import club.wpia.gigi.pages.account.FindAgentAccess;
26 import club.wpia.gigi.testUtils.IOUtils;
27 import club.wpia.gigi.testUtils.RestrictedApiTest;
28 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
29
30 public class TestFindAgent extends RestrictedApiTest {
31
32     @Test
33     public void testResolve() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
34         Certificate target2 = new Certificate(u, u, Certificate.buildDN("EMAIL", u.getEmail()), Digest.SHA256, generatePEMCSR(generateKeypair(), "EMAIL=" + u.getEmail()), CSRType.CSR, CertificateProfile.getByName("client"), new Certificate.SubjectAlternateName(SANType.EMAIL, "cats@example.com"));
35         await(target2.issue(null, "2y", u));
36
37         HttpURLConnection v = doApi(FindAgent.PATH_RESOLVE, "serial=" + target2.getSerial().toLowerCase());
38         assertEquals(501, v.getResponseCode());
39         assertThat(IOUtils.readURL(new InputStreamReader(v.getErrorStream(), "UTF-8")), containsString(FindAgentAccess.PATH));
40
41         grant(u, Group.LOCATE_AGENT);
42         v = doApi(FindAgent.PATH_RESOLVE, "serial=" + target2.getSerial().toLowerCase());
43         assertEquals(u.getId(), Integer.parseInt(IOUtils.readURL(v)));
44     }
45
46     @Test
47     public void testMailA() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
48         testMail(true);
49     }
50
51     @Test
52     public void testMailB() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
53         testMail(false);
54     }
55
56     public void testMail(boolean userUFirst) throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
57         int u2 = createVerifiedUser("f", "l", createUniqueName() + "@email.com", TEST_PASSWORD);
58         User us2 = User.getById(u2);
59
60         // email sending fails
61         HttpURLConnection v = doApi(FindAgent.PATH_MAIL, "from=" + id + "&to=" + u2 + "&subject=the-subject&body=body");
62         assertEquals(v.getResponseMessage(), 501, v.getResponseCode());
63         assertThat(v.getResponseMessage(), containsString("needs to enable access"));
64
65         // even if sender enables service
66         grant((userUFirst ? u : us2), Group.LOCATE_AGENT);
67         v = doApi(FindAgent.PATH_MAIL, "from=" + id + "&to=" + u2 + "&subject=the-subject&body=body");
68         assertEquals(v.getResponseMessage(), 501, v.getResponseCode());
69         assertThat(v.getResponseMessage(), containsString("needs to enable access"));
70
71         // receiver needs to enable access as well
72         grant((userUFirst ? us2 : u), Group.LOCATE_AGENT);
73         v = doApi(FindAgent.PATH_MAIL, "from=" + id + "&to=" + u2 + "&subject=the-subject&body=body");
74         assertEquals(v.getResponseMessage(), 200, v.getResponseCode());
75         TestMail mail = getMailReceiver().receive(us2.getEmail());
76         assertEquals("body", mail.getMessage());
77         assertThat(mail.getSubject(), containsString("the-subject"));
78     }
79
80     @Test
81     public void testLookupName() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
82         int u2 = createVerifiedUser("f", "l", createUniqueName() + "@email.com", TEST_PASSWORD);
83
84         String res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
85         res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
86         assertEquals(new JSONArray().toString(), new JSONArray(new JSONTokener(res)).toString());
87         grant(u, Group.LOCATE_AGENT);
88         grant(User.getById(u2), Group.LOCATE_AGENT);
89         res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
90         JSONTokener jt = new JSONTokener(res);
91         JSONObject j1 = new JSONObject();
92         j1.put("id", id);
93         j1.put("canVerify", true);
94         j1.put("name", u.getPreferredName().toAbbreviatedString());
95         JSONObject j2 = new JSONObject();
96         j2.put("id", u2);
97         j2.put("canVerify", false);
98         j2.put("name", User.getById(u2).getPreferredName().toAbbreviatedString());
99         JSONArray ja = new JSONArray(Arrays.asList(j1, j2));
100         assertEquals(ja.toString(), new JSONArray(jt).toString());
101     }
102 }