Skip to content

Instantly share code, notes, and snippets.

@aaira-a
Last active August 29, 2015 14:13
Show Gist options
  • Save aaira-a/0a978b3aa176e953e944 to your computer and use it in GitHub Desktop.
Save aaira-a/0a978b3aa176e953e944 to your computer and use it in GitHub Desktop.
Python 2 script to verify Akismet API key validity, usable with Jenkins
import requests
import sys
URL = 'http://rest.akismet.com/1.1/verify-key'
DATA = {'key': 'insert_key_here', 'blog': 'insert_blog_here'}
def verify_akismet_key(url_, data_):
r = requests.post(url_, data=data_)
print "\n========\nheaders:\n========\n"
for header in r.headers:
print header + ": " + r.headers[header]
print "\n========\ncontent:\n========\n"
print r.content
if 'invalid' in r.content:
sys.exit(1)
elif 'valid' in r.content:
sys.exit(0)
else:
sys.exit(1)
if __name__ == "__main__":
verify_akismet_key(URL, DATA)
coverage
requests
responses
import unittest
import responses
import runpy
from akismet import URL, DATA, verify_akismet_key
class AkismetKeyTest(unittest.TestCase):
@responses.activate
def test_module_exits_with_return_0_when_response_content_contains_valid(self):
responses.add(responses.POST, URL, body="valid")
with self.assertRaises(SystemExit) as cm:
verify_akismet_key(URL, DATA)
self.assertEqual(cm.exception.code, 0)
@responses.activate
def test_module_exits_with_return_1_when_response_content_contains_invalid(self):
responses.add(responses.POST, URL, body="invalid")
with self.assertRaises(SystemExit) as cm:
verify_akismet_key(URL, {'key': 'invalidkey', 'blog': 'invalidblog'})
self.assertEqual(cm.exception.code, 1)
@responses.activate
def test_module_exits_with_return_1_when_response_content_contains_nothing(self):
responses.add(responses.POST, URL, body="")
with self.assertRaises(SystemExit) as cm:
verify_akismet_key(URL, {'key': 'invalidkey', 'blog': 'invalidblog'})
self.assertEqual(cm.exception.code, 1)
@responses.activate
def test_module_exits_with_return_1_when_response_content_contains_random_string(self):
responses.add(responses.POST, URL, body="a4b4k72")
with self.assertRaises(SystemExit) as cm:
verify_akismet_key(URL, {'key': 'invalidkey', 'blog': 'invalidblog'})
self.assertEqual(cm.exception.code, 1)
@responses.activate
def test_run_module_from_system_returns_0_for_valid_response(self):
responses.add(responses.POST, URL, body="valid")
with self.assertRaises(SystemExit) as cm:
runpy.run_path("akismet.py", run_name="__main__")
self.assertEqual(cm.exception.code, 0)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment