"""Application tests.""" import json import boto3 from moto import mock_aws import pytest import application from manualadjustment import config from manualadjustment.models.manual_adjustment import ManualAdjustment from utils import db_ops from utils.tables.manual_adjustment_category import data as category_data from utils.tables.orchadmin_users import data as user_data from utils.tables.period import data as period_data from utils.tables.vendor import data as vendor_data test_get_params = { 'parent_id': 16031, 'parent_type': 'vendor', 'category_id': 0, 'apply_to_period_id': 156 } test_post = { 'amount': 2833.54, 'category_id': 0, 'comment': 'Adjustment', 'adjust_for_period_id': 156, 'apply_to_period_id': 156, 'parent_id': 16031, 'parent_type': 'vendor', 'created_by': 261 } def build_client(): """Build a test client.""" return application.app.test_client() @pytest.fixture(scope='module') def db_fixture(): """Reset and seed db tables.""" db_ops.create_tables() db_ops.seed_art_relations() @pytest.fixture() def test_put(): """Data to test puts.""" period_id = period_data[0].get('period_id') return { 'amount': 123.54, 'category_id': category_data[0].get('category_id'), 'comment': 'Adjusted adjustment', 'adjust_for_period_id': period_id, 'apply_to_period_id': period_id, 'parent_id': vendor_data[0].get('vendor_id'), 'created_by': user_data[0].get('id') } @pytest.fixture() def mimetype_error(): """Error response params.""" return { 'error': 'invalid mimetype', 'error_detail': 'please set the mimetype to application/json' } def test_get_manual_adjustment_pass(db_fixture): """Test a good manual adjustment get request.""" c = build_client() resp = c.get('/manual_adjustment', query_string=test_get_params) json_response = json.loads(resp.get_data(True)) response_adjustments = json_response.get('manual_adjustments') assert response_adjustments[0].get('amount') == 2833.54 assert response_adjustments[0].get('apply_to_period_id') == 156 assert resp.status_code == 200 def test_get_manual_adjustment_fail(): """Test a bad manual adjustment get request.""" c = build_client() # leave out required parent id and parent type params resp = c.get('/manual_adjustment') assert resp.status_code == 400 @mock_aws def test_post_manual_adjustment_pass(): """Test a good manual adjustment post request.""" conn = boto3.client('sqs', region_name=config.AWS_REGION) conn.create_queue(QueueName=config.queue_name) db_ops.create_tables() db_ops.seed_art_relations() c = build_client() headers = [('Content-Type', 'application/json')] resp = c.post( '/manual_adjustment', headers=headers, data=json.dumps(test_post)) json_response = json.loads(resp.get_data(True)) assert json_response.get('amount') == 2833.54 assert json_response.get('apply_to_period_id') == 156 assert resp.status_code == 201 def test_health_check(): """Test healthcheck.""" c = build_client() resp = c.get('/health') assert resp.status_code == 200 assert resp.get_data(True) == 'OK' @mock_aws def test_successful_put(db_fixture, test_put): """Test a good manual adjustment put request.""" conn = boto3.client('sqs', region_name=config.AWS_REGION) conn.create_queue(QueueName=config.queue_name) db_ops.create_tables() db_ops.seed_art_relations() first_one = db_ops.ar_session.query(ManualAdjustment).first() c = build_client() headers = [('Content-Type', 'application/json')] resp = c.put( '/manual_adjustment/{}'.format(first_one.id), headers=headers, data=json.dumps(test_put)) resp_body = json.loads(resp.get_data(True)) assert resp.status_code == 200 for param_name in test_put: assert resp_body.get(param_name) == test_put[param_name] def test_put_with_wrong_mimetime(db_fixture, test_put, mimetype_error): """Test a manual adjustment put request with a bad mimetype.""" first_one = db_ops.ar_session.query(ManualAdjustment).first() c = build_client() headers = [('Content-Type', 'application/vnd.amiga.ami')] resp = c.put( '/manual_adjustment/{}'.format(first_one.id), headers=headers, data=json.dumps(test_put)) resp_body = json.loads(resp.get_data(True)) assert resp.status_code == 400 assert resp_body == mimetype_error def test_put_with_nonexistent_id(test_put): """Test a manual adjustment put request with a bad id.""" db_ops.create_tables() db_ops.seed_art_relations() c = build_client() headers = [('Content-Type', 'application/json')] bad_id = 1234567 resp = c.put( 'manual_adjustment/{}'.format(bad_id), headers=headers, data=json.dumps(test_put)) resp_body = json.loads(resp.get_data(True)) expected_error = { 'error': 'Manual adjustment not found', 'error_detail': 1234567} assert resp.status_code == 404 assert resp_body == expected_error