Beispiel #1
0
    def test_cache_key(self):
        """Tests that the correct cache key is generated for a given object."""

        assert (utils.Validator(self.file).cache_key ==
                'validation-task:files.File:{0}:None'.format(self.file.pk))

        assert (utils.Validator(self.file_upload, listed=False).cache_key ==
                'validation-task:files.FileUpload:{0}:False'.format(
                    self.file_upload.pk))
Beispiel #2
0
    def test_adds_all_scanners(self, mock_chain):
        self.create_switch('enable-customs', active=True)
        self.create_switch('enable-wat', active=True)
        self.create_switch('enable-yara', active=True)
        file_upload = self.get_upload('webextension.xpi', with_validation=False)
        channel = amo.RELEASE_CHANNEL_LISTED

        utils.Validator(file_upload, listed=True)

        mock_chain.assert_called_once_with(
            tasks.create_initial_validation_results.si(),
            repack_fileupload.s(file_upload.pk),
            tasks.validate_upload.s(file_upload.pk, channel),
            tasks.check_for_api_keys_in_file.s(file_upload.pk),
            chord(
                [
                    tasks.forward_linter_results.s(file_upload.pk),
                    run_yara.s(file_upload.pk),
                    run_customs.s(file_upload.pk),
                    run_wat.s(file_upload.pk),
                ],
                call_mad_api.s(file_upload.pk),
            ),
            tasks.handle_upload_validation_result.s(file_upload.pk, channel, False),
        )
Beispiel #3
0
    def test_appends_final_task_for_files(self, mock_chain):
        final_task = mock.Mock()
        file = version_factory(addon=addon_factory()).files.get()

        utils.Validator(file, final_task=final_task)

        mock_chain.assert_called_once_with(
            tasks.create_initial_validation_results.si(),
            tasks.validate_file.s(file.pk),
            tasks.handle_file_validation_result.s(file.pk),
            final_task,
        )
Beispiel #4
0
    def test_does_not_add_run_wat_when_disabled(self, mock_chain):
        self.create_switch('enable-wat', active=False)
        file_upload = self.get_upload('webextension.xpi',
                                      with_validation=False)
        channel = amo.RELEASE_CHANNEL_LISTED

        utils.Validator(file_upload, listed=True)

        mock_chain.assert_called_once_with(
            tasks.create_initial_validation_results.si(),
            repack_fileupload.s(file_upload.pk),
            tasks.validate_upload.s(file_upload.pk, channel),
            chord([tasks.forward_linter_results.s(file_upload.pk)],
                  tasks.handle_upload_validation_result.s(
                      file_upload.pk, channel, False)),
        )
Beispiel #5
0
    def check_upload(self, file_upload, listed=True):
        """Check that the given new file upload is validated properly."""
        # Run validator.
        utils.Validator(file_upload, listed=listed)

        channel = (amo.RELEASE_CHANNEL_LISTED
                   if listed else amo.RELEASE_CHANNEL_UNLISTED)

        # Make sure we setup the correct validation task.
        self.mock_chain.assert_called_once_with(
            tasks.create_initial_validation_results.si(),
            repack_fileupload.s(file_upload.pk),
            tasks.validate_upload.s(file_upload.pk, channel),
            tasks.handle_upload_validation_result.s(file_upload.pk, channel,
                                                    False),
        )
Beispiel #6
0
    def test_appends_final_task_for_file_uploads(self, mock_chain):
        final_task = mock.Mock()
        file_upload = self.get_upload('webextension.xpi',
                                      with_validation=False)
        channel = amo.RELEASE_CHANNEL_LISTED

        utils.Validator(file_upload, listed=True, final_task=final_task)

        mock_chain.assert_called_once_with(
            tasks.create_initial_validation_results.si(),
            repack_fileupload.s(file_upload.pk),
            tasks.validate_upload.s(file_upload.pk, channel),
            tasks.handle_upload_validation_result.s(file_upload.pk, channel,
                                                    False),
            final_task,
        )
Beispiel #7
0
    def check_file(self, file_):
        """Check that the given file is validated properly."""
        # Run validator.
        utils.Validator(file_)

        # We shouldn't be attempting to call validate_upload task when
        # dealing with a file.
        assert not self.validate_upload.called

        # Make sure we run the correct validation task and we set up an error
        # handler.
        self.validate_file.assert_called_once_with(file_.pk)
        assert self.validate_file.return_value.on_error.called

        # Make sure we run the correct save validation task.
        self.save_file.assert_called_once_with(file_.pk, file_.version.channel,
                                               False)
Beispiel #8
0
    def check_file(self, file_):
        """Check that the given file is validated properly."""
        # Run validator.
        utils.Validator(file_)

        # We shouldn't be attempting to validate a bare upload.
        assert not self.validate_upload.called

        # Make sure we run the correct validation task.
        self.validate_file.assert_called_once_with([file_.pk])

        # Make sure we run the correct save validation task, with a
        # fallback error handler.
        self.save_file.assert_has_calls([
            mock.call([mock.ANY, file_.pk, file_.version.channel, False],
                      immutable=True),
            mock.call([file_.pk, file_.version.channel, False],
                      link_error=mock.ANY)])
Beispiel #9
0
    def test_create_file_upload_tasks(self):
        self.create_switch('enable-customs', active=True)
        self.create_switch('enable-wat', active=True)
        self.create_switch('enable-yara', active=True)
        file_upload = self.get_upload(
            'webextension.xpi', with_validation=False
        )
        channel = amo.RELEASE_CHANNEL_LISTED
        validator = utils.Validator(file_upload, listed=True)

        tasks = validator.create_file_upload_tasks(
            upload_pk=file_upload.pk, channel=channel, is_mozilla_signed=False
        )

        assert isinstance(tasks, list)

        expected_tasks = [
            'olympia.devhub.tasks.create_initial_validation_results',
            'olympia.files.tasks.repack_fileupload',
            'olympia.devhub.tasks.validate_upload',
            'olympia.devhub.tasks.check_for_api_keys_in_file',
            'celery.chord',
            'olympia.devhub.tasks.handle_upload_validation_result',
        ]
        assert len(tasks) == len(expected_tasks)
        assert expected_tasks == [task.name for task in tasks]

        scanners_chord = tasks[4]

        expected_parallel_tasks = [
            'olympia.devhub.tasks.forward_linter_results',
            'olympia.scanners.tasks.run_yara',
            'olympia.scanners.tasks.run_customs',
            'olympia.scanners.tasks.run_wat',
        ]
        assert len(scanners_chord.tasks) == len(expected_parallel_tasks)
        assert (expected_parallel_tasks == [task.name for task in
                                            scanners_chord.tasks])
        # Callback
        assert (
            scanners_chord.body.name == 'olympia.scanners.tasks.call_mad_api'
        )
Beispiel #10
0
    def check_file(self, file_):
        """Check that the given file is validated properly."""
        # Mock tasks that we should not execute.
        repack_fileupload = self.patch('olympia.files.tasks.repack_fileupload')
        validate_upload = self.patch('olympia.devhub.tasks.validate_upload')

        # Run validator.
        utils.Validator(file_)

        # We shouldn't be attempting to call the `validate_upload` tasks when
        # dealing with a file.
        assert not repack_fileupload.called
        assert not validate_upload.called

        # Make sure we setup the correct validation task.
        self.mock_chain.assert_called_once_with(
            tasks.create_initial_validation_results.si(),
            tasks.validate_file.s(file_.pk),
            tasks.handle_file_validation_result.s(file_.pk),
        )
Beispiel #11
0
    def check_upload(self, file_upload, listed=True):
        """Check that the given new file upload is validated properly."""
        # Run validator.
        utils.Validator(file_upload, listed=listed)

        # We shouldn't be attempting to call validate_file task when dealing
        # with an upload.
        assert not self.validate_file.called

        channel = (amo.RELEASE_CHANNEL_LISTED
                   if listed else amo.RELEASE_CHANNEL_UNLISTED)

        # Make sure we run the correct validation task for the upload and we
        # set up an error handler.
        self.validate_upload.assert_called_once_with(file_upload.pk,
                                                     channel=channel)
        assert self.validate_upload.return_value.on_error.called

        # Make sure we run the correct save validation task.
        self.save_upload.assert_called_once_with(file_upload.pk, channel,
                                                 False)
Beispiel #12
0
    def check_upload(self, file_upload, listed=True):
        """Check that the given new file upload is validated properly."""
        # Run validator.
        utils.Validator(file_upload, listed=listed)

        # We shouldn't be attempting to validate an existing file.
        assert not self.validate_file.called

        # Make sure we run the correct validation task for the upload.
        self.validate_upload.assert_called_once_with(
            [file_upload.path],
            {'hash_': file_upload.hash, 'listed': listed,
             'is_webextension': False})

        # Make sure we run the correct save validation task, with a
        # fallback error handler.
        channel = (amo.RELEASE_CHANNEL_LISTED if listed
                   else amo.RELEASE_CHANNEL_UNLISTED)
        self.save_upload.assert_has_calls([
            mock.call([mock.ANY, file_upload.pk, channel],
                      immutable=True),
            mock.call([file_upload.pk, channel], link_error=mock.ANY)])