-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathkeys_test.py
209 lines (171 loc) · 5.65 KB
/
keys_test.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""Tests for the Keys class."""
from __future__ import annotations
import base64
import hashlib
import hmac
import json
import time
import requests_mock
from tests.utils.object_assertions import (
assert_match_object,
assert_object_lists_match,
assert_to_contain_object,
)
from typesense.api_call import ApiCall
from typesense.keys import Keys
from typesense.types.key import ApiKeyRetrieveSchema
def test_init(fake_api_call: ApiCall) -> None:
"""Test that the Keys object is initialized correctly."""
keys = Keys(fake_api_call)
assert_match_object(keys.api_call, fake_api_call)
assert_object_lists_match(
keys.api_call.node_manager.nodes,
fake_api_call.node_manager.nodes,
)
assert_match_object(
keys.api_call.config.nearest_node,
fake_api_call.config.nearest_node,
)
assert not keys.keys
def test_get_missing_key(fake_keys: Keys) -> None:
"""Test that the Keys object can get a missing key."""
key = fake_keys[1]
assert_match_object(key.api_call, fake_keys.api_call)
assert_object_lists_match(
key.api_call.node_manager.nodes, fake_keys.api_call.node_manager.nodes
)
assert_match_object(
key.api_call.config.nearest_node,
fake_keys.api_call.config.nearest_node,
)
assert key._endpoint_path == "/keys/1" # noqa: WPS437
def test_get_existing_key(fake_keys: Keys) -> None:
"""Test that the Keys object can get an existing key."""
key = fake_keys[1]
fetched_key = fake_keys[1]
assert len(fake_keys.keys) == 1
assert key is fetched_key
def test_retrieve(fake_keys: Keys) -> None:
"""Test that the Keys object can retrieve keys."""
json_response: ApiKeyRetrieveSchema = {
"keys": [
{
"actions": ["documents:search"],
"collections": ["companies"],
"description": "Search-only key",
"expires_at": int(time.time()) + 3600,
"id": 1,
"value_prefix": "asdf",
},
],
}
with requests_mock.Mocker() as mock:
mock.get(
"http://nearest:8108/keys",
json=json_response,
)
response = fake_keys.retrieve()
assert len(response) == 1
assert response["keys"][0] == json_response.get("keys")[0]
assert response == json_response
def test_create(fake_keys: Keys) -> None:
"""Test that the Keys object can create a key."""
json_response: ApiKeyRetrieveSchema = {
"keys": [
{
"actions": ["documents:search"],
"collections": ["companies"],
"description": "Search-only key",
"expires_at": int(time.time()) + 3600,
"id": 1,
"value_prefix": "asdf",
},
],
}
with requests_mock.Mocker() as mock:
mock.post(
"http://nearest:8108/keys",
json=json_response,
)
fake_keys.create(
schema={
"actions": ["documents:search"],
"collections": ["companies"],
},
)
assert mock.call_count == 1
assert mock.called is True
assert mock.last_request.method == "POST"
assert mock.last_request.url == "http://nearest:8108/keys"
assert mock.last_request.json() == {
"actions": ["documents:search"],
"collections": ["companies"],
}
def test_actual_create(
actual_keys: Keys,
) -> None:
"""Test that the Keys object can create an key on Typesense Server."""
response = actual_keys.create(
{
"actions": ["documents:search"],
"collections": ["companies"],
"description": "Search-only key",
},
)
assert_to_contain_object(
response,
{
"actions": ["documents:search"],
"collections": ["companies"],
"description": "Search-only key",
"autodelete": False,
},
)
def test_actual_retrieve(
actual_keys: Keys,
delete_all: None,
delete_all_keys: None,
create_key_id: int,
) -> None:
"""Test that the Keys object can retrieve an key from Typesense Server."""
response = actual_keys.retrieve()
assert len(response["keys"]) == 1
assert_to_contain_object(
response["keys"][0],
{
"actions": ["documents:search"],
"collections": ["companies"],
"description": "Search-only key",
"autodelete": False,
"id": create_key_id,
},
)
def test_generate_scoped_search_key(
fake_keys: Keys,
) -> None:
"""Test that the Keys object can generate a scoped search key."""
# Use a real key that works on Typesense server
search_key = "KmacipDKNqAM3YiigXfw5pZvNOrPQUba"
search_parameters = {
"q": "search query",
"collection": "companies",
"filter_by": "num_employees:>10",
}
key = fake_keys.generate_scoped_search_key(search_key, search_parameters)
decoded_key = base64.b64decode(key).decode("utf-8")
extracted_key = {
"digest": decoded_key[:44],
"key_prefix": decoded_key[44:48],
"params_str": decoded_key[48:],
}
assert extracted_key["key_prefix"] == search_key[:4]
expected_params_str = json.dumps(search_parameters)
assert extracted_key["params_str"] == expected_params_str
recomputed_digest = base64.b64encode(
hmac.new(
search_key.encode("utf-8"),
expected_params_str.encode("utf-8"),
digestmod=hashlib.sha256,
).digest(),
).decode("utf-8")
assert extracted_key["digest"] == recomputed_digest