]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/api/ImportCATSResult.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / tests / org / cacert / gigi / api / ImportCATSResult.java
1 package org.cacert.gigi.api;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.io.UnsupportedEncodingException;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
9 import java.net.URLEncoder;
10 import java.security.GeneralSecurityException;
11 import java.security.KeyManagementException;
12 import java.security.NoSuchAlgorithmException;
13
14 import org.cacert.gigi.GigiApiException;
15 import org.cacert.gigi.dbObjects.CATS.CATSType;
16 import org.cacert.gigi.dbObjects.Certificate;
17 import org.cacert.gigi.dbObjects.Certificate.CSRType;
18 import org.cacert.gigi.dbObjects.Certificate.SANType;
19 import org.cacert.gigi.dbObjects.CertificateProfile;
20 import org.cacert.gigi.dbObjects.Digest;
21 import org.cacert.gigi.dbObjects.User;
22 import org.cacert.gigi.testUtils.IOUtils;
23 import org.cacert.gigi.testUtils.RestrictedApiTest;
24 import org.cacert.gigi.util.ServerConstants;
25 import org.junit.Test;
26
27 public class ImportCATSResult extends RestrictedApiTest {
28
29     @Test
30     public void testLookupSerial() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
31         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"));
32         await(target2.issue(null, "2y", u));
33         target2.setLoginEnabled(true);
34
35         assertEquals(u.getId(), Integer.parseInt(apiLookup(target2)));
36
37         Certificate target3 = new Certificate(selfOrg, u, Certificate.buildDN("EMAIL", ServerConstants.getQuizAdminMailAddress()), Digest.SHA256, generatePEMCSR(generateKeypair(), "EMAIL=" + ServerConstants.getQuizAdminMailAddress()), CSRType.CSR, CertificateProfile.getByName("client-orga"), new Certificate.SubjectAlternateName(SANType.EMAIL, ServerConstants.getQuizAdminMailAddress()));
38         await(target3.issue(null, "2y", u));
39         target3.setLoginEnabled(true);
40
41         assertEquals("admin", apiLookup(target3));
42     }
43
44     @Test
45     public void testImportCATS() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
46
47         assertEquals(1, u.getTrainings().length);
48         apiImport(u, "Test Training");
49         assertEquals(2, u.getTrainings().length);
50
51         User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@example.com", TEST_PASSWORD));
52         assertEquals(0, u2.getTrainings().length);
53         assertFalse(u2.hasPassedCATS());
54         apiImport(u2, "Test Training");
55         assertEquals(1, u2.getTrainings().length);
56         assertFalse(u2.hasPassedCATS());
57         apiImport(u2, CATSType.ASSURER_CHALLENGE.getDisplayName());
58         assertEquals(2, u2.getTrainings().length);
59         assertTrue(u2.hasPassedCATS());
60
61     }
62
63     @Test
64     public void testImportCATSFailures() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
65         assertEquals(1, u.getTrainings().length);
66         assertNotEquals(200, executeImportQuery("").getResponseCode());
67         assertNotEquals(200, executeImportQuery("mid=" + u.getId()).getResponseCode());
68         assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training").getResponseCode());
69         assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training&date=" + System.currentTimeMillis()).getResponseCode());
70         assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training&date=" + System.currentTimeMillis() + "&language=en").getResponseCode());
71         assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training&date=" + System.currentTimeMillis() + "&version=1.0").getResponseCode());
72         assertEquals(1, u.getTrainings().length);
73         apiImport(u, "Test Training");
74         assertEquals(2, u.getTrainings().length);
75
76     }
77
78     private void apiImport(User target, String test) throws IOException, MalformedURLException, NoSuchAlgorithmException, KeyManagementException, UnsupportedEncodingException, GeneralSecurityException {
79         HttpURLConnection connection = executeImportQuery("mid=" + target.getId() + "&variant=" + URLEncoder.encode(test, "UTF-8") + "&date=" + System.currentTimeMillis() + "&language=en&version=1.0");
80         if (connection.getResponseCode() != 200) {
81             throw new Error(connection.getResponseMessage());
82         }
83     }
84
85     private HttpURLConnection executeImportQuery(String query) throws IOException, GeneralSecurityException {
86         return doApi(CATSImport.PATH, query);
87     }
88
89     private String apiLookup(Certificate target) throws IOException, GeneralSecurityException {
90         HttpURLConnection connection = doApi(CATSResolve.PATH, "serial=" + target.cert().getSerialNumber().toString(16).toLowerCase());
91         if (connection.getResponseCode() != 200) {
92             throw new Error(connection.getResponseMessage());
93         }
94         return IOUtils.readURL(connection);
95     }
96
97 }