diff --git a/hc/accounts/tests/test_check_token.py b/hc/accounts/tests/test_check_token.py index 6084e65f..0f6750dd 100644 --- a/hc/accounts/tests/test_check_token.py +++ b/hc/accounts/tests/test_check_token.py @@ -4,14 +4,25 @@ from django.test import TestCase class CheckTokenTestCase(TestCase): - def test_it_redirects(self): - alice = User(username="alice") - alice.set_password("secret-token") - alice.save() + def setUp(self): + super(CheckTokenTestCase, self).setUp() + + self.alice = User(username="alice") + self.alice.set_password("secret-token") + self.alice.save() + def test_it_redirects(self): r = self.client.get("/accounts/check_token/alice/secret-token/") assert r.status_code == 302 # After login, password should be unusable - alice_again = User.objects.get(username="alice") - assert not alice_again.has_usable_password() + self.alice.refresh_from_db() + assert not self.alice.has_usable_password() + + def test_it_redirects_already_logged_in(self): + # Login + self.client.get("/accounts/check_token/alice/secret-token/") + + # Login again, when already authenticated + r = self.client.get("/accounts/check_token/alice/secret-token/") + assert r.status_code == 302 diff --git a/hc/accounts/views.py b/hc/accounts/views.py index 3372956a..297969b9 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -90,6 +90,10 @@ def login_link_sent(request): def check_token(request, username, token): + if request.user.is_authenticated() and request.user.username == username: + # User is already logged in + return redirect("hc-checks") + user = authenticate(username=username, password=token) if user is not None: if user.is_active: diff --git a/hc/front/views.py b/hc/front/views.py index ecd35135..b4b0a297 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -162,8 +162,9 @@ def log(request, code): # Now go through pings, calculate time gaps, and decorate # the pings list for convenient use in template wrapped = [] + now = timezone.now() for i, ping in enumerate(pings): - prev = timezone.now() if i == 0 else pings[i - 1].created + prev = now if i == 0 else pings[i - 1].created duration = prev - ping.created if duration > check.timeout: