"""Tests for account_utils module.""" import json import application from flask import jsonify from oto import status as response_code import pytest from conflict_manager.constants import error from conflict_manager.constants import header as header_const from conflict_manager.constants import query_parameters as query_const from conflict_manager.utils import account_utils @pytest.fixture def test_account_type(): """Test Account Type.""" return 'vendor' @pytest.fixture def test_account_id(): """Test Account Id.""" return '123' def test_get_account_via_headers(test_account_type, test_account_id): """Test get account.""" headers = { header_const.GRASS_ACCOUNT_TYPE: test_account_type, header_const.GRASS_ACCOUNT_ID: test_account_id} with application.app.test_request_context(headers=headers): account = account_utils.get_account() assert account.type == test_account_type assert account.id == test_account_id def test_get_account_via_query_params(test_account_type, test_account_id): """Test get account.""" query_params = { query_const.ACCOUNT_TYPE: test_account_type, query_const.ACCOUNT_ID: test_account_id} with application.app.test_request_context(query_string=query_params): account = account_utils.get_account() assert account.type == test_account_type assert account.id == test_account_id @account_utils.account_required def _test_handler(): """Test Handler to test account_required decorator.""" return jsonify({'status': 'ok'}) def test_account_required_via_headers(test_account_type, test_account_id): """Verify account data found via headers.""" headers = { header_const.GRASS_ACCOUNT_TYPE: test_account_type, header_const.GRASS_ACCOUNT_ID: test_account_id} with application.app.test_request_context(headers=headers): res = _test_handler() assert res.status_code == response_code.OK def test_account_required_via_query_params(test_account_type, test_account_id): """Verify account data found via query parameters.""" query_params = { query_const.ACCOUNT_TYPE: test_account_type, query_const.ACCOUNT_ID: test_account_id} with application.app.test_request_context(query_string=query_params): res = _test_handler() assert res.status_code == response_code.OK def test_account_required_via_headers_and_query_parms( test_account_type, test_account_id): """Expect error, because authorization info is duplicated. Only one method of authorization should be allowed. """ headers = { header_const.GRASS_ACCOUNT_TYPE: test_account_type, header_const.GRASS_ACCOUNT_ID: test_account_id} query_params = { query_const.ACCOUNT_TYPE: test_account_type, query_const.ACCOUNT_ID: test_account_id} with application.app.test_request_context( headers=headers, query_string=query_params): res = _test_handler() res_data = json.loads(res.data.decode()) assert res.status_code == response_code.BAD_REQUEST assert res_data['code'] == error.ERROR_CODE_BAD_REQUEST def test_account_required_no_account_data(): """Expect error, because authorization info is missing.""" with application.app.test_request_context(): res = _test_handler() res_data = json.loads(res.data.decode()) assert res.status_code == response_code.FORBIDDEN assert res_data['code'] == error.ERROR_CODE_AUTHORIZATION assert res_data['message'] == error.AUTHORIZATION_MSG def test_account_required_invalid_account_type( test_account_type, test_account_id): """Expect error, because account_type is invalid.""" headers = { header_const.GRASS_ACCOUNT_TYPE: 'not vendor or subaccount', header_const.GRASS_ACCOUNT_ID: test_account_id } with application.app.test_request_context(headers=headers): res = _test_handler() res_data = json.loads(res.data.decode()) assert res.status_code == response_code.FORBIDDEN assert res_data['code'] == error.ERROR_CODE_AUTHORIZATION assert res_data['message'] == error.INVALID_GRASS_ACCOUNT_TYPE_MSG def test_account_required_missing_account_id(test_account_type): """Expect error, because account_id is missing.""" headers = { header_const.GRASS_ACCOUNT_TYPE: test_account_type } with application.app.test_request_context(headers=headers): res = _test_handler() res_data = json.loads(res.data.decode()) assert res.status_code == response_code.BAD_REQUEST assert res_data['code'] == error.ERROR_CODE_AUTHORIZATION assert res_data['message'] == error.INCOMPLETE_GRASS_HEADERS_MSG def test_account_required_missing_account_type(test_account_id): """Expect error, because account_type is missing.""" headers = { header_const.GRASS_ACCOUNT_ID: test_account_id } with application.app.test_request_context(headers=headers): res = _test_handler() res_data = json.loads(res.data.decode()) assert res.status_code == response_code.BAD_REQUEST assert res_data['code'] == error.ERROR_CODE_AUTHORIZATION assert res_data['message'] == error.INCOMPLETE_GRASS_HEADERS_MSG def test_account_required_account_id_not_int(test_account_type): """Expect error, because account_id is not integer.""" headers = { header_const.GRASS_ACCOUNT_TYPE: test_account_type, header_const.GRASS_ACCOUNT_ID: 'not integer' } with application.app.test_request_context(headers=headers): res = _test_handler() res_data = json.loads(res.data.decode()) assert res.status_code == response_code.BAD_REQUEST assert res_data['code'] == error.ERROR_CODE_AUTHORIZATION assert res_data['message'] == error.INVALID_GRASS_ACCOUNT_ID_MSG def test_account_required_account_id_negative_int(test_account_type): """Expect error, because account_id is negative integer.""" headers = { header_const.GRASS_ACCOUNT_TYPE: test_account_type, header_const.GRASS_ACCOUNT_ID: '-1' } with application.app.test_request_context(headers=headers): res = _test_handler() res_data = json.loads(res.data.decode()) assert res.status_code == response_code.BAD_REQUEST assert res_data['code'] == error.ERROR_CODE_AUTHORIZATION assert res_data['message'] == error.INVALID_GRASS_ACCOUNT_ID_MSG def test_validate_user_id_valid_id(): """Expect error, because user_id is not integer.""" response = account_utils.validate_user_id('oa:123') assert response.status == response_code.OK def test_validate_user_id_id_not_int(): """Expect error, because user_id is not integer.""" response = account_utils.validate_user_id('not a integer') assert response.status == response_code.BAD_REQUEST assert response.errors['code'] == error.ERROR_CODE_BAD_REQUEST assert response.errors['message'] == error.INVALID_GRASS_USER_ID_MSG def test_validate_user_id_user_id_negative(): """Expect error, because user_id is negative integer.""" response = account_utils.validate_user_id('oa:-1') assert response.status == response_code.BAD_REQUEST assert response.errors['code'] == error.ERROR_CODE_BAD_REQUEST assert response.errors['message'] == error.INVALID_GRASS_USER_ID_MSG