def test_edit_page(self): """Functional test to verify that we can edit an existing page. Starting from the ``<slug>/edit_page`` route just like a user would. """ with self.app.test_client() as tester: posting(tester, '/published_page/edit_page', title_field="modified page title") response = self.tester.get('/published_page') self.assertIn( b'modified page title', response.data, "No message telling you your page was successfully " "edited.")
def test_duplicate_title_for_edited_post(self): """Testing that an edited post can't take the same title as an existing post. """ # First we will create a new post that we will edit later. dummy_post(title='Post to edit', content="post content", slug="post_to_edit") # We will then try to edit the newly created post so the title is the # same as the one we used for our dummy_post created in the setUp. response = posting(self.tester, '/post_to_edit/edit_post', title_field='Dummy post', content_field='test duplicate post title') self.assertIn( b'title is already in use', response.data, "No message telling you that the title" " is already in use.") response = self.tester.get('/') self.assertNotIn( b'test duplicate post title', response.data, "Test post with duplicate title was still published" " and can be found on the /index page.") self.assertIn( b'post content', response.data, "Content of the unedited test post can not be found" " on the /index page.")
def test_create_page(self): """Functional test to verify that we can create a new page. Starting from the ``/create_page`` route just like a user would. """ with self.app.test_client() as tester: response = posting(tester, '/create_page', is_page=True) self.assertIn(b'Page is now live.', response.data, "No message telling you your page went live.")
def test_edit_posts(self): """Testing of the whole editing process of a post starting from the /edit_post page. """ # The post that we will edit is the one created in the setUp. # First we will test the process of publishing an edited post: with self.app.test_client() as tester: response = posting(tester, '/dummy_post/edit_post', title_field='test editing post') self.assertIn( b'edited and published', response.data, "No message telling you your post" " was successfully edited") self.assertEqual(request.path, url_for('main.index'), "Redirect to the homepage did not happen.") response = self.tester.get('/test_editing_post') self.assertIn( b'test editing post', response.data, "Content of the edited post can't be found" " on the detail page") # Finally we will test saving an edited post as a draft: with self.app.test_client() as tester: response = posting(tester, '/test_editing_post/edit_post', title_field='test editing draft', publish='') self.assertIn( b'edited and saved', response.data, "No message telling you your post" " was successfully edited.") self.assertEqual(request.path, url_for('main.drafts'), "Redirect to the /drafts page did not happen.") self.assertIn( b'test editing draft', response.data, "Content of the edited post can't be found" " on the /drafts page.")
def test_posting_drafts(self): """Testing of the whole posting process starting from /create_post . This test concerns drafts. """ with self.app.test_client() as tester: response = posting(tester, '/create_post', title_field='test draft', publish='') self.assertIn(b'Post saved as draft', response.data, "No message telling you your draft was saved.") self.assertEqual(request.path, url_for('main.drafts'), "Redirect to the /drafts page did not happen.") response = self.tester.get('/drafts') self.assertIn(b'test draft', response.data, "Test draft can't be found on the /drafts page.")
def test_preview_new_post(self): """Tests that we can preview a new post before publishing. This test concerns posts made with the /create route. """ with self.app.test_client() as tester: response = posting(tester, '/create_post', preview='Preview') self.assertEqual(request.path, url_for('main.preview', slug='test_title'), "Redirect to the post preview did not happen.") self.assertIn( b'Preview of "test title"', response.data, "Title does not tells us that we are on the" " <slug>/preview page.") time = datetime.utcnow().strftime('%m/%d/%Y at %I:%M %p') time_ref = 'Posted ' + time self.assertIn(bytes(time_ref, 'utf-8'), response.data, "Timestamp can not be found on the post preview.")
def test_duplicate_titles(self): """Testing that a newly created post can't take the same title as an old post. """ # The post that we will create use the same title as that # of the dummy post created in the setUp response = posting(self.tester, '/create_post', title_field='Dummy post', content_field='test duplicate post title') self.assertIn( b'title is already in use', response.data, "No message telling you that the title" " is already in use.") response = self.tester.get('/') self.assertNotIn( b'test duplicate post title', response.data, "Test post was still published and can be found" " on the /index page")
def test_preview_edited_post(self): """Tests that we can preview an edited post before publishing. This test concerns posts made with the /<slug>/edit route. """ with self.app.test_client() as tester: # Post to edit is the one created in SetUp. response = posting(tester, '/dummy_post/edit_post', title_field='test editing post', preview='Preview') self.assertEqual(request.path, url_for('main.preview', slug='test_editing_post'), "Redirect to the post preview did not happen.") self.assertIn( b'Preview of "test editing post"', response.data, "Title does not tells us that we are on the" " <slug>/preview page.") match = all(x in response.data for x in [b'tiger', b'bird', b'dog']) self.assertTrue(match, "A category does not appear in the preview.")
def test_posting_published(self): """Testing of the whole posting process starting from the /create page. This test concerns published posts. """ with self.app.test_client() as tester: response = posting(tester, '/create_post', categories_field_0='camel', categories_field_1='elephant', categories_field_2='rat') self.assertIn(b'Post is now live.', response.data, "No message telling you your post went live.") self.assertEqual(request.path, url_for('main.index'), "Redirect to the post detail did not happen.") self.assertIn(b'test body', response.data, "Test post can't be found on the /index page.") match = all(x in response.data for x in [b'camel', b'elephant', b'rat']) self.assertTrue( match, "A category is missing from the" " published post.")
def test_render_categories(self): """Testing the ``render_categories`` macro. """ # We will first test the preview of a post and a published post # posted under a single category. response = posting(self.tester, '/create_post', preview='Preview', title_field='single_category', categories_field_0='cats') self.assertIn( b'\n\nin\n\n\n\n\ncats.', response.data, "Category is not displayed in the right manner when " "previewing a post with single category.") response = posting(self.tester, '/create_post', title_field='single_category', categories_field_0='cats') self.assertIn( b'\n\nin\n\n\n\n\n<a href="/cats/category">cats.</a>', response.data, "Hyperlinked category is not displayed in the right " "manner for a published post with a single category.") # We will do the same thing now but for a post with two categories response = posting(self.tester, '/create_post', preview='Preview', title_field='two_category', categories_field_0='dogs', categories_field_1='birds') self.assertTrue( b'\n\nin\n\n\n\n\ndogs,\n\n\n\n\n\nbirds.' in response.data or b'\n\nin\n\n\n\n\nbirds,\n\n\n\n\n\ndogs.' in response.data, "Categories are not displayed in the right manner" " when previewing a post with two categories.") response = posting(self.tester, '/create_post', title_field='two_category', categories_field_0='dogs', categories_field_1='birds') self.assertTrue( b'\n\nin\n\n\n\n\n<a href="/dogs/category">dogs,' b'</a>\n\n\n\n\n\n<a href="/birds/category">birds.</a>' in response.data or b'\n\nin\n\n\n\n\n<a href="/birds/category">birds,' b'</a>\n\n\n\n\n\n<a href="/dogs/category">dogs.</a>' in response.data, "Hyperlinked categories are not displayed in the" " right manner for a published post with two " "categories.") # Then we test a post with no category. response = posting(self.tester, '/create_post', preview='Preview', title_field='no_category') self.assertIn( b'\n\nin\n\n\nuncategorized.', response.data, "No tag with the word `uncategorized` can be found when" " previewing a post without categories.") response = posting(self.tester, '/create_post', title_field='no_category') self.assertIn( b'\n\nin\n\n\n<a href="/uncategorized/category">' b'uncategorized.</a>', response.data, "No tag with the hyperlinked word `uncategorized` can" " be found for a post published without categories.") # Finally we will test a post when categories have been configured to # not show up anywhere on the site. cg = CategoriesControls.query.first() cg.presence = 'no_categories' db.session.commit() response = posting(self.tester, '/create_post', title_field='single_category', categories_field_0='cats') self.assertNotIn( b'cats', response.data, "A category can still be found in our post after " "the category functionality have been disabled " "site wide.")