-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
80 lines (58 loc) · 2.02 KB
/
test_api.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
import pytest, requests, json
import main
import config
@pytest.fixture
def client():
main.app.config.from_object(config.TestingConfig)
with main.app.test_client() as client:
with main.app.app_context():
yield client
def test_create_user(client):
response = client.post(
"/user",
json={
"id": 4,
"fname": "James",
"lname": "Max",
"username": "Maxy",
"dob": "08/12/2000",
},
)
assert response.headers["Content-Type"] == "application/json"
assert response.status_code == 201
@pytest.mark.parametrize(
"id, fname, lname, username, dob",
[
(456, "James", "Max", "max34", "01/04/2000"),
(643, "Francis", "Powell", "pf3", "11/11/1999"),
(491, "Julius", "Atito", "Juati", "10/04/2005"),
],
)
def test_create_multiple_users(client, id, fname, lname, username, dob):
response = client.post(
"/user",
json={
"id": id,
"fname": fname,
"lname": lname,
"username": username,
"dob": dob,
},
)
assert response.headers["Content-Type"] == "application/json"
assert response.status_code == 201
def test_fetch_users(client):
response = client.get("/users")
assert response.headers["Content-Type"] == "application/json"
assert response.status_code == 200
def test_login_user(client):
response = client.post("/users/login", json={"id": 1, "username": "John96"})
assert response.headers["Content-Type"] == "application/json"
assert response.status_code == 200
assert b"Welcome, you are logged in as John96" in response.data
def test_login_invalid(client):
response = client.post("/users/login", json={"id": 454, "username": "miko98"})
assert response.headers["Content-Type"] == "application/json"
assert response.status_code == 401
assert b"Invalid login credentials" in response.data
assert b"Welcome, you are logged in as miko98" not in response.data