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

Commit 603c99a

Browse files
Implements list/view/create/delete for profiles.
1 parent 860580e commit 603c99a

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

src/freeseer/frontend/controller/configuration.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from flask import Blueprint, request
2727
from freeseer import settings, logging
2828
from freeseer.framework.config.exceptions import InvalidOptionValueError
29+
from freeseer.framework.config.profile import ProfileDoesNotExist, ProfileAlreadyExists
2930
from freeseer.frontend.controller.server import http_response, HTTPError
3031

3132
log = logging.getLogger(__name__)
@@ -70,7 +71,10 @@ def load_configuration(profile):
7071
"""
7172
Returns the configuration for a given profile.
7273
"""
73-
profile = settings.profile_manager.get(profile)
74+
try:
75+
profile = settings.profile_manager.get(profile, create_if_needed=False)
76+
except ProfileDoesNotExist:
77+
raise HTTPError('Profile Does Not Exist', 404)
7478
return profile.get_config('freeseer.conf', settings.FreeseerConfig, storage_args=['Global'], read_only=False)
7579

7680

@@ -90,10 +94,10 @@ def teardown_configuration(signum, frame):
9094
def list_profiles():
9195
"""
9296
List available configuration profiles.
93-
94-
TODO: add a method to the ProfileManager class to list existing profiles.
9597
"""
96-
raise HTTPError('Unimplemented', 501)
98+
return {
99+
'profiles': settings.profile_manager.list_profiles()
100+
}
97101

98102

99103
@configuration.route('/profiles/<string:profile>', methods=['GET'])
@@ -111,22 +115,26 @@ def view_profile(profile):
111115
def create_profile():
112116
"""
113117
Create new profile under 'name' specified in request arg.
114-
115-
TODO: add a method to ProfileManager to explicitly create new profiles.
116118
"""
117-
#profile_name = request.form['name']
118-
raise HTTPError('Unimplemented', 501)
119+
profile_name = request.form['name']
120+
try:
121+
settings.profile_manager.create(profile_name)
122+
except ProfileAlreadyExists:
123+
raise HTTPError('Profile Already Exists', 400)
124+
return ''
119125

120126

121127
@configuration.route('/profiles/<string:profile>', methods=['DELETE'])
122-
@http_response(200)
128+
@http_response(204)
123129
def delete_profile(profile):
124130
"""
125131
Delete the profile specified by :profile.
126-
127-
TODO: implement delete profile in ProfileManager.
128132
"""
129-
raise HTTPError('Unimplemented', 501)
133+
try:
134+
settings.profile_manager.delete(profile)
135+
except ProfileDoesNotExist:
136+
HTTPError('Profile Does Not Exist', 400)
137+
return ''
130138

131139

132140
@configuration.route('/profiles/<string:profile>', methods=['PATCH'])

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import pytest
2828

2929
from freeseer import settings
30-
from freeseer.framework.config.profile import ProfileManager
30+
from freeseer.framework.config.profile import ProfileManager, ProfileDoesNotExist
3131
from freeseer.framework.plugin import PluginManager
3232
from freeseer.frontend.controller import server
3333

@@ -56,27 +56,42 @@ def configuration(self, request, test_client, monkeypatch, tmpdir):
5656

5757
def test_list_profiles(self, test_client):
5858
response = test_client.get('/profiles')
59-
assert response.status_code == 501
59+
expected = {
60+
'profiles': settings.profile_manager.list_profiles()
61+
}
62+
data = json.loads(response.data)
63+
assert response.status_code == 200
64+
assert expected == data
6065

6166
def test_view_profile(self, test_client):
6267
response = test_client.get('/profiles/testing')
6368
assert response.status_code == 200
6469

6570
def test_view_profile_nonexistant_id(self, test_client):
6671
response = test_client.get('/profiles/doesnotexist')
72+
assert response.status_code == 404
6773

68-
# Note: Currently the ProfileManager simply creates a
69-
# new profile with given name when called to
70-
# load non-existant profile.
74+
def test_create_profile(self, test_client):
75+
response = test_client.post('/profiles',
76+
data={'name': 'new_profile'})
77+
new_profile = settings.profile_manager.get('new_profile', create_if_needed=False)
7178
assert response.status_code == 200
79+
assert new_profile
7280

73-
def test_create_profile(self, test_client):
81+
def test_create_profile_invalid_args(self, test_client):
7482
response = test_client.post('/profiles')
75-
assert response.status_code == 501
83+
assert response.status_code == 400
84+
85+
def test_create_profile_already_exists(self, test_client):
86+
response = test_client.post('/profiles',
87+
data={'name': 'testing'})
88+
assert response.status_code == 400
7689

7790
def test_delete_profile(self, test_client):
7891
response = test_client.delete('/profiles/testing')
79-
assert response.status_code == 501
92+
assert response.status_code == 204
93+
with pytest.raises(ProfileDoesNotExist):
94+
settings.profile_manager.get('testing', create_if_needed=False)
8095

8196
def test_modify_profile(self, test_client, configuration):
8297
response = test_client.patch('/profiles/testing',

0 commit comments

Comments
 (0)