-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_basic.py
More file actions
168 lines (139 loc) · 5.98 KB
/
Copy pathtest_basic.py
File metadata and controls
168 lines (139 loc) · 5.98 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from unittest import TestCase
import os
import responses
from pathlib import Path
from unittest.mock import patch, MagicMock
import tempfile
import shutil
from nextcode import config, Client
from nextcode.session import ServiceSession
from nextcode.exceptions import (
InvalidToken,
InvalidProfile,
ServiceNotFound,
ServerError,
)
from nextcode.utils import decode_token, check_resp_error
from tests import BaseTestCase, REFRESH_TOKEN, ACCESS_TOKEN, AUTH_URL, AUTH_RESP
import logging
logging.basicConfig(level=logging.DEBUG)
cfg = config.Config()
class BasicTest(BaseTestCase):
def test_decode_token(self):
with self.assertRaises(InvalidToken):
decode_token("invalid")
payload = decode_token(REFRESH_TOKEN)
self.assertEqual("Offline", payload["typ"])
payload = decode_token(ACCESS_TOKEN)
def test_profile(self):
_ = config.create_profile("dummy", REFRESH_TOKEN)
self.assertEqual(list(cfg.get("profiles").keys()), ["dummy"])
with self.assertRaises(InvalidProfile):
_ = config.create_profile("invalid", "invalid")
_ = config.set_default_profile("dummy")
with self.assertRaises(InvalidProfile):
_ = config.set_default_profile("invalid")
with patch(
"nextcode.config._load_config",
return_value={"profiles": {"name": "profile"}},
):
config._init_config()
def test_cache(self):
os.environ["NEXTCODE_DISABLE_CACHE"] = "1"
config.load_cache("name")
config.save_cache("name", {})
os.environ["NEXTCODE_DISABLE_CACHE"] = ""
class ClientTest(BaseTestCase):
def test_client(self):
with self.assertRaises(InvalidProfile):
_ = Client(profile="dummy")
cfg.get("profiles")["dummy"] = {}
client = Client(profile="dummy")
with self.assertRaises(ServiceNotFound):
svc = client.service("notfound")
@responses.activate
def test_get_access_token(self):
cfg.get("profiles")["dummy"] = {"api_key": ACCESS_TOKEN, "root_url": "dummy"}
client = Client(profile="dummy")
with responses.RequestsMock() as rsps:
rsps.add(responses.POST, AUTH_URL, json=AUTH_RESP)
jwt = client.get_access_token()
self.assertEqual(jwt, ACCESS_TOKEN)
payload = client.get_access_token(True)
self.assertTrue(isinstance(payload, dict))
repr(client)
with responses.RequestsMock() as rsps:
rsps.add(responses.POST, AUTH_URL, body="bla", status=400)
with self.assertRaises(InvalidToken):
jwt = client.get_access_token()
with responses.RequestsMock() as rsps:
rsps.add(responses.POST, AUTH_URL, body="Refresh token expired", status=400)
with self.assertRaises(InvalidToken) as ctx:
jwt = client.get_access_token()
self.assertIn("Refresh token has expired", repr(ctx.exception))
with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
AUTH_URL,
json={"error_description": "errordesc"},
status=400,
)
with self.assertRaises(InvalidToken) as ctx:
jwt = client.get_access_token()
self.assertIn("errordesc", repr(ctx.exception))
def test_available_services(self):
ret = Client.available_services()
self.assertTrue(isinstance(ret, list))
def test_check_resp_error(self):
resp = MagicMock()
resp.status_code = 500
def mock_json():
return {"error": {"description": "errordesc", "errors": ["error"]}}
resp.raise_for_status.side_effect = Exception("error")
resp.json = mock_json
with self.assertRaises(ServerError):
check_resp_error(resp)
@responses.activate
@patch("nextcode.session.load_cache", return_value=None)
def test_fetch_root_info(self, load_cache):
url_base = "https://test.wuxinextcode/api/query"
with responses.RequestsMock() as rsps:
rsps.add(responses.POST, AUTH_URL, json=AUTH_RESP)
with self.assertRaises(ServerError):
session = ServiceSession(
api_name="dummy", url_base=url_base, api_key=REFRESH_TOKEN
)
with responses.RequestsMock() as rsps:
rsps.add(responses.POST, AUTH_URL, json=AUTH_RESP)
rsps.add(responses.GET, url_base, json=AUTH_RESP)
session = ServiceSession(
api_name="dummy", url_base=url_base, api_key=REFRESH_TOKEN
)
with responses.RequestsMock() as rsps:
rsps.add(responses.POST, AUTH_URL, json=AUTH_RESP)
rsps.add(responses.GET, url_base, status=404)
with self.assertRaises(ServerError):
_ = ServiceSession(
api_name="dummy", url_base=url_base, api_key=REFRESH_TOKEN
)
with responses.RequestsMock() as rsps:
rsps.add(responses.POST, AUTH_URL, json=AUTH_RESP)
rsps.add(responses.GET, url_base, body="text body")
with self.assertRaises(ServerError):
_ = ServiceSession(
api_name="dummy", url_base=url_base, api_key=REFRESH_TOKEN
)
@responses.activate
def test_url_from_endpoint(self):
url_base = "https://test.wuxinextcode/api/query"
responses.add(responses.POST, AUTH_URL, json=AUTH_RESP)
responses.add(responses.GET, url_base, json={"endpoints": {"one": "endpoint"}})
session = ServiceSession(
api_name="dummy", url_base=url_base, api_key=REFRESH_TOKEN
)
with self.assertRaises(ServerError):
session.url_from_endpoint("nonexistent")
session.url_from_endpoint("one")
ret = session.links({"links": {"a": "b"}})
self.assertEqual(ret, {"a": "b"})