"""Tests for generation_result module.""" import datetime from unittest.mock import patch from botocore.exceptions import NoCredentialsError from flexmock import flexmock from moto import mock_aws from oto import response import pytest from salessheets import config from salessheets.connectors import s3 from salessheets.constants import error from salessheets.constants import field_const from salessheets.constants import salessheets from salessheets.logic import generation_result from salessheets.models import history from salessheets.models import ows_product @mock_aws def test_get_download_url_by_job_id_success(): """Test get_download_url_by_job_id successful result.""" job_id = 1 timestamp = datetime.date(day=1, month=12, year=2016) job_mock = history.Job( job_id=job_id, user_id=1, context=[123], output_format=salessheets.SINGLE, status=salessheets.COMPLETED, timestamp=timestamp) (flexmock(history) .should_receive('get_job_by_id') .with_args(job_id) .and_return(response.Response(job_mock))) conn = s3.connect_to_s3() test_bucket_name = config.SALESSHEETS_S3_BUCKET conn.create_bucket(Bucket=test_bucket_name) test_file_key = '20161201_1_bulk_sales_sheets.pdf' more_binary_data = b'qualitative_content' conn.put_object( Body=more_binary_data, Bucket=test_bucket_name, Key=test_file_key) result = generation_result.get_download_url_by_job_id(job_id) assert result def test_get_download_url_by_job_id_fail_on_get_data(): """Test get_download_url_by_job_id fail on get data.""" job_id = 1 (flexmock(history) .should_receive('get_job_by_id') .with_args(job_id) .and_return(response.create_not_found_response())) result = generation_result.get_download_url_by_job_id(job_id) assert result.status == 404 assert not result def test_get_download_url_by_job_id_fail_connect_to_s3(mocker): """Test get_download_url_by_job_id fail on s3 connection.""" with patch('boto3.Session') as mock_client: # Mock boto3.client to return a client with no credentials mock_client.side_effect = NoCredentialsError() job_id = 1 timestamp = datetime.date(day=1, month=12, year=2016) job_mock = history.Job( job_id=job_id, user_id=1, context=[123], output_format=salessheets.SINGLE, status=salessheets.COMPLETED, timestamp=timestamp) (flexmock(history) .should_receive('get_job_by_id') .with_args(job_id) .and_return(response.Response(job_mock))) result = generation_result.get_download_url_by_job_id(job_id) assert not result assert result.status == 500 @mock_aws def test_get_download_url_by_job_id_failed_no_file(): """Test get_download_url_by_job_id failed due to absence of file.""" job_id = 1 timestamp = datetime.date(day=1, month=12, year=2016) job_mock = history.Job( job_id=job_id, user_id=1, context=[123], output_format=salessheets.SINGLE, status=salessheets.COMPLETED, timestamp=timestamp) (flexmock(history) .should_receive('get_job_by_id') .with_args(job_id) .and_return(response.Response(job_mock))) result = generation_result.get_download_url_by_job_id(job_id) assert not result assert result.status == 404 @mock_aws def test_get_download_url_by_job_id_failed_not_completed(): """Test get_download_url_by_job_id failed due to not completed status.""" job_id = 1 timestamp = datetime.date(day=1, month=12, year=2016) job_mock = history.Job( job_id=job_id, user_id=1, context=[123], output_format=salessheets.SINGLE, status=salessheets.REQUESTED, timestamp=timestamp) (flexmock(history) .should_receive('get_job_by_id') .with_args(job_id) .and_return(response.Response(job_mock))) result = generation_result.get_download_url_by_job_id(job_id) assert not result assert result.status == 400 assert result.errors['code'] == error.JOB_IS_NOT_COMPLETED_CODE def test_get_history_for_user_success(): """Test get_history_for_user for successful result.""" user_id = '1234' sort_field = field_const.OUTPUT_FORMAT sort_order = field_const.ASC limit = 2 offset = 0 job = history.Job( job_id=1, user_id=user_id, output_format=salessheets.ALLOWED_GENERATION_METHODS[0], status=salessheets.REQUESTED, timestamp=datetime.datetime.now(), context=[2]) expected_response = response.Response([job]) (flexmock(history) .should_receive('get_jobs_count') .and_return(response.Response(1)) .once()) (flexmock(history) .should_receive('get_all_jobs_by_user_id') .and_return(expected_response)) resulted_dict = { field_const.ITEMS: [job.as_dict()], field_const.PAGINATION: { field_const.OFFSET: offset, field_const.TOTAL_RECORDS: 1, field_const.LIMIT: limit, field_const.TYPE: field_const.STANDARD, field_const.SORT_ORDER: sort_order, field_const.SORT_BY: sort_field}} result = generation_result.get_history_for_user( user_id, sort_field, sort_order, limit, offset) assert result assert result.message == resulted_dict def test_get_history_for_user_success_no_history(): """Test get_history_for_user for successful result with no items.""" user_id = '1234' sort_field = field_const.OUTPUT_FORMAT sort_order = field_const.ASC limit = 2 offset = 0 (flexmock(history) .should_receive('get_jobs_count') .and_return(response.Response(0)) .once()) resulted_dict = { field_const.ITEMS: [], field_const.PAGINATION: { field_const.OFFSET: offset, field_const.TOTAL_RECORDS: 0, field_const.LIMIT: limit, field_const.TYPE: field_const.STANDARD, field_const.SORT_ORDER: sort_order, field_const.SORT_BY: sort_field}} result = generation_result.get_history_for_user( user_id, sort_field, sort_order, limit, offset) assert result assert result.message == resulted_dict def test_get_history_for_user_error(): """Test get_history_for_user for failed result.""" user_id = '1234' sort_field = field_const.OUTPUT_FORMAT sort_order = field_const.ASC limit = 2 offset = 0 expected_response = response.create_fatal_response() (flexmock(history) .should_receive('get_jobs_count') .and_return(response.Response(1)) .once()) (flexmock(history) .should_receive('get_all_jobs_by_user_id') .and_return(expected_response)) result = generation_result.get_history_for_user( user_id, sort_field, sort_order, limit, offset) assert not result assert result.status == 500 def test_get_history_for_user_get_jobs_count_error(): """Test get_history_for_user for failed result on get count state.""" user_id = '1234' sort_field = field_const.OUTPUT_FORMAT sort_order = field_const.ASC limit = 2 offset = 0 expected_response = response.create_fatal_response() (flexmock(history) .should_receive('get_jobs_count') .and_return(expected_response) .once()) result = generation_result.get_history_for_user( user_id, sort_field, sort_order, limit, offset) assert not result assert result.status == 500 @mock_aws def test_get_last_successful_job_for_release_id_success(): """Test get_last_successful_job_for_release_id successful result.""" release_id = 123 timestamp = datetime.date(day=1, month=12, year=2016) job_mock = history.Job( job_id=1, user_id=1, context=[release_id], output_format=salessheets.SINGLE, status=salessheets.COMPLETED, timestamp=timestamp) (flexmock(history) .should_receive('get_job_by_release_id') .with_args(release_id) .and_return(response.Response(job_mock))) conn = s3.connect_to_s3() test_bucket_name = config.SALESSHEETS_S3_BUCKET conn.create_bucket(Bucket=test_bucket_name) test_file_key = '20161201_1_bulk_sales_sheets.pdf' more_binary_data = b'qualitative_content' conn.put_object( Body=more_binary_data, Bucket=test_bucket_name, Key=test_file_key) result = generation_result.get_last_successful_job_for_release_id( release_id) assert result @mock_aws def test_get_last_in_progress_job_for_release_id_success(): """Test get_last_successful_job_for_release_id successful result.""" release_id = 123 timestamp = datetime.date(day=1, month=12, year=2016) job_mock = history.Job( job_id=1, user_id=1, context=[release_id], output_format=salessheets.SINGLE, status=salessheets.REQUESTED, timestamp=timestamp) (flexmock(history) .should_receive('get_job_by_release_id') .with_args(release_id) .and_return(response.Response(job_mock))) conn = s3.connect_to_s3() test_bucket_name = config.SALESSHEETS_S3_BUCKET conn.create_bucket(Bucket=test_bucket_name) test_file_key = '20161201_1_bulk_sales_sheets.pdf' more_binary_data = b'qualitative_content' conn.put_object( Body=more_binary_data, Bucket=test_bucket_name, Key=test_file_key) result = generation_result.get_last_successful_job_for_release_id( release_id) assert result.status == 200 assert result @mock_aws def test_get_job_for_release_id_with_error_status(): """Test get_last_successful_job_for_release_id successful result.""" release_id = 123 timestamp = datetime.date(day=1, month=12, year=2016) job_mock = history.Job( job_id=1, user_id=1, context=[release_id], output_format=salessheets.SINGLE, status=salessheets.STATUS_ERROR, timestamp=timestamp) (flexmock(history) .should_receive('get_job_by_release_id') .with_args(release_id) .and_return(response.Response(job_mock))) conn = s3.connect_to_s3() test_bucket_name = config.SALESSHEETS_S3_BUCKET conn.create_bucket(Bucket=test_bucket_name) test_file_key = '20161201_1_bulk_sales_sheets.pdf' more_binary_data = b'qualitative_content' conn.put_object( Body=more_binary_data, Bucket=test_bucket_name, Key=test_file_key) result = generation_result.get_last_successful_job_for_release_id( release_id) assert result.status == 400 assert not result def test_get_last_successful_job_for_release_id_fail_on_get_data(): """Test get_last_successful_job_for_release_id fail on get data.""" release_id = 123 (flexmock(history) .should_receive('get_job_by_release_id') .with_args(release_id) .and_return(response.create_not_found_response())) result = generation_result.get_last_successful_job_for_release_id( release_id) assert result.status == 404 assert not result def test_get_last_successful_job_for_release_id_fail_connect_to_s3(): """Test get_last_successful_job_for_release_id fail on s3 connection.""" with patch('boto3.Session') as mock_client: # Mock boto3.client to return a client with no credentials mock_client.side_effect = NoCredentialsError() release_id = 123 timestamp = datetime.date(day=1, month=12, year=2016) job_mock = history.Job( job_id=1, user_id=1, context=[release_id], output_format=salessheets.SINGLE, status=salessheets.COMPLETED, timestamp=timestamp) (flexmock(history) .should_receive('get_job_by_release_id') .with_args(release_id) .and_return(response.Response(job_mock))) result = generation_result.get_last_successful_job_for_release_id( release_id) assert not result assert result.status == 500 @mock_aws def test_get_last_successful_job_for_release_id_failed_no_file(): """Test get_last_successful_job_for_release_id. Test for failure due to absence of file. """ release_id = 123 timestamp = datetime.date(day=1, month=12, year=2016) job_mock = history.Job( job_id=1, user_id=1, context=[release_id], output_format=salessheets.SINGLE, status=salessheets.COMPLETED, timestamp=timestamp) (flexmock(history) .should_receive('get_job_by_release_id') .with_args(release_id) .and_return(response.Response(job_mock))) result = generation_result.get_last_successful_job_for_release_id( release_id) assert not result assert result.status == 404 @mock_aws @pytest.mark.parametrize( ('grass_account_type, grass_account_id,' 'product_ownership_status'), [ ('vendor', '123', 200), ('vendor', '123', 403)]) def test_get_job_by_release_id_success_workstation_feature_flag( grass_account_type, grass_account_id, product_ownership_status): """Test get_last_successful_job_for_release_id successful result.""" release_id = 1235 timestamp = datetime.date(day=1, month=12, year=2016) job_mock = history.Job( job_id=1, user_id=1, context=[release_id], output_format=salessheets.SINGLE, status=salessheets.COMPLETED, timestamp=timestamp) (flexmock(history) .should_receive('get_job_by_release_id') .with_args(release_id) .and_return(response.Response(job_mock))) (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type=grass_account_type, account_id=grass_account_id, product_id=str(release_id)) .and_return(response.Response(status=product_ownership_status))) conn = s3.connect_to_s3() test_bucket_name = config.SALESSHEETS_S3_BUCKET conn.create_bucket(Bucket=test_bucket_name) test_file_key = '20161201_1_bulk_sales_sheets.pdf' more_binary_data = b'qualitative_content' conn.put_object( Body=more_binary_data, Bucket=test_bucket_name, Key=test_file_key) result = generation_result.get_last_successful_job_for_release_id( release_id, grass_account_id, grass_account_type) if product_ownership_status != 200: assert not result else: assert result def test_get_history_for_user_depending_on_duplicate_upc_feature_flag(): """Test get_history_for_user for successful result for duplicate_upc.""" expected_jobs_count = 1 expected_items = [0] user_id = '1234' sort_field = field_const.OUTPUT_FORMAT sort_order = field_const.ASC limit = 2 offset = 0 job_display_upc = history.Job( job_id=1, user_id=user_id, output_format=salessheets.ALLOWED_GENERATION_METHODS[0], status=salessheets.REQUESTED, timestamp=datetime.datetime.now(), context=[2], context_type=salessheets.DISPLAY_UPC) job_release_id = history.Job( job_id=2, user_id=user_id, output_format=salessheets.ALLOWED_GENERATION_METHODS[0], status=salessheets.REQUESTED, timestamp=datetime.datetime.now(), context=[2], context_type=salessheets.RELEASE_ID) expected_response = response.Response([job_display_upc, job_release_id]) (flexmock(history) .should_receive('get_jobs_count') .and_return(response.Response(2)) .once()) (flexmock(history) .should_receive('get_all_jobs_by_user_id') .and_return(expected_response)) expected_result = { field_const.ITEMS: [ job_display_upc.as_dict(), job_release_id.as_dict()], field_const.PAGINATION: { field_const.OFFSET: offset, field_const.TOTAL_RECORDS: 2, field_const.LIMIT: limit, field_const.TYPE: field_const.STANDARD, field_const.SORT_ORDER: sort_order, field_const.SORT_BY: sort_field}} result = generation_result.get_history_for_user( user_id, sort_field, sort_order, limit, offset) expected_result['items'] = [ expected_result['items'][item_index] for item_index in expected_items] assert result assert result.message == expected_result assert len(result.message['items']) == expected_jobs_count