def test_cache_key(self): """Tests that the correct cache key is generated for a given object.""" assert (utils.ValidationAnnotator(self.file).cache_key == 'validation-task:files.File:{0}:None'.format(self.file.pk)) assert (utils.ValidationAnnotator(self.file_upload, listed=False).cache_key == 'validation-task:files.FileUpload:{0}:False'.format( self.file_upload.pk))
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, file_new.original_hash]), mock.call([file_old.pk, file_old.original_hash]) ]) else: self.validate_file.assert_called_once_with( [file_new.pk, file_new.original_hash]) # 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) ])
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, file_.original_hash]) 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, self.file_upload.hash, listed]) # 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) ])
def test_match_beta_to_release(self): """Test that a beta submission is matched to the latest approved release version.""" va = utils.ValidationAnnotator(self.file_upload) assert va.find_previous_version(self.xpi_version) == self.file self.validate_file.assert_called_once_with([self.file.pk])
def test_match_beta_to_unsigned_beta(self): """Test that a beta submission is not matched to a prior unsigned beta version.""" self.file_1_1.update(status=amo.STATUS_BETA) self.version_1_1.update(version='1.1b0') va = utils.ValidationAnnotator(self.file_upload) assert va.find_previous_version(self.xpi_version) == self.file self.validate_file.assert_called_once_with([self.file.pk])
def test_find_file_prev_version(self): """Test that the correct previous version is found for a File.""" va = utils.ValidationAnnotator(self.file_1_1) assert va.find_previous_version(self.xpi_version) == self.file assert not self.validate_upload.called self.validate_file.assert_has_calls([mock.call([self.file_1_1.pk]), mock.call([self.file.pk])]) self.save_file.assert_called_once_with([self.file_1_1.pk])
def test_full_to_unreviewed(self): """Test that a full reviewed version is not matched to an unreviewed version.""" self.file_1_1.update(status=amo.STATUS_UNREVIEWED) va = utils.ValidationAnnotator(self.file_upload) assert va.find_previous_version(self.xpi_version) == self.file self.validate_file.assert_called_once_with([self.file.pk]) self.validate_upload.assert_called_once_with( [self.file_upload.pk, None]) self.save_upload.assert_called_once_with([self.file_upload.pk])
def test_find_future_file(self): """Test that a future version will not be matched.""" self.version.update(version='1.2') va = utils.ValidationAnnotator(self.file_1_1) assert va.find_previous_version(self.xpi_version) is None assert not self.validate_upload.called self.validate_file.assert_called_once_with([self.file_1_1.pk]) self.save_file.assert_called_once_with([self.file_1_1.pk])
def test_find_fileupload_prev_version(self): """Test that the correct previous version is found for a new upload.""" va = utils.ValidationAnnotator(self.file_upload) assert va.find_previous_version(self.xpi_version) == self.file self.validate_file.assert_called_once_with([self.file.pk]) self.validate_upload.assert_called_once_with( [self.file_upload.pk, None]) self.save_upload.assert_called_once_with([self.file_upload.pk])
def test_full_to_full_fileupload(self): """Test that a full reviewed version is matched to the nearest full reviewed version.""" self.version_1_1.update(version='1.0.1') self.file_1_1.update(status=amo.STATUS_PUBLIC) va = utils.ValidationAnnotator(self.file_upload) assert va.find_previous_version(self.xpi_version) == self.file_1_1 self.validate_file.assert_called_once_with([self.file_1_1.pk]) self.validate_upload.assert_called_once_with( [self.file_upload.pk, None]) self.save_upload.assert_called_once_with([self.file_upload.pk])
def test_full_to_full_file(self): """Test that a full reviewed version is matched to the nearest full reviewed version.""" self.file_1_1.update(status=amo.STATUS_PUBLIC) va = utils.ValidationAnnotator(self.file_1_1) assert va.find_previous_version(self.xpi_version) == self.file self.validate_file.assert_has_calls([mock.call([self.file_1_1.pk]), mock.call([self.file.pk])]) self.save_file.assert_called_once_with([self.file_1_1.pk]) for status in amo.STATUS_UNREVIEWED, amo.STATUS_LITE, amo.STATUS_BETA: self.validate_file.reset_mock() self.save_file.reset_mock() self.file.update(status=status) va = utils.ValidationAnnotator(self.file_1_1) assert va.find_previous_version(self.xpi_version) is None self.validate_file.assert_called_once_with([self.file_1_1.pk]) self.save_file.assert_called_once_with([self.file_1_1.pk])