def test_call_with_mocks(self, requests_mock, incr_mock, timer_mock): ml_results = {'some': 'results'} requests_mock.return_value = self.create_response(data=ml_results) assert len(ScannerResult.objects.all()) == self.default_results_count returned_results = call_ml_api(self.results, self.upload.pk) assert requests_mock.called requests_mock.assert_called_with( url=settings.ML_API_URL, json={'customs': self.customs_result.results}, timeout=settings.ML_API_TIMEOUT, ) assert (len( ScannerResult.objects.all()) == self.default_results_count + 1) last_result = ScannerResult.objects.latest() assert last_result.upload == self.upload assert last_result.scanner == ML_API assert last_result.results == ml_results assert returned_results == self.results[0] assert incr_mock.called assert incr_mock.call_count == 1 incr_mock.assert_has_calls([mock.call('devhub.ml_api.success')]) assert timer_mock.called timer_mock.assert_called_with('devhub.ml_api')
def test_skip_non_webextensions(self): upload = self.get_upload('search.xml') results = { **amo.VALIDATOR_SKELETON_RESULTS, 'metadata': {'is_webextension': False}, } returned_results = call_ml_api([results], upload.pk) assert len(ScannerResult.objects.all()) == self.default_results_count assert returned_results == results
def test_handles_non_200_http_responses(self, requests_mock, incr_mock): requests_mock.return_value = self.create_response( status_code=504, data={'message': 'http timeout'}) returned_results = call_ml_api(self.results, self.upload.pk) assert requests_mock.called assert len(ScannerResult.objects.all()) == self.default_results_count assert returned_results == self.results[0] assert incr_mock.called assert incr_mock.call_count == 1 incr_mock.assert_has_calls([mock.call('devhub.ml_api.failure')])
def test_handles_non_json_responses(self, requests_mock, incr_mock): response = mock.Mock(status_code=200) response.json.side_effect = ValueError('not json') requests_mock.return_value = response returned_results = call_ml_api(self.results, self.upload.pk) assert requests_mock.called assert len(ScannerResult.objects.all()) == self.default_results_count assert returned_results == self.results[0] assert incr_mock.called assert incr_mock.call_count == 1 incr_mock.assert_has_calls([mock.call('devhub.ml_api.failure')])
def test_does_not_run_when_switch_is_off(self, requests_mock): self.create_switch('enable-scanner-ml-api-call', active=False) call_ml_api(self.results, self.upload.pk) assert not requests_mock.called