def test__upload_hc_dpd_data__accessible_by_url(user): """Tests that endpoint exists at expected URL.""" # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Setup Data data = { 'data': [{ 'drug_code': 1, 'extract_data': { 'active_ingredient': [{'drug_code': 1, 'active_ingredient_code': 'A'}], }, }] } # Set up client and response client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.post( '/api/hc-dpd/v1/upload/', data=data, format='json', ) assert response.status_code == 201
def test__upload_hc_dpd_data__invalid_post___no_data(user): """Tests that proper response is returned for no post data.""" # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Set up client and response client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.post( reverse('api:hc_dpd_v1:upload_hc_dpd_data'), data={}, format='json', ) content = json.loads(response.content) # Confirm status code assert response.status_code == 400 # Confirm response details are received as expected assert 'errors' in content assert isinstance(content['errors'], dict) assert 'non_field' in content['errors'] assert isinstance(content['errors']['non_field'], list) assert len(content['errors']['non_field']) == 0 assert 'field' in content['errors'] assert isinstance(content['errors']['field'], dict) assert content['errors']['field'] == {'data': ['This field is required.']} assert 'status_code' in content assert content['status_code'] == 400
def test__upload_hc_dpd_data__201_response_on_user_with_edit_permissions(user): """Test for 201 response on user with view permission.""" # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Setup Data data = { 'data': [{ 'drug_code': 1, 'extract_data': { 'active_ingredient': [{'drug_code': 1, 'active_ingredient_code': 'A'}], }, }] } # Set up client and response client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.post( reverse('api:hc_dpd_v1:upload_hc_dpd_data'), data=data, format='json', ) assert response.status_code == 201
def test__upload_hc_dpd_data__valid_post(user): """Tests that proper response is returned.""" # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Set up client and response client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.post( reverse('api:hc_dpd_v1:upload_hc_dpd_data'), data=utils.UPLOAD_ALL_DATA, format='json', ) content = json.loads(response.content) # Confirm status code assert response.status_code == 201 # Confirm response details are received as expected assert 'message' in content assert isinstance(content['message'], list) assert len(content['message']) == 13 assert 'status_code' in content assert content['status_code'] == 201
def test__has_dpd_edit_access__user_with_permission(user): """Confirms user with permission passes permission check.""" # Add permission to user utils.add_api_edit_permission(user) permission = permissions.HasDPDEditAccess() request = RequestFactory() request.user = user assert permission.has_permission(request, None) is True
def test__checksum_list__200_response_on_user_with_edit_permissions(user): """Test for 200 response on user with view permission.""" # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Set up client and response client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.get( reverse('api:hc_dpd_v1:checksum_list'), data={'step': 1, 'source': 'active_ingredient'}, ) assert response.status_code == 200
def test__checksum_test__accessible_by_url(user): """Tests that endpoint exists at expected URL.""" # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Set up client and response client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.post( '/api/hc-dpd/v1/checksum/test/', {'active_ingredient': [{'drug_code': 1, 'active_ingredient_code': 'A'}]}, format='json', ) assert response.status_code == 200
def test__upload_hc_dpd_data__invalid_method(user): """Tests that proper response is returned for invalid method (GET).""" # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Set up client and response client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.get( reverse('api:hc_dpd_v1:upload_hc_dpd_data'), data=utils.UPLOAD_ALL_DATA, format='json', ) content = json.loads(response.content) # Confirm status code assert response.status_code == 405 # Confirm message assert content == {'detail': 'Method "GET" not allowed.'}
def test__checksum_list__invalid_method(user): """Tests that proper response is returned for invalid method (POST).""" # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Set up client and response data = {'step': 1, 'source': 'active_ingredient'} client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.post( reverse('api:hc_dpd_v1:checksum_list'), data=data, format='json', ) content = json.loads(response.content) # Confirm status code assert response.status_code == 405 # Confirm message assert content == {'detail': 'Method "POST" not allowed.'}
def test__checksum_list__pagination(user): """Test for proper details for paginated response.""" # Create enough DPDChecksum instances for testing for i in range(1, 2001): models.DPDChecksum.objects.create( drug_code_start=i, drug_code_step=1, extract_source='active_ingredient', checksum=i, ) # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Set up client and response data = {'step': 1, 'source': 'active_ingredient'} client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.get( reverse('api:hc_dpd_v1:checksum_list'), data=data, format='json', ) content = json.loads(response.content) # Confirm status code assert response.status_code == 200 # Confirm response details assert 'count' in content assert content['count'] == 2000 assert 'next' in content assert '/api/hc-dpd/v1/checksum/?page=2&source=active_ingredient&step=1' in content['next'] assert 'previous' in content assert content['previous'] is None assert 'results' in content assert isinstance(content['results'], list) assert len(content['results']) == 1000
def test__checksum_list__invalid_data(user): """Tests proper response is returned for invalid data.""" # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Set up client and response data = {'step': 0, 'source': 'active_ingredient'} client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.get( reverse('api:hc_dpd_v1:checksum_list'), data=data, format='json', ) content = json.loads(response.content) # Confirm status code assert response.status_code == 400 # Confirm response details assert content == {'step': ['"0" is not a valid choice.']}
def test__checksum_list__valid_data(user, hc_dpd_checksum_active_ingredient): """Tests proper response is returned for valid data.""" checksum = hc_dpd_checksum_active_ingredient # Create token and add user permissions token = create_token(user) utils.add_api_edit_permission(user) # Set up client and response data = {'step': checksum.drug_code_step, 'source': checksum.extract_source} client = APIClient() client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.get( reverse('api:hc_dpd_v1:checksum_list'), data=data, format='json', ) content = json.loads(response.content) # Confirm status code assert response.status_code == 200 # Confirm response details assert 'count' in content assert content['count'] == 1 assert 'next' in content assert content['next'] is None assert 'previous' in content assert content['previous'] is None assert 'results' in content assert content['results'] == [{ 'drug_code_start': checksum.drug_code_start, 'drug_code_step': checksum.drug_code_step, 'extract_source': checksum.extract_source, 'checksum': str(checksum.checksum), 'checksum_date': checksum.checksum_date.strftime('%Y-%m-%d'), }]