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

Commit

Permalink
Implements list/view/create/delete for profiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoCanas committed Nov 16, 2014
1 parent 860580e commit 603c99a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
32 changes: 20 additions & 12 deletions src/freeseer/frontend/controller/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from flask import Blueprint, request
from freeseer import settings, logging
from freeseer.framework.config.exceptions import InvalidOptionValueError
from freeseer.framework.config.profile import ProfileDoesNotExist, ProfileAlreadyExists
from freeseer.frontend.controller.server import http_response, HTTPError

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -70,7 +71,10 @@ def load_configuration(profile):
"""
Returns the configuration for a given profile.
"""
profile = settings.profile_manager.get(profile)
try:
profile = settings.profile_manager.get(profile, create_if_needed=False)
except ProfileDoesNotExist:
raise HTTPError('Profile Does Not Exist', 404)
return profile.get_config('freeseer.conf', settings.FreeseerConfig, storage_args=['Global'], read_only=False)


Expand All @@ -90,10 +94,10 @@ def teardown_configuration(signum, frame):
def list_profiles():
"""
List available configuration profiles.
TODO: add a method to the ProfileManager class to list existing profiles.
"""
raise HTTPError('Unimplemented', 501)
return {
'profiles': settings.profile_manager.list_profiles()
}


@configuration.route('/profiles/<string:profile>', methods=['GET'])
Expand All @@ -111,22 +115,26 @@ def view_profile(profile):
def create_profile():
"""
Create new profile under 'name' specified in request arg.
TODO: add a method to ProfileManager to explicitly create new profiles.
"""
#profile_name = request.form['name']
raise HTTPError('Unimplemented', 501)
profile_name = request.form['name']
try:
settings.profile_manager.create(profile_name)
except ProfileAlreadyExists:
raise HTTPError('Profile Already Exists', 400)
return ''


@configuration.route('/profiles/<string:profile>', methods=['DELETE'])
@http_response(200)
@http_response(204)
def delete_profile(profile):
"""
Delete the profile specified by :profile.
TODO: implement delete profile in ProfileManager.
"""
raise HTTPError('Unimplemented', 501)
try:
settings.profile_manager.delete(profile)
except ProfileDoesNotExist:
HTTPError('Profile Does Not Exist', 400)
return ''


@configuration.route('/profiles/<string:profile>', methods=['PATCH'])
Expand Down
31 changes: 23 additions & 8 deletions src/freeseer/tests/frontend/controller/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import pytest

from freeseer import settings
from freeseer.framework.config.profile import ProfileManager
from freeseer.framework.config.profile import ProfileManager, ProfileDoesNotExist
from freeseer.framework.plugin import PluginManager
from freeseer.frontend.controller import server

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

def test_list_profiles(self, test_client):
response = test_client.get('/profiles')
assert response.status_code == 501
expected = {
'profiles': settings.profile_manager.list_profiles()
}
data = json.loads(response.data)
assert response.status_code == 200
assert expected == data

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

def test_view_profile_nonexistant_id(self, test_client):
response = test_client.get('/profiles/doesnotexist')
assert response.status_code == 404

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

def test_create_profile(self, test_client):
def test_create_profile_invalid_args(self, test_client):
response = test_client.post('/profiles')
assert response.status_code == 501
assert response.status_code == 400

def test_create_profile_already_exists(self, test_client):
response = test_client.post('/profiles',
data={'name': 'testing'})
assert response.status_code == 400

def test_delete_profile(self, test_client):
response = test_client.delete('/profiles/testing')
assert response.status_code == 501
assert response.status_code == 204
with pytest.raises(ProfileDoesNotExist):
settings.profile_manager.get('testing', create_if_needed=False)

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

0 comments on commit 603c99a

Please sign in to comment.