|
1
|
|
|
"""Testing of only DocumentBaseModel from user.py. UserDoc and |
|
2
|
|
|
UserDocUpdate have been indirectly tested in test_user_controller.py""" |
|
3
|
|
|
|
|
4
|
1 |
|
from datetime import datetime |
|
5
|
|
|
|
|
6
|
1 |
|
import pytest |
|
7
|
1 |
|
from pydantic import ValidationError |
|
8
|
|
|
|
|
9
|
1 |
|
from kytos.core.user import DocumentBaseModel, HashSubDoc, UserDoc, hashing |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
1 |
|
def test_document_base_model_dict(): |
|
13
|
|
|
"""Test DocumentBaseModel.model_dump()""" |
|
14
|
1 |
|
_id = "random_id" |
|
15
|
1 |
|
utc_now = datetime.utcnow() |
|
16
|
1 |
|
payload = {"_id": _id, "inserted_at": utc_now, "updated_at": utc_now} |
|
17
|
1 |
|
model = DocumentBaseModel(**payload) |
|
18
|
1 |
|
assert model.model_dump(exclude_none=True) == {**payload, **{"id": _id}} |
|
19
|
1 |
|
assert "_id" not in model.model_dump(exclude={"_id"}) |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
1 |
|
class TestUserDoc: |
|
23
|
|
|
"""Test UserDoc""" |
|
24
|
|
|
|
|
25
|
1 |
|
def setup_method(self): |
|
26
|
|
|
"""Initiate values for UserDoc testing""" |
|
27
|
1 |
|
self.user_data = { |
|
28
|
|
|
"username": "Test123-_", |
|
29
|
|
|
"password": "Password123", |
|
30
|
|
|
"email": "[email protected]", |
|
31
|
|
|
"hash": HashSubDoc() |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
def test_user_doc_dict(self): |
|
35
|
|
|
"""Test UserDocModel.model_dump()""" |
|
36
|
1 |
|
correct_hash = { |
|
37
|
|
|
"n": 8192, |
|
38
|
|
|
"r": 8, |
|
39
|
|
|
"p": 1 |
|
40
|
|
|
} |
|
41
|
1 |
|
user_doc = UserDoc(**self.user_data).model_dump() |
|
42
|
1 |
|
user_hash = user_doc["hash"] |
|
43
|
1 |
|
assert user_doc["username"] == self.user_data["username"] |
|
44
|
1 |
|
assert user_doc["email"] == self.user_data["email"] |
|
45
|
1 |
|
assert user_hash["salt"] is not None |
|
46
|
1 |
|
assert user_hash["n"] == correct_hash["n"] |
|
47
|
1 |
|
assert user_hash["r"] == correct_hash["r"] |
|
48
|
1 |
|
assert user_hash["p"] == correct_hash["p"] |
|
49
|
|
|
|
|
50
|
1 |
|
def test_user_doc_dict_user_error(self): |
|
51
|
|
|
"""Test UserDoc user validation error""" |
|
52
|
1 |
|
self.user_data['username'] = "Test_error_@" |
|
53
|
1 |
|
with pytest.raises(ValidationError): |
|
54
|
1 |
|
UserDoc(**self.user_data) |
|
55
|
|
|
|
|
56
|
1 |
|
def test_user_doc_dict_pw_error(self): |
|
57
|
|
|
"""Test UserDoc password validation error""" |
|
58
|
1 |
|
self.user_data['password'] = 'password_error' |
|
59
|
1 |
|
with pytest.raises(ValidationError): |
|
60
|
1 |
|
UserDoc(**self.user_data) |
|
61
|
|
|
|
|
62
|
1 |
|
def test_user_doc_dict_email_error(self): |
|
63
|
|
|
"""Test UserDoc email validation error""" |
|
64
|
1 |
|
self.user_data['email'] = 'test_error' |
|
65
|
1 |
|
with pytest.raises(ValidationError): |
|
66
|
1 |
|
UserDoc(**self.user_data) |
|
67
|
|
|
|
|
68
|
1 |
|
def test_user_doc_projection(self): |
|
69
|
|
|
"""Test UserDoc projection return""" |
|
70
|
1 |
|
expected_dict = { |
|
71
|
|
|
"_id": 0, |
|
72
|
|
|
"username": 1, |
|
73
|
|
|
"email": 1, |
|
74
|
|
|
'password': 1, |
|
75
|
|
|
'hash': 1, |
|
76
|
|
|
'state': 1, |
|
77
|
|
|
'inserted_at': 1, |
|
78
|
|
|
'updated_at': 1, |
|
79
|
|
|
'deleted_at': 1 |
|
80
|
|
|
} |
|
81
|
1 |
|
actual_dict = UserDoc.projection() |
|
82
|
1 |
|
assert expected_dict == actual_dict |
|
83
|
|
|
|
|
84
|
1 |
|
def test_user_doc_projection_nopw(self): |
|
85
|
|
|
"""Test Userdoc projection without password""" |
|
86
|
1 |
|
expected_dict = { |
|
87
|
|
|
"_id": 0, |
|
88
|
|
|
"username": 1, |
|
89
|
|
|
"email": 1, |
|
90
|
|
|
'state': 1, |
|
91
|
|
|
'inserted_at': 1, |
|
92
|
|
|
'updated_at': 1, |
|
93
|
|
|
'deleted_at': 1 |
|
94
|
|
|
} |
|
95
|
1 |
|
actual_dict = UserDoc.projection_nopw() |
|
96
|
1 |
|
assert expected_dict == actual_dict |
|
97
|
|
|
|
|
98
|
1 |
|
def test_user_doc_hashing(self): |
|
99
|
|
|
"""Test UserDoc hashing of password""" |
|
100
|
1 |
|
user_doc = UserDoc(**self.user_data).model_dump() |
|
101
|
1 |
|
pwd_hashed = hashing("Password123".encode(), |
|
102
|
|
|
user_doc["hash"]) |
|
103
|
|
|
assert user_doc["password"] == pwd_hashed |
|
104
|
|
|
|