|
| 1 | +import json |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | +from unittest import TestCase |
| 5 | + |
| 6 | +import superannotate.lib.core as constants |
| 7 | +from superannotate import SAClient |
| 8 | + |
| 9 | + |
| 10 | +class TestClientInit(TestCase): |
| 11 | + TEST_TOKEN = "test=6085" |
| 12 | + TEST_URL = "https://test.com" |
| 13 | + |
| 14 | + def setUp(self) -> None: |
| 15 | + os.environ.pop('SA_TOKEN', None) |
| 16 | + os.environ.pop('SA_URL', None) |
| 17 | + os.environ.pop('SA_SSL', None) |
| 18 | + |
| 19 | + def test_via_token(self): |
| 20 | + sa = SAClient(token=self.TEST_TOKEN) |
| 21 | + assert sa.controller._token == self.TEST_TOKEN |
| 22 | + assert sa.controller._backend_client.api_url == constants.BACKEND_URL |
| 23 | + |
| 24 | + def test_via_env_token(self): |
| 25 | + os.environ.update( |
| 26 | + {"SA_TOKEN": self.TEST_TOKEN} |
| 27 | + ) |
| 28 | + sa = SAClient() |
| 29 | + assert sa.controller._token == self.TEST_TOKEN |
| 30 | + assert sa.controller._backend_client.api_url == constants.BACKEND_URL |
| 31 | + |
| 32 | + def test_via_env_vars(self): |
| 33 | + os.environ.update( |
| 34 | + { |
| 35 | + "SA_TOKEN": self.TEST_TOKEN, |
| 36 | + "SA_URL": self.TEST_URL, |
| 37 | + "SA_SSL": "False" |
| 38 | + } |
| 39 | + ) |
| 40 | + sa = SAClient() |
| 41 | + assert sa.controller._token == self.TEST_TOKEN |
| 42 | + assert sa.controller._backend_client.api_url == self.TEST_URL |
| 43 | + assert sa.controller._backend_client._verify_ssl == False |
| 44 | + |
| 45 | + def test_via_config_path_with_url_token(self): |
| 46 | + data = { |
| 47 | + "token": self.TEST_TOKEN, |
| 48 | + "main_endpoint": self.TEST_URL, |
| 49 | + "ssl_verify": True |
| 50 | + } |
| 51 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 52 | + file_path = f"{temp_dir}/config.json" |
| 53 | + with open(file_path, "w") as file: |
| 54 | + json.dump(data, file) |
| 55 | + sa = SAClient(config_path=file_path) |
| 56 | + assert sa.controller._token == self.TEST_TOKEN |
| 57 | + assert sa.controller._backend_client.api_url == self.TEST_URL |
| 58 | + assert sa.controller._backend_client._verify_ssl == True |
| 59 | + |
| 60 | + def test_via_config_path_with_token(self): |
| 61 | + data = { |
| 62 | + "token": self.TEST_TOKEN, |
| 63 | + } |
| 64 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 65 | + file_path = f"{temp_dir}/config.json" |
| 66 | + with open(file_path, "w") as file: |
| 67 | + json.dump(data, file) |
| 68 | + sa = SAClient(config_path=file_path) |
| 69 | + assert sa.controller._token == self.TEST_TOKEN |
| 70 | + assert sa.controller._backend_client.api_url == constants.BACKEND_URL |
| 71 | + assert sa.controller._backend_client._verify_ssl == True |
0 commit comments