def test_is_auto_approval_allowed_email_restricted(self, incr_mock): EmailUserRestriction.objects.create( email_pattern=self.request.user.email, restriction_type=RESTRICTION_TYPES.APPROVAL, ) upload = FileUpload.objects.create( user=self.request.user, ip_address='10.0.0.2', source=amo.UPLOAD_SOURCE_DEVHUB, ) incr_mock.reset_mock() checker = RestrictionChecker(upload=upload) assert not checker.is_auto_approval_allowed() assert incr_mock.call_count == 2 assert incr_mock.call_args_list[0][0] == ( 'RestrictionChecker.is_auto_approval_allowed.EmailUserRestriction.failure', ) assert incr_mock.call_args_list[1][0] == ( 'RestrictionChecker.is_auto_approval_allowed.failure', ) assert UserRestrictionHistory.objects.count() == 1 history = UserRestrictionHistory.objects.get() assert history.get_restriction_display() == 'EmailUserRestriction' assert history.user == self.request.user assert history.last_login_ip == self.request.user.last_login_ip assert history.ip_address == '10.0.0.2'
def test_is_auto_approval_allowed_email_restricted_only_for_submission( self, incr_mock): # Test with a submission restriction (the default): approval should be allowed. EmailUserRestriction.objects.create( email_pattern=self.request.user.email) upload = FileUpload.objects.create(user=self.request.user, ip_address='10.0.0.2') incr_mock.reset_mock() checker = RestrictionChecker(upload=upload) assert checker.is_auto_approval_allowed() assert incr_mock.call_count == 1 assert UserRestrictionHistory.objects.count() == 0 assert incr_mock.call_args_list[0][0] == ( 'RestrictionChecker.is_auto_approval_allowed.success', )
def test_is_auto_approval_allowed_with_mocks(self, incr_mock): upload = FileUpload.objects.create(user=self.request.user, ip_address='10.0.0.2') checker = RestrictionChecker(upload=upload) with ExitStack() as stack: allow_submission_mocks = [ stack.enter_context( mock.patch.object(choice[1], 'allow_submission')) for choice in checker.restriction_choices ] allow_auto_approval_mocks = [ stack.enter_context( mock.patch.object(choice[1], 'allow_auto_approval')) for choice in checker.restriction_choices ] assert checker.is_auto_approval_allowed() for restriction_mock in allow_submission_mocks: assert restriction_mock.call_count == 0 for restriction_mock in allow_auto_approval_mocks: assert restriction_mock.call_count == 1
def test_is_auto_approval_allowed_no_upload_raises_improperly_configured( self, incr_mock): checker = RestrictionChecker(request=self.request) with self.assertRaises(ImproperlyConfigured): assert checker.is_auto_approval_allowed()