kopia lustrzana https://github.com/OpenDroneMap/WebODM
Removed fixtures, replaced with base test class
rodzic
b971b7c911
commit
6591a25a78
|
@ -1,21 +1,11 @@
|
|||
from django.test import TestCase
|
||||
from app.tests import BootTestCase
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework import status
|
||||
|
||||
from app.models import Project
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from app.boot import boot
|
||||
|
||||
class TestApi(TestCase):
|
||||
|
||||
fixtures = ['test_users', ]
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(TestApi, cls).setUpClass()
|
||||
boot()
|
||||
|
||||
class TestApi(BootTestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
|
@ -25,15 +15,15 @@ class TestApi(TestCase):
|
|||
def test_project_list(self):
|
||||
client = APIClient()
|
||||
|
||||
nonstaff_user = User.objects.get(username="testuser2")
|
||||
self.assertFalse(nonstaff_user.is_superuser)
|
||||
user = User.objects.get(username="testuser")
|
||||
self.assertFalse(user.is_superuser)
|
||||
|
||||
project = Project.objects.create(
|
||||
owner=nonstaff_user,
|
||||
owner=user,
|
||||
name="test project"
|
||||
)
|
||||
other_project = Project.objects.create(
|
||||
owner=User.objects.get(username="testuser3"),
|
||||
owner=User.objects.get(username="testuser2"),
|
||||
name="another test project"
|
||||
)
|
||||
|
||||
|
@ -41,7 +31,7 @@ class TestApi(TestCase):
|
|||
res = client.get('/api/projects/')
|
||||
self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
client.login(username="testuser2", password="test1234")
|
||||
client.login(username="testuser", password="test1234")
|
||||
res = client.get('/api/projects/')
|
||||
self.assertEqual(res.status_code, status.HTTP_200_OK)
|
||||
self.assertTrue(len(res.data["results"]) > 0)
|
||||
|
@ -53,7 +43,7 @@ class TestApi(TestCase):
|
|||
self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND)
|
||||
|
||||
res = client.get('/api/projects/{}/'.format(other_project.id))
|
||||
self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN)
|
||||
self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND)
|
||||
|
||||
# Can filter
|
||||
res = client.get('/api/projects/?owner=-1')
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
- model: app.project
|
||||
pk: 1
|
||||
fields: {owner: 2, name: Test_Project2, description: This is a test project, created_at: '2016-09-11T22:26:52.582858+00:00'}
|
||||
- model: app.project
|
||||
pk: 2
|
||||
fields: {owner: 1, name: Test_Project1, description: This is a test project, created_at: '2016-09-11T22:26:52.582858+00:00'}
|
||||
- model: app.project
|
||||
pk: 3
|
||||
fields: {owner: 1, name: Test_Project3, description: This is a test project, created_at: '2016-09-11T22:26:52.582858+00:00'}
|
|
@ -1,48 +0,0 @@
|
|||
- model: auth.user
|
||||
pk: 1
|
||||
fields:
|
||||
# password: test1234
|
||||
password: pbkdf2_sha256$30000$pFwIz88hEEZ4$siNaZefZB3bb0DlemWNOkj6+tCq3I3LW+IgVw5VsRmE=
|
||||
last_login: null
|
||||
is_superuser: true
|
||||
username: testuser
|
||||
first_name: ''
|
||||
last_name: ''
|
||||
email: test@mail.com
|
||||
is_staff: true
|
||||
is_active: true
|
||||
date_joined: '2016-09-11T22:26:52.582858+00:00'
|
||||
groups: []
|
||||
user_permissions: []
|
||||
- model: auth.user
|
||||
pk: 2
|
||||
fields:
|
||||
# password: test1234
|
||||
password: pbkdf2_sha256$30000$pFwIz88hEEZ4$siNaZefZB3bb0DlemWNOkj6+tCq3I3LW+IgVw5VsRmE=
|
||||
last_login: null
|
||||
is_superuser: false
|
||||
username: testuser2
|
||||
first_name: ''
|
||||
last_name: ''
|
||||
email: test2@mail.com
|
||||
is_staff: true
|
||||
is_active: true
|
||||
date_joined: '2016-09-11T22:26:52.582858+00:00'
|
||||
groups: []
|
||||
user_permissions: []
|
||||
- model: auth.user
|
||||
pk: 3
|
||||
fields:
|
||||
# password: test1234
|
||||
password: pbkdf2_sha256$30000$pFwIz88hEEZ4$siNaZefZB3bb0DlemWNOkj6+tCq3I3LW+IgVw5VsRmE=
|
||||
last_login: null
|
||||
is_superuser: false
|
||||
username: testuser3
|
||||
first_name: ''
|
||||
last_name: ''
|
||||
email: test2@mail.com
|
||||
is_staff: true
|
||||
is_active: true
|
||||
date_joined: '2016-09-11T22:26:52.582858+00:00'
|
||||
groups: []
|
||||
user_permissions: []
|
57
app/tests.py
57
app/tests.py
|
@ -9,14 +9,54 @@ from .boot import boot
|
|||
|
||||
import api.tests
|
||||
|
||||
class TestApp(TestCase):
|
||||
|
||||
fixtures = ['test_users', 'test_processingnodes', ]
|
||||
class BootTestCase(TestCase):
|
||||
'''
|
||||
This class provides optional default mock data as well as
|
||||
proper boot initialization code. All tests for the app
|
||||
module should derive from this class instead of TestCase.
|
||||
|
||||
We don't use fixtures because we have signal initialization login
|
||||
for some models, which doesn't play well with them, and this: http://blog.namis.me/2012/04/21/burn-your-fixtures/
|
||||
'''
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(TestApp, cls).setUpClass()
|
||||
def setupUsers():
|
||||
User.objects.create_superuser(username='testsuperuser',
|
||||
email='superuser@test.com',
|
||||
password='test1234')
|
||||
User.objects.create_user(username='testuser',
|
||||
email='user@test.com',
|
||||
password='test1234')
|
||||
User.objects.create_user(username='testuser2',
|
||||
email='user2@test.com',
|
||||
password='test1234')
|
||||
|
||||
def setupProjects():
|
||||
Project.objects.create(
|
||||
owner=User.objects.get(username="testsuperuser"),
|
||||
name="Super User Test Project",
|
||||
description="This is a test project"
|
||||
)
|
||||
Project.objects.create(
|
||||
owner=User.objects.get(username="testuser"),
|
||||
name="User Test Project",
|
||||
description="This is a test project"
|
||||
)
|
||||
Project.objects.create(
|
||||
owner=User.objects.get(username="testuser2"),
|
||||
name="User 2 Test Project",
|
||||
description="This is a test project"
|
||||
)
|
||||
|
||||
super(BootTestCase, cls).setUpClass()
|
||||
boot()
|
||||
setupUsers()
|
||||
setupProjects()
|
||||
|
||||
|
||||
class TestApp(BootTestCase):
|
||||
|
||||
fixtures = ['test_processingnodes', ]
|
||||
|
||||
def setUp(self):
|
||||
self.credentials = {
|
||||
|
@ -104,12 +144,11 @@ class TestApp(TestCase):
|
|||
|
||||
def test_projects(self):
|
||||
# Get a normal user
|
||||
user = User.objects.get(pk=2)
|
||||
user = User.objects.get(username="testuser")
|
||||
self.assertFalse(user.is_superuser)
|
||||
|
||||
# Create a new project
|
||||
p = Project(owner=user, name="test")
|
||||
p.save()
|
||||
p = Project.objects.create(owner=user, name="test")
|
||||
|
||||
# Have the proper permissions been set?
|
||||
self.assertTrue(user.has_perm("view_project", p))
|
||||
|
@ -118,14 +157,14 @@ class TestApp(TestCase):
|
|||
self.assertTrue(user.has_perm("delete_project", p))
|
||||
|
||||
# Get a superuser
|
||||
superUser = User.objects.get(pk=1)
|
||||
superUser = User.objects.get(username="testsuperuser")
|
||||
self.assertTrue(superUser.is_superuser)
|
||||
|
||||
# He should also have permissions, although not explicitly set
|
||||
self.assertTrue(superUser.has_perm("delete_project", p))
|
||||
|
||||
# Get another user
|
||||
anotherUser = User.objects.get(pk=3)
|
||||
anotherUser = User.objects.get(username="testuser2")
|
||||
self.assertFalse(anotherUser.is_superuser)
|
||||
|
||||
# Should not have permission
|
||||
|
|
Ładowanie…
Reference in New Issue