@ -1,45 +1,34 @@ | |||||
from django.contrib.auth.models import User | from django.contrib.auth.models import User | ||||
from django.core import mail | from django.core import mail | ||||
from django.test import TestCase | from django.test import TestCase | ||||
from django.test.utils import override_settings | |||||
from hc.accounts.models import Profile | from hc.accounts.models import Profile | ||||
from hc.api.models import Check | |||||
from django.conf import settings | from django.conf import settings | ||||
class LoginTestCase(TestCase): | class LoginTestCase(TestCase): | ||||
def test_it_sends_link(self): | def test_it_sends_link(self): | ||||
alice = User(username="alice", email="[email protected]") | |||||
alice.save() | |||||
form = {"identity": "[email protected]"} | form = {"identity": "[email protected]"} | ||||
r = self.client.post("/accounts/login/", form) | r = self.client.post("/accounts/login/", form) | ||||
assert r.status_code == 302 | assert r.status_code == 302 | ||||
# An user should have been created | |||||
# Alice should be the only existing user | |||||
self.assertEqual(User.objects.count(), 1) | self.assertEqual(User.objects.count(), 1) | ||||
# And email sent | |||||
# And email should have been sent | |||||
self.assertEqual(len(mail.outbox), 1) | self.assertEqual(len(mail.outbox), 1) | ||||
subject = "Log in to %s" % settings.SITE_NAME | subject = "Log in to %s" % settings.SITE_NAME | ||||
self.assertEqual(mail.outbox[0].subject, subject) | self.assertEqual(mail.outbox[0].subject, subject) | ||||
# And check should be associated with the new user | |||||
check = Check.objects.get() | |||||
self.assertEqual(check.name, "My First Check") | |||||
def test_it_pops_bad_link_from_session(self): | def test_it_pops_bad_link_from_session(self): | ||||
self.client.session["bad_link"] = True | self.client.session["bad_link"] = True | ||||
self.client.get("/accounts/login/") | self.client.get("/accounts/login/") | ||||
assert "bad_link" not in self.client.session | assert "bad_link" not in self.client.session | ||||
@override_settings(REGISTRATION_OPEN=False) | |||||
def test_it_obeys_registration_open(self): | |||||
form = {"identity": "[email protected]"} | |||||
r = self.client.post("/accounts/login/", form) | |||||
assert r.status_code == 200 | |||||
self.assertContains(r, "Incorrect email") | |||||
def test_it_ignores_case(self): | def test_it_ignores_case(self): | ||||
alice = User(username="alice", email="[email protected]") | alice = User(username="alice", email="[email protected]") | ||||
alice.save() | alice.save() | ||||
@ -54,3 +43,31 @@ class LoginTestCase(TestCase): | |||||
profile = Profile.objects.for_user(alice) | profile = Profile.objects.for_user(alice) | ||||
self.assertIn("login", profile.token) | self.assertIn("login", profile.token) | ||||
def test_it_handles_password(self): | |||||
alice = User(username="alice", email="[email protected]") | |||||
alice.set_password("password") | |||||
alice.save() | |||||
form = { | |||||
"action": "login", | |||||
"email": "[email protected]", | |||||
"password": "password" | |||||
} | |||||
r = self.client.post("/accounts/login/", form) | |||||
self.assertEqual(r.status_code, 302) | |||||
def test_it_handles_wrong_password(self): | |||||
alice = User(username="alice", email="[email protected]") | |||||
alice.set_password("password") | |||||
alice.save() | |||||
form = { | |||||
"action": "login", | |||||
"email": "[email protected]", | |||||
"password": "wrong password" | |||||
} | |||||
r = self.client.post("/accounts/login/", form) | |||||
self.assertContains(r, "Incorrect email or password") |
@ -0,0 +1,55 @@ | |||||
from django.contrib.auth.models import User | |||||
from django.core import mail | |||||
from django.test import TestCase | |||||
from django.test.utils import override_settings | |||||
from hc.api.models import Check | |||||
from django.conf import settings | |||||
class SignupTestCase(TestCase): | |||||
def test_it_sends_link(self): | |||||
form = {"identity": "[email protected]"} | |||||
r = self.client.post("/accounts/signup/", form) | |||||
self.assertContains(r, "Account created") | |||||
# 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) | |||||
# And check should be associated with the new user | |||||
check = Check.objects.get() | |||||
self.assertEqual(check.name, "My First Check") | |||||
@override_settings(REGISTRATION_OPEN=False) | |||||
def test_it_obeys_registration_open(self): | |||||
form = {"identity": "[email protected]"} | |||||
r = self.client.post("/accounts/signup/", form) | |||||
self.assertEqual(r.status_code, 403) | |||||
def test_it_ignores_case(self): | |||||
form = {"identity": "[email protected]"} | |||||
self.client.post("/accounts/signup/", form) | |||||
# There should be exactly one user: | |||||
q = User.objects.filter(email="[email protected]") | |||||
self.assertTrue(q.exists) | |||||
def test_it_checks_for_existing_users(self): | |||||
alice = User(username="alice", email="[email protected]") | |||||
alice.save() | |||||
form = {"identity": "[email protected]"} | |||||
r = self.client.post("/accounts/signup/", form) | |||||
self.assertContains(r, "already exists") | |||||
def test_it_checks_syntax(self): | |||||
form = {"identity": "alice at example org"} | |||||
r = self.client.post("/accounts/signup/", form) | |||||
self.assertContains(r, "Enter a valid email address") |
@ -0,0 +1,20 @@ | |||||
$(function () { | |||||
$("#signup-go").on("click", function() { | |||||
var email = $("#signup-email").val(); | |||||
var token = $('input[name=csrfmiddlewaretoken]').val(); | |||||
$.ajax({ | |||||
url: "/accounts/signup/", | |||||
type: "post", | |||||
headers: {"X-CSRFToken": token}, | |||||
data: {"identity": email}, | |||||
success: function(data) { | |||||
$("#signup-result").html(data).show(); | |||||
} | |||||
}); | |||||
return false; | |||||
}); | |||||
}); |
@ -0,0 +1,7 @@ | |||||
{% for error in form.identity.errors %} | |||||
<p class="text-danger">{{ error }}</p> | |||||
{% endfor %} | |||||
{% if created %} | |||||
<p class="text-success">Account created, please check your email!</p> | |||||
{% endif %} |
@ -0,0 +1,33 @@ | |||||
<div id="signup-modal" class="modal"> | |||||
<div class="modal-dialog"> | |||||
<div class="modal-content"> | |||||
<div class="modal-header"> | |||||
<button type="button" class="close" data-dismiss="modal">×</button> | |||||
</div> | |||||
<div class="modal-body"> | |||||
<h1>Create Your Account</h1> | |||||
<p>Enter your <strong>email address</strong>.</p> | |||||
<input | |||||
type="email" | |||||
class="form-control input-lg" | |||||
id="signup-email" | |||||
value="{{ magic_form.email.value|default:"" }}" | |||||
placeholder="[email protected]" | |||||
autocomplete="off"> | |||||
<p id="link-instruction"> | |||||
We will email you a magic sign in link. | |||||
</p> | |||||
{% csrf_token %} | |||||
<button id="signup-go" class="btn btn-lg btn-primary btn-block"> | |||||
Email Me a Link | |||||
</button> | |||||
<div id="signup-result"></div> | |||||
</div> | |||||
</div> | |||||
</div> | |||||
</div> |