def test_no_hash_match(self):
     with app.test_request_context():
         g.trace_id = 'test'
         self.assertEqual(
             self.cypto.validate_signature(test_payload, test_sig,
                                           "sha-256:MADEUP"),
             (False, "Supplied hash does not match calculated hash."))
 def test_match(self):
     with app.test_request_context():
         g.trace_id = 'test'
         self.assertEqual(
             self.cypto.validate_signature(test_payload, test_sig,
                                           test_hash),
             (True, "Signature and payload match."))
 def test_invalid_hash(self):
     with app.test_request_context():
         g.trace_id = 'test'
         self.assertEqual(
             self.cypto.validate_signature(test_payload, test_sig,
                                           "NOTAHASH"),
             (False, "Invalid hash string 'NOTAHASH'."))
 def test_invalid_sig(self):
     with app.test_request_context():
         g.trace_id = 'test'
         self.assertEqual(
             self.cypto.validate_signature(test_payload, "NOTASIG",
                                           test_hash),
             (False, "Invalid signature string 'NOTASIG'."))
    def test_valid_register_fail(self, session, mock_charge_id_service, validate):
        with app.test_request_context():
            expected_register_result = {
                "error": "Bad times",
                "details": []
            }

            g.session = mock.MagicMock()
            response = mock.MagicMock()
            response.status_code = 400
            response.json.return_value = expected_register_result
            response.text.return_value = b'Bad times'
            session.return_value.post.return_value = response
            mock_charge_id_service.generate_charge_id.return_value = MOCK_CHARGE_ID

            res = self.app.post('/v1.0/records',
                                data='{"local-land-charge": "w00t"}',
                                content_type="application/json",
                                headers={'Authorization': 'Fake JWT'})
            actual_result = res.get_data().decode('utf-8')

            self.assertEqual(res.status_code, 400)
            self.assertEqual(True, 'error' in actual_result)
            self.assertEqual(True, 'details' in actual_result)
            self.assertEqual(True, 'Bad times' in actual_result)
 def test_no_match(self):
     with app.test_request_context():
         g.trace_id = 'test'
         self.assertEqual(
             self.cypto.validate_signature(test_payload,
                                           test_sig.replace('0', '2'),
                                           test_hash),
             (False, 'Signature and payload do not match.'))
Exemplo n.º 7
0
    def test_health_cascade(self, requests, pg):
        with app.test_request_context():
            response = MagicMock()
            response.status_code = 200
            response.headers = {'content-type': 'application/unit+test'}
            response.json.return_value = {}
            requests.return_value.get.return_value = response
            pg.connect.return_value.cursor.return_value.fetchone.return_value = [datetime.now()]

            resp = self.app.get('/health/cascade/4')
            self.assertEqual(resp.status_code, 200)
    def test_valid_update(self, session, mock_charge_id_service, validate):
        with app.test_request_context():
            g.session = mock.MagicMock()
            response = mock.MagicMock()
            response.status_code = 202
            response.json.return_value = EXPECTED_REGISTER_RESPONSE
            session.return_value.post.return_value = response

            res = self.app.post('/v1.0/records',
                                data='{"local-land-charge": 1}',
                                content_type="application/json",
                                headers={'Authorization': 'Fake JWT'})
            actual_result = json.loads(res.get_data().decode('utf-8'))

            self.assertEqual((res.status_code, actual_result),
                             (202, EXPECTED_MINT_RESPONSE))
            mock_charge_id_service.assert_not_called()
    def test_valid_add(self, session, mock_charge_id_service, validate):
        with app.test_request_context():
            g.session = mock.MagicMock()
            response = mock.MagicMock()
            response.status_code = 202
            response.json.return_value = EXPECTED_REGISTER_RESPONSE
            session.return_value.post.return_value = response
            mock_charge_id_service.generate_charge_id.return_value = MOCK_CHARGE_ID

            res = self.app.post('/v1.0/records',
                                data='{"test_field": "w00t"}',
                                content_type="application/json",
                                headers={'Authorization': 'Fake JWT'})
            actual_result = json.loads(res.get_data().decode('utf-8'))

            self.assertEqual((res.status_code, actual_result),
                             (202, EXPECTED_MINT_RESPONSE))
            mock_charge_id_service.generate_charge_id.assert_called_once()
    def test_minified_json_to_sign_payload(self, session, mock_cryptography, mock_charge_id_service, validate):
        with app.test_request_context():
            g.session = mock.MagicMock()
            response = mock.MagicMock()
            response.status_code = 202
            response.json.return_value = EXPECTED_REGISTER_RESPONSE
            session.return_value.post.return_value = response
            mock_cryptography.return_value = "rs256:test_sig", "sha-256:test_hash"
            mock_charge_id_service.generate_charge_id.return_value = MOCK_CHARGE_ID

            res = self.app.post('/v1.0/records',
                                data='{"my-key": "my-value"}',
                                content_type="application/json",
                                headers={'Authorization': 'Fake JWT'})
            actual_result = json.loads(res.get_data().decode('utf-8'))

            mock_cryptography.assert_called_with(
                '{{"{}":{},"my-key":"my-value"}}'.format('local-land-charge', MOCK_CHARGE_ID))
            self.assertEqual((res.status_code, actual_result), (202, EXPECTED_MINT_RESPONSE))
 def test_sign_valid(self):
     with app.test_request_context():
         g.trace_id = 'test'
         self.assertEqual(self.cypto.sign_payload(test_payload),
                          (test_sig, test_hash))
 def test_no_token(self):
     with app.test_request_context():
         res = self.app.post('/v1.0/records',
                             data='{"local-land-charge": 1}',
                             content_type="application/json")
         self.assertEqual(res.status_code, 401)