Ejemplo n.º 1
0
    def test_run_with_mocks(self, requests_mock, incr_mock):
        upload = self.get_upload('webextension.xpi')
        scanner_data = {'some': 'results'}
        requests_mock.return_value = self.create_response(data=scanner_data)
        assert len(ScannersResult.objects.all()) == 0

        run_scanner(upload.pk,
                    scanner=self.FAKE_SCANNER,
                    api_url=self.API_URL,
                    api_key=self.API_KEY)

        assert requests_mock.called
        requests_mock.assert_called_with(
            url=self.API_URL,
            json={
                'api_key': self.API_KEY,
                'download_url': upload.get_authenticated_download_url(),
            },
            timeout=123)
        result = ScannersResult.objects.all()[0]
        assert result.upload == upload
        assert result.scanner == self.FAKE_SCANNER
        assert result.results == scanner_data
        scanner_name = self.MOCK_SCANNERS.get(self.FAKE_SCANNER)
        assert incr_mock.called
        incr_mock.assert_called_with('devhub.{}.success'.format(scanner_name))
Ejemplo n.º 2
0
    def test_skip_non_xpi_files(self):
        upload = self.get_upload('search.xml')

        run_scanner(upload.pk,
                    scanner=self.FAKE_SCANNER,
                    api_url=self.API_URL,
                    api_key=self.API_KEY)

        assert len(ScannersResult.objects.all()) == 0
Ejemplo n.º 3
0
    def test_calls_statsd_timer(self, requests_mock, timer_mock):
        upload = self.get_upload('webextension.xpi')
        requests_mock.return_value = self.create_response()

        run_scanner(upload.pk,
                    scanner=self.FAKE_SCANNER,
                    api_url=self.API_URL,
                    api_key=self.API_KEY)

        assert timer_mock.called
        scanner_name = self.MOCK_SCANNERS.get(self.FAKE_SCANNER)
        timer_mock.assert_called_with('devhub.{}'.format(scanner_name))
Ejemplo n.º 4
0
    def test_run_does_not_raise(self, incr_mock):
        upload = self.get_upload('webextension.xpi')

        # This call should not raise even though there will be an error because
        # `api_url` is `None`.
        run_scanner(upload.pk,
                    scanner=self.FAKE_SCANNER,
                    api_url=None,
                    api_key='does-not-matter')

        scanner_name = self.MOCK_SCANNERS.get(self.FAKE_SCANNER)
        assert incr_mock.called
        incr_mock.assert_called_with('devhub.{}.failure'.format(scanner_name))
Ejemplo n.º 5
0
    def test_handles_scanner_errors_with_mocks(self, requests_mock):
        upload = self.get_upload('webextension.xpi')
        scanner_data = {'error': 'some error'}
        requests_mock.return_value = self.create_response(data=scanner_data)
        assert len(ScannersResult.objects.all()) == 0

        run_scanner(upload.pk,
                    scanner=self.FAKE_SCANNER,
                    api_url=self.API_URL,
                    api_key=self.API_KEY)

        assert requests_mock.called
        assert len(ScannersResult.objects.all()) == 0
Ejemplo n.º 6
0
    def test_handles_http_errors_with_mock(self, requests_mock):
        upload = self.get_upload('webextension.xpi')
        requests_mock.return_value = self.create_response(
            status_code=504, data={'message': 'http timeout'})
        assert len(ScannersResult.objects.all()) == 0

        run_scanner(upload.pk,
                    scanner=self.FAKE_SCANNER,
                    api_url=self.API_URL,
                    api_key=self.API_KEY)

        assert requests_mock.called
        assert len(ScannersResult.objects.all()) == 0
Ejemplo n.º 7
0
    def test_throws_errors_with_mocks(self, requests_mock):
        scanner_data = {'error': 'some error'}
        requests_mock.return_value = self.create_response(data=scanner_data)
        assert len(ScannerResult.objects.all()) == 0

        with self.assertRaises(ValueError):
            run_scanner(
                self.results,
                self.upload.pk,
                scanner=self.FAKE_SCANNER,
                api_url=self.API_URL,
                api_key=self.API_KEY,
            )

        assert requests_mock.called
        assert len(ScannerResult.objects.all()) == 0
