mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-22 14:54:22 +00:00
🧪 Complete tests for ResearchGroupViewSet
This commit is contained in:
parent
3db2946d07
commit
ad28656431
1 changed files with 127 additions and 13 deletions
|
@ -3,7 +3,6 @@ import collections
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from rest_framework import status
|
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
|
|
||||||
|
@ -73,10 +72,6 @@ class SophonModelTestCase(APITestCase, metaclass=abc.ABCMeta):
|
||||||
self.assertTrue(isinstance(response.data, dict))
|
self.assertTrue(isinstance(response.data, dict))
|
||||||
return response.data
|
return response.data
|
||||||
|
|
||||||
def update_fail(self, pk, data) -> None:
|
|
||||||
response = self.update(pk, data)
|
|
||||||
self.assertTrue(response.status_code >= 400)
|
|
||||||
|
|
||||||
def destroy(self, pk) -> Response:
|
def destroy(self, pk) -> Response:
|
||||||
url = self.get_url("detail", pk=pk)
|
url = self.get_url("detail", pk=pk)
|
||||||
return self.client.delete(url, format="json")
|
return self.client.delete(url, format="json")
|
||||||
|
@ -95,15 +90,17 @@ class ResearchGroupTests(SophonModelTestCase):
|
||||||
def get_basename(cls) -> str:
|
def get_basename(cls) -> str:
|
||||||
return "research-group"
|
return "research-group"
|
||||||
|
|
||||||
|
test_user: User = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
test_user = User.objects.create_user(username="TEST", password="TheGreatDjangoTest")
|
cls.test_user = User.objects.create_user(username="TEST", password="TheGreatDjangoTest")
|
||||||
|
|
||||||
models.ResearchGroup.objects.create(
|
models.ResearchGroup.objects.create(
|
||||||
slug="alpha",
|
slug="alpha",
|
||||||
name="Alpha",
|
name="Alpha",
|
||||||
description="First test group.",
|
description="First test group.",
|
||||||
owner=test_user,
|
owner=cls.test_user,
|
||||||
access="MANUAL",
|
access="MANUAL",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -111,7 +108,7 @@ class ResearchGroupTests(SophonModelTestCase):
|
||||||
slug="beta",
|
slug="beta",
|
||||||
name="Beta",
|
name="Beta",
|
||||||
description="Second test group.",
|
description="Second test group.",
|
||||||
owner=test_user,
|
owner=cls.test_user,
|
||||||
access="OPEN",
|
access="OPEN",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -130,7 +127,7 @@ class ResearchGroupTests(SophonModelTestCase):
|
||||||
self.assertIn("members", results[0])
|
self.assertIn("members", results[0])
|
||||||
self.assertIn("access", results[0])
|
self.assertIn("access", results[0])
|
||||||
|
|
||||||
def test_retrieve(self):
|
def test_retrieve_valid(self):
|
||||||
result = self.retrieve_unwrap("alpha")
|
result = self.retrieve_unwrap("alpha")
|
||||||
|
|
||||||
self.assertIn("slug", result)
|
self.assertIn("slug", result)
|
||||||
|
@ -143,11 +140,14 @@ class ResearchGroupTests(SophonModelTestCase):
|
||||||
self.assertEqual(result["slug"], "alpha")
|
self.assertEqual(result["slug"], "alpha")
|
||||||
self.assertEqual(result["name"], "Alpha")
|
self.assertEqual(result["name"], "Alpha")
|
||||||
self.assertEqual(result["description"], "First test group.")
|
self.assertEqual(result["description"], "First test group.")
|
||||||
# TODO: How to verify the owner id?
|
self.assertEqual(result["owner"], self.test_user.id)
|
||||||
self.assertEqual(result["members"], [])
|
self.assertEqual(result["members"], [])
|
||||||
self.assertEqual(result["access"], "MANUAL")
|
self.assertEqual(result["access"], "MANUAL")
|
||||||
|
|
||||||
def test_create(self):
|
def test_retrieve_not_existing(self):
|
||||||
|
self.retrieve_fail("banana")
|
||||||
|
|
||||||
|
def test_create_valid(self):
|
||||||
self.client.login(username="TEST", password="TheGreatDjangoTest")
|
self.client.login(username="TEST", password="TheGreatDjangoTest")
|
||||||
|
|
||||||
result = self.create_unwrap({
|
result = self.create_unwrap({
|
||||||
|
@ -175,9 +175,123 @@ class ResearchGroupTests(SophonModelTestCase):
|
||||||
self.assertEqual(check["slug"], "omega")
|
self.assertEqual(check["slug"], "omega")
|
||||||
self.assertEqual(check["name"], "Omega")
|
self.assertEqual(check["name"], "Omega")
|
||||||
self.assertEqual(check["description"], "Last test group.")
|
self.assertEqual(check["description"], "Last test group.")
|
||||||
# TODO: How to verify the owner id?
|
self.assertEqual(result["owner"], self.test_user.id)
|
||||||
self.assertEqual(result["members"], [])
|
self.assertEqual(result["members"], [])
|
||||||
self.assertEqual(result["access"], "OPEN")
|
self.assertEqual(result["access"], "OPEN")
|
||||||
|
|
||||||
# TODO: Create update test
|
def test_create_not_logged_in(self):
|
||||||
|
self.create_fail({
|
||||||
|
"slug": "fail",
|
||||||
|
"name": "Failure",
|
||||||
|
"description": "This creation should fail.",
|
||||||
|
"members": [],
|
||||||
|
"access": "OPEN",
|
||||||
|
})
|
||||||
|
|
||||||
|
def test_create_invalid_schema(self):
|
||||||
|
self.client.login(username="TEST", password="TheGreatDjangoTest")
|
||||||
|
|
||||||
|
self.create_fail({
|
||||||
|
"potato": "sweet",
|
||||||
|
"access": "OPEN",
|
||||||
|
})
|
||||||
|
|
||||||
|
def test_update_valid(self):
|
||||||
|
self.client.login(username="TEST", password="TheGreatDjangoTest")
|
||||||
|
|
||||||
|
creation = self.create_unwrap({
|
||||||
|
"slug": "gamma",
|
||||||
|
"name": "Gamma",
|
||||||
|
"description": "A test group to update.",
|
||||||
|
"members": [],
|
||||||
|
"access": "OPEN",
|
||||||
|
})
|
||||||
|
self.assertIn("slug", creation)
|
||||||
|
self.assertIn("name", creation)
|
||||||
|
self.assertIn("description", creation)
|
||||||
|
self.assertIn("members", creation)
|
||||||
|
self.assertIn("access", creation)
|
||||||
|
|
||||||
|
check = self.retrieve_unwrap("gamma")
|
||||||
|
|
||||||
|
self.assertEqual(check["slug"], "gamma")
|
||||||
|
self.assertEqual(check["name"], "Gamma")
|
||||||
|
self.assertEqual(check["description"], "A test group to update.")
|
||||||
|
self.assertEqual(check["owner"], self.test_user.id)
|
||||||
|
self.assertEqual(check["members"], [])
|
||||||
|
self.assertEqual(check["access"], "OPEN")
|
||||||
|
|
||||||
|
update = self.update_unwrap("gamma", {
|
||||||
|
"slug": "gamma",
|
||||||
|
"name": "Gamma",
|
||||||
|
"description": "An updated test group.",
|
||||||
|
"members": [],
|
||||||
|
"access": "MANUAL",
|
||||||
|
})
|
||||||
|
|
||||||
|
self.assertIn("slug", update)
|
||||||
|
self.assertIn("name", update)
|
||||||
|
self.assertIn("description", update)
|
||||||
|
self.assertIn("owner", update)
|
||||||
|
self.assertIn("members", update)
|
||||||
|
self.assertIn("access", update)
|
||||||
|
|
||||||
|
self.assertEqual(update["slug"], "gamma")
|
||||||
|
self.assertEqual(update["name"], "Gamma")
|
||||||
|
self.assertEqual(update["description"], "An updated test group.")
|
||||||
|
self.assertEqual(update["owner"], self.test_user.id)
|
||||||
|
self.assertEqual(update["members"], [])
|
||||||
|
self.assertEqual(update["access"], "MANUAL")
|
||||||
|
|
||||||
|
check2 = self.retrieve_unwrap("gamma")
|
||||||
|
|
||||||
|
self.assertEqual(check2["slug"], "gamma")
|
||||||
|
self.assertEqual(check2["name"], "Gamma")
|
||||||
|
self.assertEqual(check2["description"], "An updated test group.")
|
||||||
|
self.assertEqual(check2["owner"], self.test_user.id)
|
||||||
|
self.assertEqual(check2["members"], [])
|
||||||
|
self.assertEqual(check2["access"], "MANUAL")
|
||||||
|
|
||||||
|
def test_update_not_logged_in(self):
|
||||||
|
result = self.update_unwrap("alpha", {
|
||||||
|
"slug": "alpha",
|
||||||
|
"name": "AAAAA",
|
||||||
|
"description": "An hacker has updated the Alpha group without permissions!",
|
||||||
|
"members": [],
|
||||||
|
"access": "MANUAL",
|
||||||
|
})
|
||||||
|
|
||||||
|
self.assertIn("slug", result)
|
||||||
|
self.assertIn("name", result)
|
||||||
|
self.assertIn("description", result)
|
||||||
|
self.assertIn("owner", result)
|
||||||
|
self.assertIn("members", result)
|
||||||
|
self.assertIn("access", result)
|
||||||
|
|
||||||
|
self.assertEqual(result["slug"], "alpha")
|
||||||
|
self.assertEqual(result["name"], "Alpha")
|
||||||
|
self.assertEqual(result["description"], "First test group.")
|
||||||
|
self.assertEqual(result["owner"], self.test_user.id)
|
||||||
|
self.assertEqual(result["members"], [])
|
||||||
|
self.assertEqual(result["access"], "MANUAL")
|
||||||
|
|
||||||
|
def test_update_invalid_schema(self):
|
||||||
|
result = self.update_unwrap("alpha", {
|
||||||
|
"hahaha": "soccer",
|
||||||
|
})
|
||||||
|
|
||||||
|
self.assertIn("slug", result)
|
||||||
|
self.assertIn("name", result)
|
||||||
|
self.assertIn("description", result)
|
||||||
|
self.assertIn("owner", result)
|
||||||
|
self.assertIn("members", result)
|
||||||
|
self.assertIn("access", result)
|
||||||
|
|
||||||
|
self.assertEqual(result["slug"], "alpha")
|
||||||
|
self.assertEqual(result["name"], "Alpha")
|
||||||
|
self.assertEqual(result["description"], "First test group.")
|
||||||
|
self.assertEqual(result["owner"], self.test_user.id)
|
||||||
|
self.assertEqual(result["members"], [])
|
||||||
|
self.assertEqual(result["access"], "MANUAL")
|
||||||
|
|
||||||
# TODO: Create destroy test
|
# TODO: Create destroy test
|
||||||
|
|
Loading…
Reference in a new issue