-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbase.py
77 lines (53 loc) · 1.9 KB
/
base.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
"""
Base test class and utilities.
"""
# SPDX-License-Identifier: BSD-3-Clause
import os
import typing
import unittest
from http import HTTPStatus
import httpx
import akismet
from akismet import _common, _test_clients
def make_fixed_response_transport(
response_text: str = "true",
status_code: HTTPStatus = HTTPStatus.OK,
response_json: typing.Optional[dict] = None,
) -> httpx.MockTransport:
"""
Return an ``httpx`` transport that produces a fixed response, for use
in testing.
The transport will return a response consisting of:
* ``status_code`` (default 200)
* ``response_json`` as the JSON content, if supplied
* Otherwise ``response_text`` (default ``"true"``) as the response text
"""
def _handler(
request: httpx.Request, # pylint: disable=unused-argument
) -> httpx.Response:
"""
Mock transport handler which returns a controlled response.
"""
response_kwargs = {"status_code": status_code, "content": response_text}
if response_json is not None:
del response_kwargs["content"]
response_kwargs["json"] = response_json
return httpx.Response(**response_kwargs)
return httpx.MockTransport(_handler)
class CommonData: # pylint: disable=too-few-public-methods
"""
Common data for all Akismet tests.
"""
api_key = os.getenv("PYTHON_AKISMET_API_KEY")
site_url = os.getenv("PYTHON_AKISMET_BLOG_URL")
verify_key_url = f"{_common._API_URL}/{_common._API_V11}/{_common._VERIFY_KEY}"
config = akismet.Config(key=_test_clients._TEST_KEY, url=_test_clients._TEST_URL)
common_kwargs = {"user_ip": "127.0.0.1"}
class AkismetTests(CommonData, unittest.TestCase):
"""
Base class for synchronous Akismet tests.
"""
class AsyncAkismetTests(CommonData, unittest.IsolatedAsyncioTestCase):
"""
Base class for asynchronous Akismet tests.
"""