Skip to content

Commit

Permalink
CONCD-663 Unit tests for SerializedObjectView.get, admin_bulk_import_…
Browse files Browse the repository at this point in the history
…review (#2292)

* CONCD-663 test case where object does not exist

* CONCD-663 unit test for admin_bulk_import_review (WiP)

* CONCD-663 removing unfinished test
  • Loading branch information
rasarkar authored Feb 26, 2024
1 parent 4871d24 commit bb3dbbf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
32 changes: 29 additions & 3 deletions concordia/tests/test_admin_views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import json
from http import HTTPStatus

from django.test import RequestFactory, TestCase
from django.urls import reverse

from concordia.admin.views import SerializedObjectView
from concordia.tests.utils import create_card
from concordia.tests.utils import CreateTestUsers, create_card


class TestFunctionBasedViews(CreateTestUsers, TestCase):
def test_admin_bulk_import_review(self):
self.login_user(is_staff=True, is_superuser=True)
self.assertTrue(self.user.is_active)
self.assertTrue(self.user.is_staff)
self.assertTrue(self.user.is_superuser)
path = reverse("admin:bulk-review")
response = self.client.get(path)
self.assertEqual(response.status_code, 200)

data = {}
response = self.client.post(path, data=data)
self.assertEqual(response.status_code, 200)


class TestSerializedObjectView(TestCase):
Expand All @@ -12,11 +29,20 @@ def setUp(self):
# Every test needs access to the request factory.
self.factory = RequestFactory()

def test_get(self):
def test_exists(self):
request = self.factory.get(
"/admin/card/",
{"model_name": "Card", "object_id": 1, "field_name": "title"},
{"model_name": "Card", "object_id": self.card.id, "field_name": "title"},
)
response = SerializedObjectView.as_view()(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content)["title"], self.card.title)

def test_dne(self):
request = self.factory.get(
"/admin/card/",
{"model_name": "Card", "object_id": 2, "field_name": "title"},
)
response = SerializedObjectView.as_view()(request)
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)
self.assertJSONEqual(response.content, {"status": "false"})
4 changes: 2 additions & 2 deletions concordia/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ def assertValidJSON(self, response, expected_status=200):


class CreateTestUsers(object):
def login_user(self, username="tester"):
def login_user(self, username="tester", **kwargs):
"""
Create a user and log the user in
"""

if not hasattr(self, "user"):
self.user = self.create_test_user(username)
self.user = self.create_test_user(username, **kwargs)

self.client.login(username=self.user.username, password=self.user._password)

Expand Down

0 comments on commit bb3dbbf

Please sign in to comment.