Use BaseTestCase in test_login, less repetition

This commit is contained in:
Pēteris Caune 2019-01-14 12:54:42 +02:00
parent 965998df18
commit 16bff94fab
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2

View File

@ -1,34 +1,23 @@
from django.contrib.auth.models import User
from django.core import mail
from django.test import TestCase
from hc.accounts.models import Profile, Project
from hc.api.models import Check
from django.conf import settings from django.conf import settings
from django.core import mail
from hc.api.models import Check
from hc.test import BaseTestCase
class LoginTestCase(TestCase): class LoginTestCase(BaseTestCase):
def test_it_sends_link(self): def test_it_sends_link(self):
alice = User(username="alice", email="alice@example.org")
alice.save()
form = {"identity": "alice@example.org"} form = {"identity": "alice@example.org"}
r = self.client.post("/accounts/login/", form) r = self.client.post("/accounts/login/", form)
self.assertRedirects(r, "/accounts/login_link_sent/") self.assertRedirects(r, "/accounts/login_link_sent/")
# Alice should be the only existing user
self.assertEqual(User.objects.count(), 1)
# And email should have been sent # And email should have been sent
self.assertEqual(len(mail.outbox), 1) self.assertEqual(len(mail.outbox), 1)
subject = "Log in to %s" % settings.SITE_NAME subject = "Log in to %s" % settings.SITE_NAME
self.assertEqual(mail.outbox[0].subject, subject) self.assertEqual(mail.outbox[0].subject, subject)
def test_it_sends_link_with_next(self): def test_it_sends_link_with_next(self):
alice = User(username="alice", email="alice@example.org")
alice.save()
form = {"identity": "alice@example.org"} form = {"identity": "alice@example.org"}
r = self.client.post("/accounts/login/?next=/integrations/add_slack/", form) r = self.client.post("/accounts/login/?next=/integrations/add_slack/", form)
@ -45,25 +34,15 @@ class LoginTestCase(TestCase):
assert "bad_link" not in self.client.session assert "bad_link" not in self.client.session
def test_it_ignores_case(self): def test_it_ignores_case(self):
alice = User(username="alice", email="alice@example.org")
alice.save()
form = {"identity": "ALICE@EXAMPLE.ORG"} form = {"identity": "ALICE@EXAMPLE.ORG"}
r = self.client.post("/accounts/login/", form) r = self.client.post("/accounts/login/", form)
self.assertRedirects(r, "/accounts/login_link_sent/") self.assertRedirects(r, "/accounts/login_link_sent/")
# There should be exactly one user: self.profile.refresh_from_db()
self.assertEqual(User.objects.count(), 1) self.assertIn("login", self.profile.token)
profile = Profile.objects.for_user(alice)
self.assertIn("login", profile.token)
def test_it_handles_password(self): def test_it_handles_password(self):
alice = User(username="alice", email="alice@example.org")
alice.set_password("password")
alice.save()
form = { form = {
"action": "login", "action": "login",
"email": "alice@example.org", "email": "alice@example.org",
@ -74,12 +53,7 @@ class LoginTestCase(TestCase):
self.assertRedirects(r, "/checks/") self.assertRedirects(r, "/checks/")
def test_it_handles_password_login_with_redirect(self): def test_it_handles_password_login_with_redirect(self):
alice = User(username="alice", email="alice@example.org") check = Check.objects.create(user=self.alice, project=self.project)
alice.set_password("password")
alice.save()
project = Project.objects.create(owner=alice)
check = Check.objects.create(user=alice, project=project)
form = { form = {
"action": "login", "action": "login",
@ -97,10 +71,6 @@ class LoginTestCase(TestCase):
self.assertRedirects(r, s) self.assertRedirects(r, s)
def test_it_handles_bad_next_parameter(self): def test_it_handles_bad_next_parameter(self):
alice = User(username="alice", email="alice@example.org")
alice.set_password("password")
alice.save()
form = { form = {
"action": "login", "action": "login",
"email": "alice@example.org", "email": "alice@example.org",
@ -111,10 +81,6 @@ class LoginTestCase(TestCase):
self.assertRedirects(r, "/checks/") self.assertRedirects(r, "/checks/")
def test_it_handles_wrong_password(self): def test_it_handles_wrong_password(self):
alice = User(username="alice", email="alice@example.org")
alice.set_password("password")
alice.save()
form = { form = {
"action": "login", "action": "login",
"email": "alice@example.org", "email": "alice@example.org",