class TestAkismet(unittest.TestCase): akismet = None def setUp(self): try: akismet_api_key = os.environ['AKISMET_API_KEY'] except KeyError: raise EnvironmentError('Provide AKISMET_API_KEY environment setting.') self.akismet = Akismet(akismet_api_key, is_test=True) def test_check(self): self.assertEqual(self.akismet.check('127.0.0.1', USER_AGENT, blog='http://127.0.0.1'), SpamStatus.Ham) def test_check_spam(self): self.assertEqual(self.akismet.check('127.0.0.1', EVIL_USER_AGENT, comment_author='viagra-test-123', blog='http://127.0.0.1'), SpamStatus.ProbableSpam) def test_invalid_api_key(self): akismet = Akismet('invalid_api_key', is_test=True) with self.assertRaises(AkismetServerError): akismet.check('127.0.0.1', EVIL_USER_AGENT, blog='http://127.0.0.1') def test_submit_spam(self): self.akismet.submit_spam('127.0.0.1', EVIL_USER_AGENT, blog='http://127.0.0.1') def test_submit_ham(self): self.akismet.submit_ham('127.0.0.1', USER_AGENT, blog='http://127.0.0.1') def test_datetime(self): blog_url = 'http://127.0.0.1' comment_date = datetime.datetime(2016, 4, 16, 15, 12, 5) comment_post_modified = datetime.datetime(2016, 4, 16, 16, 27, 31) data = self.akismet._get_parameters({'blog': blog_url, 'comment_post_modified': comment_post_modified, 'comment_date': comment_date}) for dtkey in ['comment_date', 'comment_post_modified']: self.assertIn('{0}_gmt'.format(dtkey), data) self.assertNotIn(dtkey, data) self.assertEqual(data['{0}_gmt'.format(dtkey)], locals()[dtkey].isoformat()) def test_timeout(self): self.akismet = Akismet(os.environ['AKISMET_API_KEY'], timeout=0.000001, is_test=True) with self.assertRaises(requests.ConnectionError): self.akismet.submit_ham('127.0.0.1', USER_AGENT, blog='http://127.0.0.1') def test_require_blog_param(self): with self.assertRaises(MissingParameterError): self.akismet._get_parameters({})
def _akismet_check(self, comment, content_object, request): """ Connects to Akismet and returns True if Akismet marks this comment as spam. Otherwise returns False. """ # Get Akismet data AKISMET_API_KEY = appsettings.AKISMET_API_KEY if not AKISMET_API_KEY: raise ImproperlyConfigured( 'You must set AKISMET_API_KEY to use comment moderation with Akismet.' ) current_domain = get_current_site(request).domain auto_blog_url = '{0}://{1}/'.format( request.is_secure() and 'https' or 'http', current_domain) blog_url = appsettings.AKISMET_BLOG_URL or auto_blog_url akismet = Akismet( AKISMET_API_KEY, blog=blog_url, is_test=int(bool(appsettings.AKISMET_IS_TEST)), application_user_agent='django-fluent-comments/{0}'.format( fluent_comments.__version__), ) akismet_data = self._get_akismet_data(blog_url, comment, content_object, request) return akismet.check( **akismet_data) # raises AkismetServerError when key is invalid
def check_spam(ip, ua, author, content) -> int: # 0 means okay token = os.getenv("askismet") if token: with contextlib.suppress(Exception): akismet = Akismet(token, blog="https://yyets.dmesg.app/") return akismet.check(ip, ua, comment_author=author, blog_lang="zh_cn", comment_type="comment", comment_content=content) return 0
def check(comment): from fypress.admin import Option if False: #check akismetapikey, ip= request.remote_addr akismet = Akismet('1ba29d6f120c', blog=Options.get('url'), user_agent=request.headers.get('User-Agent')) rv = akismet.check( comment.ip, request.headers.get('User-Agent'), comment_author=comment.author, comment_author_email=comment.email, comment_author_url=comment.uri, comment_content=comment.content ) print rv
def check(comment): from fypress.admin import Option if False: #check akismetapikey, ip= request.remote_addr akismet = Akismet('1ba29d6f120c', blog=Options.get('url'), user_agent=request.headers.get('User-Agent')) rv = akismet.check(comment.ip, request.headers.get('User-Agent'), comment_author=comment.author, comment_author_email=comment.email, comment_author_url=comment.uri, comment_content=comment.content) print rv
def check(comment): akismet_key = fypress.options.get('akismet') if akismet_key: akismet = Akismet(akismet_key, blog=fypress.options.get('url')) rv = akismet.check(comment.user_ip, request.headers.get('User-Agent'), comment_author=comment.author, comment_author_email=comment.author_email, comment_author_url=comment.author_uri, comment_content=comment.content) status = {True: 'spam', False: 'valid'} comment.status = status[rv] else: comment.status = 'valid' return comment.insert()
def clean_description(self): desc = self.cleaned_data['description'] if settings.TESTING: check_spam = False else: akismet = Akismet(settings.AKISMET_KEY, blog="CC Search") check_spam = akismet.check( self.request.get_host(), user_agent=self.request.META.get('user-agent'), comment_author=self.request.user.username, comment_content=desc) wordfilter = Wordfilter() check_words = wordfilter.blacklisted(desc) if check_spam or check_words: raise forms.ValidationError( "This description failed our spam or profanity check; the description has not been updated." ) return desc
def spam_check(self, info_dict): if self.author.spam: return True approved = self.author.approved if approved: return False current_domain = Site.objects.get_current().domain user_agent = 'Marth Blog/0.0.1' akismet = Akismet(settings.AKISMET_KEY, 'http://{0}'.format(current_domain), user_agent) is_spam = akismet.check(info_dict['remote_addr'], info_dict['user_agent'], comment_author=self.author.username, comment_author_email=self.author.email, comment_author_url=self.author.website, comment_content=self.text) if is_spam: self.author.mark_spam() return is_spam
def _akismet_check(self, comment, content_object, request): """ Connects to Akismet and returns True if Akismet marks this comment as spam. Otherwise returns False. """ # Check if the akismet api key is set, fail silently if # settings.DEBUG is False and return False (not moderated) AKISMET_API_KEY = getattr(settings, 'AKISMET_SECRET_API_KEY', False) if not AKISMET_API_KEY: raise ImproperlyConfigured('You must set AKISMET_SECRET_API_KEY with your api key in your settings file.') akismet_api = Akismet( AKISMET_API_KEY, blog='%s://%s/' % (request.scheme, Site.objects.get_current().domain), ) return akismet_api.check( comment.ip_address, request.META['HTTP_USER_AGENT'], comment_content=comment.comment, )
class Akismet(SpamProtection): user_agent = 'djangocms-comments/{0}'.format(djangocms_comments.__version__) def __init__(self, token, is_test=False, blog_domain=None): super(Akismet, self).__init__(token, is_test, blog_domain) from akismet import Akismet as PyAkismet self.akismet = PyAkismet(token, application_user_agent=self.user_agent) def get_parameters(self, data): return dict( blog=self.blog_domain or data['blog_domain'], comment_author=data['author'], comment_author_email=data['email'], comment_author_url=data['url'], comment_content=data['body'], is_test=self.is_test, **dict((key, data[key]) for key in ['user_ip', 'user_agent', 'referrer'])) def check(self, author, email, body, user_ip, user_agent, url=None, referrer='unknown', blog_domain=None): return self.akismet.check(**self.get_parameters(locals())) def report(self, is_spam, author, email, body, user_ip, user_agent, url=None, referrer='unknown', blog_domain=None): return self.akismet.submit(is_spam, **self.get_parameters(locals()))
def _akismet_check(self, comment, content_object, request): """ Connects to Akismet and returns True if Akismet marks this comment as spam. Otherwise returns False. """ # Check if the akismet api key is set, fail silently if # settings.DEBUG is False and return False (not moderated) AKISMET_API_KEY = getattr(settings, 'AKISMET_SECRET_API_KEY', False) if not AKISMET_API_KEY: raise ImproperlyConfigured( 'You must set AKISMET_SECRET_API_KEY with your api key in your settings file.' ) akismet_api = Akismet( AKISMET_API_KEY, blog='%s://%s/' % (request.scheme, Site.objects.get_current().domain), ) return akismet_api.check( comment.ip_address, request.META['HTTP_USER_AGENT'], comment_content=comment.comment, )
def _akismet_check(self, comment, content_object, request): """ Connects to Akismet and returns True if Akismet marks this comment as spam. Otherwise returns False. """ # Get Akismet data AKISMET_API_KEY = appsettings.AKISMET_API_KEY if not AKISMET_API_KEY: raise ImproperlyConfigured('You must set AKISMET_API_KEY to use comment moderation with Akismet.') current_domain = get_current_site(request).domain auto_blog_url = '{0}://{1}/'.format(request.is_secure() and 'https' or 'http', current_domain) blog_url = appsettings.AKISMET_BLOG_URL or auto_blog_url akismet = Akismet( AKISMET_API_KEY, blog=blog_url, is_test=int(bool(appsettings.AKISMET_IS_TEST)), application_user_agent='django-fluent-comments/{0}'.format(fluent_comments.__version__), ) akismet_data = self._get_akismet_data(blog_url, comment, content_object, request) return akismet.check(**akismet_data) # raises AkismetServerError when key is invalid
def akismet_check(comment, content_object, request): """ Connects to Akismet and evaluates to True if Akismet marks this comment as spam. :rtype: akismet.SpamStatus """ # Return previously cached response akismet_result = getattr(comment, "_akismet_result_", None) if akismet_result is not None: return akismet_result # Get Akismet data AKISMET_API_KEY = appsettings.AKISMET_API_KEY if not AKISMET_API_KEY: raise ImproperlyConfigured( "You must set AKISMET_API_KEY to use comment moderation with Akismet." ) current_domain = get_current_site(request).domain auto_blog_url = "{0}://{1}/".format( request.is_secure() and "https" or "http", current_domain) blog_url = appsettings.AKISMET_BLOG_URL or auto_blog_url akismet = Akismet( AKISMET_API_KEY, blog=blog_url, is_test=int(bool(appsettings.AKISMET_IS_TEST)), application_user_agent="django-fluent-comments/{0}".format( fluent_comments.__version__), ) akismet_data = _get_akismet_data(blog_url, comment, content_object, request) akismet_result = akismet.check( **akismet_data) # raises AkismetServerError when key is invalid setattr(comment, "_akismet_result_", akismet_result) return akismet_result
class TestAkismet(unittest.TestCase): akismet = None def setUp(self): try: akismet_api_key = os.environ['AKISMET_API_KEY'] except KeyError: raise EnvironmentError( 'Provide AKISMET_API_KEY environment setting.') self.akismet = Akismet(akismet_api_key, is_test=True) def test_check(self): self.assertEqual( self.akismet.check('127.0.0.1', USER_AGENT, blog='http://127.0.0.1'), SpamStatus.Ham) def test_check_spam(self): self.assertEqual( self.akismet.check('127.0.0.1', EVIL_USER_AGENT, comment_author='viagra-test-123', blog='http://127.0.0.1'), SpamStatus.ProbableSpam) def test_invalid_api_key(self): akismet = Akismet('invalid_api_key', is_test=True) with self.assertRaises(AkismetServerError): akismet.check('127.0.0.1', EVIL_USER_AGENT, blog='http://127.0.0.1') def test_submit_spam(self): self.akismet.submit_spam('127.0.0.1', EVIL_USER_AGENT, blog='http://127.0.0.1') def test_submit_ham(self): self.akismet.submit_ham('127.0.0.1', USER_AGENT, blog='http://127.0.0.1') def test_datetime(self): blog_url = 'http://127.0.0.1' comment_date = datetime.datetime(2016, 4, 16, 15, 12, 5) comment_post_modified = datetime.datetime(2016, 4, 16, 16, 27, 31) data = self.akismet._get_parameters({ 'blog': blog_url, 'comment_post_modified': comment_post_modified, 'comment_date': comment_date }) for dtkey in ['comment_date', 'comment_post_modified']: self.assertIn('{0}_gmt'.format(dtkey), data) self.assertNotIn(dtkey, data) self.assertEqual(data['{0}_gmt'.format(dtkey)], locals()[dtkey].isoformat()) def test_timeout(self): self.akismet = Akismet(os.environ['AKISMET_API_KEY'], timeout=0.000001, is_test=True) with self.assertRaises(requests.ConnectionError): self.akismet.submit_ham('127.0.0.1', USER_AGENT, blog='http://127.0.0.1') def test_require_blog_param(self): with self.assertRaises(MissingParameterError): self.akismet._get_parameters({})
def test_invalid_api_key(self): akismet = Akismet('invalid_api_key', is_test=True) with self.assertRaises(AkismetServerError): akismet.check('127.0.0.1', EVIL_USER_AGENT, blog='http://127.0.0.1')