|
| 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 | + } |
0 commit comments