|
1
|
|
|
""" |
|
2
|
|
|
:Copyright: 2007-2025 Jochen Kupperschmidt |
|
3
|
|
|
:License: MIT, see LICENSE for details. |
|
4
|
|
|
""" |
|
5
|
|
|
|
|
6
|
|
|
from io import StringIO |
|
7
|
|
|
|
|
8
|
|
|
from weitersager.config import ( |
|
9
|
|
|
HttpConfig, |
|
10
|
|
|
IrcChannel, |
|
11
|
|
|
IrcConfig, |
|
12
|
|
|
IrcServer, |
|
13
|
|
|
load_config, |
|
14
|
|
|
) |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
TOML_CONFIG = """\ |
|
18
|
|
|
log_level = "warning" |
|
19
|
|
|
|
|
20
|
|
|
[http] |
|
21
|
|
|
host = "0.0.0.0" |
|
22
|
|
|
port = 55555 |
|
23
|
|
|
api_tokens = ["qsSUx9KM-DBuDndUhGNi9_kxNHd08TypiHYM05ZTxVc"] |
|
24
|
|
|
|
|
25
|
|
|
[irc.server] |
|
26
|
|
|
host = "orion.astrochat.test" |
|
27
|
|
|
port = 6669 |
|
28
|
|
|
ssl = true |
|
29
|
|
|
password = "ToTheStars!" |
|
30
|
|
|
rate_limit = 0.5 |
|
31
|
|
|
|
|
32
|
|
|
[irc.bot] |
|
33
|
|
|
nickname = "SpaceCowboy" |
|
34
|
|
|
realname = "Monsieur Weitersager" |
|
35
|
|
|
|
|
36
|
|
|
[irc] |
|
37
|
|
|
commands = [ |
|
38
|
|
|
"MODE SpaceCowboy +i", |
|
39
|
|
|
] |
|
40
|
|
|
channels = [ |
|
41
|
|
|
{ name = "#skyscreeners" }, |
|
42
|
|
|
{ name = "#elite-astrology", password = "twinkle-twinkle" }, |
|
43
|
|
|
{ name = "#hubblebubble" }, |
|
44
|
|
|
] |
|
45
|
|
|
""" |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def test_load_config(): |
|
49
|
|
|
toml = StringIO(TOML_CONFIG) |
|
50
|
|
|
|
|
51
|
|
|
config = load_config(toml) |
|
52
|
|
|
|
|
53
|
|
|
assert config.log_level == 'WARNING' |
|
54
|
|
|
|
|
55
|
|
|
assert config.http == HttpConfig( |
|
56
|
|
|
host='0.0.0.0', |
|
57
|
|
|
port=55555, |
|
58
|
|
|
api_tokens={'qsSUx9KM-DBuDndUhGNi9_kxNHd08TypiHYM05ZTxVc'}, |
|
59
|
|
|
channel_tokens_to_channel_names={}, |
|
60
|
|
|
) |
|
61
|
|
|
|
|
62
|
|
|
assert config.irc == IrcConfig( |
|
63
|
|
|
server=IrcServer( |
|
64
|
|
|
host='orion.astrochat.test', |
|
65
|
|
|
port=6669, |
|
66
|
|
|
ssl=True, |
|
67
|
|
|
password='ToTheStars!', |
|
68
|
|
|
rate_limit=0.5, |
|
69
|
|
|
), |
|
70
|
|
|
nickname='SpaceCowboy', |
|
71
|
|
|
realname='Monsieur Weitersager', |
|
72
|
|
|
commands=[ |
|
73
|
|
|
'MODE SpaceCowboy +i', |
|
74
|
|
|
], |
|
75
|
|
|
channels={ |
|
76
|
|
|
IrcChannel('#skyscreeners'), |
|
77
|
|
|
IrcChannel('#elite-astrology', password='twinkle-twinkle'), |
|
78
|
|
|
IrcChannel('#hubblebubble'), |
|
79
|
|
|
}, |
|
80
|
|
|
) |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
TOML_CONFIG_WITH_DEFAULTS = """\ |
|
84
|
|
|
[irc.server] |
|
85
|
|
|
host = "irc.onlinetalk.test" |
|
86
|
|
|
|
|
87
|
|
|
[irc.bot] |
|
88
|
|
|
nickname = "TownCrier" |
|
89
|
|
|
""" |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
def test_load_config_with_defaults(): |
|
93
|
|
|
toml = StringIO(TOML_CONFIG_WITH_DEFAULTS) |
|
94
|
|
|
|
|
95
|
|
|
config = load_config(toml) |
|
96
|
|
|
|
|
97
|
|
|
assert config.log_level == 'DEBUG' |
|
98
|
|
|
|
|
99
|
|
|
assert config.http == HttpConfig( |
|
100
|
|
|
host='127.0.0.1', |
|
101
|
|
|
port=8080, |
|
102
|
|
|
api_tokens=set(), |
|
103
|
|
|
channel_tokens_to_channel_names={}, |
|
104
|
|
|
) |
|
105
|
|
|
|
|
106
|
|
|
assert config.irc == IrcConfig( |
|
107
|
|
|
server=IrcServer( |
|
108
|
|
|
host='irc.onlinetalk.test', |
|
109
|
|
|
port=6667, |
|
110
|
|
|
ssl=False, |
|
111
|
|
|
password=None, |
|
112
|
|
|
rate_limit=None, |
|
113
|
|
|
), |
|
114
|
|
|
nickname='TownCrier', |
|
115
|
|
|
realname='Weitersager', |
|
116
|
|
|
commands=[], |
|
117
|
|
|
channels=set(), |
|
118
|
|
|
) |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
TOML_CONFIG_WITHOUT_IRC_SERVER_TABLE = """\ |
|
122
|
|
|
[irc.bot] |
|
123
|
|
|
nickname = "Lokalrunde" |
|
124
|
|
|
""" |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
def test_load_config_without_irc_server_table(): |
|
128
|
|
|
toml = StringIO(TOML_CONFIG_WITHOUT_IRC_SERVER_TABLE) |
|
129
|
|
|
|
|
130
|
|
|
config = load_config(toml) |
|
131
|
|
|
|
|
132
|
|
|
assert config.irc.server is None |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
TOML_CONFIG_WITHOUT_IRC_SERVER_HOST = """\ |
|
136
|
|
|
[irc.server] |
|
137
|
|
|
|
|
138
|
|
|
[irc.bot] |
|
139
|
|
|
nickname = "Lokalrunde" |
|
140
|
|
|
""" |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
def test_load_config_without_irc_server_host(): |
|
144
|
|
|
toml = StringIO(TOML_CONFIG_WITHOUT_IRC_SERVER_HOST) |
|
145
|
|
|
|
|
146
|
|
|
config = load_config(toml) |
|
147
|
|
|
|
|
148
|
|
|
assert config.irc.server is None |
|
149
|
|
|
|