-
-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathtest_commands.py
82 lines (66 loc) · 3.22 KB
/
test_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
from io import StringIO
from django.core.management import CommandError, call_command
from django.test import TestCase
from django_otp import devices_for_user
from .utils import UserMixin
class DisableCommandTest(UserMixin, TestCase):
def _assert_raises(self, err_type, err_message):
return self.assertRaisesMessage(err_type, err_message)
def test_raises(self):
stdout = StringIO()
stderr = StringIO()
with self._assert_raises(CommandError, 'User "some_username" does not exist'):
call_command(
'two_factor_disable', 'some_username',
stdout=stdout, stderr=stderr)
with self._assert_raises(CommandError, 'User "other_username" does not exist'):
call_command(
'two_factor_disable', 'other_username', 'some_username',
stdout=stdout, stderr=stderr)
def test_disable_single(self):
user = self.create_user()
self.enable_otp(user)
call_command('two_factor_disable', 'bouke@example.com')
self.assertEqual(list(devices_for_user(user)), [])
def test_happy_flow_multiple(self):
usernames = ['user%d@example.com' % i for i in range(0, 3)]
users = [self.create_user(username) for username in usernames]
[self.enable_otp(user) for user in users]
call_command('two_factor_disable', *usernames[:2])
self.assertEqual(list(devices_for_user(users[0])), [])
self.assertEqual(list(devices_for_user(users[1])), [])
self.assertNotEqual(list(devices_for_user(users[2])), [])
class StatusCommandTest(UserMixin, TestCase):
def _assert_raises(self, err_type, err_message):
return self.assertRaisesMessage(err_type, err_message)
def setUp(self):
super().setUp()
os.environ['DJANGO_COLORS'] = 'nocolor'
def test_raises(self):
stdout = StringIO()
stderr = StringIO()
with self._assert_raises(CommandError, 'User "some_username" does not exist'):
call_command(
'two_factor_status', 'some_username',
stdout=stdout, stderr=stderr)
with self._assert_raises(CommandError, 'User "other_username" does not exist'):
call_command(
'two_factor_status', 'other_username', 'some_username',
stdout=stdout, stderr=stderr)
def test_status_single(self):
user = self.create_user()
stdout = StringIO()
call_command('two_factor_status', 'bouke@example.com', stdout=stdout)
self.assertEqual(stdout.getvalue(), 'bouke@example.com: disabled\n')
stdout = StringIO()
self.enable_otp(user)
call_command('two_factor_status', 'bouke@example.com', stdout=stdout)
self.assertEqual(stdout.getvalue(), 'bouke@example.com: enabled\n')
def test_status_mutiple(self):
users = [self.create_user(n) for n in ['user0@example.com', 'user1@example.com']]
self.enable_otp(users[0])
stdout = StringIO()
call_command('two_factor_status', 'user0@example.com', 'user1@example.com', stdout=stdout)
self.assertEqual(stdout.getvalue(), 'user0@example.com: enabled\n'
'user1@example.com: disabled\n')