Skip to content

Instantly share code, notes, and snippets.

@vinigracindo
Created November 24, 2015 18:17
Show Gist options
  • Save vinigracindo/0df7958b384c58eebd10 to your computer and use it in GitHub Desktop.
Save vinigracindo/0df7958b384c58eebd10 to your computer and use it in GitHub Desktop.
def test_login_page(self):
"""
Test Login Page
"""
# Faz um requisição GET
response = self.client.get(reverse("login"))
# Checa o status.
self.assertEqual(response.status_code, 200)
# Checa se o template login.html é renderizado após a requisição GET
# Usuários não autenticados são redireiconados para o login.html
self.assertTemplateUsed(response, 'login.html')
# Realiza uma requisição POST autenticando o usuário
response = self.client.post(reverse("login"), {'username': 'unknown', 'password': 'doesnotexist'})
# Verifica se o usuário não foi autenticado, pois o usuário não existe.
self.assertNotIn('_auth_user_id', self.client.session)
# Checa se o html continua sendo o login.html, pois o usuário
# não foi autenticado
self.assertTemplateUsed(response, 'login.html')
# Realiza uma requisição POST autenticando o usuário
response = self.client.post(reverse("login"), {'username': 'userjohn', 'password': 'mypass'})
# Verifica se o usuário foi autenticado
self.assertIn('_auth_user_id', self.client.session)
# Checa se o usuário, após a autenticação, foi redirecionado.
self.assertEqual(response.status_code, 302)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment