-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from Lightmatter/feature/test-coverage
Feature/test coverage
- Loading branch information
Showing
5 changed files
with
247 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate | ||
Authorization: | ||
- API_TOKEN_752685dc-679c-4ac5-b117-063ede39f931 | ||
Connection: | ||
- keep-alive | ||
User-Agent: | ||
- python-welkin/0.0.5 | ||
method: GET | ||
uri: https://api.live.welkincloud.io/tenant_REDACTED/admin/users/me | ||
response: | ||
body: | ||
string: '[{"errorCode": "USER_NOT_FOUND", "componentType": "USER", "message": | ||
"User doesn''t exist", "details": {}, "date": "2022-09-16T22:22:39.437Z"}]' | ||
headers: | ||
Access-Control-Allow-Headers: | ||
- authorization, content-type, xsrf-token, security-role | ||
Access-Control-Allow-Methods: | ||
- GET, POST, PUT, DELETE, PATCH, OPTIONS | ||
Access-Control-Allow-Origin: | ||
- '*' | ||
Access-Control-Expose-Headers: | ||
- xsrf-token | ||
Access-Control-Max-Age: | ||
- '3600' | ||
Cache-Control: | ||
- no-cache, no-store, max-age=0, must-revalidate | ||
Connection: | ||
- keep-alive | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Fri, 16 Sep 2022 22:22:39 GMT | ||
Expires: | ||
- '0' | ||
Pragma: | ||
- no-cache | ||
Set-Cookie: | ||
- AWSALB=+x9R4DkRsJT6cNBrIWMs7Ynnq98L8ABw9esO072EZs21NsAgweoL3da/PnnsSwn3E2fE34ahU5ymyTXkfDxZvQrscrascof+yAbdrXRsjV9jW1HpSmyvVsvoDRvR; | ||
Expires=Fri, 23 Sep 2022 22:22:39 GMT; Path=/ | ||
- AWSALBCORS=+x9R4DkRsJT6cNBrIWMs7Ynnq98L8ABw9esO072EZs21NsAgweoL3da/PnnsSwn3E2fE34ahU5ymyTXkfDxZvQrscrascof+yAbdrXRsjV9jW1HpSmyvVsvoDRvR; | ||
Expires=Fri, 23 Sep 2022 22:22:39 GMT; Path=/; SameSite=None; Secure | ||
Transfer-Encoding: | ||
- chunked | ||
X-Content-Type-Options: | ||
- nosniff | ||
X-XSS-Protection: | ||
- 1; mode=block | ||
status: | ||
code: 404 | ||
message: '' | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate | ||
Authorization: | ||
- API_TOKEN_f700bac7-69db-4c84-a1e4-1d02faea8337 | ||
Connection: | ||
- keep-alive | ||
User-Agent: | ||
- python-welkin/0.0.5 | ||
method: GET | ||
uri: https://httpbin.org/status/400 | ||
response: | ||
body: | ||
string: '' | ||
headers: | ||
Access-Control-Allow-Credentials: | ||
- 'true' | ||
Access-Control-Allow-Origin: | ||
- '*' | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '0' | ||
Content-Type: | ||
- text/html; charset=utf-8 | ||
Date: | ||
- Fri, 16 Sep 2022 22:22:32 GMT | ||
Server: | ||
- gunicorn/19.9.0 | ||
status: | ||
code: 400 | ||
message: BAD REQUEST | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import json | ||
|
||
import pytest | ||
|
||
from welkin.exceptions import WelkinHTTPError | ||
|
||
|
||
@pytest.mark.vcr() | ||
def test_exception(client): | ||
url = "admin/users/me" | ||
with pytest.raises(WelkinHTTPError) as exc_info: | ||
client.get(url) | ||
|
||
exc = exc_info.value | ||
assert str(exc).endswith(json.dumps(exc.response.json(), indent=2)) | ||
|
||
|
||
@pytest.mark.vcr() | ||
def test_exception_no_json(client): | ||
url = "https://httpbin.org/status/400" | ||
with pytest.raises(WelkinHTTPError) as exc_info: | ||
client.get(url) | ||
|
||
exc = exc_info.value | ||
assert str(exc) == f"400 Client Error: BAD REQUEST for url: {url}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import sys | ||
from datetime import date, datetime, timedelta, timezone | ||
|
||
from welkin.util import ( | ||
clean_date, | ||
clean_datetime, | ||
clean_json_list, | ||
clean_request_params, | ||
clean_request_payload, | ||
) | ||
|
||
UTC = timezone.utc | ||
PST = timezone(timedelta(hours=-8)) | ||
EST = timezone(timedelta(hours=-5)) | ||
|
||
|
||
def test_clean_request_payload(): | ||
payload = { | ||
"datetime": datetime(2022, 9, 15, 23, 0, 0, 0, PST), | ||
"date": date(2022, 9, 15), | ||
"dict": {"foo": "bar", "nested": {"date": date(2022, 9, 15)}}, | ||
"list": [datetime(2022, 9, 15, 23, 0, 0, 0, UTC)], | ||
"str": "Don't clean me please.", | ||
"int": [-sys.maxsize, 0, sys.maxsize], | ||
"float": [sys.float_info.min, 1, sys.float_info.max], | ||
"bool": [True, False], | ||
"none": None, | ||
} | ||
payload_copy = dict(payload) | ||
|
||
cleaned = clean_request_payload(payload) | ||
assert payload == payload_copy, "Payload was modified" | ||
|
||
assert cleaned["datetime"] == "2022-09-16T07:00:00.000Z" | ||
assert cleaned["date"] == "2022-09-15T00:00:00.000Z" | ||
assert cleaned["dict"]["nested"]["date"] == "2022-09-15T00:00:00.000Z" | ||
assert cleaned["list"][0] == "2022-09-15T23:00:00.000Z" | ||
|
||
|
||
def test_clean_json_list(): | ||
json_list = [ | ||
datetime(2022, 9, 15, 23, 0, 0, 0, UTC), | ||
date(2022, 9, 15), | ||
[ | ||
{ | ||
"foo": [datetime(2022, 9, 15, 23, 0, 0, 0, PST)], | ||
}, | ||
], | ||
"Strings don't get cleaned", | ||
] | ||
json_list_copy = list(json_list) | ||
|
||
cleaned = clean_json_list(json_list) | ||
assert json_list == json_list_copy, "JSON list was modified" | ||
|
||
assert cleaned[0] == "2022-09-15T23:00:00.000Z" | ||
assert cleaned[1] == "2022-09-15T00:00:00.000Z" | ||
assert cleaned[2][0]["foo"][0] == "2022-09-16T07:00:00.000Z" | ||
|
||
|
||
def test_clean_request_params(): | ||
params = { | ||
"datetime": datetime(2022, 9, 15, 23, 0, 0, 0, PST), | ||
"date": date(2022, 9, 15), | ||
"list": ["foo", "bar", "baz"], | ||
} | ||
params_copy = dict(params) | ||
cleaned = clean_request_params(params) | ||
|
||
assert params == params_copy, "Parameter dict was modified" | ||
|
||
assert cleaned["datetime"] == "2022-09-16T07:00:00.000Z" | ||
assert cleaned["date"] == "2022-09-15T00:00:00.000Z" | ||
assert cleaned["list"] == "foo,bar,baz" | ||
|
||
|
||
def test_clean_date(): | ||
cleaned = clean_date(date(2022, 9, 15)) | ||
|
||
assert cleaned == "2022-09-15T00:00:00.000Z" | ||
|
||
|
||
def test_clean_datetime(): | ||
datetime_tests = { | ||
"utc": { | ||
"dt": datetime(2022, 9, 15, 23, 0, 0, 0, UTC), | ||
"expected": "2022-09-15T23:00:00.000Z", | ||
}, | ||
"pst": { | ||
"dt": datetime(2022, 9, 15, 23, 0, 0, 0, PST), | ||
"expected": "2022-09-16T07:00:00.000Z", | ||
}, | ||
"est": { | ||
"dt": datetime(2022, 9, 15, 23, 0, 0, 0, EST), | ||
"expected": "2022-09-16T04:00:00.000Z", | ||
}, | ||
} | ||
timedelta() | ||
for name, data in datetime_tests.items(): | ||
cleaned = clean_datetime(data["dt"]) | ||
assert cleaned == data["expected"], f"Unexpected result for '{name}'" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters