-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_booker.py
More file actions
148 lines (119 loc) · 4.48 KB
/
test_api_booker.py
File metadata and controls
148 lines (119 loc) · 4.48 KB
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
147
148
import pytest
import requests
# site https://restful-booker.herokuapp.com/apidoc/index.html
BASE_URL = "https://restful-booker.herokuapp.com/booking"
AUTH_URL = "https://restful-booker.herokuapp.com/auth"
STATUS_OK = 200
@pytest.fixture(scope='module')
def auth_token():
payload = {
"username": "admin",
"password": "password123"
}
response = requests.post(AUTH_URL, json=payload)
response_data = response.json()
token = response_data['token']
assert response.status_code == STATUS_OK
yield token
@pytest.fixture(scope='module')
def booking_id():
payload = {
"firstname": "Alla",
"lastname": "Claret",
"totalprice": 111,
"depositpaid": True,
"bookingdates": {
"checkin": "2023-01-01",
"checkout": "2023-02-01"
},
"additionalneeds": "Breakfast"
}
response = requests.post(BASE_URL, json=payload)
booking_id = response.json()['bookingid']
assert response.status_code == STATUS_OK
response_get = requests.get(f'{BASE_URL}/{booking_id}')
assert response_get.status_code == STATUS_OK
yield booking_id
def test_get_all_bookings():
response = requests.get(BASE_URL)
assert response.status_code == STATUS_OK
print(f'\n(response.headers)')
print(response.json())
def test_get_all_requisites():
response = requests.get(BASE_URL)
assert response.status_code == STATUS_OK
headers = ('Connection', 'keep-alive')
assert headers in response.headers.items()
def test_get_how_many_requests():
response = requests.get(BASE_URL)
print(f'\n{len(response.json())}')
assert response.status_code == STATUS_OK
key = 'Connection'
assert key in response.headers
def test_get_booking_with_id():
response = requests.get(f'{BASE_URL}/1')
response_data = response.json()
print(f'\n{response_data}')
def test_get_booking_with_name():
response = requests.get(f'{BASE_URL}/1')
response_data = response.json()
# assert response_data['firstname'] == 'Sally'
print(f'\n{response_data}')
def test_get_booking_many_keys(): # check: эти поля есть в запросе Nr.1
response = requests.get(f'{BASE_URL}/1')
response_data = response.json()
expected_keys = ['firstname', 'lastname', 'totalprice', 'depositpaid']
for key in expected_keys:
assert key in response_data.keys()
print(f'\n{response_data}')
def test_create_booking(booking_id):
response = requests.get(f'{BASE_URL}/{booking_id}')
assert response.status_code == STATUS_OK
assert response.json()['firstname'] == 'Alla'
def test_user_authorization():
payload = {
"username" : "admin",
"password" : "password123"
}
response = requests.post(f'{AUTH_URL}', json=payload)
response_data = response.json()
print(response)
assert response.status_code == STATUS_OK
assert 'token' in response_data
def test_update_booking(booking_id, auth_token):
payload = {
"firstname": "Alla",
"lastname": "Claret",
"totalprice": 222,
"depositpaid": True,
"bookingdates": {
"checkin": "2022-01-01",
"checkout": "2022-02-01"
},
"additionalneeds": "Lunch"
}
headers = {'Cookie': f'token={auth_token}'}
response = requests.put(f'{BASE_URL}/{booking_id}', json=payload, headers=headers)
assert response.status_code == STATUS_OK
response_get = requests.get(f'{BASE_URL}/{booking_id}')
response_data = response_get.json()
assert response_data['totalprice'] == payload['totalprice']
assert response_data['additionalneeds'] == payload['additionalneeds']
def test_update_patch_booking(booking_id, auth_token):
payload = {
"firstname": "Alla New",
"additionalneeds": "Dinner"
}
headers = {'Cookie': f'token={auth_token}'}
response = requests.patch(f'{BASE_URL}/{booking_id}', json=payload, headers=headers)
assert response.status_code == STATUS_OK
response_get = requests.get(f'{BASE_URL}/{booking_id}')
response_data = response_get.json()
assert response_data['firstname'] == payload['firstname']
assert response_data['additionalneeds'] == payload['additionalneeds']
def test_delete_booking(booking_id, auth_token):
headers = {'Cookie': f'token={auth_token}'}
response = requests.delete(f'{BASE_URL}/{booking_id}', headers=headers)
assert response.status_code == 201
response_get = requests.get(f'{BASE_URL}/{booking_id}')
assert response_get.status_code == 404