|
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() |