-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_client_endpoints.py
More file actions
123 lines (102 loc) · 3.83 KB
/
test_api_client_endpoints.py
File metadata and controls
123 lines (102 loc) · 3.83 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
import pytest
import requests_mock
from .utils import get_test_client
def test_wrapped_methods() -> None:
client = get_test_client(authenticated=False)
assert client._api_token is None
assert client._api_token_exp is None
# POST: This one will generate since its the first one.
with requests_mock.Mocker() as mocker:
mocker.register_uri(
"POST",
"https://api.flare.io/tokens/generate",
json={
"token": "test-token-hello",
},
status_code=200,
)
mocker.register_uri(
"POST",
"https://api.flare.io/hello-post",
status_code=200,
)
client.post("https://api.flare.io/hello-post", json={"foo": "bar"})
assert mocker.last_request.url == "https://api.flare.io/hello-post"
assert mocker.last_request.headers["Authorization"] == "Bearer test-token-hello"
assert mocker.last_request.json() == {"foo": "bar"}
# GET
with requests_mock.Mocker() as mocker:
mocker.register_uri(
"GET",
"https://api.flare.io/hello-get",
json={"foo": "bar"},
status_code=200,
)
client.get("https://api.flare.io/hello-get")
assert mocker.last_request.url == "https://api.flare.io/hello-get"
assert mocker.last_request.headers["Authorization"] == "Bearer test-token-hello"
# PUT
with requests_mock.Mocker() as mocker:
mocker.register_uri(
"PUT",
"https://api.flare.io/hello-put",
json={"foo": "bar"},
status_code=200,
)
client.put("https://api.flare.io/hello-put")
assert mocker.last_request.url == "https://api.flare.io/hello-put"
assert mocker.last_request.headers["Authorization"] == "Bearer test-token-hello"
# DELETE
with requests_mock.Mocker() as mocker:
mocker.register_uri(
"DELETE",
"https://api.flare.io/hello-delete",
json={"foo": "bar"},
status_code=200,
)
client.delete("https://api.flare.io/hello-delete")
assert mocker.last_request.url == "https://api.flare.io/hello-delete"
assert mocker.last_request.headers["Authorization"] == "Bearer test-token-hello"
def test_get_path_only() -> None:
client = get_test_client()
with requests_mock.Mocker() as mocker:
mocker.register_uri(
"GET",
"https://api.flare.io/hello/test",
status_code=200,
)
client.get("/hello/test")
assert mocker.last_request.url == "https://api.flare.io/hello/test"
def test_get_eu_domain() -> None:
client = get_test_client(
api_domain="api.eu.flare.io",
_enable_beta_features=True,
)
with requests_mock.Mocker() as mocker:
mocker.register_uri(
"GET",
"https://api.eu.flare.io/hello/test",
status_code=200,
)
client.get("/hello/test")
assert mocker.last_request.url == "https://api.eu.flare.io/hello/test"
def test_get_user_agent() -> None:
client = get_test_client()
with requests_mock.Mocker() as mocker:
mocker.register_uri(
"GET",
"https://api.flare.io/hello/test",
status_code=200,
)
client.get("/hello/test")
assert mocker.last_request.url == "https://api.flare.io/hello/test"
user_agent = mocker.last_request.headers["User-Agent"]
assert "python-flareio/" in user_agent
assert "requests/" in user_agent
def test_bad_domain() -> None:
client = get_test_client()
with pytest.raises(
Exception,
match="Client was used to access netloc='bad.com' at url='https://bad.com/hello-post'. Only the domain api.flare.io is supported.",
):
client.post("https://bad.com/hello-post")