-
-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Project-Teams API tests #5631
Open
d-rita
wants to merge
1
commit into
develop
Choose a base branch
from
chore/add-project-teams-tests
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,238 @@ | ||
from tests.backend.base import BaseTestCase | ||
from tests.backend.helpers.test_helpers import ( | ||
assign_team_to_project, | ||
create_canned_project, | ||
create_canned_team, | ||
return_canned_user, | ||
generate_encoded_token, | ||
TEST_TEAM_NAME, | ||
) | ||
from backend.models.postgis.statuses import UserRole, TeamRoles | ||
|
||
|
||
class TestProjectsTeamsAPI(BaseTestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.test_project, self.test_author = create_canned_project() | ||
self.test_author.role = UserRole.ADMIN.value | ||
self.test_team = create_canned_team() | ||
self.test_user = return_canned_user("test_user", 11111111) | ||
self.test_user.create() | ||
self.test_author_session_token = generate_encoded_token(self.test_author.id) | ||
self.test_user_session_token = generate_encoded_token(self.test_user.id) | ||
self.all_project_teams_url = f"/api/v2/projects/{self.test_project.id}/teams/" | ||
self.single_project_team_url = ( | ||
f"/api/v2/projects/{self.test_project.id}/teams/{self.test_team.id}/" | ||
) | ||
self.non_existent_project_team_url = "/api/v2/projects/99/teams/99/" | ||
|
||
# get | ||
def test_get_project_teams_by_unauthenticated_user_fails(self): | ||
""" | ||
Test that endpoint returns 401 when an unauthenticated user retrieves teams | ||
""" | ||
response = self.client.get(self.all_project_teams_url) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 401) | ||
self.assertEqual(response_body["SubCode"], "InvalidToken") | ||
|
||
def test_get_project_teams_for_non_existent_project_fails(self): | ||
""" | ||
Test that endpoint returns 404 when retrieving teams for non-existent projects | ||
""" | ||
response = self.client.get( | ||
"/api/v2/projects/99/teams/", | ||
headers={"Authorization": self.test_user_session_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_project_teams_passes(self): | ||
""" | ||
Test that endpoint returns 200 when an authenticated user retrieves teams | ||
""" | ||
response = self.client.get( | ||
self.all_project_teams_url, | ||
headers={"Authorization": self.test_user_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(len(response_body["teams"]), 0) | ||
self.assertEqual(response_body["teams"], []) | ||
# setup: add team to project | ||
assign_team_to_project(project=self.test_project, team=self.test_team, role=0) | ||
response = self.client.get( | ||
self.all_project_teams_url, | ||
headers={"Authorization": self.test_user_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(len(response_body["teams"]), 1) | ||
self.assertEqual(response_body["teams"][0]["name"], TEST_TEAM_NAME) | ||
self.assertEqual(response_body["teams"][0]["role"], 0) | ||
|
||
# post | ||
def test_assign_team_to_project_by_unauthenticated_user_fails(self): | ||
""" | ||
Test that endpoint returns 401 when unauthenticated user assigns team to project | ||
""" | ||
response = self.client.post( | ||
self.single_project_team_url, json={"role": TeamRoles.MAPPER.name} | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 401) | ||
self.assertEqual(response_body["SubCode"], "InvalidToken") | ||
|
||
def test_assign_team_to_project_by_non_admin_fails(self): | ||
""" | ||
Test that endpoint returns 403 when non admin assigns team to a project | ||
""" | ||
response = self.client.post( | ||
self.single_project_team_url, | ||
json={"role": TeamRoles.MAPPER.name}, | ||
headers={"Authorization": self.test_user_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 403) | ||
self.assertEqual( | ||
response_body["Error"], "User is not an admin or a manager for the team" | ||
) | ||
self.assertEqual(response_body["SubCode"], "UserPermissionError") | ||
|
||
def test_assign_team_to_non_existent_project_fails(self): | ||
""" | ||
Test that endpoint returns 404 when admin assigns a team to a non-existent project | ||
""" | ||
response = self.client.post( | ||
f"/api/v2/projects/99/teams/{self.test_team.id}/", | ||
json={"role": TeamRoles.MAPPER.name}, | ||
headers={"Authorization": self.test_author_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 404) | ||
self.assertEqual(response_body["Error"], "No Project Found") | ||
self.assertEqual(response_body["SubCode"], "NotFound") | ||
|
||
def test_assign_team_to_project_by_admin_passes(self): | ||
""" | ||
Test that endpoint returns 201 when admin successfully assigns a team to a project | ||
""" | ||
response = self.client.post( | ||
self.single_project_team_url, | ||
json={"role": TeamRoles.MAPPER.name}, | ||
headers={"Authorization": self.test_author_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 201) | ||
self.assertEqual( | ||
response_body["Success"], | ||
f"Team {self.test_team.id} assigned to project {self.test_project.id} with role MAPPER", | ||
) | ||
|
||
# patch | ||
def test_update_team_role_by_unauthenticated_user_fails(self): | ||
""" | ||
Test that endpoint returns 401 when unauthenticated user updates project team role | ||
""" | ||
response = self.client.patch( | ||
self.single_project_team_url, json={"role": TeamRoles.MAPPER.name} | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 401) | ||
self.assertEqual(response_body["SubCode"], "InvalidToken") | ||
|
||
def test_update_team_role_by_non_admin_fails(self): | ||
""" | ||
Test that endpoint returns 403 when non admin updates project team role | ||
""" | ||
response = self.client.patch( | ||
self.single_project_team_url, | ||
json={"role": TeamRoles.MAPPER.name}, | ||
headers={"Authorization": self.test_user_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 403) | ||
self.assertEqual(response_body["Error"], "User is not a manager of the project") | ||
self.assertEqual(response_body["SubCode"], "UserPermissionError") | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here a test to determine whether a 400 is raised when an invalid team role is passed appears to be missing. |
||
def test_update_team_role_of_non_existent_project_fails(self): | ||
""" | ||
Test that endpoint returns 404 when admin updates non-existent project team role | ||
""" | ||
response = self.client.patch( | ||
self.non_existent_project_team_url, | ||
json={"role": TeamRoles.MAPPER.name}, | ||
headers={"Authorization": self.test_author_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 404) | ||
self.assertEqual(response_body["SubCode"], "NotFound") | ||
|
||
def test_update_team_role_by_admin_passes(self): | ||
""" | ||
Test that endpoint returns 200 when admin successfully updates project team role | ||
""" | ||
assign_team_to_project( | ||
self.test_project, self.test_team, TeamRoles.MAPPER.value | ||
) | ||
response = self.client.patch( | ||
self.single_project_team_url, | ||
json={"role": TeamRoles.VALIDATOR.name}, | ||
headers={"Authorization": self.test_author_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response_body["Status"], "Team role updated successfully") | ||
|
||
# delete | ||
def test_delete_project_team_by_unauthenticated_user_fails(self): | ||
""" | ||
Test that endpoint returns 401 when unauthenticated user deletes project team | ||
""" | ||
response = self.client.delete(self.single_project_team_url) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 401) | ||
self.assertEqual(response_body["SubCode"], "InvalidToken") | ||
|
||
def test_delete_project_team_by_non_admin_fails(self): | ||
""" | ||
Test that endpoint returns 403 when non admin deletes project team | ||
""" | ||
response = self.client.delete( | ||
self.single_project_team_url, | ||
headers={"Authorization": self.test_user_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 403) | ||
self.assertEqual(response_body["Error"], "User is not a manager of the project") | ||
self.assertEqual(response_body["SubCode"], "UserPermissionError") | ||
|
||
def test_delete_non_existent_project_team_fails(self): | ||
""" | ||
Test that endpoint returns 404 when admin deletes non-existent project team | ||
""" | ||
response = self.client.delete( | ||
self.non_existent_project_team_url, | ||
headers={"Authorization": self.test_author_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 404) | ||
self.assertEqual(response_body["Error"], "No team found") | ||
self.assertEqual(response_body["SubCode"], "NotFound") | ||
|
||
def test_delete_project_team_by_admin_passes(self): | ||
""" | ||
Test that endpoint returns 200 when admin successfully deletes project team | ||
""" | ||
assign_team_to_project( | ||
self.test_project, self.test_team, TeamRoles.MAPPER.value | ||
) | ||
response = self.client.delete( | ||
self.single_project_team_url, | ||
headers={"Authorization": self.test_author_session_token}, | ||
) | ||
response_body = response.get_json() | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response_body["Success"], True) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A test to determine if 403 is raised when the user does not have permission to update the project appears to be missing as well. Only a team's user permission is tested in the test above
Also test to determine whether a 400 is raised when an invalid team role is passed appears to be missing.