"""Tests for Handler utils.""" import json from unittest.mock import MagicMock from unittest.mock import patch from flask import Request from flask import Response import marshmallow import pytest import application # noqa from lyrics.constants import header # noqa from lyrics.exceptions import RequestError from lyrics.exceptions import ValidationError from lyrics.utils import handlers as handlers_utils class _TestSchema(marshmallow.Schema): name = marshmallow.fields.Str(required=True) def test_parse_request_json(client_headers): """Test parsing and validating request JSON.""" request_data = {'name': 'Snowball'} with application.app.test_request_context( headers=client_headers, data=json.dumps(request_data)): data = handlers_utils.parse_request_json(schema=_TestSchema) assert data == request_data @pytest.mark.parametrize('data', [ b'true', b'null', b'\'\'', b'[]', b'', b'{"items": [}']) # noqa def test_parse_request_json_invalid_data(client_headers, data): """Test non-dict values returns a bad request.""" with application.app.test_request_context( headers=client_headers, data=data): with pytest.raises(RequestError): handlers_utils.parse_request_json(schema=_TestSchema) def test_parse_request_json_invalid_headers(): """Test missing header data returns a bad request.""" request_data = {'name': 'Snowball'} with application.app.test_request_context(data=json.dumps(request_data)): with pytest.raises(RequestError): handlers_utils.parse_request_json(schema=_TestSchema) def test_parse_request_json_missing_required_field(client_headers): """Test schema validator returns error.""" request_data = {} with application.app.test_request_context( headers=client_headers, data=json.dumps(request_data)): with pytest.raises(ValidationError): handlers_utils.parse_request_json(schema=_TestSchema) def test_parse_request_no_schema(client_headers): """Test schema validator pass with no schema specified.""" request_data = {} with application.app.test_request_context( headers=client_headers, data=json.dumps(request_data)): handlers_utils.parse_request_json() @pytest.mark.parametrize( 'context_type, orchard_user_id, profile_type, status_code', ( ( header.ERROR_CONTEXT_TYPE, 'oa:562', '', 400, ), ( header.ACCOUNT_CONTEXT_TYPE, 'alw:562', header.PROFILE_TYPE_ORCH_ADMIN, 403, ), ( header.ACCOUNT_CONTEXT_TYPE, 'oa:test', header.PROFILE_TYPE_ORCH_ADMIN, 403, ), ( header.ACCOUNT_CONTEXT_TYPE, 'oa:123', header.PROFILE_TYPE_ORCH_ADMIN, 200, ), ( '', '', header.PROFILE_TYPE_ORCH_ADMIN, 403, ), ( '', '', '', 403, ), ( '', '', header.PROFILE_TYPE_LABEL, 403, ), ) ) def test_verify_profile( context_type, orchard_user_id, profile_type, status_code): """Test verify_profile function.""" request_context = MagicMock() request_context.context_type = context_type request_context.orchard_user_id = orchard_user_id request_context.profile_type = profile_type response = handlers_utils.verify_profile(request_context, profile_type) assert response.status == status_code def run_with_verify_authorization(client_headers): """Submit request with headers and get response.""" request = MagicMock(spec=Request, headers=client_headers) success_response = MagicMock(spec=Response, status_code=200) def inner(): return success_response with patch.object(handlers_utils, 'request', request): return handlers_utils.verify_authorization(inner)() def test_valid_authorization(): """Test that 200 is returned when authorized request made.""" result = run_with_verify_authorization( {'Authorization': 'ows-track/ows-lyrics:randomhmac'}) assert result.status_code == 200 def test_no_authorization(): """Test that inner function called when request is not authorized.""" result = run_with_verify_authorization({}) assert result.status_code == 403 def blank_authorization(): """Test that inner function called when request is not authorized.""" result = run_with_verify_authorization({'Authorization': ''}) assert result.status_code == 403