mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-22 06:44:21 +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.urls import reverse
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
|
@ -73,10 +72,6 @@ class SophonModelTestCase(APITestCase, metaclass=abc.ABCMeta):
|
|||
self.assertTrue(isinstance(response.data, dict))
|
||||
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:
|
||||
url = self.get_url("detail", pk=pk)
|
||||
return self.client.delete(url, format="json")
|
||||
|
@ -95,15 +90,17 @@ class ResearchGroupTests(SophonModelTestCase):
|
|||
def get_basename(cls) -> str:
|
||||
return "research-group"
|
||||
|
||||
test_user: User = None
|
||||
|
||||
@classmethod
|
||||
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(
|
||||
slug="alpha",
|
||||
name="Alpha",
|
||||
description="First test group.",
|
||||
owner=test_user,
|
||||
owner=cls.test_user,
|
||||
access="MANUAL",
|
||||
)
|
||||
|
||||
|
@ -111,7 +108,7 @@ class ResearchGroupTests(SophonModelTestCase):
|
|||
slug="beta",
|
||||
name="Beta",
|
||||
description="Second test group.",
|
||||
owner=test_user,
|
||||
owner=cls.test_user,
|
||||
access="OPEN",
|
||||
)
|
||||
|
||||
|
@ -130,7 +127,7 @@ class ResearchGroupTests(SophonModelTestCase):
|
|||
self.assertIn("members", results[0])
|
||||
self.assertIn("access", results[0])
|
||||
|
||||
def test_retrieve(self):
|
||||
def test_retrieve_valid(self):
|
||||
result = self.retrieve_unwrap("alpha")
|
||||
|
||||
self.assertIn("slug", result)
|
||||
|
@ -143,11 +140,14 @@ class ResearchGroupTests(SophonModelTestCase):
|
|||
self.assertEqual(result["slug"], "alpha")
|
||||
self.assertEqual(result["name"], "Alpha")
|
||||
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["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")
|
||||
|
||||
result = self.create_unwrap({
|
||||
|
@ -175,9 +175,123 @@ class ResearchGroupTests(SophonModelTestCase):
|
|||
self.assertEqual(check["slug"], "omega")
|
||||
self.assertEqual(check["name"], "Omega")
|
||||
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["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
|
||||
|
|
Loading…
Reference in a new issue