Beispiel #1
0
    def check_upload(self, file_, listed=None):
        """Check that our file upload is matched to the given file."""

        # Create an annotator, make sure it matches the expected older file.
        va = utils.ValidationAnnotator(self.file_upload)
        assert va.prev_file == file_

        # Make sure we run the correct validation task for the matched file,
        # if there is a match.
        if file_:
            self.validate_file.assert_called_once_with([file_.pk], {
                'hash_': file_.original_hash,
                'is_webextension': False
            })
        else:
            assert not self.validate_file.called

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

        # Make sure we run the correct save validation task, with a
        # fallback error handler.
        self.save_upload.assert_has_calls([
            mock.call([mock.ANY, self.file_upload.pk], {'annotate': False},
                      immutable=True),
            mock.call([self.file_upload.pk], link_error=mock.ANY)
        ])
Beispiel #2
0
    def test_update_annotations(self):
        """Test that annotations are correctly copied from an old file to a new
        one."""

        HASH_0 = 'xxx'
        HASH_1 = 'yyy'

        RESULTS = deepcopy(amo.VALIDATOR_SKELETON_RESULTS)
        RESULTS['messages'] = [
            {
                'id': ['foo'],
                'context': ['foo'],
                'file': 'foo'
            },
            {
                'id': ['baz'],
                'context': ['baz'],
                'file': 'baz'
            },
        ]

        self.file.update(original_hash=HASH_0)
        self.file_1_1.update(original_hash=HASH_1)

        # Attach the validation results to our previous version's file,
        # and update the object's cached foreign key value.
        self.file.validation = FileValidation.objects.create(
            file=self.file_1_1, validation=json.dumps(RESULTS))

        def annotation(hash_, key, **kw):
            return ValidationAnnotation(file_hash=hash_, message_key=key, **kw)

        def key(metasyntatic_variable):
            """Return an arbitrary, but valid, message key for the given
            arbitrary string."""
            return '[["{0}"], ["{0}"], "{0}", null, false]'.format(
                metasyntatic_variable)

        # Create two annotations which match the above messages, and
        # one which does not.
        ValidationAnnotation.objects.bulk_create((
            annotation(HASH_0, key('foo'), ignore_duplicates=True),
            annotation(HASH_0, key('bar'), ignore_duplicates=True),
            annotation(HASH_0, key('baz'), ignore_duplicates=False),
        ))

        # Create the annotator and make sure it links our target
        # file to the previous version.
        annotator = utils.ValidationAnnotator(self.file_1_1)
        assert annotator.prev_file == self.file

        annotator.update_annotations()

        # The two annotations which match messages in the above
        # validation results should be duplicated for this version.
        # The third annotation should not.
        assert (set(
            ValidationAnnotation.objects.filter(file_hash=HASH_1).values_list(
                'message_key', 'ignore_duplicates')) == set(
                    ((key('foo'), True), (key('baz'), False))))
    def check_file(self, file_new, file_old):
        """Check that the given new file is matched to the given old file."""

        # Create an annotator, make sure it matches the expected older file.
        va = utils.ValidationAnnotator(file_new)
        assert va.prev_file == file_old

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

        # Make sure we run the correct validation tasks for both files,
        # or only one validation task if there's no match.
        if file_old:
            self.validate_file.assert_has_calls([
                mock.call([file_new.pk], {
                    'hash_': file_new.original_hash,
                    'is_webextension': False}),
                mock.call([file_old.pk], {
                    'hash_': file_old.original_hash,
                    'is_webextension': False})
            ])
        else:
            self.validate_file.assert_called_once_with(
                [file_new.pk],
                {'hash_': file_new.original_hash, 'is_webextension': False})

        # 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_new.pk], {'annotate': False},
                      immutable=True),
            mock.call([file_new.pk], link_error=mock.ANY)])