Fix up db for testing
Project: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/commit/823aee6a Tree: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/tree/823aee6a Diff: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/diff/823aee6a Branch: refs/heads/master Commit: 823aee6abbf6fd90dbdc1caf0619d57df0a79c5c Parents: d705e33 Author: BroganD1993 <darrenbro...@hotmail.com> Authored: Wed Jun 18 20:30:25 2014 +0100 Committer: BroganD1993 <darrenbro...@hotmail.com> Committed: Wed Jun 18 20:30:25 2014 +0100 ---------------------------------------------------------------------- gstack/controllers/__init__.py | 10 ++++++++++ gstack/controllers/errors.py | 16 ++++++++-------- gstack/controllers/instances.py | 17 +++-------------- gstack/controllers/zones.py | 26 ++++++++++---------------- gstack/helpers.py | 16 ++++------------ gstack/models/__init__.py | 16 ++++++++++++++++ tests/__init__.py | 9 ++++++--- 7 files changed, 57 insertions(+), 53 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/823aee6a/gstack/controllers/__init__.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/__init__.py b/gstack/controllers/__init__.py index 5198b53..57400fb 100644 --- a/gstack/controllers/__init__.py +++ b/gstack/controllers/__init__.py @@ -68,6 +68,16 @@ def _get_item_with_name(authorization, name, args, type): return None +def get_item_with_name_or_error(authorization, name, args, type, error, func_route, to_cloudstack, **kwargs): + cloudstack_item = _get_item_with_name(authorization, name, args, type) + + if cloudstack_item: + return helpers.create_response(to_cloudstack( + cloudstack_response=cloudstack_item, **kwargs + )) + else: + return error(func_route) + def _get_requested_items(authorization, args, type, to_cloudstack, **kwargs): name = None filter = helpers.get_filter(request.args) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/823aee6a/gstack/controllers/errors.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/errors.py b/gstack/controllers/errors.py index 4ddbb57..7e935e1 100644 --- a/gstack/controllers/errors.py +++ b/gstack/controllers/errors.py @@ -19,6 +19,7 @@ import urllib from gstack import app +from gstack import helpers from flask import jsonify, Response @@ -29,7 +30,7 @@ def not_found(e): @app.errorhandler(401) def unauthorized(e): - res = jsonify({ + res = { 'error': { 'errors': [ { @@ -43,14 +44,13 @@ def unauthorized(e): }, 'code': 401, 'message': 'Login Required', - }) + } - res.status_code = 401 - return res + return helpers.create_errored_response(res, 401) def resource_not_found(func_url): - res = jsonify({ + res = { 'error': { 'errors': [ { @@ -62,9 +62,9 @@ def resource_not_found(func_url): 'code': 404, 'message': 'The resource \'' + urllib.unquote_plus(func_url) + '\' was not found' } - }) - res.status_code = 404 - return res + } + + return helpers.create_errored_response(res, 404) def no_results_found(scope): http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/823aee6a/gstack/controllers/instances.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/instances.py b/gstack/controllers/instances.py index af53ab2..e33c094 100755 --- a/gstack/controllers/instances.py +++ b/gstack/controllers/instances.py @@ -173,21 +173,10 @@ def listinstances(authorization, projectid, zone): @app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances/<instance>', methods=['GET']) @authentication.required def getinstance(projectid, authorization, zone, instance): + func_route = url_for('getinstance', projectid=projectid, zone=zone, instance=instance) args = {'command':'listVirtualMachines'} - kwargs = {'projectid':projectid, 'zone':zone} - items = controllers.describe_items( - authorization, args, 'virtualmachine', - _cloudstack_virtual_machine_to_gce, name=instance, **kwargs) - - if items: - helpers.create_response(items) - else: - function_route = url_for( - 'getinstance', - projectid=projectid, - zone=zone, - instance=instance) - return errors.resource_not_found(function_route) + return controllers.get_item_with_name_or_error(authorization, zone, args, 'zone', + errors.resource_not_found(func_route)) @app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances', methods=['POST']) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/823aee6a/gstack/controllers/zones.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/zones.py b/gstack/controllers/zones.py index 831c048..76f1733 100755 --- a/gstack/controllers/zones.py +++ b/gstack/controllers/zones.py @@ -52,17 +52,17 @@ def get_zone_names(authorization): return zones -def _cloudstack_zone_to_gce(response_item): +def _cloudstack_zone_to_gce(cloudstack_response): translate_zone_status = { 'Enabled': 'UP', 'Disabled': 'DOWN' } return ({ 'kind': 'compute#zone', - 'name': response_item['name'], - 'description': response_item['name'], - 'id': response_item['id'], - 'status': translate_zone_status[str(response_item['allocationstate'])] + 'name': cloudstack_response['name'], + 'description': cloudstack_response['name'], + 'id': cloudstack_response['id'], + 'status': translate_zone_status[str(cloudstack_response['allocationstate'])] }) @@ -89,15 +89,9 @@ def listzones(projectid, authorization): @app.route('/compute/v1/projects/<projectid>/zones/<zone>', methods=['GET']) @authentication.required def getzone(projectid, authorization, zone): - response = get_zone_by_name( - authorization=authorization, - zone=zone - ) + func_route = url_for('getzone', projectid=projectid, zone=zone) + args = {'command':'listZones'} + return controllers.get_item_with_name_or_error( + authorization, zone, args, 'zone', errors.resource_not_found, func_route, + _cloudstack_zone_to_gce, **{}) - if response: - return helpers.create_response( - data=_cloudstack_zone_to_gce(response) - ) - else: - func_route = url_for('getzone', projectid=projectid, zone=zone) - return errors.resource_not_found(func_route) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/823aee6a/gstack/helpers.py ---------------------------------------------------------------------- diff --git a/gstack/helpers.py b/gstack/helpers.py index c445065..b6a0162 100644 --- a/gstack/helpers.py +++ b/gstack/helpers.py @@ -29,17 +29,11 @@ def create_response(data): return res +def create_errored_response(data, status_code): + res = jsonify(data) + res.status_code = status_code -def successful_response(**kwargs): - content = render_template(**kwargs) - response = make_response(content) - response.headers['Content-Type'] = 'application/json' - return _create_response(response, '200') - - -def _create_response(response, code): - response.status_code = int(code) - return response + return res def get_filter(data): @@ -52,8 +46,6 @@ def get_filter(data): return filter - - def get_root_url(): return 'https://' + \ app.config['GSTACK_BIND_ADDRESS'] + ':' + app.config['GSTACK_PORT'] http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/823aee6a/gstack/models/__init__.py ---------------------------------------------------------------------- diff --git a/gstack/models/__init__.py b/gstack/models/__init__.py index 7cf0837..59cf9f0 100644 --- a/gstack/models/__init__.py +++ b/gstack/models/__init__.py @@ -16,3 +16,19 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. + +from gstack.core import Service +from gstack.models.accesstoken import AccessToken +from gstack.models.refreshtoken import RefreshToken +from gstack.models.client import Client + + +class AccessTokenService(Service): + __model__ = AccessToken + +class RefreshTokenService(Service): + __model__ = RefreshToken + +class ClientService(Service): + __model__ = Client + http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/823aee6a/tests/__init__.py ---------------------------------------------------------------------- diff --git a/tests/__init__.py b/tests/__init__.py index b7a5e33..01c4810 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -4,12 +4,12 @@ from unittest import TestCase import mock - import json from gstack import app, configure_app from gstack.helpers import read_file from . import settings +from gstack.core import db from .utils import FlaskTestCaseMixin class GStackTestCase(TestCase): @@ -22,6 +22,7 @@ class GStackAppTestCase(FlaskTestCaseMixin, GStackTestCase): def _configure_app(self): configure_app(settings=settings) + def _auth_example_user(self): data = {} data['code'] = 'hjrZryvgLYo3R833NkHHV8jYmxQhsD8TjKWzOm2f' @@ -49,11 +50,13 @@ class GStackAppTestCase(FlaskTestCaseMixin, GStackTestCase): self._configure_app() self.app = app self.client = self.app.test_client() - self._auth_example_user() self.app_context = self.app.app_context() self.app_context.push() + db.create_all() + self._auth_example_user() + def tearDown(self): super(GStackTestCase, self).tearDown() + db.drop_all() self.app_context.pop() -