1
1
import pytest
2
2
from tests import common
3
3
from datetime import datetime
4
+ from meilisearch .errors import MeiliSearchApiError
4
5
5
6
def test_get_keys_default (client ):
6
7
"""Tests if public and private keys have been generated and can be retrieved."""
@@ -12,10 +13,10 @@ def test_get_keys_default(client):
12
13
assert key [0 ]['key' ] is not None
13
14
assert key [1 ]['key' ] is not None
14
15
15
- def test_get_key (client ):
16
+ def test_get_key (client , test_key ):
16
17
"""Tests if a key can be retrieved."""
17
18
keys = client .get_keys ()
18
- key = client .get_key (keys [ 0 ] ['key' ])
19
+ key = client .get_key (test_key ['key' ])
19
20
assert isinstance (key , dict )
20
21
assert 'actions' in key
21
22
assert 'indexes' in key
@@ -26,9 +27,9 @@ def test_get_key_inexistent(client):
26
27
with pytest .raises (Exception ):
27
28
client .get_key ('No existing key' )
28
29
29
- def test_create_keys_default (client ):
30
+ def test_create_keys_default (client , test_key_info ):
30
31
"""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 )
32
33
assert isinstance (key , dict )
33
34
assert 'key' in key
34
35
assert 'actions' in key
@@ -38,45 +39,43 @@ def test_create_keys_default(client):
38
39
assert key ['createdAt' ] is not None
39
40
assert key ['updatedAt' ] is not None
40
41
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' ]
44
44
45
- def test_create_keys_with_options (client ):
45
+ def test_create_keys_with_options (client , test_key_info ):
46
46
"""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' })
48
48
assert isinstance (key , dict )
49
49
assert key ['key' ] is not None
50
- assert key ['description' ] == 'Test key'
50
+ assert key ['description' ] == test_key_info [ 'description' ]
51
51
assert key ['expiresAt' ] is not None
52
52
assert key ['createdAt' ] is not None
53
53
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' ]
57
56
58
57
def test_create_keys_without_actions (client ):
59
58
"""Tests the creation of a key with missing arguments."""
60
- with pytest .raises (Exception ):
59
+ with pytest .raises (MeiliSearchApiError ):
61
60
client .create_key (options = {'indexes' : [common .INDEX_UID ]})
62
61
63
- def test_update_keys (client ):
62
+ def test_update_keys (client , test_key_info ):
64
63
"""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 ' ]
67
66
update_key = client .udpate_key (key = key ['key' ], options = { 'actions' : ['search' ] })
68
67
assert update_key ['key' ] is not None
69
68
assert update_key ['expiresAt' ] is None
70
69
assert update_key ['actions' ] == ['search' ]
71
- client .delete_key (update_key ['key' ])
72
70
73
- def test_delete_key (client ):
71
+ def test_delete_key (client , test_key ):
74
72
"""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' ])
77
74
assert resp .status_code == 204
75
+ with pytest .raises (MeiliSearchApiError ):
76
+ client .get_key (test_key ['key' ])
78
77
79
78
def test_delete_key_inexisting (client ):
80
79
"""Tests deleting a key that does not exists."""
81
- with pytest .raises (Exception ):
80
+ with pytest .raises (MeiliSearchApiError ):
82
81
client .delete_key ('No existing key' )
0 commit comments