]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/api/TestFindAgent.java
upd: rename package name and all references to it
[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.api.FindAgent;
19 import club.wpia.gigi.dbObjects.Certificate;
20 import club.wpia.gigi.dbObjects.CertificateProfile;
21 import club.wpia.gigi.dbObjects.Digest;
22 import club.wpia.gigi.dbObjects.Group;
23 import club.wpia.gigi.dbObjects.User;
24 import club.wpia.gigi.dbObjects.Certificate.CSRType;
25 import club.wpia.gigi.dbObjects.Certificate.SANType;
26 import club.wpia.gigi.pages.account.FindAgentAccess;
27 import club.wpia.gigi.testUtils.IOUtils;
28 import club.wpia.gigi.testUtils.RestrictedApiTest;
29 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
30
31 public class TestFindAgent extends RestrictedApiTest {
32
33     @Test
34     public void testResolve() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
35         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"));
36         await(target2.issue(null, "2y", u));
37
38         HttpURLConnection v = doApi(FindAgent.PATH_RESOLVE, "serial=" + target2.getSerial().toLowerCase());
39         assertEquals(501, v.getResponseCode());
40         assertThat(IOUtils.readURL(new InputStreamReader(v.getErrorStream(), "UTF-8")), containsString(FindAgentAccess.PATH));
41
42         grant(u, Group.LOCATE_AGENT);
43         v = doApi(FindAgent.PATH_RESOLVE, "serial=" + target2.getSerial().toLowerCase());
44         assertEquals(u.getId(), Integer.parseInt(IOUtils.readURL(v)));
45     }
46
47     @Test
48     public void testMailA() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
49         testMail(true);
50     }
51
52     @Test
53     public void testMailB() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
54         testMail(false);
55     }
56
57     public void testMail(boolean userUFirst) throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
58         int u2 = createVerifiedUser("f", "l", createUniqueName() + "@email.com", TEST_PASSWORD);
59         User us2 = User.getById(u2);
60
61         // email sending fails
62         HttpURLConnection v = doApi(FindAgent.PATH_MAIL, "from=" + id + "&to=" + u2 + "&subject=the-subject&body=body");
63         assertEquals(v.getResponseMessage(), 501, v.getResponseCode());
64         assertThat(v.getResponseMessage(), containsString("needs to enable access"));
65
66         // even if sender enables service
67         grant((userUFirst ? u : us2), Group.LOCATE_AGENT);
68         v = doApi(FindAgent.PATH_MAIL, "from=" + id + "&to=" + u2 + "&subject=the-subject&body=body");
69         assertEquals(v.getResponseMessage(), 501, v.getResponseCode());
70         assertThat(v.getResponseMessage(), containsString("needs to enable access"));
71
72         // receiver needs to enable access as well
73         grant((userUFirst ? us2 : u), Group.LOCATE_AGENT);
74         v = doApi(FindAgent.PATH_MAIL, "from=" + id + "&to=" + u2 + "&subject=the-subject&body=body");
75         assertEquals(v.getResponseMessage(), 200, v.getResponseCode());
76         TestMail mail = getMailReceiver().receive();
77         assertEquals("body", mail.getMessage());
78         assertThat(mail.getSubject(), containsString("the-subject"));
79         assertEquals(us2.getEmail(), mail.getTo());
80     }
81
82     @Test
83     public void testLookupName() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
84         int u2 = createVerifiedUser("f", "l", createUniqueName() + "@email.com", TEST_PASSWORD);
85
86         String res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
87         res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
88         assertEquals(new JSONArray().toString(), new JSONArray(new JSONTokener(res)).toString());
89         grant(u, Group.LOCATE_AGENT);
90         grant(User.getById(u2), Group.LOCATE_AGENT);
91         res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
92         JSONTokener jt = new JSONTokener(res);
93         JSONObject j1 = new JSONObject();
94         j1.put("id", id);
95         j1.put("canAssure", true);
96         j1.put("name", u.getPreferredName().toAbbreviatedString());
97         JSONObject j2 = new JSONObject();
98         j2.put("id", u2);
99         j2.put("canAssure", false);
100         j2.put("name", User.getById(u2).getPreferredName().toAbbreviatedString());
101         JSONArray ja = new JSONArray(Arrays.asList(j1, j2));
102         assertEquals(ja.toString(), new JSONArray(jt).toString());
103     }
104 }