-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_utils.py
57 lines (45 loc) · 2 KB
/
test_utils.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
from os import environ
import json
import re
from utils import (LOG_LEVEL, Response, HandledError)
def test_default_loglevel():
assert LOG_LEVEL == 'INFO'
def test_response_is_serializable():
response = json.dumps(Response())
assert isinstance(response, str)
def test_response_default_cors_content():
cors = environ.get("CORS_ALLOW_ORIGIN", "*")
response = json.dumps(Response())
assert response == '{"isBase64Encoded": false, ' \
'"headers": {"Access-Control-Allow-Origin": "' + cors + '"}, "body": "{}"}'
def test_response_custom_cors_content():
global environ # pylint: disable=global-statement, invalid-name
temp = environ.copy()
cors = "https://bogus"
environ["CORS_ALLOW_ORIGIN"] = cors
response = json.dumps(Response())
assert response == '{"isBase64Encoded": false, ' \
'"headers": {"Access-Control-Allow-Origin": "' + cors + '"}, "body": "{}"}'
environ = temp
def test_response_body():
response = Response(name="TestResponse")
assert response.body == '{"name": "TestResponse"}'
response.put("Some Message")
assert re.search('"http_code": 200', response.body)
assert re.search('"message": "Some Message"', response.body)
assert re.search(r'"timestamp": "[0-9|\-|\.|:|T|Z]+"', response.body)
response.put(Exception("Some error"))
assert re.search('"message": "Some error"', response.body)
assert re.search('"http_code": 500', response.body)
response.put(HandledError("Some other error", status_code=400))
assert re.search('"message": "Some other error"', response.body)
assert re.search('"http_code": 400', response.body)
response.put(NotImplementedError("Something else"))
assert re.search('"message": "Something else"', response.body)
assert re.search('"http_code": 501', response.body)
def test_exception_parent_class():
error = HandledError("bogus")
assert isinstance(error, Exception)
def test_default_exception_code():
error = HandledError("bogus")
assert error.status_code == 400