1 package club.wpia.gigi.api;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
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;
12 import org.json.JSONArray;
13 import org.json.JSONObject;
14 import org.json.JSONTokener;
15 import org.junit.Test;
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;
31 public class TestFindAgent extends RestrictedApiTest {
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));
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));
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)));
48 public void testMailA() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
53 public void testMailB() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
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);
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"));
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"));
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());
83 public void testLookupName() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
84 int u2 = createVerifiedUser("f", "l", createUniqueName() + "@email.com", TEST_PASSWORD);
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();
95 j1.put("canVerify", true);
96 j1.put("name", u.getPreferredName().toAbbreviatedString());
97 JSONObject j2 = new JSONObject();
99 j2.put("canVerify", 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());