2019-10-14 16:12:56 +01:00
|
|
|
"""recovery tests"""
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
from django.core.management import call_command
|
|
|
|
from django.shortcuts import reverse
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2020-05-16 15:11:53 +01:00
|
|
|
from passbook.core.models import Token, User
|
2020-05-11 14:01:14 +01:00
|
|
|
from passbook.lib.config import CONFIG
|
2019-10-14 16:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestRecovery(TestCase):
|
|
|
|
"""recovery tests"""
|
|
|
|
|
|
|
|
def setUp(self):
|
2019-12-31 11:51:16 +00:00
|
|
|
self.user = User.objects.create_user(username="recovery-test-user")
|
2019-10-14 16:12:56 +01:00
|
|
|
|
|
|
|
def test_create_key(self):
|
|
|
|
"""Test creation of a new key"""
|
2020-05-11 14:01:14 +01:00
|
|
|
CONFIG.update_from_dict({"domain": "testserver"})
|
2019-10-14 16:12:56 +01:00
|
|
|
out = StringIO()
|
2020-05-16 15:11:53 +01:00
|
|
|
self.assertEqual(len(Token.objects.all()), 0)
|
2019-12-31 11:51:16 +00:00
|
|
|
call_command("create_recovery_key", "1", self.user.username, stdout=out)
|
2020-05-16 15:11:53 +01:00
|
|
|
self.assertIn("https://testserver/recovery/use-token/", out.getvalue())
|
|
|
|
self.assertEqual(len(Token.objects.all()), 1)
|
2019-10-14 16:12:56 +01:00
|
|
|
|
|
|
|
def test_recovery_view(self):
|
|
|
|
"""Test recovery view"""
|
2019-11-08 12:56:09 +00:00
|
|
|
out = StringIO()
|
2019-12-31 11:51:16 +00:00
|
|
|
call_command("create_recovery_key", "1", self.user.username, stdout=out)
|
2020-05-16 15:11:53 +01:00
|
|
|
token = Token.objects.first()
|
2019-12-31 11:51:16 +00:00
|
|
|
self.client.get(
|
2020-05-20 08:17:06 +01:00
|
|
|
reverse(
|
|
|
|
"passbook_recovery:use-token", kwargs={"uuid": str(token.token_uuid)}
|
|
|
|
)
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
2020-05-16 15:11:53 +01:00
|
|
|
self.assertEqual(int(self.client.session["_auth_user_id"]), token.user.pk)
|