def test_create_markdown_content_invalid_form(self): with app.test_request_context(method='GET'): form_mock, markdown_mock = self.markdown_content_mocks() form_mock.validate = mock.Mock(return_value=False) rv = common.create_markdown_content(form_mock, markdown_mock) self.assertEqual(rv[1], 403) self.assertEqual(1, form_mock.validate.call_count) self.assertEqual(1, len(form_mock.mock_calls)) self.assertEqual(0, len(markdown_mock.mock_calls))
def test_create_markdown_content_already_exists(self): with app.test_request_context(method='GET'): form_mock, markdown_mock = self.markdown_content_mocks() form_mock.validate = mock.Mock(return_value=True) markdown_mock.create = mock.Mock(side_effect=common.ContentExistsException) rv = common.create_markdown_content(form_mock, markdown_mock) self.assertEqual(rv[1], 409) self.assertEqual(1, form_mock.validate.call_count) self.assertEqual(1, len(form_mock.mock_calls)) self.assertEqual(1, len(markdown_mock.mock_calls)) self.assertEqual(1, markdown_mock.create.call_count)
def test_create_markdown_content_normal(self): with app.test_request_context(method='GET'): form_mock, markdown_mock = self.markdown_content_mocks() form_mock.validate = mock.Mock(return_value=True) rv = common.create_markdown_content(form_mock, markdown_mock) expected = {'status': 'success', 'form': {'some-key': 'some-value', 'slug': 'some-slug'}} self.assertEqual(json.loads(rv.data), expected) self.assertEqual(1, form_mock.validate.call_count) self.assertEqual(1, len(form_mock.mock_calls)) self.assertEqual(1, len(markdown_mock.mock_calls)) self.assertEqual(1, markdown_mock.create.call_count)
def test_create_markdown_content_io_error(self): with app.test_request_context(method='GET'): form_mock, markdown_mock = self.markdown_content_mocks() form_mock.validate = mock.Mock(return_value=True) markdown_mock.create = mock.Mock(side_effect=IOError) rv = common.create_markdown_content(form_mock, markdown_mock) self.assertEqual(rv[1], 500) self.assertEqual(1, form_mock.validate.call_count) self.assertEqual(1, len(form_mock.mock_calls)) self.assertEqual(1, len(markdown_mock.mock_calls)) self.assertEqual(1, markdown_mock.create.call_count)
def create_post_json(): form = PostForm.from_json(request.json) post = parse_form(form) return create_markdown_content(form, post)