Skip to content

Commit dbfecbf

Browse files
committed
added test for piccolo user create
1 parent 75b87f5 commit dbfecbf

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

piccolo/apps/user/commands/create.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,53 @@
44
from piccolo.apps.user.tables import BaseUser
55

66

7-
def create():
8-
"""
9-
Create a new user.
10-
"""
7+
def get_username() -> str:
118
default_username = getuser()
129
username = input(f"Enter username ({default_username}):\n")
13-
username = default_username if not username else username
10+
return default_username if not username else username
1411

15-
email = input("Enter email:\n")
1612

17-
password = getpass("Enter password:\n")
18-
confirmed_password = getpass("Confirm password:\n")
13+
def get_email() -> str:
14+
return input("Enter email:\n")
1915

20-
if not password == confirmed_password:
21-
print("Passwords don't match!")
22-
sys.exit(1)
2316

24-
if len(password) < 4:
25-
print("The password is too short")
26-
sys.exit(1)
17+
def get_password() -> str:
18+
return getpass("Enter password:\n")
19+
2720

21+
def get_confirmed_password() -> str:
22+
return getpass("Confirm password:\n")
23+
24+
25+
def get_is_admin() -> bool:
2826
while True:
2927
admin = input("Admin user? Enter y or n:\n")
3028
if admin in ("y", "n"):
3129
break
3230
else:
3331
print("Unrecognised option")
3432

35-
is_admin = admin == "y"
33+
return admin == "y"
34+
35+
36+
def create():
37+
"""
38+
Create a new user.
39+
"""
40+
username = get_username()
41+
email = get_email()
42+
password = get_password()
43+
confirmed_password = get_confirmed_password()
44+
45+
if not password == confirmed_password:
46+
print("Passwords don't match!")
47+
sys.exit(1)
48+
49+
if len(password) < 4:
50+
print("The password is too short")
51+
sys.exit(1)
52+
53+
is_admin = get_is_admin()
3654

3755
user = BaseUser(
3856
username=username,

tests/apps/user/__init__.py

Whitespace-only changes.

tests/apps/user/commands/__init__.py

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
4+
from piccolo.apps.user.commands.create import create
5+
from piccolo.apps.user.tables import BaseUser
6+
7+
8+
class TestCreate(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.create.get_username",
17+
return_value="bob123",
18+
)
19+
@patch(
20+
"piccolo.apps.user.commands.create.get_email",
21+
return_value="[email protected]",
22+
)
23+
@patch(
24+
"piccolo.apps.user.commands.create.get_password",
25+
return_value="password123",
26+
)
27+
@patch(
28+
"piccolo.apps.user.commands.create.get_confirmed_password",
29+
return_value="password123",
30+
)
31+
@patch(
32+
"piccolo.apps.user.commands.create.get_is_admin", return_value=True,
33+
)
34+
def test_create(self, *args, **kwargs):
35+
create()
36+
37+
self.assertTrue(
38+
BaseUser.exists()
39+
.where(
40+
(BaseUser.admin == True)
41+
& (BaseUser.username == "bob123")
42+
& (BaseUser.email == "[email protected]") # noqa
43+
)
44+
.run_sync()
45+
)

0 commit comments

Comments
 (0)