-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
212 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
tests/backend/integration/api/comments/test_resources.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |