Skip to content

Commit

Permalink
Merge branch 'backend' into node-update
Browse files Browse the repository at this point in the history
  • Loading branch information
Simurgan authored Nov 26, 2023
2 parents e3b537f + dce8906 commit 5701edb
Show file tree
Hide file tree
Showing 6 changed files with 435 additions and 58 deletions.
97 changes: 95 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 @@ -406,6 +493,13 @@ def test_get_random_node(self):
self.assertContains(response, 'question_set')
self.assertContains(response, 'semantic_tags')
self.assertContains(response, 'annotations')
def test_get_random_node_id(self):
url = reverse('get_random_node_id')
response = self.client.get(url , {'count':2})
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertIn('node_ids', data)
self.assertEqual(len(data['node_ids']), 2)

class TheoremGETAPITestCase(TestCase):
def setUp(self):
Expand Down Expand Up @@ -478,7 +572,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 @@ -498,14 +591,14 @@ 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)
self.assertEqual(response.json()['contributors'], [{'id':self.cont.id,'first_name':self.user.first_name,'last_name':self.user.last_name,'username':self.user.username}])
self.assertEqual(response.json()['workspace_title'], self.workspace.workspace_title)
self.assertEqual(response.json()['status'], 'workable')
self.assertEqual(response.json()['references'], [])
self.assertEqual(response.json()['semantic_tags'], [])
self.assertEqual(response.json()['pending_contributors'], [])
self.assertEqual(response.json()['num_approvals'], 0)
# self.assertEqual(response.json()['created_at'], self.workspace.created_at)
Expand Down
6 changes: 6 additions & 0 deletions project/backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
path('signup/', SignUpAPIView.as_view(), name='signup'),
path('login/', obtain_auth_token, name='login'),
path('get_authenticated_user/', UserDetailAPI.as_view(), name='get_authenticated_user'),
path('get_authenticated_basic_user/', BasicUserDetailAPI.as_view(), name='get_authenticated_basic_user'),
path('get_node/', NodeAPIView.as_view(), name='get_node'),
path('search/', search, name='search'),
path('get_profile_info/', get_profile, name='get_profile'),
Expand All @@ -17,10 +18,12 @@
path('get_cont/', get_contributor_from_id, name='get_cont'),
path('get_user_workspaces/',get_workspaces,name='get_user_workspaces'),
path('get_workspace/',get_workspace_from_id,name='get_workspace'),
path('edit_entry/', edit_entry, name='edit_entry'),
path('delete_entry/',delete_entry,name='delete_entry'),
path('add_entry/',add_entry,name='add_entry'),
path('get_random_node_id/',get_random_node_id,name='get_random_node_id'),
path('create_workspace/',create_workspace,name='create_workspace'),
path('workspace_post/', WorkspacePostAPIView.as_view(), name='workspace_post'),
path('add_reference/',add_reference,name='add_reference'),
path('finalize_workspace/',finalize_workspace,name='finalize_workspace'),
path('delete_contributor/',delete_contributor,name='delete_contributor'),
Expand All @@ -30,4 +33,7 @@
path('send_collab_req/', send_collaboration_request, name='send_col_req'),
path('update_req/', update_request_status, name='update_req'),
path('send_rev_req/', send_review_request, name='send_rev_req'),
path('get_semantic_suggestion/', get_semantic_suggestion, name='get_semantic_suggestion'),


]
Loading

0 comments on commit 5701edb

Please sign in to comment.