Skip to content

Commit

Permalink
Add comments endpoints tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d-rita committed Mar 22, 2023
1 parent 0a70a63 commit a9a7ec6
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 4 deletions.
4 changes: 0 additions & 4 deletions backend/api/comments/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,6 @@ def get(self, project_id, task_id):
description: Comment retrieved
400:
description: Client Error
401:
description: Unauthorized - Invalid credentials
403:
description: Forbidden
404:
description: Task not found
500:
Expand Down
212 changes: 212 additions & 0 deletions tests/backend/integration/api/comments/test_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
from tests.backend.base import BaseTestCase
from tests.backend.helpers.test_helpers import (
create_canned_project,
generate_encoded_token,
return_canned_user,
)
from backend.models.postgis.statuses import UserRole

TEST_MESSAGE = "Test comment"


class TestCommentsProjectsRestAPI(BaseTestCase):
def setUp(self):
super().setUp()
self.test_project, self.test_author = create_canned_project()
self.test_author_token = generate_encoded_token(self.test_author.id)
self.endpoint_url = f"/api/v2/projects/{self.test_project.id}/comments/"

# post
def test_post_comment_to_project_chat_by_unauthenticated_user_fails(self):
"""
Test that endpoint returns 401 when an unauthenticated user posts a comment to project chat
"""
response = self.client.post(self.endpoint_url, json={"message": TEST_MESSAGE})
response_body = response.get_json()
self.assertEqual(response.status_code, 401)
self.assertEqual(response_body["SubCode"], "InvalidToken")

def test_post_comment_to_project_chat_by_unauthorised_user_fails(self):
"""
Test that endpoint returns 403 when a blocked user posts a comment to project chat
"""
self.test_user = return_canned_user(username="test_user", id=33333)
self.test_user.role = UserRole.READ_ONLY.value
self.test_token = generate_encoded_token(self.test_user.id)
response = self.client.post(
self.endpoint_url,
headers={"Authorization": self.test_token},
json={"message": TEST_MESSAGE},
)
response_body = response.get_json()
self.assertEqual(response.status_code, 403)
self.assertEqual(response_body["Error"], "ReadOnly")
self.assertEqual(response_body["SubCode"], "ReadOnly")

def test_invalid_request_to_post_comment_to_project_chat_fails(self):
"""
Test that endpoint returns 400 when an invalid request to post a comment to the project chat is made
"""
response = self.client.post(
self.endpoint_url,
headers={"Authorization": self.test_author_token},
json={"comment": TEST_MESSAGE},
)
response_body = response.get_json()
self.assertEqual(response.status_code, 400)
self.assertEqual(response_body["Error"], "Unable to add chat message")
self.assertEqual(response_body["SubCode"], "InvalidData")

def test_post_comment_to_chat_of_non_existent_project_fails(self):
"""
Test that endpoint returns 404 when user fails to post a comment to a non-existent project chat
"""
response = self.client.post(
"/api/v2/projects/99/comments/",
headers={"Authorization": self.test_author_token},
json={"comment": TEST_MESSAGE},
)
response_body = response.get_json()
self.assertEqual(response.status_code, 400)
self.assertEqual(response_body["Error"], "Unable to add chat message")
self.assertEqual(response_body["SubCode"], "InvalidData")

def test_post_comment_to_project_chat_passes(self):
"""
Test that endpoint returns 201 when user successfully posts a comment to the project chat
"""
response = self.client.post(
self.endpoint_url,
headers={"Authorization": self.test_author_token},
json={"message": TEST_MESSAGE},
)
response_body = response.get_json()
self.assertEqual(response.status_code, 201)
self.assertEqual(len(response_body["chat"]), 1)
chat_details = response_body["chat"][0]
self.assertEqual(chat_details["message"], "<p>Test comment</p>")

# get
def test_get_chat_messages_of_non_existent_project_fails(self):
"""
Test that endpoint returns 404 when retrieving the chat of a non-existent project
"""
response = self.client.get("/api/v2/projects/99/comments/")
response_body = response.get_json()
self.assertEqual(response.status_code, 404)
self.assertEqual(response_body["Error"], "Project not found")
self.assertEqual(response_body["SubCode"], "NotFound")

