-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathtest_profile.py
146 lines (111 loc) · 4.19 KB
/
test_profile.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
from fastapi import status
from PIL import Image
import pytest
from app import config
from app.dependencies import MEDIA_PATH
from app.routers.profile import get_image_crop_area, get_placeholder_user
from starlette.status import HTTP_302_FOUND
CROP_RESULTS = [
(20, 10, (5, 0, 15, 10)),
(10, 20, (0, 5, 10, 15)),
(10, 10, (0, 0, 10, 10))
]
def test_get_placeholder_user():
user = get_placeholder_user()
assert user.username == 'new_user'
assert user.email == '[email protected]'
assert user.password == '1a2s3d4f5g6'
assert user.full_name == 'My Name'
@pytest.mark.parametrize('width, height, result', CROP_RESULTS)
def test_get_image_crop_area(width, height, result):
assert get_image_crop_area(width, height) == result
def test_profile_page(profile_test_client):
profile = profile_test_client.get('/profile')
data = profile.content
assert profile.ok
assert b'profile.png' in data
assert b'FakeName' in data
assert b'Happy new user!' in data
assert b'On This Day' in data
def test_update_user_fullname(profile_test_client):
new_name_data = {
'fullname': 'Peter'
}
# Get profile page and initialize database
profile = profile_test_client.get('/profile')
# Post new data
profile = profile_test_client.post(
'/profile/update_user_fullname', data=new_name_data)
assert profile.status_code == status.HTTP_302_FOUND
# Get updated data
data = profile_test_client.get('/profile').content
assert b'Peter' in data
def test_update_user_email(profile_test_client):
new_email = {
'email': '[email protected]'
}
# Get profile page and initialize database
profile = profile_test_client.get('/profile')
# Post new data
profile = profile_test_client.post(
'/profile/update_user_email', data=new_email)
assert profile.status_code == status.HTTP_302_FOUND
# Get updated data
data = profile_test_client.get('/profile').content
assert b'[email protected]' in data
def test_update_user_description(profile_test_client):
new_description = {
'description': "FastAPI Developer"
}
# Get profile page and initialize database
profile = profile_test_client.get('/profile')
# Post new data
profile = profile_test_client.post(
'/profile/update_user_description', data=new_description)
assert profile.status_code == status.HTTP_302_FOUND
# Get updated data
data = profile_test_client.get('/profile').content
assert b"FastAPI Developer" in data
def test_update_telegram_id(profile_test_client):
new_telegram_id = {
'telegram_id': "12345"
}
# Get profile page and initialize database
profile = profile_test_client.get('/profile')
# Post new data
profile = profile_test_client.post(
'/profile/update_telegram_id', data=new_telegram_id)
assert profile.status_code == status.HTTP_302_FOUND
# Get updated data
data = profile_test_client.get('/profile').content
assert b"12345" in data
def test_upload_user_photo(profile_test_client):
example_new_photo = f"{MEDIA_PATH}/example.png"
# Get profile page and initialize database
profile = profile_test_client.get('/profile')
# Post new data
profile = profile_test_client.post(
'/profile/upload_user_photo',
files={'file': (
"filename", open(example_new_photo, "rb"), "image/png")})
assert profile.status_code == status.HTTP_302_FOUND
# Validate new picture saved in media directory
assert 'fake_user.png' in os.listdir(MEDIA_PATH)
# Validate new picture size
new_avatar_path = os.path.join(MEDIA_PATH, 'fake_user.png')
assert Image.open(new_avatar_path).size == config.AVATAR_SIZE
os.remove(new_avatar_path)
def test_update_calendar_privacy(profile_test_client):
new_privacy = {
'privacy': "Public"
}
# Get profile page and initialize database
profile = profile_test_client.get('/profile')
# Post new data
profile = profile_test_client.post(
'/profile/privacy', data=new_privacy)
assert profile.status_code == status.HTTP_302_FOUND
# Get updated data
data = profile_test_client.get('/profile').content
assert b"Public" in data