Merge pull request #14 from BetterWorks/loggedIn

redirect already logged in user
This commit is contained in:
Pēteris Caune 2015-12-02 13:56:05 +02:00
commit 404744f235
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

@ -165,8 +165,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: