def test_not_enough_files_error(self, create_summary_for_version_mock): create_summary_for_version_mock.side_effect = ( AutoApprovalNotEnoughFilesError) call_command('auto_approve') assert get_reviewing_cache(self.addon.pk) is None assert create_summary_for_version_mock.call_count == 1 self._check_stats({'total': 1, 'error': 1})
def test_failed_verdict(self, create_summary_for_version_mock): fake_verdict_info = { 'uses_custom_csp': True, 'uses_native_messaging': True, 'uses_content_script_for_all_urls': True, 'too_many_average_daily_users': True, 'too_few_approved_updates': True, } create_summary_for_version_mock.return_value = ( AutoApprovalSummary(verdict=amo.NOT_AUTO_APPROVED), fake_verdict_info) call_command('auto_approve') assert create_summary_for_version_mock.call_args == ( (self.version, ), {'max_average_daily_users': 10000, 'min_approved_updates': 1, 'dry_run': False}) assert get_reviewing_cache(self.addon.pk) is None self._check_stats({ 'total': 1, 'uses_custom_csp': 1, 'uses_native_messaging': 1, 'uses_content_script_for_all_urls': 1, 'too_many_average_daily_users': 1, 'too_few_approved_updates': 1, })
def test_no_validation_result(self, create_summary_for_version_mock): create_summary_for_version_mock.side_effect = ( AutoApprovalNoValidationResultError) call_command('auto_approve') assert get_reviewing_cache(self.addon.pk) is None assert create_summary_for_version_mock.call_count == 1 self._check_stats({'total': 1, 'error': 1})
def test_full(self, sign_file_mock): # Simple integration test with as few mocks as possible. assert not AutoApprovalSummary.objects.exists() assert not self.file.reviewed ActivityLog.objects.all().delete() self.author = user_factory() self.addon.addonuser_set.create(user=self.author) call_command('auto_approve', '--dry-run') call_command('auto_approve') self.addon.reload() self.file.reload() assert AutoApprovalSummary.objects.count() == 1 assert AutoApprovalSummary.objects.get(version=self.version) assert get_reviewing_cache(self.addon.pk) is None assert self.addon.status == amo.STATUS_PUBLIC assert self.file.status == amo.STATUS_PUBLIC assert self.file.reviewed assert ActivityLog.objects.count() activity_log = ActivityLog.objects.latest('pk') assert activity_log.action == amo.LOG.APPROVE_VERSION.id assert sign_file_mock.call_count == 1 assert sign_file_mock.call_args[0][0] == self.file assert len(mail.outbox) == 1 msg = mail.outbox[0] assert msg.to == [self.author.email] assert msg.from_email == settings.EDITORS_EMAIL assert msg.subject == 'Mozilla Add-ons: %s %s Approved' % (unicode( self.addon.name), self.version.version)
def test_already_locked(self, create_summary_for_version_mock): # Test that when an add-on is locked, we handle that gracefully, not # touching it. set_reviewing_cache(self.addon.pk, 666) call_command('auto_approve') assert get_reviewing_cache(self.addon.pk) == 666 assert create_summary_for_version_mock.call_count == 0 self._check_stats({'total': 1, 'locked': 1})
def test_full(self): # Simple integration test. assert not AutoApprovalSummary.objects.exists() call_command('auto_approve', '--dry-run') call_command('auto_approve') assert AutoApprovalSummary.objects.count() == 1 assert AutoApprovalSummary.objects.get(version=self.version) assert get_reviewing_cache(self.addon.pk) is None
def test_successful_verdict(self, create_summary_for_version_mock): create_summary_for_version_mock.return_value = ( AutoApprovalSummary(verdict=amo.AUTO_APPROVED), {}) call_command('auto_approve') assert create_summary_for_version_mock.call_args == ( (self.version, ), {'max_average_daily_users': 10000, 'min_approved_updates': 1, 'dry_run': False}) assert get_reviewing_cache(self.addon.pk) is None self._check_stats({'total': 1, 'auto_approved': 1})
def test_skip_if_has_info_request(self, create_summary_for_version_mock): self.version.update(has_info_request=True) call_command('auto_approve') assert create_summary_for_version_mock.call_count == 0 assert get_reviewing_cache(self.addon.pk) is None self._check_stats({'total': 1, 'flagged': 1})
def handle(self, *args, **options): dry_run = options.get('dry_run', False) max_average_daily_users = int( get_config('AUTO_APPROVAL_MAX_AVERAGE_DAILY_USERS') or 0) min_approved_updates = int( get_config('AUTO_APPROVAL_MIN_APPROVED_UPDATES') or 0) if min_approved_updates <= 0 or max_average_daily_users <= 0: # Auto approval are shut down if one of those values is not present # or <= 0. url = '%s%s' % (settings.SITE_URL, reverse('admin:zadmin_config_changelist')) raise CommandError( 'Auto-approvals are deactivated because either ' 'AUTO_APPROVAL_MAX_AVERAGE_DAILY_USERS or ' 'AUTO_APPROVAL_MIN_APPROVED_UPDATES have not been ' 'set or were set to 0. Use the admin tools Config model to ' 'set them by going to %s.' % url) stats = Counter() qs = self.fetch_candidates() stats['total'] = len(qs) successful_verdict = (amo.WOULD_HAVE_BEEN_AUTO_APPROVED if dry_run else amo.AUTO_APPROVED) for version in qs: # Is the addon already locked by a reviewer ? if get_reviewing_cache(version.addon.pk): stats['locked'] += 1 continue # If admin review or more information was requested, skip this # version, let a human handle it. if version.addon.admin_review or version.has_info_request: stats['flagged'] += 1 continue # Lock the addon for ourselves, no reviewer should touch it. set_reviewing_cache(version.addon.pk, settings.TASK_USER_ID) try: log.info('Processing %s version %s...', unicode(version.addon.name), unicode(version.version)) summary, info = AutoApprovalSummary.create_summary_for_version( version, max_average_daily_users=max_average_daily_users, min_approved_updates=min_approved_updates, dry_run=dry_run) log.info('Auto Approval for %s version %s: %s', unicode(version.addon.name), unicode(version.version), summary.get_verdict_display()) stats.update({k: int(v) for k, v in info.items()}) if summary.verdict == successful_verdict: stats['auto_approved'] += 1 # FIXME: implement auto-approve if verdict is amo.AUTO_APPROVED except (AutoApprovalNotEnoughFilesError, AutoApprovalNoValidationResultError): log.info( 'Version %s was skipped either because it had no ' 'file or because it had no validation attached.', version) stats['error'] += 1 finally: clear_reviewing_cache(version.addon.pk) self.log_final_summary(stats, dry_run=dry_run)