add: revocation state in the result list of support cert search
[gigi.git] / tests / club / wpia / gigi / pages / admin / TestSEAdminPageCertSearch.java
1 package club.wpia.gigi.pages.admin;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.net.URLConnection;
7 import java.net.URLEncoder;
8 import java.security.GeneralSecurityException;
9 import java.security.KeyPair;
10 import java.text.SimpleDateFormat;
11
12 import org.hamcrest.CoreMatchers;
13 import org.junit.Test;
14
15 import club.wpia.gigi.GigiApiException;
16 import club.wpia.gigi.dbObjects.Certificate;
17 import club.wpia.gigi.dbObjects.Certificate.CSRType;
18 import club.wpia.gigi.dbObjects.Certificate.CertificateStatus;
19 import club.wpia.gigi.dbObjects.Certificate.RevocationType;
20 import club.wpia.gigi.dbObjects.Certificate.SANType;
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.output.template.Template;
25 import club.wpia.gigi.pages.account.certs.Certificates;
26 import club.wpia.gigi.pages.admin.support.FindCertPage;
27 import club.wpia.gigi.pages.admin.support.SupportEnterTicketPage;
28 import club.wpia.gigi.testUtils.ClientTest;
29 import club.wpia.gigi.testUtils.IOUtils;
30 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
31 import club.wpia.gigi.util.ServerConstants;
32 import club.wpia.gigi.util.ServerConstants.Host;
33
34 public class TestSEAdminPageCertSearch extends ClientTest {
35
36     private Certificate c;
37
38     private String certMail;
39
40     private int id;
41
42     public TestSEAdminPageCertSearch() throws IOException, GigiApiException, GeneralSecurityException, InterruptedException {
43         grant(u, Group.SUPPORTER);
44         cookie = login(email, TEST_PASSWORD);
45         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
46
47         certMail = uniq + "_certowner@example.com";
48         id = createVerifiedUser("fn", "ln", certMail, TEST_PASSWORD);
49         c = createCertificate();
50     }
51
52     @Test
53     public void testSerialSearch() throws IOException {
54         URLConnection uc = post(cookie, FindCertPage.PATH, "certType=serial&process=Next&cert=" + c.getSerial(), 0);
55         assertEquals("https://" + ServerConstants.getHostNamePortSecure(Host.WWW) + Certificates.SUPPORT_PATH + "/" + c.getSerial(), uc.getHeaderField("Location"));
56     }
57
58     @Test
59     public void testEmailSearch() throws IOException {
60         URLConnection uc = post(cookie, FindCertPage.PATH, "certType=email&process=Next&cert=" + URLEncoder.encode(certMail, "UTF-8"), 0);
61         assertEquals("https://" + ServerConstants.getHostNamePortSecure(Host.WWW) + Certificates.SUPPORT_PATH + "/" + c.getSerial(), uc.getHeaderField("Location"));
62     }
63
64     @Test
65     public void testDetails() throws IOException {
66         String s = IOUtils.readURL(get(Certificates.SUPPORT_PATH + "/" + c.getSerial()));
67         assertThat(s, CoreMatchers.containsString("SHA512"));
68         assertThat(s, CoreMatchers.containsString(certMail));
69         assertThat(s, CoreMatchers.containsString(c.getSerial()));
70         assertThat(s, CoreMatchers.containsString("ISSUED"));
71     }
72
73     @Test
74     public void testRevoke() throws IOException, GeneralSecurityException, GigiApiException, InterruptedException {
75         URLConnection conn = post(Certificates.SUPPORT_PATH + "/" + c.getSerial(), "action=revoke");
76         assertEquals("https://" + ServerConstants.getHostNamePortSecure(Host.WWW) + Certificates.SUPPORT_PATH + "/" + c.getSerial(), conn.getHeaderField("Location"));
77         for (int i = 0; i < 2; i++) {
78             TestMail tm = getMailReceiver().receive(i == 0 ? ServerConstants.getSupportMailAddress() : certMail);
79             assertThat(tm.getMessage(), CoreMatchers.containsString(certMail));
80             assertThat(tm.getMessage(), CoreMatchers.containsString(c.getSerial()));
81         }
82         assertEquals(CertificateStatus.REVOKED, c.getStatus());
83
84     }
85
86     @Test
87     public void testShowRevocation() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
88         Certificate c1 = createCertificate();
89         await(c1.revoke(RevocationType.SUPPORT));
90         URLConnection uc = post(cookie, FindCertPage.PATH, "certType=email&process=Next&cert=" + URLEncoder.encode(certMail, "UTF-8"), 0);
91         SimpleDateFormat sdf = new SimpleDateFormat(Template.UTC_TIMESTAMP_FORMAT);
92         String revokeDate = sdf.format(c1.getRevocationDate());
93         String result = IOUtils.readURL(uc);
94         assertThat(result, CoreMatchers.containsString(revokeDate));
95         assertThat(result, CoreMatchers.containsString("N/A"));
96     }
97
98     private Certificate createCertificate() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
99         User u1 = User.getById(id);
100         KeyPair kp = generateKeypair();
101         String key = generatePEMCSR(kp, "CN=" + certMail);
102         Certificate c1 = new Certificate(u1, u1, Certificate.buildDN("CN", certMail), Digest.SHA512, key, CSRType.CSR, getClientProfile(), new Certificate.SubjectAlternateName(SANType.EMAIL, certMail));
103         await(c1.issue(null, "2y", u));
104         return c1;
105     }
106 }