Skip to content

Commit

Permalink
feat(controller): Add users:list
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-Anderson committed Apr 12, 2015
1 parent 95746aa commit ecc1acc
Show file tree
Hide file tree
Showing 11 changed files with 1,296 additions and 4 deletions.
32 changes: 31 additions & 1 deletion client/deis.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
keys manage ssh keys used for `git push` deployments
perms manage permissions for applications
git manage git for applications
users manage users
Shortcut commands, use ``deis shortcuts`` to see all::
Expand Down Expand Up @@ -75,7 +76,7 @@
__version__ = '1.6.0-dev'

# what version of the API is this client compatible with?
__api_version__ = '1.2'
__api_version__ = '1.3'


locale.setlocale(locale.LC_ALL, '')
Expand Down Expand Up @@ -2196,6 +2197,35 @@ def shortcuts(self, args):
self._logger.info("{:<10} -> {}".format(shortcut, command))
self._logger.info('\nUse `deis help [command]` to learn more')

def users(self, args):
"""
Valid commands for users:
users:list list all registered users
Use `deis help [command]` to learn more.
"""
sys.argv[1] = 'users:list'
args = docopt(self.users_list.__doc__)
return self.users_list(args)

def users_list(self, args):
"""
Lists all registered users.
Requires admin privilages.
Usage: deis users:list
"""
response = self._dispatch('get', '/v1/users/')
if response.status_code == requests.codes.ok:
data = response.json()
self._logger.info('=== Users')
for item in data['results']:
self._logger.info('{username}'.format(**item))
else:
raise ResponseError(response)


SHORTCUTS = OrderedDict([
('create', 'apps:create'),
('destroy', 'apps:destroy'),
Expand Down
2 changes: 1 addition & 1 deletion controller/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
The **api** Django app presents a RESTful web API for interacting with the **deis** system.
"""

__version__ = '1.2.0'
__version__ = '1.3.0'
1 change: 1 addition & 0 deletions controller/api/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
from .test_perm import * # noqa
from .test_release import * # noqa
from .test_scheduler import * # noqa
from .test_users import * # noqa
36 changes: 36 additions & 0 deletions controller/api/tests/test_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

from __future__ import unicode_literals

from django.contrib.auth.models import User
from django.test import TestCase
from rest_framework.authtoken.models import Token


class TestUsers(TestCase):
""" Tests users endpoint"""

fixtures = ['tests.json']

def test_super_user_can_list(self):
url = '/v1/users/'

user = User.objects.get(username='autotest')
token = Token.objects.get(user=user)

response = self.client.get(url,
HTTP_AUTHORIZATION='token {}'.format(token))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 2)
self.assertEqual(response.data['results'][0]['username'], 'autotest')
self.assertEqual(response.data['results'][1]['username'], 'autotest2')

def test_non_super_user_cannot_list(self):
url = '/v1/users/'

user = User.objects.get(username='autotest2')
token = Token.objects.get(user=user)

response = self.client.get(url,
HTTP_AUTHORIZATION='token {}'.format(token))
self.assertEqual(response.status_code, 403)
2 changes: 2 additions & 0 deletions controller/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,6 @@
views.CertificateViewSet.as_view({'get': 'retrieve', 'delete': 'destroy'})),
url(r'^certs/?',
views.CertificateViewSet.as_view({'get': 'list', 'post': 'create'})),
# list users
url(r'^users/', views.UserView.as_view({'get': 'list'})),
)
10 changes: 10 additions & 0 deletions controller/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,13 @@ def destroy(self, request, **kwargs):
user.is_superuser = user.is_staff = False
user.save(update_fields=['is_superuser', 'is_staff'])
return Response(status=status.HTTP_204_NO_CONTENT)


class UserView(BaseDeisViewSet):
"""A Viewset for interacting with User objects."""
model = User
serializer_class = serializers.UserSerializer
permission_classes = [permissions.IsAdmin]

def get_queryset(self):
return self.model.objects.exclude(username='AnonymousUser')
2 changes: 0 additions & 2 deletions docs/reference/api-v1.2.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
:title: Controller API v1.2
:description: The v1.2 REST API for Deis' Controller

.. _controller_api_v1:

Controller API v1.2
===================

Expand Down
Loading

0 comments on commit ecc1acc

Please sign in to comment.