Skip to content

Commit 70b9301

Browse files
committed
Adding tests
1 parent 7d41151 commit 70b9301

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

meilisearch/client.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ def wait_for_task(
472472
def generate_tenant_token(
473473
self,
474474
search_rules: Dict[str, Any],
475-
expired_at: Optional[float] = None,
475+
expires_at: Optional[int] = None,
476476
api_key: Optional[str] = None
477477
) -> str:
478478
"""Generate a JWT token for the use of multitenancy.
@@ -483,21 +483,18 @@ def generate_tenant_token(
483483
A Dictionary or an object which contains the rules to be enforced at search time for all or specific
484484
accessible indexes for the signing API Key.
485485
In the specific case of you want to have any restrictions you can also use a array ["*"].
486-
expired_at (optional):
486+
expires_at (optional):
487487
Date and time when the key will expire.
488-
Note that if an expired_at value is included it should a `timestamp`.
488+
Note that if an expires_at value is included it should a `timestamp`.
489489
api_key (optional):
490490
The API key parent of the token. If you let it empty the client API Key will be used.
491491
492492
Returns
493493
-------
494494
jwt_token:
495495
A string containing the jwt tenant token.
496-
497-
Raises
498-
------
499-
MeiliSearchApiError
500-
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
496+
Note: If your token does not work remember that the searchrules is madatory and should be well formatted.
497+
`exp` must be a timestamp in the future.
501498
"""
502499
# Standard JWT header for encryption with SHA256/HS256 algorithm
503500
header = {
@@ -511,9 +508,10 @@ def generate_tenant_token(
511508
payload = {
512509
'apiKeyPrefix': api_key[0:8],
513510
'searchRules': search_rules,
514-
'exp': expired_at if expired_at is not None else None
511+
'exp': expires_at if expires_at is not None else None
515512
}
516513

514+
print(payload)
517515
# Serialize the header and the payload
518516
json_header = json.dumps(header, separators=(",",":")).encode()
519517
json_payload = json.dumps(payload, separators=(",",":")).encode()

tests/client/test_client_token.py renamed to tests/client/test_client_tenant_token.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# pylint: disable=invalid-name
22

33
from re import search
4+
import pytest
45
import meilisearch
56
from tests import BASE_URL, MASTER_KEY
7+
from meilisearch.errors import MeiliSearchApiError
68
import datetime
79

810
def test_generate_tenant_token_with_search_rules(get_private_key, index_with_documents):
@@ -33,17 +35,17 @@ def test_generate_tenant_token_with_api_key(client, get_private_key, index_with_
3335
assert len(response['hits']) == 20
3436
assert response['query'] == ''
3537

36-
def test_generate_tenant_token_with_expired_at(client, get_private_key, index_with_documents):
38+
def test_generate_tenant_token_with_expires_at(client, get_private_key, index_with_documents):
3739
"""Tests create a tenant token with only search rules."""
3840
index_with_documents()
3941
key = get_private_key
4042

4143
client = meilisearch.Client(BASE_URL, key['key'])
4244

43-
tomorrow = datetime.datetime.now() + datetime.timedelta(days=1, hours=3)
45+
tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
4446
timestamp = datetime.datetime.timestamp(tomorrow)
4547

46-
token = client.generate_tenant_token(search_rules=["*"], expired_at=int(timestamp))
48+
token = client.generate_tenant_token(search_rules=["*"], expires_at=int(timestamp))
4749

4850
token_client = meilisearch.Client(BASE_URL, token)
4951
response = token_client.index('indexUID').search('')
@@ -61,7 +63,32 @@ def test_generate_tenant_token_without_search_rules(get_private_key, index_with_
6163
token = client.generate_tenant_token(search_rules='')
6264

6365
token_client = meilisearch.Client(BASE_URL, token)
64-
response = token_client.index('indexUID').search('')
65-
assert isinstance(response, dict)
66-
assert len(response['hits']) == 20
67-
assert response['query'] == ''
66+
with pytest.raises(MeiliSearchApiError):
67+
token_client.index('indexUID').search('')
68+
69+
def test_generate_tenant_token_with_master_key(client, get_private_key, index_with_documents):
70+
"""Tests create a tenant token with only search rules."""
71+
index_with_documents()
72+
key = get_private_key
73+
74+
token = client.generate_tenant_token(search_rules=['*'])
75+
76+
token_client = meilisearch.Client(BASE_URL, token)
77+
with pytest.raises(MeiliSearchApiError):
78+
token_client.index('indexUID').search('')
79+
80+
def test_generate_tenant_token_with_bad_expires_at(client, get_private_key, index_with_documents):
81+
"""Tests create a tenant token with only search rules."""
82+
index_with_documents()
83+
key = get_private_key
84+
85+
client = meilisearch.Client(BASE_URL, key['key'])
86+
87+
yesterday = datetime.datetime.now() + datetime.timedelta(days=-1)
88+
timestamp = datetime.datetime.timestamp(yesterday)
89+
90+
token = client.generate_tenant_token(search_rules=["*"], expires_at=int(timestamp))
91+
92+
token_client = meilisearch.Client(BASE_URL, token)
93+
with pytest.raises(MeiliSearchApiError):
94+
token_client.index('indexUID').search('')

0 commit comments

Comments
 (0)