|
8 | 8 |
|
9 | 9 |
|
10 | 10 | def test_read_all_has_kent():
|
| 11 | + # We use requests.get() with url to make a get request |
11 | 12 | response = requests.get(BASE_URI)
|
| 13 | + # response from requests has many useful properties |
| 14 | + # We can get python dict as response by using .json() method |
12 | 15 | response_text = response.json()
|
13 | 16 | pretty_print(response_text)
|
14 | 17 |
|
| 18 | + # Also we can assert on the response status code |
15 | 19 | assert_that(response.status_code).is_equal_to(200)
|
16 | 20 | first_names = [people['fname'] for people in response_text]
|
17 | 21 | assert_that(first_names).contains('Kent')
|
18 | 22 |
|
19 | 23 |
|
20 | 24 | def test_new_person_can_be_added():
|
| 25 | + # Ensure a user with a unique last name is created everytime the test runs |
| 26 | + # Note: json.dumps() is used to convert python dict to json string |
21 | 27 | unique_last_name = f'User {str(uuid4())}'
|
22 | 28 | payload = dumps({
|
23 | 29 | 'fname': 'New',
|
24 | 30 | 'lname': unique_last_name
|
25 | 31 | })
|
26 | 32 |
|
| 33 | + # Setting default headers to show that the client accepts json |
| 34 | + # And will send json in the headers |
27 | 35 | headers = {
|
28 | 36 | 'Content-Type': 'application/json',
|
29 | 37 | 'Accept': 'application/json'
|
30 | 38 | }
|
31 | 39 |
|
| 40 | + # We use requests.post method with keyword params to make the request more readable |
32 | 41 | response = requests.post(url=BASE_URI, data=payload, headers=headers)
|
33 | 42 | assert_that(response.status_code).is_equal_to(204)
|
34 | 43 |
|
| 44 | + # After user is created, we read all the users and then use filter expression to find if the |
| 45 | + # created user is present in the response list |
35 | 46 | people = requests.get(BASE_URI).json()
|
36 | 47 | is_new_user_created = filter(lambda person: person['lname'] == unique_last_name, people)
|
37 | 48 | assert_that(is_new_user_created).is_true()
|
0 commit comments