"""Tests for base handlers.""" import json import random from http.client import OK from unittest.mock import MagicMock, NonCallableMagicMock, call import pytest import sentry_sdk from artwork.api import app from artwork.constants import permissions as permissions_constants from artwork.constants import service as service_constants from artwork.handlers import base as base_handlers from botocore.errorfactory import ClientError from flask import g from owsrequest import flask_request from owsrequest.utils import mock_request def test_invalid_usage_exception_handler(): """Test invalid_usage_exception_handler.""" @pytest.mark.parametrize( ( 'exception', 'expected_message', 'expected_status_code', 'expected_error_code', 'logger_calls', 'sentry_calls', ), [ ( Exception, 'I have erred and am unable to complete your request.', 500, 'internal_error', [call(Exception)], [call()], ), ( ClientError({'Error': {'Code': 'NoSuchKey'}}, ''), 'Oopsies, I don\'t know how to respond to that ¯\\_(ツ)_/¯', 400, 400, [], [], ), ( NonCallableMagicMock(code=random.randint(400, 499)), 'Oopsies, I don\'t know how to respond to that ¯\\_(ツ)_/¯', 400, 400, [call(Exception)], [call()], ), ], ) def test_exception_handler( mocker, exception, expected_message, expected_status_code, expected_error_code, logger_calls, sentry_calls, ): """Verify exception_Handler returns 500 status code and json payload.""" mocker.patch.object(sentry_sdk, 'capture_exception') with app.test_request_context(): g.log = MagicMock(exception=MagicMock()) server_response = base_handlers.exception_handler(exception) assert sentry_sdk.capture_exception.mock_calls == sentry_calls # assert status code is 500 assert server_response.status_code == expected_status_code # assert json payload response_message = json.loads(server_response.data.decode()) assert expected_message == response_message['message'] assert expected_error_code == response_message['code'] def test_health(): """Test health.""" with app.test_request_context(): result = base_handlers.health() assert result.status_code == OK assert result.json == {'status': 'ok'} test_get_vendor_and_subaccount_junk_resources = [ {'type': 'junk asdfowq3g4oh',}, {'type': 'junk asdfowq3g4oh',}, {'type': 'junk asdfowq3asfdgg4oh',}, {'type': 'junk asdfowq3asfdgg4oh',}, ] @pytest.mark.parametrize( ( 'test_description', 'ows_permissions_response_json', 'ows_permissions_response_status', 'expected_result', ), [ ( 'test subaccount context', { 'items': sorted( [ *test_get_vendor_and_subaccount_junk_resources, { 'type': permissions_constants.SUBACCOUNT_RESOURCE_TYPE, 'id': 235, 'vendor_id': 436326, }, ], key=lambda _: random.random(), ) }, OK, (436326, 235), ), ( 'test vendor context', { 'items': sorted( [ *test_get_vendor_and_subaccount_junk_resources, { 'type': permissions_constants.VENDOR_RESOURCE_TYPE, 'id': 123, }, ], key=lambda _: random.random(), ) }, OK, (123, None), ), ( 'test with new subaccount context', { 'items': sorted( [ {'type': 'SubAccount', 'id': 235, 'vendor_id': 436326,}, *test_get_vendor_and_subaccount_junk_resources, ], key=lambda _: random.random(), ) }, OK, (436326, 235), ), ( 'test subaccount new and old context', { 'items': sorted( [ {'type': 'SubAccount', 'id': 235, 'vendor_id': 436326,}, *test_get_vendor_and_subaccount_junk_resources, { 'type': permissions_constants.SUBACCOUNT_RESOURCE_TYPE, 'id': 235, 'vendor_id': 436326, }, ], key=lambda _: random.random(), ) }, OK, (436326, 235), ), ( 'test with all caps subaccount context', { 'items': sorted( [ {'type': 'SUBACCOUNT', 'id': 235, 'vendor_id': 436326,}, *test_get_vendor_and_subaccount_junk_resources, ], key=lambda _: random.random(), ) }, OK, (436326, 235), ), ( 'test with bad context', { 'items': sorted( [*test_get_vendor_and_subaccount_junk_resources], key=lambda _: random.random(), ) }, OK, (None, None), ), ], ) def test_get_vendor_and_subaccount( test_description, ows_permissions_response_json, ows_permissions_response_status, expected_result, mocker, ): """Test get_vendor_and_subaccount.""" profile_type = 'abcasdf' profile_id = 12321 ows_permissions_mock = mock_request.get( service_constants.OWS_PERMISSIONS, service_constants.OWS_PERMISSIONS_PROFILE_RESOURCE.format( profile_type=profile_type, profile_id=profile_id, resource='label' ), ows_permissions_response_json, ows_permissions_response_status, ) with app.test_request_context(): g.request_context = MagicMock() flask_request.get_ows().correlation_id = 'some-correlation-id-abc-123' result = base_handlers.get_vendor_and_subaccount(profile_type, profile_id) assert result == expected_result assert ows_permissions_mock.called assert len(ows_permissions_mock.calls) == 1 assert ows_permissions_mock.calls[0]['method'] == 'GET' @pytest.mark.parametrize( ('url_rule', 'view_args', 'expected_result',), [(MagicMock(rule='something/fun/cool',), {'abc': '112'}, True,)], ) def test_is_public_request(url_rule, view_args, expected_result): """Test _is_public_request helper.""" assert not base_handlers._is_public_request()