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