forked from GithubBackups/healthchecks
_associate_demo_check was crashing for returning users after cleaning up old checks.
This commit is contained in:
parent
56168b17d0
commit
d9171adb1d
@ -4,6 +4,7 @@ from django.test import TestCase
|
|||||||
from hc.api.models import Check
|
from hc.api.models import Check
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
class LoginTestCase(TestCase):
|
class LoginTestCase(TestCase):
|
||||||
|
|
||||||
def test_it_sends_link(self):
|
def test_it_sends_link(self):
|
||||||
@ -24,7 +25,8 @@ class LoginTestCase(TestCase):
|
|||||||
|
|
||||||
# And email sent
|
# And email sent
|
||||||
self.assertEqual(len(mail.outbox), 1)
|
self.assertEqual(len(mail.outbox), 1)
|
||||||
self.assertEqual(mail.outbox[0].subject, 'Log in to {0}'.format(getattr(settings, "SITE_NAME")))
|
subject = "Log in to %s" % settings.SITE_NAME
|
||||||
|
self.assertEqual(mail.outbox[0].subject, subject)
|
||||||
|
|
||||||
# And check should be associated with the new user
|
# And check should be associated with the new user
|
||||||
check_again = Check.objects.get(code=check.code)
|
check_again = Check.objects.get(code=check.code)
|
||||||
@ -34,3 +36,24 @@ class LoginTestCase(TestCase):
|
|||||||
self.client.session["bad_link"] = True
|
self.client.session["bad_link"] = True
|
||||||
self.client.get("/accounts/login/")
|
self.client.get("/accounts/login/")
|
||||||
assert "bad_link" not in self.client.session
|
assert "bad_link" not in self.client.session
|
||||||
|
|
||||||
|
def test_it_handles_missing_welcome_check(self):
|
||||||
|
|
||||||
|
# This check does not exist in database,
|
||||||
|
# but login should still work.
|
||||||
|
session = self.client.session
|
||||||
|
session["welcome_code"] = "00000000-0000-0000-0000-000000000000"
|
||||||
|
session.save()
|
||||||
|
|
||||||
|
form = {"email": "alice@example.org"}
|
||||||
|
|
||||||
|
r = self.client.post("/accounts/login/", form)
|
||||||
|
assert r.status_code == 302
|
||||||
|
|
||||||
|
# An user should have been created
|
||||||
|
self.assertEqual(User.objects.count(), 1)
|
||||||
|
|
||||||
|
# And email sent
|
||||||
|
self.assertEqual(len(mail.outbox), 1)
|
||||||
|
subject = "Log in to %s" % settings.SITE_NAME
|
||||||
|
self.assertEqual(mail.outbox[0].subject, subject)
|
||||||
|
@ -39,17 +39,24 @@ def _make_user(email):
|
|||||||
|
|
||||||
|
|
||||||
def _associate_demo_check(request, user):
|
def _associate_demo_check(request, user):
|
||||||
if "welcome_code" in request.session:
|
if "welcome_code" not in request.session:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
check = Check.objects.get(code=request.session["welcome_code"])
|
check = Check.objects.get(code=request.session["welcome_code"])
|
||||||
|
except Check.DoesNotExist:
|
||||||
|
return
|
||||||
|
|
||||||
# Only associate demo check if it doesn't have an owner already.
|
# Only associate demo check if it doesn't have an owner already.
|
||||||
if check.user is None:
|
if check.user:
|
||||||
check.user = user
|
return
|
||||||
check.save()
|
|
||||||
|
|
||||||
check.assign_all_channels()
|
check.user = user
|
||||||
|
check.save()
|
||||||
|
|
||||||
del request.session["welcome_code"]
|
check.assign_all_channels()
|
||||||
|
|
||||||
|
del request.session["welcome_code"]
|
||||||
|
|
||||||
|
|
||||||
def login(request):
|
def login(request):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user