mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-05 00:43:53 +00:00
104
tests/api/endpoints/admin/test_institution_users.py
Normal file
104
tests/api/endpoints/admin/test_institution_users.py
Normal file
@@ -0,0 +1,104 @@
|
||||
import json
|
||||
import logging
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from seahub.test_utils import BaseTestCase
|
||||
from tests.common.utils import randstring
|
||||
from seahub.institutions.models import Institution, InstitutionAdmin
|
||||
from seahub.profile.models import Profile
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AdminInstitutionUsersTest(BaseTestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def _add_institution(self, name=''):
|
||||
return Institution.objects.create(name=name)
|
||||
|
||||
def _delete_institution(self, name=''):
|
||||
try:
|
||||
institution = Institution.objects.get(name=name)
|
||||
institution.delete()
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
|
||||
|
||||
def test_can_get(self):
|
||||
self.login_as(self.admin)
|
||||
inst = self._add_institution('int1')
|
||||
url = reverse('api-v2.1-admin-institution-users', args=[inst.id])
|
||||
resp = self.client.get(url)
|
||||
|
||||
self.assertEqual(200, resp.status_code)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert type(json_resp['user_list']) is list
|
||||
|
||||
inst.delete()
|
||||
|
||||
def test_can_create(self):
|
||||
self.login_as(self.admin)
|
||||
inst = self._add_institution('int1')
|
||||
url = reverse('api-v2.1-admin-institution-users', args=[inst.id])
|
||||
data = {
|
||||
'email': 'invalid_email_string',
|
||||
}
|
||||
resp = self.client.post(url, data)
|
||||
|
||||
self.assertEqual(200, resp.status_code)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert type(json_resp['success']) is list
|
||||
assert type(json_resp['failed']) is list
|
||||
|
||||
|
||||
class AdminInstitutionUserTest(BaseTestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def _add_institution(self, name=''):
|
||||
return Institution.objects.create(name=name)
|
||||
|
||||
def _delete_institution(self, name=''):
|
||||
try:
|
||||
institution = Institution.objects.get(name=name)
|
||||
institution.delete()
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
|
||||
def _add_user_in_institution(self, email, inst_name):
|
||||
profile = Profile.objects.get_profile_by_user(email)
|
||||
if not profile:
|
||||
profile = Profile.objects.add_or_update(username=email, institution=inst_name)
|
||||
else:
|
||||
profile.institution = inst_name
|
||||
profile.save()
|
||||
|
||||
def test_can_update(self):
|
||||
self.login_as(self.admin)
|
||||
inst = self._add_institution('int1')
|
||||
self._add_user_in_institution(self.user.email, inst.name)
|
||||
|
||||
url = reverse('api-v2.1-admin-institution-user', args=[inst.id, self.user.email])
|
||||
data = 'is_institution_admin=True'
|
||||
resp = self.client.put(url, data, 'application/x-www-form-urlencoded')
|
||||
|
||||
self.assertEqual(200, resp.status_code)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['is_institution_admin'] is True
|
||||
|
||||
inst.delete()
|
||||
|
||||
def test_can_delete(self):
|
||||
self.login_as(self.admin)
|
||||
inst = self._add_institution('int1')
|
||||
self._add_user_in_institution(self.user.email, inst.name)
|
||||
|
||||
url = reverse('api-v2.1-admin-institution-user', args=[inst.id, self.user.email])
|
||||
resp = self.client.delete(url)
|
||||
|
||||
self.assertEqual(200, resp.status_code)
|
||||
|
||||
inst.delete()
|
Reference in New Issue
Block a user