1
1
# Copyright 2016 OpenMarket Ltd
2
+ # Copyright 2021 The Matrix.org Foundation C.I.C.
2
3
#
3
4
# Licensed under the Apache License, Version 2.0 (the "License");
4
5
# you may not use this file except in compliance with the License.
11
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
13
# See the License for the specific language governing permissions and
13
14
# limitations under the License.
14
- import os .path
15
- import shutil
16
- import tempfile
17
- from contextlib import redirect_stdout
18
- from io import StringIO
19
-
20
15
import yaml
21
16
22
17
from synapse .config import ConfigError
23
18
from synapse .config .homeserver import HomeServerConfig
24
19
25
- from tests import unittest
26
-
27
-
28
- class ConfigLoadingTestCase (unittest .TestCase ):
29
- def setUp (self ):
30
- self .dir = tempfile .mkdtemp ()
31
- self .file = os .path .join (self .dir , "homeserver.yaml" )
20
+ from tests .config .utils import ConfigFileTestCase
32
21
33
- def tearDown (self ):
34
- shutil .rmtree (self .dir )
35
22
23
+ class ConfigLoadingFileTestCase (ConfigFileTestCase ):
36
24
def test_load_fails_if_server_name_missing (self ):
37
25
self .generate_config_and_remove_lines_containing ("server_name" )
38
26
with self .assertRaises (ConfigError ):
39
- HomeServerConfig .load_config ("" , ["-c" , self .file ])
27
+ HomeServerConfig .load_config ("" , ["-c" , self .config_file ])
40
28
with self .assertRaises (ConfigError ):
41
- HomeServerConfig .load_or_generate_config ("" , ["-c" , self .file ])
29
+ HomeServerConfig .load_or_generate_config ("" , ["-c" , self .config_file ])
42
30
43
31
def test_generates_and_loads_macaroon_secret_key (self ):
44
32
self .generate_config ()
45
33
46
- with open (self .file ) as f :
34
+ with open (self .config_file ) as f :
47
35
raw = yaml .safe_load (f )
48
36
self .assertIn ("macaroon_secret_key" , raw )
49
37
50
- config = HomeServerConfig .load_config ("" , ["-c" , self .file ])
38
+ config = HomeServerConfig .load_config ("" , ["-c" , self .config_file ])
51
39
self .assertTrue (
52
40
hasattr (config .key , "macaroon_secret_key" ),
53
41
"Want config to have attr macaroon_secret_key" ,
@@ -58,7 +46,7 @@ def test_generates_and_loads_macaroon_secret_key(self):
58
46
"was: %r" % (config .key .macaroon_secret_key ,)
59
47
)
60
48
61
- config = HomeServerConfig .load_or_generate_config ("" , ["-c" , self .file ])
49
+ config = HomeServerConfig .load_or_generate_config ("" , ["-c" , self .config_file ])
62
50
self .assertTrue (
63
51
hasattr (config .key , "macaroon_secret_key" ),
64
52
"Want config to have attr macaroon_secret_key" ,
@@ -71,9 +59,9 @@ def test_generates_and_loads_macaroon_secret_key(self):
71
59
72
60
def test_load_succeeds_if_macaroon_secret_key_missing (self ):
73
61
self .generate_config_and_remove_lines_containing ("macaroon" )
74
- config1 = HomeServerConfig .load_config ("" , ["-c" , self .file ])
75
- config2 = HomeServerConfig .load_config ("" , ["-c" , self .file ])
76
- config3 = HomeServerConfig .load_or_generate_config ("" , ["-c" , self .file ])
62
+ config1 = HomeServerConfig .load_config ("" , ["-c" , self .config_file ])
63
+ config2 = HomeServerConfig .load_config ("" , ["-c" , self .config_file ])
64
+ config3 = HomeServerConfig .load_or_generate_config ("" , ["-c" , self .config_file ])
77
65
self .assertEqual (
78
66
config1 .key .macaroon_secret_key , config2 .key .macaroon_secret_key
79
67
)
@@ -87,15 +75,15 @@ def test_disable_registration(self):
87
75
["enable_registration: true" , "disable_registration: true" ]
88
76
)
89
77
# Check that disable_registration clobbers enable_registration.
90
- config = HomeServerConfig .load_config ("" , ["-c" , self .file ])
78
+ config = HomeServerConfig .load_config ("" , ["-c" , self .config_file ])
91
79
self .assertFalse (config .registration .enable_registration )
92
80
93
- config = HomeServerConfig .load_or_generate_config ("" , ["-c" , self .file ])
81
+ config = HomeServerConfig .load_or_generate_config ("" , ["-c" , self .config_file ])
94
82
self .assertFalse (config .registration .enable_registration )
95
83
96
84
# Check that either config value is clobbered by the command line.
97
85
config = HomeServerConfig .load_or_generate_config (
98
- "" , ["-c" , self .file , "--enable-registration" ]
86
+ "" , ["-c" , self .config_file , "--enable-registration" ]
99
87
)
100
88
self .assertTrue (config .registration .enable_registration )
101
89
@@ -104,33 +92,5 @@ def test_stats_enabled(self):
104
92
self .add_lines_to_config (["enable_metrics: true" ])
105
93
106
94
# The default Metrics Flags are off by default.
107
- config = HomeServerConfig .load_config ("" , ["-c" , self .file ])
95
+ config = HomeServerConfig .load_config ("" , ["-c" , self .config_file ])
108
96
self .assertFalse (config .metrics .metrics_flags .known_servers )
109
-
110
- def generate_config (self ):
111
- with redirect_stdout (StringIO ()):
112
- HomeServerConfig .load_or_generate_config (
113
- "" ,
114
- [
115
- "--generate-config" ,
116
- "-c" ,
117
- self .file ,
118
- "--report-stats=yes" ,
119
- "-H" ,
120
- "lemurs.win" ,
121
- ],
122
- )
123
-
124
- def generate_config_and_remove_lines_containing (self , needle ):
125
- self .generate_config ()
126
-
127
- with open (self .file ) as f :
128
- contents = f .readlines ()
129
- contents = [line for line in contents if needle not in line ]
130
- with open (self .file , "w" ) as f :
131
- f .write ("" .join (contents ))
132
-
133
- def add_lines_to_config (self , lines ):
134
- with open (self .file , "a" ) as f :
135
- for line in lines :
136
- f .write (line + "\n " )
0 commit comments