def save(self, **kwargs): # Take a copy of the body before the save because we pass it to # maybe_check_with_akismet to confirm it changed. pre_save_body = unicode(self.instance.body) if self.instance else None instance = super(RatingSerializer, self).save(**kwargs) request = self.context.get('request') maybe_check_with_akismet(request, instance, pre_save_body) return instance
def save(self, **kwargs): # Take a copy of the body before the save because we pass it to # maybe_check_with_akismet to confirm it changed. pre_save_body = unicode(self.instance.body) if self.instance else None instance = super(RatingSerializer, self).save(**kwargs) request = self.context.get('request') maybe_check_with_akismet(request, instance, pre_save_body) return instance
def test_maybe_check_with_akismet(body, pre_save_body, user_id, user_id_resp, waffle_enabled, is_checked): user = user_factory( id=user_id, username='******' % user_id, email='%s@e' % user_id) rating_kw = { 'addon': addon_factory(), 'user': user, 'rating': 4, 'body': body, 'ip_address': '1.2.3.4'} if user_id_resp: rating_kw['user_responsible'] = ( user if user_id_resp == user_id else user_factory( id=user_id_resp, username='******' % user_id_resp, email='%s@e' % user_id_resp)) rating = Rating.objects.create(**rating_kw) request = RequestFactory().get('/') to_patch = 'olympia.ratings.utils.check_akismet_reports.delay' with mock.patch(to_patch) as check_akismet_reports_mock: with override_switch('akismet-spam-check', active=waffle_enabled): result = maybe_check_with_akismet(request, rating, pre_save_body) assert result == is_checked if is_checked: assert AkismetReport.objects.count() == 1 check_akismet_reports_mock.assert_called() else: assert AkismetReport.objects.count() == 0 check_akismet_reports_mock.assert_not_called()
def test_maybe_check_with_akismet(body, pre_save_body, user_id, user_id_resp, waffle_enabled, is_checked): user = user_factory( id=user_id, username='******' % user_id, email='%s@e' % user_id) rating_kw = { 'addon': addon_factory(), 'user': user, 'rating': 4, 'body': body, 'ip_address': '1.2.3.4'} if user_id_resp: rating_kw['user_responsible'] = ( user if user_id_resp == user_id else user_factory( id=user_id_resp, username='******' % user_id_resp, email='%s@e' % user_id_resp)) rating = Rating.objects.create(**rating_kw) request = RequestFactory().get('/') with mock.patch('olympia.ratings.utils.check_with_akismet.delay') as cmock: with override_switch('akismet-spam-check', active=waffle_enabled): result = maybe_check_with_akismet(request, rating, pre_save_body) assert result == is_checked if is_checked: cmock.assert_called() else: cmock.assert_not_called()