Ejemplo n.º 8
0
    def test_run_with_mocks(self, requests_mock, incr_mock):
        rule = ScannerRule.objects.create(name='r', scanner=self.FAKE_SCANNER)
        scanner_data = {'matchedRules': [rule.name]}
        requests_mock.return_value = self.create_response(data=scanner_data)
        assert len(ScannerResult.objects.all()) == 0

        returned_results = run_scanner(
            self.results,
            self.upload.pk,
            scanner=self.FAKE_SCANNER,
            api_url=self.API_URL,
            api_key=self.API_KEY,
        )

        assert requests_mock.called
        requests_mock.assert_called_with(
            url=self.API_URL,
            json={
                'api_key': self.API_KEY,
                'download_url': self.upload.get_authenticated_download_url(),
            },
            timeout=123,
        )
        result = ScannerResult.objects.all()[0]
        assert result.upload == self.upload
        assert result.scanner == self.FAKE_SCANNER
        assert result.results == scanner_data
        scanner_name = self.MOCK_SCANNERS.get(self.FAKE_SCANNER)
        assert incr_mock.called
        assert incr_mock.call_count == 2
        incr_mock.assert_has_calls([
            mock.call('devhub.{}.has_matches'.format(scanner_name)),
            mock.call('devhub.{}.success'.format(scanner_name))
        ])
        assert returned_results == self.results
Ejemplo n.º 9
0
    def test_calls_statsd_timer(self, requests_mock, timer_mock):
        requests_mock.return_value = self.create_response()

        returned_results = run_scanner(
            self.results,
            self.upload.pk,
            scanner=self.FAKE_SCANNER,
            api_url=self.API_URL,
            api_key=self.API_KEY,
        )

        assert timer_mock.called
        scanner_name = self.MOCK_SCANNERS.get(self.FAKE_SCANNER)
        timer_mock.assert_called_with('devhub.{}'.format(scanner_name))
        assert returned_results == self.results
Ejemplo n.º 10
0
    def test_run_does_not_raise(self, incr_mock):
        # This call should not raise even though there will be an error because
        # `api_url` is `None`.
        returned_results = run_scanner(
            self.results,
            self.upload.pk,
            scanner=self.FAKE_SCANNER,
            api_url=None,
            api_key='does-not-matter',
        )

        scanner_name = self.MOCK_SCANNERS.get(self.FAKE_SCANNER)
        assert incr_mock.called
        incr_mock.assert_called_with('devhub.{}.failure'.format(scanner_name))
        assert returned_results == self.results
Ejemplo n.º 11
0
    def test_handles_http_errors_with_mock(self, requests_mock):
        requests_mock.return_value = self.create_response(
            status_code=504, data={'message': 'http timeout'})
        assert len(ScannerResult.objects.all()) == 0

        returned_results = run_scanner(
            self.results,
            self.upload.pk,
            scanner=self.FAKE_SCANNER,
            api_url=self.API_URL,
            api_key=self.API_KEY,
        )

        assert requests_mock.called
        assert len(ScannerResult.objects.all()) == 0
        assert returned_results == self.results
Ejemplo n.º 12
0
    def test_run_does_not_raise(self, incr_mock):
        self.create_switch('ignore-exceptions-in-scanner-tasks', active=True)
        # This call should not raise even though there will be an error because
        # `api_url` is `None`.
        returned_results = run_scanner(
            self.results,
            self.upload.pk,
            scanner=self.FAKE_SCANNER,
            api_url=None,
            api_key='does-not-matter',
        )

        scanner_name = self.MOCK_SCANNERS.get(self.FAKE_SCANNER)
        assert incr_mock.called
        incr_mock.assert_called_with(f'devhub.{scanner_name}.failure')
        assert returned_results == self.results
Ejemplo n.º 13
0
    def test_handles_scanner_errors_with_mocks(self, requests_mock):
        self.create_switch('ignore-exceptions-in-scanner-tasks', active=True)
        scanner_data = {'error': 'some error'}
        requests_mock.return_value = self.create_response(data=scanner_data)
        assert len(ScannerResult.objects.all()) == 0

        returned_results = run_scanner(
            self.results,
            self.upload.pk,
            scanner=self.FAKE_SCANNER,
            api_url=self.API_URL,
            api_key=self.API_KEY,
        )

        assert requests_mock.called
        assert len(ScannerResult.objects.all()) == 0
        assert returned_results == self.results
Ejemplo n.º 14
0
    def test_skip_non_webextensions(self):
        upload = self.get_upload('search.xml')
        results = {
            **amo.VALIDATOR_SKELETON_RESULTS,
            'metadata': {'is_webextension': False},
        }

        returned_results = run_scanner(
            results,
            upload.pk,
            scanner=self.FAKE_SCANNER,
            api_url=self.API_URL,
            api_key=self.API_KEY,
        )

        assert len(ScannerResult.objects.all()) == 0
        assert returned_results == results