Skip to content
This repository was archived by the owner on May 28, 2022. It is now read-only.

Commit 518d190

Browse files
Adds decoding of option values, and some more tests.
1 parent 36ee2d3 commit 518d190

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

src/freeseer/frontend/controller/configuration.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
# For support, questions, suggestions or any other inquiries, visit:
2323
# http://wiki.github.com/Freeseer/freeseer/
24-
2524
import signal
2625

2726
from flask import Blueprint, request
@@ -42,6 +41,22 @@
4241
}
4342

4443

44+
def update_config(config, changes):
45+
"""
46+
Helper to update Config instances with request data.
47+
"""
48+
for key, value in changes.items():
49+
try:
50+
opt_instance = config.options[key]
51+
value = opt_instance.decode(value)
52+
setattr(config, key, value)
53+
except KeyError:
54+
raise HTTPError('Invalid Option: {}'.format(key), 400)
55+
except InvalidOptionValueError:
56+
raise HTTPError('Invalid Value: {}'.format(value), 400)
57+
config.save()
58+
59+
4560
@configuration.before_app_first_request
4661
def configure_configuration():
4762
"""
@@ -122,13 +137,7 @@ def modify_profile(profile):
122137
"""
123138
config = load_configuration(profile)
124139
changes = request.form
125-
for key, value in changes.items():
126-
try:
127-
setattr(config, key, value)
128-
except InvalidOptionValueError:
129-
raise HTTPError('Invalid Argument', 400)
130-
131-
config.save()
140+
update_config(config, changes)
132141
return ''
133142

134143
#

src/freeseer/tests/frontend/controller/test_configuration.py

+42-16
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,7 @@
3232
from freeseer.frontend.controller import server
3333

3434

35-
def unimplemented(response):
36-
response_data = json.loads(response.data)
37-
if response_data['error_code'] != 501:
38-
return False
39-
if response.status_code != 501:
40-
return False
41-
return True
42-
43-
4435
class TestConfigurationApp:
45-
4636
@pytest.fixture(scope='module')
4737
def test_client(self):
4838
server.app.config['TESTING'] = True
@@ -91,7 +81,7 @@ def test_delete_profile(self, test_client):
9181
def test_modify_profile(self, test_client, configuration):
9282
response = test_client.patch('/profiles/testing',
9383
data={
94-
'default_language': 'tr_en_FR.qm'
84+
'default_language': 'tr_en_FR.qm',
9585
})
9686

9787
assert response.status_code == 200
@@ -107,8 +97,44 @@ def test_modify_profile_arg_needs_encoding(self, test_client):
10797
data={
10898
'auto_hide': True
10999
})
110-
# Note: Currently returns 'Invalid Args' because config's
111-
# set_value method does not encode the value of the option.
112-
# In this case, the request object has encoded the boolean
113-
# True as the string 'true', so BooleanOption rejects it.
114-
assert response.status_code == 400
100+
assert response.status_code == 200
101+
102+
def test_modify_profile_invalid_option(self, test_client):
103+
response = test_client.patch('/profiles/testing',
104+
data={
105+
'not_a_real_option': True
106+
})
107+
response_data = json.loads(response.data)
108+
assert response_data == {
109+
'error_message': 'Invalid Option: not_a_real_option',
110+
'error_code': 400
111+
}
112+
113+
def test_view_general_configuration(self, test_client, configuration):
114+
response = test_client.get('/profiles/testing/general')
115+
response_data = json.loads(response.data)
116+
117+
general = configuration.profile.get_config('freeseer.conf',
118+
settings.FreeseerConfig,
119+
['Global'],
120+
read_only=True)
121+
assert response_data == {
122+
'default_language': general.default_language,
123+
'auto_hide': general.auto_hide,
124+
}
125+
126+
assert response.status_code == 200
127+
128+
def test_modify_general_configuration(self, test_client, configuration):
129+
response = test_client.patch('/profiles/testing',
130+
data={
131+
'default_language': 'tr_en_FR.qm',
132+
})
133+
134+
assert response.status_code == 200
135+
136+
configuration.config = configuration.profile.get_config('freeseer.conf',
137+
settings.FreeseerConfig,
138+
['Global'],
139+
read_only=True)
140+
assert configuration.config.default_language == 'tr_en_FR.qm'

0 commit comments

Comments
 (0)