class TestJOSETransport3rdParty(unittest.TestCase): def setUp(self): self._transport = JOSETransport() self._transport.get = MagicMock(return_value=MagicMock( spec=APIResponse)) public_key = APIResponse(valid_public_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 test_public_key_parse(self): self._transport.get.return_value.data = valid_public_key keys = self._transport.api_public_keys self.assertEqual(len(keys), 1) self.assertIsInstance(keys[0], RSAKey) def test_build_jwt_signature_no_key(self): with self.assertRaises(NoIssuerKey): self._transport._build_jwt_signature(MagicMock(spec=str), ANY, ANY, ANY, ANY) def test_build_jwt_signature(self): self._transport.set_issuer(ANY, uuid4(), valid_private_key) jwt = self._transport._build_jwt_signature("PUT", "/test", str(uuid4()), "test:subject", "ABCDEFG") self.assertIn('IOV-JWT', jwt) jwt = jwt.strip('IOV-JWT ') self.assertEqual(len(jwt.split(".")), 3) def test_invalid_jwt_response(self): headers = {"X-IOV-JWT": 'invalid'} with self.assertRaises(InvalidJWTResponse): self._transport.verify_jwt_response(headers, ANY, ANY, ANY) def test_jwt_response_error(self): headers = {"X-IOV-JWT": 'invalid'} with self.assertRaises(InvalidJWTResponse): self._transport.verify_jwt_response(headers, ANY, ANY, ANY) def test_empty_jwt_response(self): headers = {} with self.assertRaises(InvalidJWTResponse): self._transport.verify_jwt_response(headers, ANY, ANY, ANY) @patch.object(JWS, 'verify_compact') def test_jwt_error_raises_expected_exception(self, verify_compact_patch): verify_compact_patch.side_effect = JWKESTException with self.assertRaises(JWTValidationFailure): self._transport.verify_jwt_response({}, ANY, ANY, ANY)
class TestJOSETransport3rdParty(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 test_public_key_parse(self): self._transport.get.return_value.data = valid_public_key keys = self._transport.api_public_keys self.assertEqual(len(keys), 1) self.assertIsInstance(keys[0], RSAKey) def test_build_jwt_signature_no_key(self): with self.assertRaises(NoIssuerKey): self._transport._build_jwt_signature(MagicMock(spec=str), ANY, ANY, ANY, ANY) def test_build_jwt_signature(self): self._transport.set_issuer(ANY, uuid4(), valid_private_key) jwt = self._transport._build_jwt_signature("PUT", "/test", str(uuid4()), "test:subject", "ABCDEFG") self.assertIn('IOV-JWT', jwt) jwt = jwt.strip('IOV-JWT ') self.assertEqual(len(jwt.split(".")), 3) def test_invalid_jwt_response(self): headers = {"X-IOV-JWT": 'invalid'} with self.assertRaises(InvalidJWTResponse): self._transport.verify_jwt_response(headers, ANY, ANY, ANY) def test_jwt_response_error(self): headers = {"X-IOV-JWT": 'invalid'} with self.assertRaises(InvalidJWTResponse): self._transport.verify_jwt_response(headers, ANY, ANY, ANY) def test_empty_jwt_response(self): headers = {} with self.assertRaises(InvalidJWTResponse): self._transport.verify_jwt_response(headers, ANY, ANY, ANY) @patch.object(JWS, 'verify_compact') def test_jwt_error_raises_expected_exception(self, verify_compact_patch): verify_compact_patch.side_effect = JWKESTException with self.assertRaises(JWTValidationFailure): self._transport.verify_jwt_response({}, ANY, ANY, ANY)
class TestJOSETransport3rdParty(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 test_public_key_parse(self): self._transport.get.return_value.data = valid_public_key keys = self._transport.api_public_keys self.assertEqual(len(keys), 1) self.assertIsInstance(keys[0], RSAKey) def test_build_jwt_signature_no_key(self): with self.assertRaises(NoIssuerKey): self._transport._build_jwt_signature(MagicMock(spec=str), ANY, ANY, ANY, ANY) def test_build_jwt_signature(self): self._transport.set_issuer(ANY, uuid4(), valid_private_key) jwt = self._transport._build_jwt_signature("PUT", "/test", str(uuid4()), "test:subject", "ABCDEFG") self.assertIn('IOV-JWT', jwt) jwt = jwt.strip('IOV-JWT ') self.assertEqual(len(jwt.split(".")), 3) def test_invalid_jwt_response(self): headers = {"X-IOV-JWT": 'invalid'} with self.assertRaises(InvalidJWTResponse): self._transport.verify_jwt_response(headers, ANY, ANY, ANY) def test_empty_jwt_response(self): headers = {} with self.assertRaises(InvalidJWTResponse): self._transport.verify_jwt_response(headers, ANY, ANY, ANY)
class TestJOSETransportJWT(unittest.TestCase): def setUp(self): self._transport = JOSETransport() self._transport.get = MagicMock(return_value=MagicMock(spec=APIResponse)) self._transport._get_jwt_signature = MagicMock(return_value='x.x.x') public_key = APIResponse(valid_public_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 test_build_jwt_signature(self): self._transport.set_issuer(ANY, uuid4(), valid_private_key) jwt = self._transport._build_jwt_signature("PUT", "/test", str(uuid4()), "test:subject", "ABCDEFG") self.assertIn('IOV-JWT', jwt) jwt = jwt.strip('IOV-JWT ') self.assertEqual(len(jwt.split(".")), 3)