"""Functional tests around rejection notes.""" import json import pytest from product_workflow.constants import header as header_constants from tests.factories import rejection_notes as rejection_notes_factory from tests.factories import release_approval_queue \ as release_approval_queue_factory from tests.testutils import db @pytest.fixture def release_approval_id(): """Create a release id for testing.""" return 123 def rejection_items(rejection_notes): """Convert language objects to dicts.""" return [notes.to_dict() for notes in rejection_notes] @db.test_schema def test_get_rejection_notes_success( client, valid_headers, release_approval_id): """Test successful status and response payload.""" rejection_notes = rejection_notes_factory.RejectionNoteFactory.build_batch( 3, release_approval_id=release_approval_id) db.seed_models(rejection_notes) expected_payload = json.dumps({'items': rejection_items(rejection_notes)}) api_response = client.get( '/release-approval/{}/rejections'.format( release_approval_id), headers=valid_headers) assert api_response.status_code == 200 assert api_response.data.decode('utf-8') == expected_payload def test_get_rejection_notes_missing_correlation_id( client, release_approval_id): """Test that an error is returned if Correlation-Id header is missing.""" api_response = client.get( '/release-approval/{}/rejections'.format( release_approval_id)) response_body = json.loads(api_response.data.decode('utf-8')) assert api_response.status_code == 400 assert header_constants.CORRELATION_ID in response_body['message'] @db.test_schema def test_update_rejection_notes_success(client, valid_headers): """Test successful status and response payload.""" release_approval_queue = release_approval_queue_factory \ .ReleaseApprovalQueueFactory \ .build(release_approval_id=123) rejections = rejection_notes_factory.RejectionNoteFactory.build_batch( 3, release_approval_id=release_approval_queue.release_approval_id) db.seed_models(rejections + [release_approval_queue]) for note in rejections: setattr(note, 'corrected', 'Y') expected_payload = json.dumps({'rejections': rejection_items(rejections)}) api_response = client.put( '/release-approval/rejections', headers=valid_headers, data=expected_payload) assert api_response.status_code == 200 assert api_response.data.decode('utf-8') == expected_payload