]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/dbObjects/TestCertificate.java
0b4d6549d962ceb255ad924d3622a74ac6f3c021
[gigi.git] / tests / club / wpia / gigi / dbObjects / TestCertificate.java
1 package club.wpia.gigi.dbObjects;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.security.GeneralSecurityException;
7 import java.security.KeyPair;
8
9 import org.junit.Test;
10
11 import club.wpia.gigi.GigiApiException;
12 import club.wpia.gigi.dbObjects.Certificate.AttachmentType;
13 import club.wpia.gigi.dbObjects.Certificate.CSRType;
14 import club.wpia.gigi.testUtils.ClientBusinessTest;
15
16 public class TestCertificate extends ClientBusinessTest {
17
18     @Test
19     public void testSetLoginEnabled() throws GeneralSecurityException, IOException, GigiApiException {
20         KeyPair kp = generateKeypair();
21         String key = generatePEMCSR(kp, "CN=testmail@example.com");
22         Certificate c = new Certificate(u, u, Certificate.buildDN("CN", "testmail@example.com"), Digest.SHA256, key, CSRType.CSR, getClientProfile());
23
24         assertFalse(c.isLoginEnabled());
25         c.setLoginEnabled(true);
26         assertTrue(c.isLoginEnabled());
27         c.setLoginEnabled(true);
28         assertTrue(c.isLoginEnabled());
29         c.setLoginEnabled(false);
30         assertFalse(c.isLoginEnabled());
31         c.setLoginEnabled(false);
32         assertFalse(c.isLoginEnabled());
33     }
34
35     @Test
36     public void testAttachment() throws GeneralSecurityException, IOException, GigiApiException {
37         KeyPair kp = generateKeypair();
38         String key = generatePEMCSR(kp, "CN=testmail@example.com");
39         Certificate c = new Certificate(u, u, Certificate.buildDN("CN", "testmail@example.com"), Digest.SHA256, key, CSRType.CSR, getClientProfile());
40         assertNull(c.getAttachment(AttachmentType.CRT));
41         assertEquals(key, c.getAttachment(AttachmentType.CSR));
42         try {
43             c.addAttachment(AttachmentType.CSR, "different CSR");
44             fail("double add attachment must fail");
45         } catch (GigiApiException e) {
46             // expected
47         }
48         assertNull(c.getAttachment(AttachmentType.CRT));
49         assertEquals(key, c.getAttachment(AttachmentType.CSR));
50         try {
51             c.addAttachment(AttachmentType.CRT, null);
52             fail("attachment must not be null");
53         } catch (GigiApiException e) {
54             // expected
55         }
56         assertNull(c.getAttachment(AttachmentType.CRT));
57         assertEquals(key, c.getAttachment(AttachmentType.CSR));
58         c.addAttachment(AttachmentType.CRT, "b");
59         assertEquals(key, c.getAttachment(AttachmentType.CSR));
60         assertEquals("b", c.getAttachment(AttachmentType.CRT));
61         try {
62             c.addAttachment(AttachmentType.CRT, "different CRT");
63             fail("double add attachment must fail");
64         } catch (GigiApiException e) {
65             // expected
66         }
67         assertEquals(key, c.getAttachment(AttachmentType.CSR));
68         assertEquals("b", c.getAttachment(AttachmentType.CRT));
69     }
70
71     @Test
72     public void testActor() throws GeneralSecurityException, IOException, GigiApiException {
73         KeyPair kp = generateKeypair();
74         String key = generatePEMCSR(kp, "CN=testmail@example.com");
75         Certificate c = new Certificate(u, u, Certificate.buildDN("CN", "testmail@example.com"), Digest.SHA256, key, CSRType.CSR, getClientProfile());
76
77         assertEquals(u, c.getActor());
78         assertEquals("AB", c.getActor().getInitials());
79     }
80 }