Skip to content

Commit 13b6e65

Browse files
committed
Modification due to review
1 parent 1e3354c commit 13b6e65

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

meilisearch/client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ def create_key(
280280
MeiliSearchApiError
281281
An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
282282
"""
283-
payload = {**options}
284-
return self.http.post(f'{self.config.paths.keys}', payload)
283+
return self.http.post(f'{self.config.paths.keys}', options)
285284

286285
def udpate_key(
287286
self,
@@ -310,9 +309,8 @@ def udpate_key(
310309
MeiliSearchApiError
311310
An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
312311
"""
313-
payload = {**options}
314312
url = f'{self.config.paths.keys}/{key}'
315-
return self.http.patch(url, payload)
313+
return self.http.patch(url, options)
316314

317315
def delete_key(self, key: str) -> Dict[str, int]:
318316
"""Deletes an API key.
Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from tests import common
33
from datetime import datetime
4+
from meilisearch.errors import MeiliSearchApiError
45

56
def test_get_keys_default(client):
67
"""Tests if public and private keys have been generated and can be retrieved."""
@@ -12,10 +13,10 @@ def test_get_keys_default(client):
1213
assert key[0]['key'] is not None
1314
assert key[1]['key'] is not None
1415

15-
def test_get_key(client):
16+
def test_get_key(client, test_key):
1617
"""Tests if a key can be retrieved."""
1718
keys = client.get_keys()
18-
key = client.get_key(keys[0]['key'])
19+
key = client.get_key(test_key['key'])
1920
assert isinstance(key, dict)
2021
assert 'actions' in key
2122
assert 'indexes' in key
@@ -26,9 +27,9 @@ def test_get_key_inexistent(client):
2627
with pytest.raises(Exception):
2728
client.get_key('No existing key')
2829

29-
def test_create_keys_default(client):
30+
def test_create_keys_default(client, test_key_info):
3031
"""Tests the creation of a key with no optional argument."""
31-
key = client.create_key(options={ 'actions': ['*'], 'indexes': [common.INDEX_UID], 'expiresAt': None })
32+
key = client.create_key(test_key_info)
3233
assert isinstance(key, dict)
3334
assert 'key' in key
3435
assert 'actions' in key
@@ -38,45 +39,43 @@ def test_create_keys_default(client):
3839
assert key['createdAt'] is not None
3940
assert key['updatedAt'] is not None
4041
assert key['key'] is not None
41-
assert key['actions'] == ['*']
42-
assert key['indexes'] == [common.INDEX_UID]
43-
client.delete_key(key['key'])
42+
assert key['actions'] == test_key_info['actions']
43+
assert key['indexes'] == test_key_info['indexes']
4444

45-
def test_create_keys_with_options(client):
45+
def test_create_keys_with_options(client, test_key_info):
4646
"""Tests the creation of a key with arguments."""
47-
key = client.create_key(options={ 'actions': ['*'], 'indexes': [common.INDEX_UID], 'description': 'Test key', 'expiresAt': datetime(2030, 6, 4, 21, 8, 12, 32).isoformat()[:-3]+'Z' })
47+
key = client.create_key(options={'description': test_key_info['description'], 'actions': test_key_info['actions'], 'indexes': test_key_info['indexes'], 'expiresAt': datetime(2030, 6, 4, 21, 8, 12, 32).isoformat()[:-3]+'Z' })
4848
assert isinstance(key, dict)
4949
assert key['key'] is not None
50-
assert key['description'] == 'Test key'
50+
assert key['description'] == test_key_info['description']
5151
assert key['expiresAt'] is not None
5252
assert key['createdAt'] is not None
5353
assert key['updatedAt'] is not None
54-
assert key['actions'] == ['*']
55-
assert key['indexes'] == [common.INDEX_UID]
56-
client.delete_key(key['key'])
54+
assert key['actions'] == test_key_info['actions']
55+
assert key['indexes'] == test_key_info['indexes']
5756

5857
def test_create_keys_without_actions(client):
5958
"""Tests the creation of a key with missing arguments."""
60-
with pytest.raises(Exception):
59+
with pytest.raises(MeiliSearchApiError):
6160
client.create_key(options={'indexes': [common.INDEX_UID]})
6261

63-
def test_update_keys(client):
62+
def test_update_keys(client, test_key_info):
6463
"""Tests updating a key."""
65-
key = client.create_key(options={ 'actions': ['*'], 'indexes': ['*'], 'expiresAt': None })
66-
assert key['actions'] == ['*']
64+
key = client.create_key(test_key_info)
65+
assert key['actions'] == test_key_info['actions']
6766
update_key = client.udpate_key(key=key['key'], options={ 'actions': ['search'] })
6867
assert update_key['key'] is not None
6968
assert update_key['expiresAt'] is None
7069
assert update_key['actions'] == ['search']
71-
client.delete_key(update_key['key'])
7270

73-
def test_delete_key(client):
71+
def test_delete_key(client, test_key):
7472
"""Tests deleting a key."""
75-
key = client.create_key(options={ 'actions': ['*'], 'indexes': ['*'], 'expiresAt': None })
76-
resp = client.delete_key(key['key'])
73+
resp = client.delete_key(test_key['key'])
7774
assert resp.status_code == 204
75+
with pytest.raises(MeiliSearchApiError):
76+
client.get_key(test_key['key'])
7877

7978
def test_delete_key_inexisting(client):
8079
"""Tests deleting a key that does not exists."""
81-
with pytest.raises(Exception):
80+
with pytest.raises(MeiliSearchApiError):
8281
client.delete_key('No existing key')

tests/conftest.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from tests import common
66
import meilisearch
7+
from meilisearch.errors import MeiliSearchApiError
78

89
@fixture(scope='session')
910
def client():
@@ -81,3 +82,30 @@ def index_maker(index_name=common.INDEX_UID, documents=small_movies):
8182
index.wait_for_task(task['uid'])
8283
return index
8384
return index_maker
85+
86+
@fixture(scope='function')
87+
def test_key(client):
88+
key_info = {'description': 'test', 'actions': ['search'], 'indexes': ['movies'], 'expiresAt': None}
89+
90+
key = client.create_key(key_info)
91+
92+
yield key
93+
94+
try:
95+
client.delete_key(key['key'])
96+
except MeiliSearchApiError:
97+
pass
98+
99+
100+
@fixture(scope='function')
101+
def test_key_info(client):
102+
key_info = {'description': 'test', 'actions': ['search'], 'indexes': [common.INDEX_UID], 'expiresAt': None}
103+
104+
yield key_info
105+
106+
try:
107+
keys = client.get_keys()
108+
key = next(x for x in keys if x['description'] == key_info['description'])
109+
client.delete_key(key['key'])
110+
except MeiliSearchApiError:
111+
pass

0 commit comments

Comments
 (0)