class TestJWKESTSupportedAlgs(unittest.TestCase): def setUp(self): self._transport = JOSETransport() self._transport.get = MagicMock(return_value=MagicMock(spec=APIResponse)) public_key = APIResponse(valid_private_key, {"X-IOV-KEY-ID": "59:12:e2:f6:3f:79:d5:1e:18:75:c5:25:ff:b3:b7:f2"}, 200) self._transport.get.return_value = public_key self._transport._server_time_difference = 0, time() def _encrypt_decrypt(self): self._transport.add_issuer_key(valid_private_key) to_encrypt = {"tobe": "encrypted"} encrypted = self._transport._encrypt_request(to_encrypt) self.assertEqual(len(encrypted.split('.')), 5) decrypted = self._transport.decrypt_response(encrypted) self.assertEqual(loads(decrypted), to_encrypt) def test_encrypt_decrypt_defaults(self): self._encrypt_decrypt() def test_supported_jwt_algorithms_success(self): for alg in JOSE_SUPPORTED_JWT_ALGS: self._transport.jwt_algorithm = alg self._encrypt_decrypt() def test_supported_jwe_algorithms_success(self): for alg in JOSE_SUPPORTED_JWE_ALGS: self._transport.jwe_cek_encryption = alg self._encrypt_decrypt() def test_supported_jwe_encryptions_success(self): for enc in JOSE_SUPPORTED_JWE_ENCS: self._transport.jwe_claims_encryption = enc self._encrypt_decrypt()
class TestJWKESTSupportedAlgs(unittest.TestCase): def setUp(self): self._transport = JOSETransport() self._transport.get = MagicMock(return_value=MagicMock( spec=APIResponse)) public_key = APIResponse(valid_private_key, transport_request_headers, 200) self._transport.get.return_value = public_key self._transport._server_time_difference = 0, time() self._jwt_patch = patch("launchkey.transports.jose_auth.JWT", return_value=MagicMock(spec=JWT)).start() self._jwt_patch.return_value.unpack.return_value.headers = faux_jwt_headers self.addCleanup(patch.stopall) def _encrypt_decrypt(self): self._transport.add_issuer_key(valid_private_key) to_encrypt = {"tobe": "encrypted"} encrypted = self._transport._encrypt_request(to_encrypt) self.assertEqual(len(encrypted.split('.')), 5) decrypted = self._transport.decrypt_response(encrypted) self.assertEqual(loads(decrypted), to_encrypt) def test_encrypt_decrypt_defaults(self): self._encrypt_decrypt() def test_supported_jwt_algorithms_success(self): for alg in JOSE_SUPPORTED_JWT_ALGS: self._transport.jwt_algorithm = alg self._encrypt_decrypt() def test_supported_jwe_algorithms_success(self): for alg in JOSE_SUPPORTED_JWE_ALGS: self._transport.jwe_cek_encryption = alg self._encrypt_decrypt() def test_supported_jwe_encryptions_success(self): for enc in JOSE_SUPPORTED_JWE_ENCS: self._transport.jwe_claims_encryption = enc self._encrypt_decrypt()
class TestJOSETransportIssuers(unittest.TestCase): def setUp(self): self._transport = JOSETransport() def test_add_issuer_key(self): self.assertEqual(len(self._transport.issuer_private_keys), 0) self._transport.add_issuer_key(valid_private_key) self.assertEqual(len(self._transport.issuer_private_keys), 1) def test_add_duplicate_issuer_key(self): self.assertEqual(len(self._transport.issuer_private_keys), 0) self._transport.add_issuer_key(valid_private_key) self.assertEqual(len(self._transport.issuer_private_keys), 1) resp = self._transport.add_issuer_key(valid_private_key) self.assertFalse(resp) self.assertEqual(len(self._transport.issuer_private_keys), 1) def test_generate_key_id(self): self._transport.add_issuer_key(valid_private_key) self.assertEqual(self._transport.issuer_private_keys[0].kid, '59:12:e2:f6:3f:79:d5:1e:18:75:c5:25:ff:b3:b7:f2') def test_set_url(self): self._transport._http_client = MagicMock() self._transport.set_url(ANY, ANY) self._transport._http_client.set_url.assert_called_once() def test_set_issuer_invalid_entity_id(self): with self.assertRaises(InvalidEntityID): self._transport.set_issuer(ANY, ANY, ANY) def test_set_issuer_invalid_entity_issuer(self): with self.assertRaises(InvalidIssuer): self._transport.set_issuer(MagicMock(spec=str), uuid4(), ANY) def test_set_issuer_invalid_private_key(self): with self.assertRaises(InvalidPrivateKey): self._transport.set_issuer(ANY, uuid4(), "InvalidKey") @patch("launchkey.transports.jose_auth.RSAKey") @patch("launchkey.transports.jose_auth.import_rsa_key") def test_issuer_list(self, rsa_key_patch, import_key_patch): rsa_key_patch.return_value = MagicMock(spec=RSAKey) import_key_patch.return_value = MagicMock() self._transport.add_issuer_key = MagicMock() for issuer in VALID_JWT_ISSUER_LIST: self._transport.set_issuer(issuer, uuid4(), ANY)