#!/usr/bin/env python import imp, os, sys, re, unittest sys.path.append("..") from os import path from piki_conf import * # Load the CGI script as a module f = file("../atom.cgi", "r") atomcgi = imp.load_module("atomcgi", f, "../atom.cgi", ("cgi", "r", imp.PY_SOURCE)) f.close() # Wiki words used during the tests TEST_WORDS = ["TestWikiWord", "TestUtf", "TestUnicodeFour", "ThisIsMyTest", "SomeImageTitle"] # Oft used regular expressions status_200 = re.compile("^Status: 200", re.M) status_201 = re.compile("^Status: 201", re.M) status_400 = re.compile("^Status: 400", re.M) xml_prolog = re.compile("^<\?xml ", re.M) empty_good_body = """ %(word)s %(issued)s %(content)s """ empty_good_body_sans_content_type = """ %(word)s %(issued)s %(content)s """ empty_good_body_sans_content = """ %(word)s %(issued)s """ image_body = """ SomeImageTitle A Picture of .... R0lGODlh1wAfALMAAP/XygoICMyTWenQimhXUNe1qbmckZB5cfftzPbRwkA2M+3JvPzUyP7Wyfnf y/PltCH5BAAAAAAALAAAAADXAB8AAAT/EMhJq7046827/2AojmRpnmiqrmzrvnAsz3Rt33iu73zv /8CgcEgsGo+UxeFAIBwcyKh0tiAoDgsAg1lJGA7T8NCBQDgWCwag/EAnOoXAIaGeHAqThcJAgIr/ PGVsCAULgg8PAwVvGgdyDA0VBWAAegVfgJk5ZIIIiIeJAwaLkRcGcn4WBwx6WApamrEzDZ2eiA+2 oQJYGAsBBBheBgxWVVkfDQoBBrLNHQy30dED1AK7eBcEAcfZCY4GCtgfpwR1zucWDQnSA4mh1dYE 4klyGA1fcQrhIOoB2+gALSRoR62gwQHWdsm74OiYtjQSCpRTFq5BgwMKzDWqV2HBvICx/wq6Iwgv Hq8KF18BKKAMDwOWC04pONYggMoN+SwwuAKyWYMCBw8mZGJgQSkKTBiVkwAOjb+PHlpqBMClZ7MF QQsmFOCkwNEJW4AxbUKAmZ5fEn7alOBtj5KyEhiQo8pTi4KvVjO9zLq1SVG8EQOkiuvFXwBxypgB MACuCQArkHwF8IZRARQnU/P+YaBIgMGtTLAAluCg6gSJjjjKRRvzgMzFvxoo+XUnTgGJEhVr1rTX 81ZrVxZlpgAutJOzfSb4Myqv5mEAiR/b5LOIqs0GTRjtBqTOAMLfVwwkwKtuiZNRaVcqowTgHirX W+o5AoZxmYO1cQiovbmde0zwZaUBWJJZjFnhzxUYlTOYMg5M0uBaDihD2S8vyVGFWNqI118mFi3Q V4CjXeSEa5Upc+AdEzhgxRJ2PafiiC25x0cV4Ng03IZSNPAScAFCgkwCMWljGIL6MBPJKOMxtQhj E2By5B034jjFS0sUFaUGDDByiXnoUWARWFe2Z9FoUorBSiF0pDBYmWzaw4CPbcYp55x01jlnBAA7 """ def getpath(wikiword): "Returns the absolute file path to the wiki word." return path.join(text_dir, wikiword) def wikiwordExists(wikiword): "Does this wiki word exist in the file system." return path.exists(getpath(wikiword)) class WikiWord(unittest.TestCase): """Base class for testing, contains the default behaviors for setUp and tearDown.""" def setUp(self): os.environ['REMOTE_ADDR'] = '127.0.0.1' for word in TEST_WORDS: try: os.unlink(path.join(text_dir, word)) except: pass def tearDown(self): self.setUp() class Methods(WikiWord): """Test for proper operation of the basic HTTP methods on the wiki words.""" def testGetMissing(self): """Test that you can do a GET on any valid WikiWord, regardless of whether it exists on file or not. The GET should always succeed""" word = TEST_WORDS[0] os.environ["PATH_INFO"] = "/" + word os.environ["REQUEST_METHOD"] = "GET" res = atomcgi.main("") assert status_200.search(res) assert xml_prolog.search(res) assert not wikiwordExists(word) def testGetInvalid(self): """Test that you get a 400 when doing a GET on an invalid WikiWord""" os.environ["PATH_INFO"] = "/Test.WikiWord" os.environ["REQUEST_METHOD"] = "GET" res = atomcgi.main("") assert status_400.search(res) assert not wikiwordExists("Test.WikiWord") def testPutMissing(self): """Test that you can PUT to a WikiWord that does not exist in the 'text' directory.""" word = TEST_WORDS[0] os.environ["PATH_INFO"] = "/" + word os.environ["REQUEST_METHOD"] = "PUT" issued = "2004-02-14T21:38:00-05:00" content = "This is a test" body = empty_good_body % vars() res = atomcgi.main(body) assert status_200.search(res) def testPutUtf8Ver4(self): """Test that you can PUT utf-8 encoded text that does not exist in the text directory.""" word = TEST_WORDS[2] os.environ["PATH_INFO"] = "/" + word os.environ["REQUEST_METHOD"] = "PUT" issued = "2004-02-14T21:38:00-05:00" content = u"UNDERTIE = \u203f.".encode("utf-8") body = empty_good_body % vars() res = atomcgi.main(body) stored_content = file(getpath(word), "r").read() assert content == stored_content assert status_200.search(res) def testPutUtf8(self): """Test that you can PUT utf-8 encoded text that does not exist in the text directory.""" word = TEST_WORDS[1] os.environ["PATH_INFO"] = "/" + word os.environ["REQUEST_METHOD"] = "PUT" issued = "2004-02-14T21:38:00-05:00" content = u"Rook = \u2656.".encode("utf-8") body = empty_good_body % vars() res = atomcgi.main(body) stored_content = file(getpath(word), "r").read() assert content == stored_content assert status_200.search(res) def testPutTwice(self): """Test that you can PUT twice to a WikiWord that does not exist in the 'text' directory and the second one will overwrite the first.""" word = TEST_WORDS[0] os.environ["PATH_INFO"] = "/" + word os.environ["REQUEST_METHOD"] = "PUT" issued = "2004-02-14T21:38:00-05:00" content = u"Bishop = \u2657.".encode("utf-8") body = empty_good_body % vars() res = atomcgi.main(body) assert status_200.search(res) content = u"Rook = \u2656.".encode("utf-8") body = empty_good_body % vars() res = atomcgi.main(body) stored_content = file(getpath(word), "r").read() assert content == stored_content assert status_200.search(res) def testPutMissingContentType(self): """Test that you can PUT to a WikiWord with a missing content type attribute.""" word = TEST_WORDS[0] os.environ["PATH_INFO"] = "/" + word os.environ["REQUEST_METHOD"] = "PUT" issued = "2004-02-14T21:38:00-05:00" content = "This is a test" body = empty_good_body_sans_content_type % vars() res = atomcgi.main(body) assert status_200.search(res) def testPutMissingContent(self): """Test that you can PUT to a WikiWord with a missing 'content' element.""" word = TEST_WORDS[0] os.environ["PATH_INFO"] = "/" + word os.environ["REQUEST_METHOD"] = "PUT" issued = "2004-02-14T21:38:00-05:00" content = "This is a test" body = empty_good_body_sans_content % vars() res = atomcgi.main(body) assert status_200.search(res) def testDelete(self): """Test that you can DELETE a WikiWord.""" word = TEST_WORDS[0] os.environ["PATH_INFO"] = "/" + word os.environ["REQUEST_METHOD"] = "PUT" issued = "2004-02-14T21:38:00-05:00" content = "This is a test" body = empty_good_body_sans_content % vars() res = atomcgi.main(body) assert status_200.search(res) os.environ["REQUEST_METHOD"] = "DELETE" body = "" res = atomcgi.main(body) assert status_200.search(res) assert not wikiwordExists(word) def testPost(self): """Test that you can create a new WikiWord with POST.""" word = "This Is My Test" os.environ["REQUEST_METHOD"] = "POST" issued = "2004-02-14T21:38:00-05:00" content = "This Is Content" body = empty_good_body % vars() res = atomcgi.main(body) assert status_201.search(res) stored_content = file(getpath("ThisIsMyTest"), "r").read() assert content == stored_content def testPostImage(self): """Test that you can post in image""" os.environ["REQUEST_METHOD"] = "POST" res = atomcgi.main(image_body) assert status_201.search(res) if __name__ == "__main__": unittest.main()