import unittest from model import model from config import config from urls.admin import urls as app from tests.utils import cleanup try: from xml.etree.ElementTree import fromstring, tostring except: from elementtree.ElementTree import fromstring, tostring from StringIO import StringIO from urllib import urlencode ATOM = "http://www.w3.org/2005/Atom" XHTML = "http://www.w3.org/1999/xhtml" ENTRY = """ My entry title 1 Joe Gregorio 2007-04-01T12:00:00 <b>A summary</b> <i>Passes for content.</i> """ class APP(unittest.TestCase): def setUp(self): cleanup() """Add an entry to start.""" # Load the test configuration config.read('tests/test_comments.ini') def add(self): request_body = StringIO(ENTRY) environ = { 'PATH_INFO': '/service/entry/', 'REQUEST_METHOD': 'POST', 'HTTP_HOST': 'bitworking.org', 'REQUEST_URI': '/service/entry/', 'wsgi.input': request_body, 'wsgi.url_scheme': 'http' } self.body = app(environ, self.start_response) def start_response(self, status, headers): self.status = status self.headers = headers def test_add(self): self.add() body = self.body # Confirm the template gets filled in properly # and that the response is right body = fromstring("".join(body)) title = body.find(".//{%s}title" % ATOM) self.assertEqual("My entry title", title.text) self.assertEqual("201 Created", self.status) headers = dict(self.headers) self.assertTrue('etag' in headers) self.assertTrue('location' in headers) self.assertTrue('content-location' in dict(self.headers)) def test_get(self): self.add() environ = { 'PATH_INFO': '/service/entry/1/', 'REQUEST_METHOD': 'GET', 'HTTP_HOST': 'bitworking.org', 'REQUEST_URI': '/service/entry/1/', 'wsgi.url_scheme': 'http' } body = app(environ, self.start_response) # Confirm the template gets filled in properly # and that the response is right body = fromstring("".join(body)) title = body.find(".//{%s}title" % ATOM) self.assertEqual("My entry title", title.text) self.assertEqual("200 Ok", self.status) headers = dict(self.headers) self.assertTrue('etag' in headers) def test_update(self): self.add() environ = { 'PATH_INFO': '/service/entry/1/', 'REQUEST_METHOD': 'GET', 'HTTP_HOST': 'bitworking.org', 'REQUEST_URI': '/service/entry/1/', 'wsgi.url_scheme': 'http' } body = app(environ, self.start_response) # Confirm the template gets filled in properly # and that the response is right body = fromstring("".join(body)) title = body.find(".//{%s}title" % ATOM) self.assertEqual("My entry title", title.text) title.text = "My New Entry Title" headers = dict(self.headers) self.assertTrue('etag' in headers) etag = headers['etag'] request_body = StringIO(tostring(body)) environ = { 'PATH_INFO': '/service/entry/1/', 'REQUEST_METHOD': 'PUT', 'HTTP_HOST': 'bitworking.org', 'REQUEST_URI': '/service/entry/1/', 'wsgi.url_scheme': 'http', 'wsgi.input': request_body, 'HTTP_IF_MATCH': etag } body = app(environ, self.start_response) self.assertEqual("200 Ok", self.status) entries = model.entry m = entries.get('1') self.assertEqual(m['title'], "My New Entry Title") def test_delete(self): self.add() environ = { 'PATH_INFO': '/service/entry/1/', 'REQUEST_METHOD': 'DELETE', 'HTTP_HOST': 'bitworking.org', 'REQUEST_URI': '/service/entry/1/', 'wsgi.url_scheme': 'http' } body = app(environ, self.start_response) # Confirm the template gets filled in properly # and that the response is right self.assertEqual("200 Ok", self.status) environ = { 'PATH_INFO': '/service/entry/1/', 'REQUEST_METHOD': 'GET', 'HTTP_HOST': 'bitworking.org', 'REQUEST_URI': '/service/entry/1/', 'wsgi.url_scheme': 'http', } body = app(environ, self.start_response) self.assertEqual("404 Not Found", self.status) def test_list(self): pass def test_service(self): pass