Skip to content

Commit

Permalink
implement unit tests of workspace post api
Browse files Browse the repository at this point in the history
  • Loading branch information
Simurgan committed Nov 25, 2023
1 parent 73cc194 commit 16e97bd
Showing 1 changed file with 87 additions and 2 deletions.
89 changes: 87 additions & 2 deletions project/backend/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,95 @@
from database.serializers import RegisterSerializer, UserSerializer
from database.models import *
import datetime

# Create your tests here for each class or API call.

class WorkspacePOSTAPITestCase(TestCase):
def setUp(self):
self.client = APIClient()

self.user_for_basic = User.objects.create_user(id=1, email= '[email protected]', username='[email protected]', first_name='Basic User', last_name='Test1')
self.user_for_contributor1 = User.objects.create_user(id=2, email= '[email protected]', username='[email protected]', first_name='Contributor User 2', last_name='Test2')
self.user_for_contributor2 = User.objects.create_user(id=3, email= '[email protected]', username='[email protected]', first_name='Contributor User 3', last_name='Test3')

self.basic_user = BasicUser.objects.create(user=self.user_for_basic, bio="I am a basic user")
self.contributor1 = Contributor.objects.create(user=self.user_for_contributor1, bio="I am the first contributor")
self.contributor2 = Contributor.objects.create(user=self.user_for_contributor2, bio="I am the second contributor")

self.basic_user_token = Token.objects.create(user=self.user_for_basic)
self.contributor1_token = Token.objects.create(user=self.user_for_contributor1)
self.contributor2_token = Token.objects.create(user=self.user_for_contributor2)

self.semantic_tag1 = SemanticTag.objects.create(wid="Q1", label="Semantic Tag 1")
self.semantic_tag2 = SemanticTag.objects.create(wid="Q2", label="Semantic Tag 2")

def tearDown(self):
User.objects.all().delete()
BasicUser.objects.all().delete()
Contributor.objects.all().delete()
Token.objects.all().delete()
Workspace.objects.all().delete()
SemanticTag.objects.all().delete()

print("All tests for the Workspace POST API are completed!")

def test_create_workspace(self):
# Testing the POST method for basic user tries to create workspace
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.basic_user_token.key}")
data = {
"workspace_title": "Basic User Workspace",
"semantic_tags": [self.semantic_tag1.pk]
}

response = self.client.post(reverse("workspace_post"), data, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, "Test failed: Basic user shouldnot be able to create a workspace")

# Testing the POST method for creating without title
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.contributor1_token.key}")

data = {
"semantic_tags": [self.semantic_tag1.pk]
}
response = self.client.post(reverse("workspace_post"), data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, "Test failed: Workspace cannot be created without a title")

# Testing the POST method for creating without semantic tags
data = {
"workspace_title": "Contributor1 Workspace"
}
response = self.client.post(reverse("workspace_post"), data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Test failed: Workspace can be created without semantic tags")

workspaces = self.contributor1.workspaces.all()
self.assertEqual(workspaces.count(), 1, "Test failed: Workspace could not be created")

# Testing the POST method for updating workspace
if workspaces.count() > 0:
workspace = workspaces[0]

data = {
"workspace_id": workspace.workspace_id,
"workspace_title": "Contributor1 Workspace Updated",
"semantic_tags": [self.semantic_tag2.pk]
}
response = self.client.post(reverse("workspace_post"), data, format="json")

workspace = Workspace.objects.get(workspace_id=workspace.workspace_id)
self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Update failure")
self.assertEqual(workspace.semantic_tags.count(), 1, "Update failure for semantic tags count")
self.assertEqual(workspace.semantic_tags.all()[0].wid, self.semantic_tag2.wid, "Update failure for semantic tag")
self.assertEqual(workspace.workspace_title, "Contributor1 Workspace Updated", "Update failure for title")

# Try to update workspace of a different contributor
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.contributor2_token.key}")
data = {
"workspace_id": workspace.workspace_id,
"workspace_title": "Contributor2 Workspace Updated",
"semantic_tags": [self.semantic_tag1.pk]
}
response = self.client.post(reverse("workspace_post"), data, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, "Test failed: Contributor2 shouldnot be able to update Contributor1's workspace")


class SignUpAPIViewTestCase(TestCase):
def setUp(self):
Expand Down Expand Up @@ -458,7 +545,6 @@ def setUp(self):

def test_get_workspaces_of_user(self):
response = self.client.get(self.url, {'user_id': self.cont.id})
print(response)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['workspaces'][0]['workspace_id'],self.workspace.workspace_id)
Expand All @@ -478,7 +564,6 @@ def setUp(self):

def test_get_workspace_from_id(self):
response = self.client.get(self.url, {'workspace_id': self.workspace.workspace_id})
print(response)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['workspace_id'],self.workspace.workspace_id)
self.assertEqual(response.json()['workspace_id'], self.workspace.workspace_id)
Expand Down

0 comments on commit 16e97bd

Please sign in to comment.