# -*- coding: utf-8 -*- from __future__ import absolute_import from .helper import DashboardTestCase class UserTest(DashboardTestCase): @classmethod def _create_user(cls, username=None, password=None, name=None, email=None, roles=None): data = {} if username: data['username'] = username if password: data['password'] = password if name: data['name'] = name if email: data['email'] = email if roles: data['roles'] = roles cls._post("/api/user", data) def test_crud_user(self): self._create_user(username='user1', password='mypassword', name='My Name', email='my@email.com', roles=['administrator']) self.assertStatus(201) self._get('/api/user/user1') self.assertStatus(200) self.assertJsonBody({ 'username': 'user1', 'name': 'My Name', 'email': 'my@email.com', 'roles': ['administrator'] }) self._put('/api/user/user1', { 'name': 'My New Name', 'email': 'mynew@email.com', 'roles': ['block-manager'], }) self.assertStatus(200) self.assertJsonBody({ 'username': 'user1', 'name': 'My New Name', 'email': 'mynew@email.com', 'roles': ['block-manager'] }) self._delete('/api/user/user1') self.assertStatus(204) def test_list_users(self): self._get('/api/user') self.assertStatus(200) self.assertJsonBody([{ 'username': 'admin', 'name': None, 'email': None, 'roles': ['administrator'] }]) def test_create_user_already_exists(self): self._create_user(username='admin', password='mypassword', name='administrator', email='my@email.com', roles=['administrator']) self.assertStatus(400) self.assertError(code='username_already_exists', component='user') def test_create_user_invalid_role(self): self._create_user(username='user1', password='mypassword', name='My Name', email='my@email.com', roles=['invalid-role']) self.assertStatus(400) self.assertError(code='role_does_not_exist', component='user') def test_delete_user_does_not_exist(self): self._delete('/api/user/user2') self.assertStatus(404) @DashboardTestCase.RunAs('test', 'test', [{'user': ['create', 'read', 'update', 'delete']}]) def test_delete_current_user(self): self._delete('/api/user/test') self.assertStatus(400) self.assertError(code='cannot_delete_current_user', component='user') def test_update_user_does_not_exist(self): self._put('/api/user/user2', {'name': 'My New Name'}) self.assertStatus(404) def test_update_user_invalid_role(self): self._put('/api/user/admin', {'roles': ['invalid-role']}) self.assertStatus(400) self.assertError(code='role_does_not_exist', component='user')