redirect already logged in user

This commit is contained in:
Di Wu 2015-11-28 00:35:02 -08:00
parent 81116431dd
commit 427a0421c8
3 changed files with 23 additions and 7 deletions

View File

@ -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

View File

@ -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:

View File

@ -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: