-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #323 from ExCiteS/mstevens-info_api
New core API to request server info
- Loading branch information
Showing
6 changed files
with
196 additions
and
26 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import json | ||
|
||
from django.test import TestCase | ||
from django.core.urlresolvers import reverse, resolve | ||
from django.http import HttpRequest | ||
from django.contrib.auth.models import AnonymousUser | ||
|
||
from geokey.version import get_version | ||
from geokey.users.models import User | ||
from geokey.core.views import InfoAPIView | ||
from geokey.extensions.base import register, deregister | ||
|
||
|
||
# ############################################################################ | ||
# | ||
# PUBLIC API VIEWS | ||
# | ||
# ############################################################################ | ||
|
||
class InfoAPIViewTest(TestCase): | ||
def setUp(self): | ||
# register bogus extensions: | ||
register('A', 'A', display_admin=True, superuser=False, version='1.0') | ||
register('B', 'B', display_admin=True, superuser=False) | ||
register('S', 'S', display_admin=True, superuser=True, version='0.1.0') | ||
|
||
def tearDown(self): | ||
# deregister bogus extensions: | ||
deregister('A') | ||
deregister('B') | ||
deregister('S') | ||
|
||
def test_url(self): | ||
self.assertEqual(reverse('api:info_api'), '/api/info/') | ||
resolved = resolve('/api/info/') | ||
self.assertEqual(resolved.func.func_name, InfoAPIView.__name__) | ||
|
||
def contains_extension(self, ext_id, exts_json): | ||
for ext in exts_json: | ||
if ext_id in ext: | ||
return True | ||
return False | ||
|
||
def test_get_with_anonymous(self): | ||
view = InfoAPIView.as_view() | ||
request = HttpRequest() | ||
request.method = 'GET' | ||
request.user = AnonymousUser() | ||
response = view(request).render() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
|
||
response_json = json.loads(response.content) | ||
|
||
self.assertIn('geokey', response_json) | ||
geokey_json = response_json.get('geokey') | ||
self.assertEqual(geokey_json.get('version'), get_version()) | ||
self.assertIn('installed_extensions', geokey_json) | ||
|
||
exts_json = response_json.get('geokey').get('installed_extensions') | ||
self.assertTrue(self.contains_extension('A', exts_json)) | ||
self.assertTrue(self.contains_extension('B', exts_json)) | ||
# superuser extension must be hidden: | ||
self.assertFalse(self.contains_extension('S', exts_json)) | ||
|
||
def test_get_with_user(self): | ||
view = InfoAPIView.as_view() | ||
request = HttpRequest() | ||
request.method = 'GET' | ||
request.user = User.objects.create_user("[email protected]", "test", "test") | ||
response = view(request).render() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
|
||
response_json = json.loads(response.content) | ||
|
||
self.assertIn('geokey', response_json) | ||
geokey_json = response_json.get('geokey') | ||
self.assertEqual(geokey_json.get('version'), get_version()) | ||
self.assertIn('installed_extensions', geokey_json) | ||
|
||
exts_json = response_json.get('geokey').get('installed_extensions') | ||
self.assertTrue(self.contains_extension('A', exts_json)) | ||
self.assertTrue(self.contains_extension('B', exts_json)) | ||
# superuser extension must be hidden: | ||
self.assertFalse(self.contains_extension('S', exts_json)) | ||
|
||
def test_get_with_superuser(self): | ||
view = InfoAPIView.as_view() | ||
request = HttpRequest() | ||
request.method = 'GET' | ||
request.user = User.objects.create_superuser("[email protected]", "test", "test") | ||
response = view(request).render() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
|
||
response_json = json.loads(response.content) | ||
|
||
self.assertIn('geokey', response_json) | ||
geokey_json = response_json.get('geokey') | ||
self.assertEqual(geokey_json.get('version'), get_version()) | ||
self.assertIn('installed_extensions', geokey_json) | ||
|
||
exts_json = response_json.get('geokey').get('installed_extensions') | ||
self.assertTrue(self.contains_extension('A', exts_json)) | ||
self.assertTrue(self.contains_extension('B', exts_json)) | ||
# superuser extension must be shown: | ||
self.assertTrue(self.contains_extension('S', exts_json)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
|
||
from geokey.version import get_version | ||
|
||
from geokey.extensions.base import extensions | ||
|
||
# ############################################################################ | ||
# | ||
# PUBLIC API VIEWS | ||
# | ||
# ############################################################################ | ||
|
||
class InfoAPIView(APIView): | ||
""" | ||
API endpoint to get GeoKey server information | ||
""" | ||
def get(self, request): | ||
""" | ||
Returns GeoKey server information | ||
Parameter | ||
--------- | ||
request : rest_framework.request.Request | ||
Represents the HTTP request | ||
Response | ||
-------- | ||
rest_framework.response.Response | ||
Containing the GeoKey server information | ||
""" | ||
info = {} | ||
gk_info = info['geokey'] = {} | ||
# GeoKey version: | ||
gk_info['version'] = get_version() | ||
# Installed extensions (with their version): | ||
gk_info['installed_extensions'] = map( | ||
lambda (ext_id, ext): | ||
{ext_id: | ||
{'version': | ||
(ext['version'] if 'version' in ext else None)}}, | ||
filter( | ||
lambda (ext_id, ext): | ||
request.user.is_superuser or not ext['superuser'], | ||
extensions.iteritems())) | ||
# TODO Add more info later? | ||
|
||
# Return info: | ||
return Response(info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,25 @@ | ||
from django.test import TestCase | ||
from geokey.extensions.base import ExtensionExists, register, extensions as ext | ||
from ..base import ExtensionExists, register, deregister, extensions as ext | ||
|
||
|
||
class RegisterExtensionTest(TestCase): | ||
def setUp(self): | ||
register('test_ext', 'Test', True, True, '1.0.0') | ||
|
||
def tearDown(self): | ||
deregister('test_ext') | ||
|
||
def test(self): | ||
register('text_ext', 'Test', True) | ||
extension = ext.get('text_ext') | ||
self.assertEqual(extension.get('ext_id'), 'text_ext') | ||
extension = ext.get('test_ext') | ||
self.assertEqual(extension.get('ext_id'), 'test_ext') | ||
self.assertEqual(extension.get('name'), 'Test') | ||
self.assertEqual(extension.get('index_url'), 'text_ext:index') | ||
self.assertEqual(extension.get('index_url'), 'test_ext:index') | ||
self.assertTrue(extension.get('display_admin')) | ||
self.assertTrue(extension.get('superuser')) | ||
self.assertEqual(extension.get('version'), '1.0.0') | ||
|
||
def test_exiting(self): | ||
ext['text_ext'] = { | ||
'ext_id': 'text_ext', | ||
'name': 'Test', | ||
'display_admin': True, | ||
'index_url': 'text_ext:index' | ||
} | ||
try: | ||
register('text_ext', 'Test Name', False) | ||
except ExtensionExists: | ||
extension = ext.get('text_ext') | ||
self.assertEqual(extension.get('ext_id'), 'text_ext') | ||
self.assertEqual(extension.get('name'), 'Test') | ||
self.assertEqual( | ||
extension.get('index_url'), | ||
'text_ext:index' | ||
) | ||
self.assertTrue(extension.get('display_admin')) | ||
else: | ||
self.fail('ExtensionExists not raised') | ||
self.assertRaises( | ||
ExtensionExists, | ||
register, | ||
'test_ext', 'abc', False, False, '0.0.1') |