Skip to content

Commit

Permalink
Merge pull request #323 from ExCiteS/mstevens-info_api
Browse files Browse the repository at this point in the history
New core API to request server info
  • Loading branch information
mstevens83 committed Dec 11, 2015
2 parents e14a1cc + d0f56a5 commit 56ba700
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 26 deletions.
Empty file added geokey/core/tests/__init__.py
Empty file.
108 changes: 108 additions & 0 deletions geokey/core/tests/test_views.py
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))
7 changes: 7 additions & 0 deletions geokey/core/url/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.conf.urls import patterns, url

from geokey.core.views import InfoAPIView

from geokey.projects import views as project_views
from geokey.categories import views as category_views

Expand All @@ -9,6 +11,11 @@

urlpatterns = patterns(
'',
# ###########################
# CORE
# ###########################
url(r'^info/$', InfoAPIView.as_view(), name='info_api'),

# ###########################
# USER
# ###########################
Expand Down
49 changes: 49 additions & 0 deletions geokey/core/views.py
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)
18 changes: 16 additions & 2 deletions geokey/extensions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class ExtensionExists(BaseException):
pass


def register(ext_id, name, display_admin=False, superuser=False):
def register(ext_id, name, display_admin=False, superuser=False, version=None):
"""
Registeres a new extension to the system.
Registers a new extension to the system.
Parameters
----------
Expand All @@ -23,6 +23,8 @@ def register(ext_id, name, display_admin=False, superuser=False):
Indicates if the extension provides pages for the admin interface
superuser : Boolean
Indicates if the extentsion is available for superusers only
version : str
Version of the extension (optional)
Raises
------
Expand All @@ -37,7 +39,19 @@ def register(ext_id, name, display_admin=False, superuser=False):
extensions[ext_id] = {
'ext_id': ext_id,
'name': name,
'version': version,
'display_admin': display_admin,
'superuser': superuser,
'index_url': ext_id + ':index'
}

def deregister(ext_id):
"""
De-registers an extension to the system. Only to be used for testing.
Parameters
----------
ext_id : str
Unique identifier for the extension
"""
extensions.pop(ext_id, None)
40 changes: 16 additions & 24 deletions geokey/extensions/tests/test_base.py
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')

0 comments on commit 56ba700

Please sign in to comment.