@ -1,6 +1,5 @@ | |||||
from unittest.mock import patch | from unittest.mock import patch | ||||
from django.core.signing import TimestampSigner | |||||
from hc.test import BaseTestCase | from hc.test import BaseTestCase | ||||
from hc.accounts.models import Credential | from hc.accounts.models import Credential | ||||
@ -11,11 +10,6 @@ class AddCredentialTestCase(BaseTestCase): | |||||
self.url = "/accounts/two_factor/add/" | self.url = "/accounts/two_factor/add/" | ||||
def _set_sudo_flag(self): | |||||
session = self.client.session | |||||
session["sudo"] = TimestampSigner().sign("active") | |||||
session.save() | |||||
def test_it_requires_sudo_mode(self): | def test_it_requires_sudo_mode(self): | ||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
@ -24,7 +18,7 @@ class AddCredentialTestCase(BaseTestCase): | |||||
def test_it_shows_form(self): | def test_it_shows_form(self): | ||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
self._set_sudo_flag() | |||||
self.set_sudo_flag() | |||||
r = self.client.get(self.url) | r = self.client.get(self.url) | ||||
self.assertContains(r, "Add Security Key") | self.assertContains(r, "Add Security Key") | ||||
@ -37,7 +31,7 @@ class AddCredentialTestCase(BaseTestCase): | |||||
mock_get_credential_data.return_value = b"dummy-credential-data" | mock_get_credential_data.return_value = b"dummy-credential-data" | ||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
self._set_sudo_flag() | |||||
self.set_sudo_flag() | |||||
payload = { | payload = { | ||||
"name": "My New Key", | "name": "My New Key", | ||||
@ -54,7 +48,7 @@ class AddCredentialTestCase(BaseTestCase): | |||||
def test_it_rejects_bad_base64(self): | def test_it_rejects_bad_base64(self): | ||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
self._set_sudo_flag() | |||||
self.set_sudo_flag() | |||||
payload = { | payload = { | ||||
"name": "My New Key", | "name": "My New Key", | ||||
@ -67,7 +61,7 @@ class AddCredentialTestCase(BaseTestCase): | |||||
def test_it_requires_client_data_json(self): | def test_it_requires_client_data_json(self): | ||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
self._set_sudo_flag() | |||||
self.set_sudo_flag() | |||||
payload = { | payload = { | ||||
"name": "My New Key", | "name": "My New Key", | ||||
@ -9,23 +9,6 @@ from hc.api.models import Check | |||||
class ProfileTestCase(BaseTestCase): | class ProfileTestCase(BaseTestCase): | ||||
def test_it_sends_set_password_link(self): | |||||
self.client.login(username="[email protected]", password="password") | |||||
form = {"set_password": "1"} | |||||
r = self.client.post("/accounts/profile/", form) | |||||
assert r.status_code == 302 | |||||
# profile.token should be set now | |||||
self.profile.refresh_from_db() | |||||
token = self.profile.token | |||||
self.assertTrue(len(token) > 10) | |||||
# And an email should have been sent | |||||
self.assertEqual(len(mail.outbox), 1) | |||||
expected_subject = "Set password on %s" % settings.SITE_NAME | |||||
self.assertEqual(mail.outbox[0].subject, expected_subject) | |||||
def test_it_sends_report(self): | def test_it_sends_report(self): | ||||
check = Check(project=self.project, name="Test Check") | check = Check(project=self.project, name="Test Check") | ||||
check.last_ping = now() | check.last_ping = now() | ||||
@ -1,5 +1,3 @@ | |||||
from django.core.signing import TimestampSigner | |||||
from hc.test import BaseTestCase | from hc.test import BaseTestCase | ||||
from hc.accounts.models import Credential | from hc.accounts.models import Credential | ||||
@ -11,14 +9,15 @@ class RemoveCredentialTestCase(BaseTestCase): | |||||
self.c = Credential.objects.create(user=self.alice, name="Alices Key") | self.c = Credential.objects.create(user=self.alice, name="Alices Key") | ||||
self.url = f"/accounts/two_factor/{self.c.code}/remove/" | self.url = f"/accounts/two_factor/{self.c.code}/remove/" | ||||
def _set_sudo_flag(self): | |||||
session = self.client.session | |||||
session["sudo"] = TimestampSigner().sign("active") | |||||
session.save() | |||||
def test_it_requires_sudo_mode(self): | |||||
self.client.login(username="[email protected]", password="password") | |||||
r = self.client.get(self.url) | |||||
self.assertContains(r, "We have sent a confirmation code") | |||||
def test_it_shows_form(self): | def test_it_shows_form(self): | ||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
self._set_sudo_flag() | |||||
self.set_sudo_flag() | |||||
r = self.client.get(self.url) | r = self.client.get(self.url) | ||||
self.assertContains(r, "Remove Security Key") | self.assertContains(r, "Remove Security Key") | ||||
@ -26,7 +25,7 @@ class RemoveCredentialTestCase(BaseTestCase): | |||||
def test_it_removes_credential(self): | def test_it_removes_credential(self): | ||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
self._set_sudo_flag() | |||||
self.set_sudo_flag() | |||||
r = self.client.post(self.url, {"remove_credential": ""}, follow=True) | r = self.client.post(self.url, {"remove_credential": ""}, follow=True) | ||||
self.assertRedirects(r, "/accounts/profile/") | self.assertRedirects(r, "/accounts/profile/") | ||||
@ -36,7 +35,7 @@ class RemoveCredentialTestCase(BaseTestCase): | |||||
def test_it_checks_owner(self): | def test_it_checks_owner(self): | ||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
self._set_sudo_flag() | |||||
self.set_sudo_flag() | |||||
r = self.client.post(self.url, {"remove_credential": ""}) | r = self.client.post(self.url, {"remove_credential": ""}) | ||||
self.assertEqual(r.status_code, 400) | self.assertEqual(r.status_code, 400) |
@ -2,45 +2,37 @@ from hc.test import BaseTestCase | |||||
class SetPasswordTestCase(BaseTestCase): | class SetPasswordTestCase(BaseTestCase): | ||||
def test_it_shows_form(self): | |||||
token = self.profile.prepare_token("set-password") | |||||
def test_it_requires_sudo_mod(self): | |||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
r = self.client.get("/accounts/set_password/%s/" % token) | |||||
self.assertEqual(r.status_code, 200) | |||||
self.assertContains(r, "Please pick a password") | |||||
r = self.client.get("/accounts/set_password/") | |||||
self.assertContains(r, "We have sent a confirmation code") | |||||
def test_it_checks_token(self): | |||||
self.profile.prepare_token("set-password") | |||||
def test_it_shows_form(self): | |||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
self.set_sudo_flag() | |||||
# GET | |||||
r = self.client.get("/accounts/set_password/invalid-token/") | |||||
self.assertEqual(r.status_code, 400) | |||||
# POST | |||||
r = self.client.post("/accounts/set_password/invalid-token/") | |||||
self.assertEqual(r.status_code, 400) | |||||
r = self.client.get("/accounts/set_password/") | |||||
self.assertContains(r, "Please pick a password") | |||||
def test_it_sets_password(self): | def test_it_sets_password(self): | ||||
token = self.profile.prepare_token("set-password") | |||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
self.set_sudo_flag() | |||||
payload = {"password": "correct horse battery staple"} | payload = {"password": "correct horse battery staple"} | ||||
r = self.client.post("/accounts/set_password/%s/" % token, payload) | |||||
self.assertEqual(r.status_code, 302) | |||||
r = self.client.post("/accounts/set_password/", payload) | |||||
self.assertRedirects(r, "/accounts/profile/") | |||||
old_password = self.alice.password | old_password = self.alice.password | ||||
self.alice.refresh_from_db() | self.alice.refresh_from_db() | ||||
self.assertNotEqual(self.alice.password, old_password) | self.assertNotEqual(self.alice.password, old_password) | ||||
def test_post_checks_length(self): | def test_post_checks_length(self): | ||||
token = self.profile.prepare_token("set-password") | |||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
self.set_sudo_flag() | |||||
payload = {"password": "abc"} | payload = {"password": "abc"} | ||||
r = self.client.post("/accounts/set_password/%s/" % token, payload) | |||||
r = self.client.post("/accounts/set_password/", payload) | |||||
self.assertEqual(r.status_code, 200) | self.assertEqual(r.status_code, 200) | ||||
old_password = self.alice.password | old_password = self.alice.password | ||||
@ -1,13 +0,0 @@ | |||||
{% extends "emails/base.html" %} | |||||
{% load hc_extras %} | |||||
{% block content %} | |||||
Hello,<br /> | |||||
To set up a password for your account on {% site_name %}, please press the | |||||
button below:</p> | |||||
{% endblock %} | |||||
{% block content_more %} | |||||
Regards,<br /> | |||||
The {% site_name %} Team | |||||
{% endblock %} |
@ -1,11 +0,0 @@ | |||||
{% load hc_extras %} | |||||
Hello, | |||||
Here's a link to set a password for your account on {% site_name %}: | |||||
{{ button_url }} | |||||
-- | |||||
Regards, | |||||
{% site_name %} |
@ -1,2 +0,0 @@ | |||||
{% load hc_extras %} | |||||
Set password on {% site_name %} |