Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Der-Henning
GitHub Repository: Der-Henning/tgtg
Path: blob/main/tests/test_config.py
725 views
1
import configparser
2
import platform
3
import tempfile
4
from uuid import uuid4
5
6
import pytest
7
8
from tgtg_scanner.models import Config, Cron
9
10
SYS_PLATFORM = platform.system()
11
IS_WINDOWS = SYS_PLATFORM.lower() in {"windows", "cygwin"}
12
13
14
def test_default_ini_config():
15
with tempfile.NamedTemporaryFile(delete=not IS_WINDOWS) as temp_file:
16
config = Config(temp_file.name)
17
assert hasattr(config, "metrics_port")
18
assert config.metrics_port == 8000
19
20
21
def test_default_env_config():
22
config = Config()
23
assert hasattr(config, "metrics_port")
24
assert config.metrics_port == 8000
25
26
27
def test_config_set():
28
with tempfile.NamedTemporaryFile(delete=not IS_WINDOWS) as temp_file:
29
config = Config(temp_file.name)
30
31
assert config.set("MAIN", "debug", True)
32
33
config_parser = configparser.ConfigParser()
34
config_parser.read(temp_file.name, encoding="utf-8")
35
36
assert config_parser.getboolean("MAIN", "debug")
37
38
39
def test_save_tokens_to_ini():
40
with tempfile.NamedTemporaryFile(delete=not IS_WINDOWS) as temp_file:
41
access_token = uuid4().hex
42
refresh_token = uuid4().hex
43
datadome = uuid4().hex
44
config = Config(temp_file.name)
45
config.save_tokens(access_token, refresh_token, datadome)
46
47
config_parser = configparser.ConfigParser()
48
config_parser.read(temp_file.name, encoding="utf-8")
49
50
assert config_parser.get("TGTG", "AccessToken") == access_token
51
assert config_parser.get("TGTG", "RefreshToken") == refresh_token
52
assert config_parser.get("TGTG", "Datadome") == datadome
53
54
55
def test_token_path(monkeypatch: pytest.MonkeyPatch):
56
with tempfile.TemporaryDirectory() as temp_dir:
57
monkeypatch.setenv("TGTG_TOKEN_PATH", temp_dir)
58
access_token = uuid4().hex
59
refresh_token = uuid4().hex
60
datadome = uuid4().hex
61
config = Config()
62
config.save_tokens(access_token, refresh_token, datadome)
63
config._load_tokens()
64
65
assert config.tgtg.access_token == access_token
66
assert config.tgtg.refresh_token == refresh_token
67
assert config.tgtg.datadome == datadome
68
69
70
def test_ini_get():
71
with tempfile.NamedTemporaryFile(delete=not IS_WINDOWS) as temp_file:
72
content = (
73
"[MAIN]\n"
74
"Debug = true\n"
75
"ItemIDs = 23423, 32432, 234532\n"
76
"[WEBHOOK]\n"
77
"timeout = 42\n"
78
'headers = {"Accept": "json"}\n'
79
"cron = * * 1-5 * *\n"
80
'body = {"content": "${{items_available}} panier(s) à ${{price}} € \\nÀ récupérer 🍔"}'
81
)
82
83
temp_file.write(content.encode("utf-8"))
84
temp_file.seek(0)
85
config = Config(temp_file.name)
86
87
assert config.debug is True
88
assert config.item_ids == ["23423", "32432", "234532"]
89
assert config.webhook.timeout == 42
90
assert config.webhook.headers == {"Accept": "json"}
91
assert config.webhook.cron == Cron("* * 1-5 * *")
92
assert config.webhook.body == '{"content": "${{items_available}} panier(s) à ${{price}} € \nÀ récupérer 🍔"}'
93
94
95
def test_env_get(monkeypatch: pytest.MonkeyPatch):
96
monkeypatch.setenv("DEBUG", "true")
97
monkeypatch.setenv("ITEM_IDS", "23423, 32432, 234532")
98
monkeypatch.setenv("WEBHOOK_TIMEOUT", "42")
99
monkeypatch.setenv("WEBHOOK_HEADERS", '{"Accept": "json"}')
100
monkeypatch.setenv("WEBHOOK_CRON", "* * 1-5 * *")
101
monkeypatch.setenv("WEBHOOK_BODY", '{"content": "${{items_available}} panier(s) à ${{price}} € \\nÀ récupérer 🍔"}')
102
103
config = Config()
104
105
assert config.debug is True
106
assert config.item_ids == ["23423", "32432", "234532"]
107
assert config.webhook.timeout == 42
108
assert config.webhook.headers == {"Accept": "json"}
109
assert config.webhook.cron == Cron("* * 1-5 * *")
110
assert config.webhook.body == '{"content": "${{items_available}} panier(s) à ${{price}} € \nÀ récupérer 🍔"}'
111
112