import unittest from model import model from config import config from urls.main import urls as app from tests.utils import cleanup try: from xml.etree.ElementTree import fromstring, tostring except: from elementtree.ElementTree import fromstring from StringIO import StringIO from urllib import urlencode ATOM = "http://www.w3.org/2005/Atom" XHTML = "http://www.w3.org/1999/xhtml" class Comments(unittest.TestCase): def setUp(self): cleanup() """Add an entry to start.""" # Load the test configuration config.read('tests/test_comments.ini') # Create a new entry entries = model.entry self.id = entries.create(dict(title="Test")) def start_response(self, status, headers): self.status = status self.headers = headers def test_add(self): # Add a comment to an entry comments = model.comments('entry', self.id) cid = comments.create(dict(title="My Comment", content="My Comment Content")) # Now build a WSGI/HTTP request and submit it to the app environ = { 'PATH_INFO': '/', 'REQUEST_METHOD': 'GET', } body = app(environ, self.start_response) # Confirm the template gets filled in properly # and that the response is right body = "".join(body) self.assertTrue(body.find("1/Test") > 0) self.assertEqual("200 Ok", self.status) self.assertTrue('etag' in dict(self.headers)) # Now do the same for the individual entry page environ = { 'PATH_INFO': '/' + self.id + '/' + 'Test', 'REQUEST_METHOD': 'GET', } 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) self.assertTrue('etag' in dict(self.headers)) body = "".join(body) self.assertTrue(body.find("Test") > 0) self.assertTrue(body.find("My Comment Content") > 0) def test_create(self): request_body = StringIO(urlencode(dict( name="Joe (http://bitworking.org)", example="Joe (http://bitworking.org)", comment="This is bold" ))) environ = { 'PATH_INFO': '/1/Test', 'REQUEST_METHOD': 'POST', 'REQUEST_URI': '/1/Test', 'wsgi.input': request_body } body = app(environ, self.start_response) self.assertEqual("303 See Other", self.status) headers = dict(self.headers) self.assertTrue('location' in headers) self.assertEqual(headers['location'], '/1/Test;edit_comment_form?key=1d08b9105f889a78fa0936b8e9d1c609d96816e3&comment_id=1#X1') def test_create_bad_author(self): request_body = StringIO(urlencode(dict( name="Joe (http://bitworking.", example="Joe (http://bitworking.org)", comment="This is bold" ))) environ = { 'PATH_INFO': '/1/Test', 'REQUEST_METHOD': 'POST', 'REQUEST_URI': '/1/Test', 'wsgi.input': request_body } body = app(environ, self.start_response) body = "".join(body) self.assertEqual("200 Ok", self.status) self.assertTrue("robot" in body) def test_create_missing_example(self): request_body = StringIO(urlencode(dict( name="Joe (http://bitworking.org)", comment="This is bold" ))) environ = { 'PATH_INFO': '/1/Test', 'REQUEST_METHOD': 'POST', 'REQUEST_URI': '/1/Test', 'wsgi.input': request_body } body = app(environ, self.start_response) body = "".join(body) self.assertEqual("200 Ok", self.status) self.assertTrue("robot" in body)