]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/api/TestFindAgent.java
add: Allow multiple names, name-schemes, multi-name-assurance, etc.
[gigi.git] / tests / org / cacert / gigi / api / TestFindAgent.java
1 package org.cacert.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
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.dbObjects.Certificate;
13 import org.cacert.gigi.dbObjects.Certificate.CSRType;
14 import org.cacert.gigi.dbObjects.Certificate.SANType;
15 import org.cacert.gigi.dbObjects.CertificateProfile;
16 import org.cacert.gigi.dbObjects.Digest;
17 import org.cacert.gigi.dbObjects.Group;
18 import org.cacert.gigi.dbObjects.User;
19 import org.cacert.gigi.pages.account.FindAgentAccess;
20 import org.cacert.gigi.testUtils.IOUtils;
21 import org.cacert.gigi.testUtils.RestrictedApiTest;
22 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
23 import org.junit.Test;
24
25 public class TestFindAgent extends RestrictedApiTest {
26
27     @Test
28     public void testResolve() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
29         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@cacert.org"));
30         await(target2.issue(null, "2y", u));
31
32         HttpURLConnection v = doApi(FindAgent.PATH_RESOLVE, "serial=" + target2.getSerial().toLowerCase());
33         assertEquals(501, v.getResponseCode());
34         assertThat(IOUtils.readURL(new InputStreamReader(v.getErrorStream(), "UTF-8")), containsString(FindAgentAccess.PATH));
35
36         grant(u.getEmail(), Group.LOCATE_AGENT);
37         v = doApi(FindAgent.PATH_RESOLVE, "serial=" + target2.getSerial().toLowerCase());
38         assertEquals(u.getId(), Integer.parseInt(IOUtils.readURL(v)));
39     }
40
41     @Test
42     public void testMailA() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
43         testMail(true);
44     }
45
46     @Test
47     public void testMailB() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
48         testMail(false);
49     }
50
51     public void testMail(boolean userUFirst) throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
52         int u2 = createVerifiedUser("f", "l", createUniqueName() + "@email.com", TEST_PASSWORD);
53         User us2 = User.getById(u2);
54
55         // email sending fails
56         HttpURLConnection v = doApi(FindAgent.PATH_MAIL, "from=" + id + "&to=" + u2 + "&subject=the-subject&body=body");
57         assertEquals(v.getResponseMessage(), 501, v.getResponseCode());
58         assertThat(v.getResponseMessage(), containsString("needs to enable access"));
59
60         // even if sender enables service
61         grant((userUFirst ? u : us2).getEmail(), Group.LOCATE_AGENT);
62         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         // receiver needs to enable access as well
67         grant((userUFirst ? us2 : u).getEmail(), Group.LOCATE_AGENT);
68         v = doApi(FindAgent.PATH_MAIL, "from=" + id + "&to=" + u2 + "&subject=the-subject&body=body");
69         assertEquals(v.getResponseMessage(), 200, v.getResponseCode());
70         TestMail mail = getMailReceiver().receive();
71         assertEquals("body", mail.getMessage());
72         assertThat(mail.getSubject(), containsString("the-subject"));
73         assertEquals(us2.getEmail(), mail.getTo());
74     }
75
76     @Test
77     public void testLookupName() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
78         int u2 = createVerifiedUser("f", "l", createUniqueName() + "@email.com", TEST_PASSWORD);
79
80         String res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
81         assertEquals(res, "");
82         grant(email, Group.LOCATE_AGENT);
83         grant(User.getById(u2).getEmail(), Group.LOCATE_AGENT);
84         res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
85         assertEquals(id + ",true," + u.getPreferredName().toAbbreviatedString() + "\n" + u2 + ",false," + User.getById(u2).getPreferredName().toAbbreviatedString() + "\n", res);
86     }
87 }