Skip to content

Instantly share code, notes, and snippets.

@ArcTanSusan
Last active August 29, 2015 14:05
Show Gist options
  • Save ArcTanSusan/9e13f4abd00fac8dc149 to your computer and use it in GitHub Desktop.
Save ArcTanSusan/9e13f4abd00fac8dc149 to your computer and use it in GitHub Desktop.
OH/account/tests --> test_logout_web
# I use both django web-test AND django's client session to test that a user can be logged in and then logged out.
class Login(WebTest):
# {{{
fixtures = ['user-paulproteus', 'person-paulproteus']
def test_login(self):
user = authenticate(username='paulproteus', password="paulproteus's unbreakable password")
self.assert_(user and user.is_active)
def test_logout_web(self):
# Test the logout feature by inspecting Django's client session. Log in first before you test the log out feature.
self.client = Client()
username = 'paulproteus'
password = "paulproteus's unbreakable password"
self.client.login(username=username, password=password)
# Check that the user is indeed still logged in
self.assertEqual(1, self.client.session.get('_auth_user_id'))
# Log out the user
self.client.logout()
# Test that user is indeed logged out by checking the client session object
self.assertNotEqual(1, self.client.session.get('_auth_user_id'))
# Test that the links are expectedly present or expectedly absent by using django web-test. Log in first before you test the log out feature.
search_page = self.app.get('/search/')
self.assertIn('log in', search_page.content)
login_page = search_page.click('log in')
self.assertNotIn('log out', login_page.content)
login_page_old = login_page.click('Log in with a password')
login_page_form = login_page_old.form
login_page_form['username'] = username
login_page_form['password'] = password
login_page_form.submit()
# Go to ANY page to confirm that 'log out' link is present and 'log in' link is absent
search_page = self.app.get('/search/')
self.assertIn('log out', search_page.content)
self.assertNotIn('log in', search_page.content)
# After the log out link is clicked, go to ANY page to confirm that the log out link is gone and log in link is present instead
logout_page = search_page.click('log out')
search_page = self.app.get('/search/')
self.assertIn('log in', search_page.content)
self.assertNotIn('log out', search_page.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment