|
| 1 | +import pytest |
| 2 | +import time |
| 3 | +import grpc |
| 4 | +import threading |
| 5 | + |
| 6 | +import tests.auth.test_credentials |
| 7 | +import ydb.aio.iam |
| 8 | + |
| 9 | + |
| 10 | +class TestServiceAccountCredentials(ydb.aio.iam.ServiceAccountCredentials): |
| 11 | + def _channel_factory(self): |
| 12 | + return grpc.aio.insecure_channel( |
| 13 | + self._iam_endpoint |
| 14 | + ) |
| 15 | + |
| 16 | + def get_expire_time(self): |
| 17 | + return self._expires_in - time.time() |
| 18 | + |
| 19 | + |
| 20 | +class TestNebiusServiceAccountCredentials(ydb.aio.iam.NebiusServiceAccountCredentials): |
| 21 | + def get_expire_time(self): |
| 22 | + return self._expires_in - time.time() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.asyncio |
| 26 | +async def test_yandex_service_account_credentials(): |
| 27 | + server = tests.auth.test_credentials.IamTokenServiceTestServer() |
| 28 | + credentials = TestServiceAccountCredentials(tests.auth.test_credentials.SERVICE_ACCOUNT_ID, tests.auth.test_credentials.ACCESS_KEY_ID, tests.auth.test_credentials.PRIVATE_KEY, server.get_endpoint()) |
| 29 | + t = (await credentials.auth_metadata())[0][1] |
| 30 | + assert t == "test_token" |
| 31 | + assert credentials.get_expire_time() <= 42 |
| 32 | + server.stop() |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.asyncio |
| 36 | +async def test_nebius_service_account_credentials(): |
| 37 | + server = tests.auth.test_credentials.NebiusTokenServiceForTest() |
| 38 | + |
| 39 | + def serve(s): |
| 40 | + s.handle_request() |
| 41 | + |
| 42 | + serve_thread = threading.Thread(target=serve, args=(server,)) |
| 43 | + serve_thread.start() |
| 44 | + |
| 45 | + credentials = TestNebiusServiceAccountCredentials(tests.auth.test_credentials.SERVICE_ACCOUNT_ID, tests.auth.test_credentials.ACCESS_KEY_ID, tests.auth.test_credentials.PRIVATE_KEY, server.endpoint()) |
| 46 | + t = (await credentials.auth_metadata())[0][1] |
| 47 | + assert t == "test_nebius_token" |
| 48 | + assert credentials.get_expire_time() <= 42 |
| 49 | + |
| 50 | + serve_thread.join() |
0 commit comments