X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=tests%2Forg%2Fcacert%2Fgigi%2Fapi%2FImportCATSResult.java;h=e16657a08523859cf0819988f39c9b7d774116c4;hb=dbb3046c3a3b833305535937f1c7e2653f2ca247;hp=41c49c564dd3ce00a479c5dad7d2cab628f47fbf;hpb=31bd86b7903b06266a84b3350914858c8a207b35;p=gigi.git diff --git a/tests/org/cacert/gigi/api/ImportCATSResult.java b/tests/org/cacert/gigi/api/ImportCATSResult.java index 41c49c56..e16657a0 100644 --- a/tests/org/cacert/gigi/api/ImportCATSResult.java +++ b/tests/org/cacert/gigi/api/ImportCATSResult.java @@ -3,59 +3,32 @@ package org.cacert.gigi.api; import static org.junit.Assert.*; import java.io.IOException; -import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; -import java.net.URL; import java.net.URLEncoder; import java.security.GeneralSecurityException; import java.security.KeyManagementException; -import java.security.KeyPair; import java.security.NoSuchAlgorithmException; -import java.security.PrivateKey; -import java.security.cert.X509Certificate; import org.cacert.gigi.GigiApiException; -import org.cacert.gigi.dbObjects.CATS; +import org.cacert.gigi.dbObjects.CATS.CATSType; import org.cacert.gigi.dbObjects.Certificate; import org.cacert.gigi.dbObjects.Certificate.CSRType; import org.cacert.gigi.dbObjects.Certificate.SANType; import org.cacert.gigi.dbObjects.CertificateProfile; import org.cacert.gigi.dbObjects.Digest; -import org.cacert.gigi.dbObjects.Group; -import org.cacert.gigi.dbObjects.Organisation; import org.cacert.gigi.dbObjects.User; -import org.cacert.gigi.testUtils.ClientTest; import org.cacert.gigi.testUtils.IOUtils; +import org.cacert.gigi.testUtils.RestrictedApiTest; import org.junit.Test; -public class ImportCATSResult extends ClientTest { - - private PrivateKey pk; - - private X509Certificate ce; - - public ImportCATSResult() throws IOException, GeneralSecurityException, InterruptedException, GigiApiException { - makeAssurer(id); - - grant(u.getEmail(), Group.ORGASSURER); - clearCaches(); - u = User.getById(u.getId()); - Organisation o = new Organisation(Organisation.SELF_ORG_NAME, "NA", "NA", "NA", "contact@cacert.org", u); - assertTrue(o.isSelfOrganisation()); - KeyPair kp = generateKeypair(); - String key1 = generatePEMCSR(kp, "EMAIL=cats@cacert.org"); - Certificate c = new Certificate(o, u, Certificate.buildDN("EMAIL", "cats@cacert.org"), Digest.SHA256, key1, CSRType.CSR, CertificateProfile.getByName("client-orga"), new Certificate.SubjectAlternateName(SANType.EMAIL, "cats@cacert.org")); - pk = kp.getPrivate(); - c.issue(null, "2y", u).waitFor(60000); - ce = c.cert(); - } +public class ImportCATSResult extends RestrictedApiTest { @Test public void testLookupSerial() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException { 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")); - target2.issue(null, "2y", u).waitFor(60000); + await(target2.issue(null, "2y", u)); assertEquals(u.getId(), Integer.parseInt(apiLookup(target2))); } @@ -73,32 +46,44 @@ public class ImportCATSResult extends ClientTest { apiImport(u2, "Test Training"); assertEquals(1, u2.getTrainings().length); assertFalse(u2.hasPassedCATS()); - apiImport(u2, CATS.ASSURER_CHALLANGE_NAME); + apiImport(u2, CATSType.ASSURER_CHALLENGE.getDisplayName()); assertEquals(2, u2.getTrainings().length); assertTrue(u2.hasPassedCATS()); } + @Test + public void testImportCATSFailures() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException { + assertEquals(1, u.getTrainings().length); + assertNotEquals(200, executeImportQuery("").getResponseCode()); + assertNotEquals(200, executeImportQuery("mid=" + u.getId()).getResponseCode()); + assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training").getResponseCode()); + assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training&date=" + System.currentTimeMillis()).getResponseCode()); + assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training&date=" + System.currentTimeMillis() + "&language=en").getResponseCode()); + assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training&date=" + System.currentTimeMillis() + "&version=1.0").getResponseCode()); + assertEquals(1, u.getTrainings().length); + apiImport(u, "Test Training"); + assertEquals(2, u.getTrainings().length); + + } + private void apiImport(User target, String test) throws IOException, MalformedURLException, NoSuchAlgorithmException, KeyManagementException, UnsupportedEncodingException, GeneralSecurityException { - HttpURLConnection connection = (HttpURLConnection) new URL("https://" + getServerName().replaceFirst("^www.", "api.") + CATSImport.PATH).openConnection(); - authenticateClientCert(pk, ce, connection); - connection.setDoOutput(true); - OutputStream os = connection.getOutputStream(); - os.write(("mid=" + target.getId() + "&variant=" + URLEncoder.encode(test, "UTF-8") + "&date=" + System.currentTimeMillis()).getBytes("UTF-8")); + HttpURLConnection connection = executeImportQuery("mid=" + target.getId() + "&variant=" + URLEncoder.encode(test, "UTF-8") + "&date=" + System.currentTimeMillis() + "&language=en&version=1.0"); if (connection.getResponseCode() != 200) { throw new Error(connection.getResponseMessage()); } } - private String apiLookup(Certificate target) throws IOException, MalformedURLException, NoSuchAlgorithmException, KeyManagementException, UnsupportedEncodingException, GeneralSecurityException { - HttpURLConnection connection = (HttpURLConnection) new URL("https://" + getServerName().replaceFirst("^www.", "api.") + CATSResolve.PATH).openConnection(); - authenticateClientCert(pk, ce, connection); - connection.setDoOutput(true); - OutputStream os = connection.getOutputStream(); - os.write(("serial=" + target.cert().getSerialNumber().toString(16).toLowerCase()).getBytes()); + private HttpURLConnection executeImportQuery(String query) throws IOException, GeneralSecurityException { + return doApi(CATSImport.PATH, query); + } + + private String apiLookup(Certificate target) throws IOException, GeneralSecurityException { + HttpURLConnection connection = doApi(CATSResolve.PATH, "serial=" + target.cert().getSerialNumber().toString(16).toLowerCase()); if (connection.getResponseCode() != 200) { throw new Error(connection.getResponseMessage()); } return IOUtils.readURL(connection); } + }