class TestBlogLogicalInterface(unittest.TestCase): """Unit Test for api/core/admin/authorize_view.py Database access is mocked""" @classmethod def setUpClass(self): '''''' self.blog_interface = BlogLogicalInterface() @classmethod def tearDownClass(self): pass @mock.patch('api.DAL.blog.blog_select.all_post_headers') def test_post_headers_retrieval(self, mock_select_headers): mock_select_headers.return_value = '''{"posts":[{"index": "1", "title" : "A Blog", "description" : "A short descrition", "image" : "/images/temp/jass.jpg", "created": "Now", "modified": "Now"}]}''' self.blog_interface.get_headers() self.assertTrue(mock_select_headers.called) @mock.patch('api.DAL.blog.blog_select.comments') @mock.patch('api.DAL.blog.blog_select.post') def test_full_post_retrieval(self, mock_select_post, mock_select_comments): mock_select_post.return_value = '''{"index": "1", "title" : "A Blog", "image": "/images/temp/jass.jpg", "caption" : "A pretty Picture", "created": "Now", "modified": "Now", "body": "This is a supper long post"}''' mock_select_comments.return_value = '''{"comments":[{"commenter": "Me", "comment" : "An insightful comment", "comment_date": "Now"}]}''' test_string = self.blog_interface.retrieve_post("1") base_string = '''{"comments":[{"commenter": "Me", "comment" : "An insightful comment", "comment_date": "Now"}], "index": "1", "title" : "A Blog", "image": "/images/temp/jass.jpg", "caption" : "A pretty Picture", "created": "Now", "modified": "Now", "body": "This is a supper long post"}''' test_value = sorted(json.loads(test_string)) baseline = sorted(json.loads(base_string)) self.assertEqual(test_value, baseline) @mock.patch('api.DAL.blog.blog_select.comments') @mock.patch('api.DAL.blog.blog_select.post') def test_full_post_retrieval_no_image(self, mock_select_post, mock_select_comments): mock_select_post.return_value = '''{"index": "1", "title" : "A Blog", "image": "None", "caption" : "A pretty Picture", "created": "Now", "modified": "Now", "body": "This is a supper long post"}''' mock_select_comments.return_value = '''{"comments":[{"commenter": "Me", "comment" : "An insightful comment", "comment_date": "Now"}]}''' test_string = self.blog_interface.retrieve_post("1") base_string = '''{"comments":[{"commenter": "Me", "comment" : "An insightful comment", "comment_date": "Now"}], "index": "1", "title" : "A Blog", "image": "/images/default/jazz.jpg", "caption" : "A pretty Picture", "created": "Now", "modified": "Now", "body": "This is a supper long post"}''' test_value = sorted(json.loads(test_string)) baseline = sorted(json.loads(base_string)) self.assertEqual(test_value, baseline) def test_sanatizing_strings(self): test_string = self.blog_interface.sanitize_string('Hello\nWorld!!') base_line = 'Hello<br>World!!' self.assertEqual(test_string, base_line) test_string = self.blog_interface.sanitize_string('I am testing\n\ntwo lines!!') base_line = 'I am testing<br><br>two lines!!' self.assertEqual(test_string, base_line) test_string = self.blog_interface.sanitize_string('There is a " in my string.') base_line = 'There is a \\" in my string.' self.assertEqual(test_string, base_line) test_string = self.blog_interface.sanitize_string('Now there are both!\nWhat are "you" going to do?') base_line = 'Now there are both!<br>What are \\"you\\" going to do?' self.assertEqual(test_string, base_line)
def setUpClass(self): '''''' self.blog_interface = BlogLogicalInterface()