Skip to content

Commit a0b1a1f

Browse files
committed
added test for piccolo user change_password
1 parent dbfecbf commit a0b1a1f

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

piccolo/apps/user/commands/change_password.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import sys
2-
from getpass import getpass
32

43
from piccolo.apps.user.tables import BaseUser
4+
from piccolo.apps.user.commands.create import (
5+
get_username,
6+
get_password,
7+
get_confirmed_password,
8+
)
59

610

711
def change_password():
812
"""
913
Change a user's password.
1014
"""
11-
username = input("Enter username:\n")
12-
13-
password = getpass("Enter password:\n")
14-
confirmed_password = getpass("Confirm password:\n")
15+
username = get_username()
16+
password = get_password()
17+
confirmed_password = get_confirmed_password()
1518

1619
if not password == confirmed_password:
1720
print("Passwords don't match!")
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
4+
from piccolo.apps.user.commands.change_password import change_password
5+
from piccolo.apps.user.tables import BaseUser
6+
7+
8+
class TestChangePassword(TestCase):
9+
def setUp(self):
10+
BaseUser.create_table(if_not_exists=True).run_sync()
11+
12+
def tearDown(self):
13+
BaseUser.alter().drop_table().run_sync()
14+
15+
@patch(
16+
"piccolo.apps.user.commands.change_password.get_username",
17+
return_value="bob123",
18+
)
19+
@patch(
20+
"piccolo.apps.user.commands.change_password.get_password",
21+
return_value="new_password",
22+
)
23+
@patch(
24+
"piccolo.apps.user.commands.change_password.get_confirmed_password",
25+
return_value="new_password",
26+
)
27+
def test_create(self, *args, **kwargs):
28+
user = BaseUser(username="bob123", password="old_password")
29+
user.save().run_sync()
30+
31+
change_password()
32+
33+
self.assertTrue(
34+
BaseUser.login_sync(username="bob123", password="new_password")
35+
is not None
36+
)

0 commit comments

Comments
 (0)