make welcome code logic resilient

This commit is contained in:
Di Wu 2015-11-15 15:58:48 -08:00
parent c2e9bab536
commit 5c1d21f91e
2 changed files with 19 additions and 4 deletions

View File

@ -1,4 +1,5 @@
from django.test import TestCase
from hc.api.models import Check
class BasicsTestCase(TestCase):
@ -6,3 +7,15 @@ class BasicsTestCase(TestCase):
def test_it_shows_welcome(self):
r = self.client.get("/")
self.assertContains(r, "Get Notified", status_code=200)
def test_welcome_code(self):
r = self.client.get("/")
code = self.client.session["welcome_code"]
assert Check.objects.filter(code=code).exists()
self.client.session["welcome_code"] = "x"
r = self.client.get("/")
code = self.client.session["welcome_code"]
assert r.status_code == 200
assert code != "x"
assert Check.objects.filter(code=code).exists()

View File

@ -27,14 +27,16 @@ def index(request):
if request.user.is_authenticated():
return redirect("hc-checks")
if "welcome_code" not in request.session:
check = None
if "welcome_code" in request.session:
code = request.session["welcome_code"]
check = Check.objects.filter(code=code).first()
if check is None:
check = Check()
check.save()
code = str(check.code)
request.session["welcome_code"] = code
else:
code = request.session["welcome_code"]
check = Check.objects.get(code=code)
ctx = {
"page": "welcome",