def test_task_makes_http_request( self, update_consent_service_feature_flag, requests_mock, email_address, accepts_dit_email_marketing, modified_at, ): """ Ensure correct http request with correct payload is generated when task executes. """ matcher = requests_mock.post('/api/v1/person/', json={}, status_code=status.HTTP_201_CREATED) update_contact_consent( email_address, accepts_dit_email_marketing, modified_at=modified_at, ) assert matcher.called_once expected = { 'email': email_address, 'consents': ['email_marketing'] if accepts_dit_email_marketing else [], } if modified_at: expected['modified_at'] = modified_at assert matcher.last_request.json() == expected
def test_not_configured_error(self, ): """ Test that if feature flag is enabled, but environment variables are not set then task will throw exception """ with pytest.raises(ImproperlyConfigured): update_contact_consent('*****@*****.**', True)
def test_task_doesnt_retry_on_other_exception( self, mock_post, mock_retry, ): """ Test to ensure that celery raises on non-requests exception """ with pytest.raises(Exception): update_contact_consent('*****@*****.**', True) assert mock_post.called assert not mock_retry.called
def test_task_retries_on_connect_timeout( self, mock_post, mock_retry, ): """ Test to ensure that celery retries on connect timeout """ with pytest.raises(Retry): update_contact_consent('*****@*****.**', True) assert mock_post.called assert isinstance(mock_retry.call_args.kwargs['exc'], ConnectTimeout)
def test_task_retries_on_request_exceptions( self, mock_retry, update_consent_service_feature_flag, requests_mock, status_code, ): """ Test to ensure that celery retries on request exceptions like 5xx, 404 """ matcher = requests_mock.post('/api/v1/person/', json={}, status_code=status_code) with pytest.raises(Retry): update_contact_consent('*****@*****.**', True) assert matcher.called_once assert mock_retry.call_args.kwargs[ 'exc'].response.status_code == status_code