Browse Source

Update test projects api for passing tests

pull/69/head
Hironsan 5 years ago
parent
commit
a7d2b0b99f
1 changed files with 31 additions and 90 deletions
  1. 121
      app/server/tests/test_projects_api.py

121
app/server/tests/test_projects_api.py

@ -7,59 +7,37 @@ from ..models import *
class TestProjects(APITestCase):
def setUp(self):
self.username, self.password = 'user', 'pass'
self.url = reverse('project-list')
def create_user(self):
user = User.objects.create_user(username=self.username, password=self.password)
return user
def create_superuser(self):
user = User.objects.create_superuser(username=self.username,
password=self.password,
email='hoge@example.com')
return user
@classmethod
def setUpTestData(cls):
cls.username = 'user'
cls.password = 'pass'
cls.super_username = 'super'
cls.normal_user = User.objects.create_user(username=cls.username, password=cls.password)
cls.super_user = User.objects.create_superuser(username=cls.super_username,
password=cls.password, email='fizz@buzz.com')
cls.project1 = mixer.blend('server.Project', project_type=Project.DOCUMENT_CLASSIFICATION,
users=[cls.normal_user, cls.super_user])
cls.project2 = mixer.blend('server.Project', project_type=Project.DOCUMENT_CLASSIFICATION,
users=[cls.super_user])
cls.url = reverse('project-list')
def create_project(self):
project = mixer.blend('server.Project')
return project
def add_user_to_project(self, user, project):
project.users.add(user)
def setUp(self):
self.client.login(username=self.username, password=self.password)
def test_get_projects(self):
"""
Ensure we can get project objects.
Ensure user can get project.
"""
user = self.create_user()
project = self.create_project()
self.add_user_to_project(user, project)
self.client.login(username=self.username, password=self.password)
response = self.client.get(self.url, format='json')
p = response.data[0]
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIsInstance(response.data, list)
self.assertEqual(p['id'], project.id)
self.assertEqual(p['name'], project.name)
self.assertEqual(p['description'], project.description)
self.assertEqual(p['project_type'], project.project_type)
self.assertEqual(p['image'], project.image)
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]['id'], self.project1.id)
def test_get_progress(self):
"""
Ensure we can get project's progress.
Ensure user can get project's progress.
"""
user = self.create_user()
project = self.create_project()
self.add_user_to_project(user, project)
url = '{}{}/progress/'.format(self.url, project.id)
self.client.login(username=self.username, password=self.password)
url = '{}{}/progress/'.format(self.url, self.project1.id)
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
@ -68,60 +46,23 @@ class TestProjects(APITestCase):
self.assertIsInstance(response.data['total'], int)
self.assertIsInstance(response.data['remaining'], int)
def test_create_project_by_admin(self):
"""
Ensure we can create a new project object by admin.
"""
user = self.create_superuser()
data = {'name': 'DabApps',
'description': 'desc',
'project_type': Project.DOCUMENT_CLASSIFICATION,
'users': [user.id]}
self.client.login(username=self.username, password=self.password)
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Project.objects.count(), 1)
self.assertEqual(Project.objects.get().name, 'DabApps')
def test_create_project_by_user(self):
"""
Ensure we cannot create a new project object by user.
"""
user = self.create_user()
data = {'name': 'DabApps',
'description': 'desc',
'project_type': Project.DOCUMENT_CLASSIFICATION,
'users': [user.id]}
self.client.login(username=self.username, password=self.password)
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_delete_project_by_superuser(self):
def test_superuser_can_delete_project(self):
"""
Ensure we can delete a project by superuser.
Ensure superuser can delete a project.
"""
user = self.create_superuser()
project = self.create_project()
self.assertEqual(Project.objects.count(), 1)
self.client.login(username=self.username, password=self.password)
url = '{}{}/'.format(self.url, project.id)
self.assertEqual(Project.objects.count(), 2)
self.client.login(username=self.super_username, password=self.password)
url = '{}{}/'.format(self.url, self.project2.id)
response = self.client.delete(url, format='json')
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(Project.objects.count(), 0)
self.assertEqual(Project.objects.count(), 1)
def test_delete_project_by_user(self):
def test_normal_user_cannot_delete_project(self):
"""
Ensure we cannot delete a project by user.
Ensure normal user cannot delete a project.
"""
user = self.create_user()
project = self.create_project()
self.assertEqual(Project.objects.count(), 1)
self.client.login(username=self.username, password=self.password)
url = '{}{}/'.format(self.url, project.id)
self.assertEqual(Project.objects.count(), 2)
url = '{}{}/'.format(self.url, self.project2.id)
response = self.client.delete(url, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(Project.objects.count(), 1)
self.assertEqual(Project.objects.count(), 2)
Loading…
Cancel
Save