def test_pages_dropdown_menu(self): """Testing the ``Pages`` dropdown menu in the navbar. This menu is only shown to logged in users. The test will verify the presence of the menu and the two hyperlinks it should contain: - The static ``Create Page`` hyperlink. - The dynamic ``Pages Index`` hyperlink. This item only appears when at least one page have already been created. """ login(self.tester) response = self.tester.get('/') self.assertIn(b'<a class="nav-link dropdown-toggle" href="#" ' b'id="PagesDropdown" role="button" data-toggle="dropdown"' b' aria-haspopup="true" ' b'aria-expanded="false">\n Pages', response.data, "The ``pages`` dropdown menu can not be found in the " "navbar.") self.assertIn(b'<a class="dropdown-item" href="/create_page">' b'Create Page</a>', response.data, "The ``Create Page`` hyperlink can not be found in the" " ``Pages`` dropdown menu in the navbar.") self.assertNotIn(b'<a class="dropdown-item" href="/pages">' b'Pages Index</a>', response.data, "The ``Pages Index`` hyperlink can still be found in " "the ``Pages`` dropdown menu in the navbar.") dummy_post(is_page=True) response = self.tester.get('/') self.assertIn(b'<a class="dropdown-item" href="/pages">Pages Index</a>', response.data, "The ``Pages Index`` hyperlink can not be found in the " "``Pages`` dropdown menu in the navbar.")
def test_remember_me(self): """Testing that the ``remember me`` checkbox can extend our session duration. """ with self.app.test_client() as tester: login(tester) self.assertIn("remember_token", request.cookies, "remember_me cookie can't be found.")
def test_logout(self): """Test that we can effectively log out of our application. """ with self.app.test_client() as tester: login(tester) tester.post('/logout', follow_redirects=True) self.assertEqual(request.path, url_for('auth.login'), "Redirect to the /login page did not happen " "after attempting to logout.")
def test_login_required_accepted(self): """Testing that a logged in user can access protected routes. Protection comes from the ``login_required`` decorator. """ # A logged in user can access the /create_post route. login(self.tester) response = self.tester.get('/create_post') self.assertIn(b'New Post', response.data, "You failed to access a page protected " "by ``login_required``.")
def test_already_logged_in(self): """Test that a user already logged in is redirected away from the /login page. """ with self.app.test_client() as tester: login(tester) response = tester.get('/login', follow_redirects=True) self.assertEqual(request.path, url_for('main.index'), "Redirect to the /index page failed.") self.assertIn(b'You are already logged in', response.data, "No message telling you you are already logged in.")
def test_non_existing_drafts_in_navbar(self): """Testing that no hyperlink to the /drafts page appear when no drafts exists. We will do the test for logged in users, because non-logged in users are not supposed to see that hyperlink anyway. This hyperlink is supposed to be displayed in the navbar.""" login(self.tester) response = self.tester.get('/') self.assertNotIn(b'<a class="nav-link" href="/drafts">Drafts</a>', response.data, "Hyperlink to the /drafts page can not be found" " in the navbar")
def test_logout_in_navbar(self): """Testing the presence of the /logout page hyperlink. Will only be displayed in the navbar to logged in users. """ response = self.tester.get('/') self.assertNotIn(b'<a class="nav-link" href="/logout">Logout</a>', response.data, "Hyperlink to the /logout page can still be found" " in the menu") login(self.tester) response = self.tester.get('/') self.assertIn(b'<a class="nav-link" href="/logout">Logout</a>', response.data, "Hyperlink to the /logout page can not be found" " in the menu")
def test_failed_login(self): """Testing that a user is blocked from login in using an invalid password. """ response = login(self.tester, password="******") self.assertIn(b'Incorrect password', response.data, "No message telling you you entered a wrong password.")
def test_existing_drafts_in_navbar(self): """Testing the presence of the /draft page hyperlink in the navbar. It is supposed to show in the navbar for logged in users only. The hyperlink can only be displayed when a draft already exist.""" dummy_post(is_published=False) response = self.tester.get('/') self.assertNotIn(b'<a class="nav-link" href="/drafts">Drafts</a>', response.data, "Hyperlink to the /drafts page can still be found" " in the menu for non-logged in users.") login(self.tester) response = self.tester.get('/') self.assertIn(b'<a class="nav-link" href="/drafts">Drafts</a>', response.data, "Hyperlink to the /drafts page can not be found" " in the menu for logged-in users.")
def test_create_post_in_navbar(self): """Testing the presence of the /create_post hyperlink. Will only be displayed in the navbar to logged in users. """ response = self.tester.get('/') self.assertNotIn(b'<a class="nav-link" href="/create_post">Create ' b'Post</a>', response.data, "Hyperlink to the /create_post page can still be " "found in the menu") login(self.tester) response = self.tester.get('/') self.assertIn(b'<a class="nav-link" href="/create_post">' b'Create Post</a>', response.data, "Hyperlink to the /create_post page can not be found" " in the menu")
def test_successful_login(self): """Test that a user can log into the app after inputting the right password. """ with self.app.test_client() as tester: response = login(tester) self.assertEqual(request.path, url_for('main.index'), "Redirect to the /index page failed.") self.assertIn(b'You are now logged', response.data, "Login failed.")
def test_controls_dropdown_menu(self): """Testing the ``Controls`` dropdown menu in the navbar. This menu is only shown to logged in users. The test will verify the presence of the menu and the four hyperlinks it should contain: - The static ``Search Bar`` hyperlink. - The static ``Categories`` hyperlink. - The static ``Socials`` hyperlink. - The dynamic ``Widgets Order`` hyperlink. This item only appears when at least two widgets have been assigned to the sidebar. """ login(self.tester) response = self.tester.get('/') self.assertIn(b'Controls\n </a>', response.data, "The ``Controls`` dropdown menu can not be found" " in the navbar.") self.assertIn(b'<a class="dropdown-item" href="/controls/search_bar">' b'Search Bar</a>', response.data, "The ``Search Bar`` hyperlink can not be found in the" " ``Controls`` dropdown menu in the navbar.") self.assertIn(b'<a class="dropdown-item" href="/controls/categories">' b'Categories</a>', response.data, "The ``Categories`` hyperlink can not be found in the" " ``Controls`` dropdown menu in the navbar.") self.assertIn(b'<a class="dropdown-item" href="/controls/categories">' b'Categories</a>', response.data, "The ``Categories`` hyperlink can not be found in the" " ``Controls`` dropdown menu in the navbar.") self.assertNotIn(b'<a class="dropdown-item" href="/controls/' b'widgets_order">Widgets Order</a>', response.data, "The ``Widgets Order`` hyperlink can still be found" " in the ``Controls`` dropdown menu in the navbar.") dmc = dummy_content_widget() dmc.add_to_or_remove_from_sidebar() dummy_post() response = self.tester.get('/') self.assertIn(b'<a class="dropdown-item" href="/controls/' b'widgets_order">Widgets Order</a>', response.data, "The ``Widgets Order`` hyperlink can not be found in " "the ``Controls`` dropdown menu in the navbar.")
def test_post_url(self): """Testing the ``post_url`` macro. """ self.app.config['LOGIN_DISABLED'] = False response = self.tester.get('/') self.assertIn( b'<a href="/dummy_post">Dummy post</a>', response.data, "Title of the post on the /index page is not leading " "to the post detail for anonymous users.") login(self.tester) response = self.tester.get('/') self.assertIn( b'<a href="/dummy_post/edit_post">Dummy post</a>', response.data, "Title of the post on the /index page is not leading " "to the post edit page for logged-in users.") response = self.tester.get('/drafts') self.assertIn( b'<a href="/draft_dummy_post/edit_post">Dummy post</a>', response.data, "Title of the post on the /drafts page is not leading " "to the post edit page.")
def test_content_widgets_dropdown_menu(self): """Testing the ``Content Widgets`` dropdown menu in the navbar. This menu is only shown to logged in users. The test will verify the presence of the menu and the two hyperlinks it should contain: - The static ``Create Widget`` hyperlink. - The dynamic ``Widgets Index`` hyperlink. This item only appears when at least one widget have already been created. """ login(self.tester) response = self.tester.get('/') self.assertIn(b'<a class="nav-link dropdown-toggle" href="#" ' b'id="ContentWidgetsDropdown" role="button" ' b'data-toggle="dropdown" aria-haspopup="true" ' b'aria-expanded="false">\n Content Widgets', response.data, "The ``Content Widgets`` dropdown menu can not be found" " in the navbar.") self.assertIn(b'<a class="dropdown-item" ' b'href="/create_content_widget">Create Widget</a>', response.data, "The ``Create Widget`` hyperlink can not be found in the" " ``Content Widgets`` dropdown menu in the navbar.") self.assertNotIn(b'<a class="dropdown-item" ' b'href="/content_widgets">Widgets Index</a>', response.data, "The ``Widgets Index`` hyperlink can still be found" " in the ``Content Widgets`` dropdown menu in the" " navbar.") dummy_content_widget() response = self.tester.get('/') self.assertIn(b'<a class="dropdown-item" ' b'href="/content_widgets">Widgets Index</a>', response.data, "The ``Widgets Index`` hyperlink can not be found" " in the ``Content Widgets`` dropdown menu in the " "navbar.")