From 9cbd0138da1e151bdf5c4128dcf2521826eee5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 14 Jun 2018 23:42:39 +0300 Subject: [PATCH] Demo checks shown on welcome page are not saved to database. User's first check is created when creating the user. --- hc/accounts/tests/test_login.py | 31 ++---------------------------- hc/accounts/views.py | 34 +++++++-------------------------- hc/front/tests/test_basics.py | 12 ------------ hc/front/views.py | 16 +--------------- 4 files changed, 10 insertions(+), 83 deletions(-) diff --git a/hc/accounts/tests/test_login.py b/hc/accounts/tests/test_login.py index 34dbfc7b..e7a7e66c 100644 --- a/hc/accounts/tests/test_login.py +++ b/hc/accounts/tests/test_login.py @@ -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": "alice@example.org"} 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": "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) @override_settings(REGISTRATION_OPEN=False) def test_it_obeys_registration_open(self): diff --git a/hc/accounts/views.py b/hc/accounts/views.py index 92ded0b2..2b0dee56 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -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) diff --git a/hc/front/tests/test_basics.py b/hc/front/tests/test_basics.py index 7de2014e..43f9ad14 100644 --- a/hc/front/tests/test_basics.py +++ b/hc/front/tests/test_basics.py @@ -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("/") diff --git a/hc/front/views.py b/hc/front/views.py index e12ebfeb..63f00e60 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -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",