def test_get_project_chat_messages_passes(self):
"""
Test that endpoint returns 200 when retrieving the task chat of an existing project
"""
response = self.client.get(self.endpoint_url)
response_body = response.get_json()
self.assertEqual(response.status_code, 200)
self.assertEqual(response_body, {"chat": [], "pagination": None})
# to do: add comments and test again


class TestCommentsTasksRestAPI(BaseTestCase):
def setUp(self):
super().setUp()
self.test_project, self.test_author = create_canned_project()
self.test_author_token = generate_encoded_token(self.test_author.id)
self.endpoint_url = f"/api/v2/projects/{self.test_project.id}/comments/tasks/1/"

# post
def test_post_comment_to_task_chat_by_unauthenticated_user_fails(self):
"""
Test that endpoint returns 401 when an unauthenticated user posts a comment to the task chat
"""
response = self.client.post(self.endpoint_url, json={"comment": TEST_MESSAGE})
response_body = response.get_json()
self.assertEqual(response.status_code, 401)
self.assertEqual(response_body["SubCode"], "InvalidToken")

def test_post_comment_to_task_chat_by_unauthorised_user_fails(self):
"""
Test that endpoint returns 403 when a blocked user posts a comment to the task chat
"""
self.test_user = return_canned_user("test_user", 33333)
self.test_user.role = UserRole.READ_ONLY.value
self.test_user.save()
self.test_token = generate_encoded_token(self.test_user.id)
response = self.client.post(
self.endpoint_url,
headers={"Authorization": self.test_token},
json={"comment": TEST_MESSAGE},
)
response_body = response.get_json()
self.assertEqual(response.status_code, 403)
self.assertEqual(response_body["Error"], "ReadOnly")
self.assertEqual(response_body["SubCode"], "ReadOnly")

def test_post_comment_to_task_chat_using_an_invalid_request_fails(self):
"""
Test that endpoint returns 400 when an invalid request to post a comment to the task chat is made
"""
response = self.client.post(
self.endpoint_url,
headers={"Authorization": self.test_author_token},
json={"message": TEST_MESSAGE},
)
response_body = response.get_json()
self.assertEqual(response.status_code, 400)
self.assertEqual(response_body["Error"], "Unable to add comment")
self.assertEqual(response_body["SubCode"], "InvalidData")

def test_post_comment_to_task_chat_of_non_existent_project_fails(self):
"""
Test that endpoint returns 404 when user fails to post a comment to a non-existent project task chat
"""
response = self.client.post(
"/api/v2/projects/99/comments/tasks/1/",
headers={"Authorization": self.test_author_token},
json={"comment": TEST_MESSAGE},
)
response_body = response.get_json()
self.assertEqual(response.status_code, 404)
self.assertEqual(response_body["Error"], "Task Not Found")
self.assertEqual(response_body["SubCode"], "NotFound")

def test_post_comment_to_task_chat_passes(self):
"""
Test that endpoint returns 201 when user successfully posts a comment to the task chat
"""
response = self.client.post(
self.endpoint_url,
headers={"Authorization": self.test_author_token},
json={"comment": TEST_MESSAGE},
)
response_body = response.get_json()
self.assertEqual(response.status_code, 201)
self.assertEqual(response_body["taskId"], 1)

# get
def test_get_task_chat_of_non_existent_project_fails(self):
"""
Test that endpoint returns 404 when retrieving the task chat of a non-existent project
"""
response = self.client.get(
"/api/v2/projects/99/comments/",
headers={"Authorization": self.test_author_token},
)
response_body = response.get_json()
self.assertEqual(response.status_code, 404)
self.assertEqual(response_body["Error"], "Project not found")
self.assertEqual(response_body["SubCode"], "NotFound")

def test_get_task_chat_of_existing_project_passes(self):
"""
Test does not run
Reason: unimplemented
backend/api/comments/resources.py L228
GET method has no 200 return
"""
# response =
self.client.get(
self.endpoint_url, headers={"Authorization": self.test_author_token}
)
# self.assertEqual(response.status_code, 200)

0 comments on commit a9a7ec6

Please sign in to comment.