VRE Backend API and Scheduler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

188 lines
7.1 KiB

from django.test import TestCase, override_settings
from rest_framework.test import RequestsClient
from django.contrib.auth.models import User
from apps import researcher
from apps.researcher.models import Researcher
from apps.study.views import Contributors
from apps.university.models import Faculty, StudyField
from apps.study.models import Study
from django.urls import reverse
import random
# Create your tests here.
class StudyCreateTest(TestCase):
fixtures = ('university_initial_data', 'virtual_machine_initial_data')
@classmethod
def setUpTestData(cls):
cls.firstname = 'Jan'
cls.lastname = 'Doedel'
cls.email = 'dummy@rug.nl'
cls.username = 'dummy@rug.nl'
cls.password = ';t2QG*LLtzdnC*'
cls.faculty = Faculty.objects.get(pk=9)
# Create a new user
cls.user = User.objects.create_user(username=cls.username, password=cls.password, email=cls.email)
cls.user.first_name = cls.firstname
cls.user.last_name = cls.lastname
cls.user.save()
# Make a researcher member of a faculty
cls.user.researcher.faculty = cls.faculty
# Save the updated researcher
cls.user.researcher.save()
# Create some extra researchers.....
for x in range(5):
user = User.objects.create_user(username=f'Username_{x}', password=f'{cls.password}_{x}', email=f'researcher_{x}@rug.nl')
user.first_name = f'Rs_{x}'
user.last_name = f'LN_{x}'
user.save()
if x < 3:
user.researcher.faculty = Faculty.objects.get(pk=9)
cls.valid_contributor = user
else:
user.researcher.faculty = Faculty.objects.get(pk=random.randint(2, 8))
cls.invalid_contributor = user
user.researcher.save()
def setUp(self):
# We want to use the API REST Framework request client, as this enables us to use HAWK authentication during testing
self.client = RequestsClient()
# Get a JWT token
login_data = {'username': self.username, 'password': self.password}
endpoint = 'http://testserver/api/auth/jwt/create/'
response = self.client.post(endpoint, json=login_data)
self.client.headers.update({'Authorization': 'JWT ' + response.json()['access']})
def test_create_study(self):
endpoint = 'http://testserver' + reverse('api:v1:study-list')
response = self.client.get(endpoint)
# First step, make sure we do not have any studies
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()['results']), 0)
# Now we create a new study
# Need a study field based on faculty
study_field = StudyField.objects.filter(faculty=self.faculty).first()
endpoint = 'http://testserver' + reverse('api:v1:study-list')
data = {
"name": "Test Onderzoek",
"description": "Doe maar een lange onderzoek",
"code": "12345",
"human_subject": False,
"field": study_field.id
}
response = self.client.post(endpoint, json=data)
# Make sure we get a 201 result back
self.assertEqual(response.status_code, 201)
study = response.json()
# Check if we are the only reseacher....
self.assertEqual(len(study['contributors']), 1)
# And make sure we are 'ADMIN'
self.assertEqual(study['contributors'][0]['role'], 'ADMIN')
self.assertEqual(study['contributors'][0]['researcher']['id'], self.user.researcher.id)
# And now we should have 1 study
endpoint = 'http://testserver' + reverse('api:v1:study-list')
response = self.client.get(endpoint)
# Check if we have 1 study
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()['results']), 1)
def test_create_study_invalid_study_field(self):
# Now we create a new study
# Need a study field based on faculty
study_field = StudyField.objects.exclude(faculty=self.faculty).first()
endpoint = 'http://testserver' + reverse('api:v1:study-list')
data = {
"name": "Test Onderzoek",
"description": "Doe maar een lange onderzoek",
"code": "12345",
"human_subject": False,
"field": study_field.id
}
response = self.client.post(endpoint, json=data)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()[0]['message'], f'Study field {study_field} is not valid for faculty {self.faculty}')
def test_update_study(self):
# We need a new study to test with.....:(
self.test_create_study()
# Get the first study through Django Models, which should be the new created study
study = Study.objects.get(contributors__in=[self.user.researcher])
endpoint = 'http://testserver' + reverse('api:v1:study-detail', kwargs={'pk': study.id})
data = {
"name": "Test Onderzoek - UPDATE",
"description": "Doe maar een lange onderzoek - UPDATE",
"code": "12345",
"human_subject": False,
"field": study.field.id
}
response = self.client.put(endpoint, json=data)
# And now we should have 1 study
endpoint = 'http://testserver' + reverse('api:v1:study-list')
response = self.client.get(endpoint)
# Check if we have 1 study
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()['results']), 1)
self.assertEqual(response.json()['results'][0]['id'], study.id)
def test_add_new_contributor(self):
# We need a new study to test with.....:(
self.test_create_study()
# Get the first study through Django Models, which should be the new created study
study = Study.objects.get(contributors__in=[self.user.researcher])
endpoint = 'http://testserver' + reverse('api:v1:study-contributors', kwargs={'pk': study.id})
data = [{
"researcher_id": self.valid_contributor.researcher.id,
"role": "RESEARCHER"
}]
response = self.client.post(endpoint, json=data)
# We expect 2 contributors now.
self.assertEqual(len(response.json()), 2)
# is the ID the same a the one we added?
self.assertEqual(response.json()[1]['researcher']['id'], self.valid_contributor.researcher.id)
def test_faling_invalid_contributor(self):
# We need a new study to test with.....:(
self.test_create_study()
# Get the first study through Django Models, which should be the new created study
study = Study.objects.get(contributors__in=[self.user.researcher])
endpoint = 'http://testserver' + reverse('api:v1:study-contributors', kwargs={'pk': study.id})
data = [{
"researcher_id": self.invalid_contributor.researcher.id,
"role": "RESEARCHER"
}]
response = self.client.post(endpoint, json=data)
# We expect ONLY 1 contributors now. The invalid researcher should not be added
self.assertEqual(len(response.json()), 1)