81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
import os
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from windfree_poller.config import load_config
|
|
|
|
|
|
class ConfigTests(unittest.TestCase):
|
|
def test_environment_secrets_override_toml(self):
|
|
text = '''
|
|
[poller]
|
|
collector_id="pi"
|
|
device_id="id"
|
|
[smartthings]
|
|
access_token="file-token"
|
|
[redpanda_connect]
|
|
url="http://localhost/windfree"
|
|
[[fields]]
|
|
name="temperature"
|
|
component="main"
|
|
capability="temperatureMeasurement"
|
|
attribute="temperature"
|
|
'''
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder) / "config.toml"
|
|
path.write_text(text, encoding="utf-8")
|
|
with patch.dict(os.environ, {"SMARTTHINGS_ACCESS_TOKEN": "env-token"}):
|
|
self.assertEqual(load_config(path).access_token, "env-token")
|
|
|
|
def test_access_token_can_be_read_from_file(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
token_path = Path(folder) / "token"
|
|
token_path.write_text("file-token\n", encoding="utf-8")
|
|
config_path = Path(folder) / "config.toml"
|
|
config_path.write_text(f'''
|
|
[poller]
|
|
collector_id="pi"
|
|
device_id="id"
|
|
[smartthings]
|
|
access_token_file="{token_path.as_posix()}"
|
|
[redpanda_connect]
|
|
url="http://localhost/windfree"
|
|
[[fields]]
|
|
name="temperature"
|
|
component="main"
|
|
capability="temperatureMeasurement"
|
|
attribute="temperature"
|
|
''', encoding="utf-8")
|
|
self.assertEqual(load_config(config_path).access_token, "file-token")
|
|
|
|
def test_saved_oauth_token_state_is_accepted(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
token_path = Path(folder) / "token.json"
|
|
token_path.write_text('{"access_token":"access","refresh_token":"refresh"}', encoding="utf-8")
|
|
config_path = Path(folder) / "config.toml"
|
|
config_path.write_text(f'''
|
|
[poller]
|
|
collector_id="pi"
|
|
device_id="id"
|
|
[smartthings]
|
|
client_id="client"
|
|
client_secret="secret"
|
|
token_file="{token_path.as_posix()}"
|
|
[redpanda_connect]
|
|
url="http://localhost/windfree"
|
|
[[fields]]
|
|
name="temperature"
|
|
component="main"
|
|
capability="temperatureMeasurement"
|
|
attribute="temperature"
|
|
''', encoding="utf-8")
|
|
loaded = load_config(config_path)
|
|
self.assertEqual(loaded.client_id, "client")
|
|
self.assertEqual(loaded.access_token, "")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|