1 package club.wpia.gigi.pages.account;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
6 import java.io.IOException;
7 import java.security.GeneralSecurityException;
8 import java.security.KeyPair;
9 import java.util.Locale;
11 import org.junit.Test;
13 import club.wpia.gigi.GigiApiException;
14 import club.wpia.gigi.database.GigiPreparedStatement;
15 import club.wpia.gigi.dbObjects.EmailAddress;
16 import club.wpia.gigi.dbObjects.Group;
17 import club.wpia.gigi.pages.account.certs.CertificateRequest;
18 import club.wpia.gigi.testUtils.ClientTest;
19 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
20 import club.wpia.gigi.util.AuthorizationContext;
21 import club.wpia.gigi.util.TimeConditions;
23 public class TestCertificateRequest extends ClientTest {
25 KeyPair kp = generateKeypair();
27 AuthorizationContext ac;
29 public TestCertificateRequest() throws GeneralSecurityException, IOException, GigiApiException {
30 ac = new AuthorizationContext(u, u);
35 public void testIssuingOtherName() throws Exception {
37 new CertificateRequest(ac, generatePEMCSR(kp, "CN=hansi")).draft();
39 } catch (GigiApiException e) {
40 assertThat(e.getMessage(), containsString("name you entered was invalid"));
45 public void testIssuingDefault() throws Exception {
46 new CertificateRequest(ac, generatePEMCSR(kp, "CN=" + CertificateRequest.DEFAULT_CN + ",EMAIL=" + email)).draft();
50 public void testIssuingRealName() throws Exception {
51 new CertificateRequest(ac, generatePEMCSR(kp, "CN=a b,EMAIL=" + email)).draft();
55 public void testIssuingModifiedName() throws Exception {
57 new CertificateRequest(ac, generatePEMCSR(kp, "CN=a ab")).draft();
59 } catch (GigiApiException e) {
60 assertThat(e.getMessage(), containsString("name you entered was invalid"));
65 // TODO annotate that this depends on default config
67 public void testCodesignModifiedName() throws Exception {
69 u.grantGroup(getSupporter(), Group.CODESIGNING);
70 CertificateRequest cr = new CertificateRequest(ac, generatePEMCSR(kp, "CN=a ab"));
71 cr.update("name", "SHA512", "code-a", null, null, "email:" + email);
74 } catch (GigiApiException e) {
75 assertThat(e.getMessage(), containsString("does not match the details"));
80 // TODO annotate that this depends on default config
82 public void testCodesignNoPermModifiedName() throws Exception {
84 CertificateRequest cr = new CertificateRequest(ac, generatePEMCSR(kp, "CN=a ab"));
85 cr.update("name", "SHA512", "code-a", null, null, "email:" + email);
88 } catch (GigiApiException e) {
89 assertThat(e.getMessage(), containsString("Certificate Profile is invalid."));
95 public void testPingPeriodOneAddress() throws IOException, GeneralSecurityException, GigiApiException {
96 // get new email address with last ping in past
97 String furtherEmail = createUniqueName() + "@example.org";
98 EmailAddress ea = new EmailAddress(u, furtherEmail, Locale.ENGLISH);
99 TestMail mail = getMailReceiver().receive(furtherEmail);
100 try (GigiPreparedStatement stmt = new GigiPreparedStatement("UPDATE `emailPinglog` SET `status`='success'::`pingState`, `when` = (now() - interval '1 months' * ?::INTEGER) WHERE `email`=? ")) {
101 stmt.setInt(1, TimeConditions.getInstance().getEmailPingMonths());
102 stmt.setString(2, furtherEmail);
103 stmt.executeUpdate();
107 CertificateRequest cr = new CertificateRequest(ac, generatePEMCSR(kp, "CN=a ab"));
108 cr.update("name", "SHA512", "mail", null, null, "email:" + furtherEmail);
111 } catch (GigiApiException e) {
112 assertThat(e.getMessage(), containsString("needs an email ping within the past"));
118 public void testPingPeriodTwoAddresses() throws IOException, GeneralSecurityException, GigiApiException {
119 // get new email address with last ping in past
120 String furtherEmail = createUniqueName() + "@example.org";
121 EmailAddress ea = new EmailAddress(u, furtherEmail, Locale.ENGLISH);
122 TestMail mail = getMailReceiver().receive(furtherEmail);
123 try (GigiPreparedStatement stmt = new GigiPreparedStatement("UPDATE `emailPinglog` SET `status`='success'::`pingState`, `when` = (now() - interval '1 months' * ?::INTEGER) WHERE `email`=? ")) {
124 stmt.setInt(1, TimeConditions.getInstance().getEmailPingMonths());
125 stmt.setString(2, furtherEmail);
126 stmt.executeUpdate();
130 CertificateRequest cr = new CertificateRequest(ac, generatePEMCSR(kp, "CN=a ab"));
131 cr.update("name", "SHA512", "mail", null, null, "email:" + furtherEmail + ",email:" + email);
134 } catch (GigiApiException e) {
135 assertThat(e.getMessage(), containsString("needs an email ping within the past"));