-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_api.py
More file actions
62 lines (49 loc) · 1.85 KB
/
test_api.py
File metadata and controls
62 lines (49 loc) · 1.85 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
import json
import uuid
import pytest
from dotenv import dotenv_values
from app import create_app
config = dotenv_values(".env")
@pytest.fixture
def app():
app = create_app()
yield app
def test_index_route(app):
response = app.test_client().get("/")
res = json.loads(response.data.decode("utf-8"))
assert response.status_code == 200
assert type(res) is dict
assert res["status"]["code"] == 200
assert res["status"]["message"] == "Success fetching the API!"
assert res["data"] == None
def test_post_route_without_authorization(app):
payload = {"test": "test"}
response = app.test_client().post("/post", json=payload)
res = json.loads(response.data.decode("utf-8"))
assert response.status_code == 401
assert type(res) is dict
def test_post_route_with_authorization(app):
payload = {"test": "test"}
token = config["SECRET_KEY"]
response = app.test_client().post("/post",
json=payload,
headers={"Authorization": "Bearer {}".format(token)})
res = json.loads(response.data.decode("utf-8"))
assert response.status_code == 200
assert type(res) is dict
def test_not_found_route(app):
response = app.test_client().get(str(uuid.uuid4()))
assert response.status_code == 404
res = json.loads(response.data.decode('utf-8'))
assert type(res) is dict
assert res["status"]["code"] == 404
assert res["status"]["message"] == "URL not found!"
assert res["data"] == None
def test_method_not_allowed_route(app):
response = app.test_client().get("/post")
assert response.status_code == 405
res = json.loads(response.data.decode('utf-8'))
assert type(res) is dict
assert res["status"]["code"] == 405
assert res["status"]["message"] == "Request method not allowed!"
assert res["data"] == None