Browse Source

Demo checks shown on welcome page are not saved to database. User's first check is created when creating the user.

pull/178/head
Pēteris Caune 7 years ago
parent
commit
9cbd0138da
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
4 changed files with 10 additions and 83 deletions
  1. +2
    -29
      hc/accounts/tests/test_login.py
  2. +7
    -27
      hc/accounts/views.py
  3. +0
    -12
      hc/front/tests/test_basics.py
  4. +1
    -15
      hc/front/views.py

+ 2
- 29
hc/accounts/tests/test_login.py View File

@ -10,13 +10,6 @@ from django.conf import settings
class LoginTestCase(TestCase):
def test_it_sends_link(self):
check = Check()
check.save()
session = self.client.session
session["welcome_code"] = str(check.code)
session.save()
form = {"identity": "[email protected]"}
r = self.client.post("/accounts/login/", form)
@ -31,34 +24,14 @@ class LoginTestCase(TestCase):
self.assertEqual(mail.outbox[0].subject, subject)
# And check should be associated with the new user
check_again = Check.objects.get(code=check.code)
assert check_again.user
check = Check.objects.get()
self.assertEqual(check.name, "My First Check")
def test_it_pops_bad_link_from_session(self):
self.client.session["bad_link"] = True
self.client.get("/accounts/login/")
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 = {"identity": "[email protected]"}
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)
@override_settings(REGISTRATION_OPEN=False)
def test_it_obeys_registration_open(self):


+ 7
- 27
hc/accounts/views.py View File

@ -33,35 +33,19 @@ def _make_user(email):
# Ensure a profile gets created
Profile.objects.for_user(user)
channel = Channel()
channel.user = user
check = Check(user=user)
check.name = "My First Check"
check.save()
channel = Channel(user=user)
channel.kind = "email"
channel.value = email
channel.email_verified = True
channel.save()
return user
def _associate_demo_check(request, user):
if "welcome_code" not in request.session:
return
try:
check = Check.objects.get(code=request.session["welcome_code"])
except Check.DoesNotExist:
return
channel.checks.add(check)
# Only associate demo check if it doesn't have an owner already.
if check.user:
return
check.user = user
check.save()
check.assign_all_channels()
del request.session["welcome_code"]
return user
def _ensure_own_team(request):
@ -94,7 +78,6 @@ def login(request, show_password=False):
except User.DoesNotExist:
if settings.REGISTRATION_OPEN:
user = _make_user(email)
_associate_demo_check(request, user)
else:
bad_credentials = True
@ -143,9 +126,6 @@ def check_token(request, username, token):
if request.method == "POST":
user = authenticate(username=username, token=token)
if user is not None and user.is_active:
# This should get rid of "welcome_code" in session
request.session.flush()
user.profile.token = ""
user.profile.save()
auth_login(request, user)


+ 0
- 12
hc/front/tests/test_basics.py View File

@ -10,18 +10,6 @@ class BasicsTestCase(TestCase):
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()
@override_settings(REGISTRATION_OPEN=False)
def test_it_obeys_registration_open(self):
r = self.client.get("/")


+ 1
- 15
hc/front/views.py View File

@ -128,25 +128,11 @@ def switch_channel(request, code, channel_code):
return HttpResponse()
def _welcome_check(request):
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()
request.session["welcome_code"] = str(check.code)
return check
def index(request):
if request.user.is_authenticated:
return redirect("hc-checks")
check = _welcome_check(request)
check = Check()
ctx = {
"page": "welcome",


Loading…
Cancel
Save