Skip to content

Commit 78e59c8

Browse files
committed
fix iam auth from file, add tests
1 parent 0696e6a commit 78e59c8

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

tests/iam/__init__.py

Whitespace-only changes.

tests/iam/test_auth.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from ydb.iam.auth import BaseJWTCredentials
2+
from unittest.mock import patch, mock_open
3+
4+
CONTENT1 = '{"service_account_id":"my_sa", "id":"123", "private_key":"pppp", "user_account_id":"ua_id"}'
5+
CONTENT2 = '{"id":"123", "private_key":"pppp", "user_account_id":"ua_id"}'
6+
7+
8+
class FakeAuth(BaseJWTCredentials):
9+
def __init__(self, account_id, key_id, private_key, iam_endpoint=None, iam_channel_credentials=None):
10+
self.account_id = account_id
11+
self.key_id = key_id
12+
self.private_key = private_key
13+
self.iam_endpoint = iam_endpoint
14+
self.iam_channel_credentials = iam_channel_credentials
15+
16+
def __eq__(self, other):
17+
return self.__dict__ == other.__dict__ if type(self) == type(other) else False
18+
19+
20+
@patch("builtins.open", new_callable=mock_open, read_data=CONTENT1)
21+
def test_auth_from_file(mock_file):
22+
r1 = FakeAuth.from_file("my_file.json", iam_endpoint="my_iam_address", iam_channel_credentials="my_creds")
23+
mock_file.assert_called_with("my_file.json", "r")
24+
25+
r2 = FakeAuth.from_content(CONTENT1, iam_endpoint="my_iam_address", iam_channel_credentials="my_creds")
26+
assert r1 == r2
27+
assert r1.__dict__ == {
28+
"account_id": "my_sa",
29+
"key_id": "123",
30+
"private_key": "pppp",
31+
"iam_endpoint": "my_iam_address",
32+
"iam_channel_credentials": "my_creds"
33+
}
34+
35+
r3 = FakeAuth.from_content(CONTENT2)
36+
37+
assert r3.__dict__ == {
38+
"account_id": "ua_id",
39+
"key_id": "123",
40+
"private_key": "pppp",
41+
"iam_endpoint": None,
42+
"iam_channel_credentials": None
43+
}

ydb/iam/auth.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ def from_file(cls, key_file, iam_endpoint=None, iam_channel_credentials=None):
101101
with open(os.path.expanduser(key_file), "r") as r:
102102
key = r.read()
103103

104-
return BaseJWTCredentials.from_content(
105-
cls, key, iam_endpoint=iam_endpoint, iam_channel_credentials=iam_channel_credentials
106-
)
104+
return cls.from_content(key, iam_endpoint=iam_endpoint, iam_channel_credentials=iam_channel_credentials)
107105

108106
@classmethod
109107
def from_content(cls, key, iam_endpoint=None, iam_channel_credentials=None):

0 commit comments

Comments
 (0)