|
| 1 | +from tests.backend.base import BaseTestCase |
| 2 | +from tests.backend.helpers.test_helpers import ( |
| 3 | + assign_team_to_project, |
| 4 | + create_canned_project, |
| 5 | + create_canned_team, |
| 6 | + return_canned_user, |
| 7 | + generate_encoded_token, |
| 8 | + TEST_TEAM_NAME, |
| 9 | +) |
| 10 | +from backend.models.postgis.statuses import UserRole, TeamRoles |
| 11 | + |
| 12 | + |
| 13 | +class TestProjectsTeamsAPI(BaseTestCase): |
| 14 | + def setUp(self): |
| 15 | + super().setUp() |
| 16 | + self.test_project, self.test_author = create_canned_project() |
| 17 | + self.test_author.role = UserRole.ADMIN.value |
| 18 | + self.test_team = create_canned_team() |
| 19 | + self.test_user = return_canned_user("test_user", 11111111) |
| 20 | + self.test_user.create() |
| 21 | + self.test_author_session_token = generate_encoded_token(self.test_author.id) |
| 22 | + self.test_user_session_token = generate_encoded_token(self.test_user.id) |
| 23 | + self.all_project_teams_url = f"/api/v2/projects/{self.test_project.id}/teams/" |
| 24 | + self.single_project_team_url = ( |
| 25 | + f"/api/v2/projects/{self.test_project.id}/teams/{self.test_team.id}/" |
| 26 | + ) |
| 27 | + self.non_existent_project_team_url = "/api/v2/projects/99/teams/99/" |
| 28 | + |
| 29 | + # get |
| 30 | + def test_get_project_teams_by_unauthenticated_user_fails(self): |
| 31 | + """ |
| 32 | + Test that endpoint returns 401 when an unauthenticated user retrieves teams |
| 33 | + """ |
| 34 | + response = self.client.get(self.all_project_teams_url) |
| 35 | + response_body = response.get_json() |
| 36 | + self.assertEqual(response.status_code, 401) |
| 37 | + self.assertEqual(response_body["SubCode"], "InvalidToken") |
| 38 | + |
| 39 | + def test_get_project_teams_for_non_existent_project_fails(self): |
| 40 | + """ |
| 41 | + Test that endpoint returns 404 when retrieving teams for non-existent projects |
| 42 | + """ |
| 43 | + response = self.client.get( |
| 44 | + "/api/v2/projects/99/teams/", |
| 45 | + headers={"Authorization": self.test_user_session_token}, |
| 46 | + ) |
| 47 | + response_body = response.get_json() |
| 48 | + self.assertEqual(response.status_code, 404) |
| 49 | + self.assertEqual(response_body["Error"], "Project Not Found") |
| 50 | + self.assertEqual(response_body["SubCode"], "NotFound") |
| 51 | + |
| 52 | + def test_get_project_teams_passes(self): |
| 53 | + """ |
| 54 | + Test that endpoint returns 200 when an authenticated user retrieves teams |
| 55 | + """ |
| 56 | + response = self.client.get( |
| 57 | + self.all_project_teams_url, |
| 58 | + headers={"Authorization": self.test_user_session_token}, |
| 59 | + ) |
| 60 | + response_body = response.get_json() |
| 61 | + self.assertEqual(response.status_code, 200) |
| 62 | + self.assertEqual(len(response_body["teams"]), 0) |
| 63 | + self.assertEqual(response_body["teams"], []) |
| 64 | + # setup: add team to project |
| 65 | + assign_team_to_project(project=self.test_project, team=self.test_team, role=0) |
| 66 | + response = self.client.get( |
| 67 | + self.all_project_teams_url, |
| 68 | + headers={"Authorization": self.test_user_session_token}, |
| 69 | + ) |
| 70 | + response_body = response.get_json() |
| 71 | + self.assertEqual(response.status_code, 200) |
| 72 | + self.assertEqual(len(response_body["teams"]), 1) |
| 73 | + self.assertEqual(response_body["teams"][0]["name"], TEST_TEAM_NAME) |
| 74 | + self.assertEqual(response_body["teams"][0]["role"], 0) |
| 75 | + |
| 76 | + # post |
| 77 | + def test_assign_team_to_project_by_unauthenticated_user_fails(self): |
| 78 | + """ |
| 79 | + Test that endpoint returns 401 when unauthenticated user assigns team to project |
| 80 | + """ |
| 81 | + response = self.client.post( |
| 82 | + self.single_project_team_url, json={"role": TeamRoles.MAPPER.name} |
| 83 | + ) |
| 84 | + response_body = response.get_json() |
| 85 | + self.assertEqual(response.status_code, 401) |
| 86 | + self.assertEqual(response_body["SubCode"], "InvalidToken") |
| 87 | + |
| 88 | + def test_assign_team_to_project_by_non_admin_fails(self): |
| 89 | + """ |
| 90 | + Test that endpoint returns 403 when non admin assigns team to a project |
| 91 | + """ |
| 92 | + response = self.client.post( |
| 93 | + self.single_project_team_url, |
| 94 | + json={"role": TeamRoles.MAPPER.name}, |
| 95 | + headers={"Authorization": self.test_user_session_token}, |
| 96 | + ) |
| 97 | + response_body = response.get_json() |
| 98 | + self.assertEqual(response.status_code, 403) |
| 99 | + self.assertEqual( |
| 100 | + response_body["Error"], "User is not an admin or a manager for the team" |
| 101 | + ) |
| 102 | + self.assertEqual(response_body["SubCode"], "UserPermissionError") |
| 103 | + |
| 104 | + def test_assign_team_to_non_existent_project_fails(self): |
| 105 | + """ |
| 106 | + Test that endpoint returns 404 when admin assigns a team to a non-existent project |
| 107 | + """ |
| 108 | + response = self.client.post( |
| 109 | + f"/api/v2/projects/99/teams/{self.test_team.id}/", |
| 110 | + json={"role": TeamRoles.MAPPER.name}, |
| 111 | + headers={"Authorization": self.test_author_session_token}, |
| 112 | + ) |
| 113 | + response_body = response.get_json() |
| 114 | + self.assertEqual(response.status_code, 404) |
| 115 | + self.assertEqual(response_body["Error"], "No Project Found") |
| 116 | + self.assertEqual(response_body["SubCode"], "NotFound") |
| 117 | + |
| 118 | + def test_assign_team_to_project_by_admin_passes(self): |
| 119 | + """ |
| 120 | + Test that endpoint returns 201 when admin successfully assigns a team to a project |
| 121 | + """ |
| 122 | + response = self.client.post( |
| 123 | + self.single_project_team_url, |
| 124 | + json={"role": TeamRoles.MAPPER.name}, |
| 125 | + headers={"Authorization": self.test_author_session_token}, |
| 126 | + ) |
| 127 | + response_body = response.get_json() |
| 128 | + self.assertEqual(response.status_code, 201) |
| 129 | + self.assertEqual( |
| 130 | + response_body["Success"], |
| 131 | + f"Team {self.test_team.id} assigned to project {self.test_project.id} with role MAPPER", |
| 132 | + ) |
| 133 | + |
| 134 | + # patch |
| 135 | + def test_update_team_role_by_unauthenticated_user_fails(self): |
| 136 | + """ |
| 137 | + Test that endpoint returns 401 when unauthenticated user updates project team role |
| 138 | + """ |
| 139 | + response = self.client.patch( |
| 140 | + self.single_project_team_url, json={"role": TeamRoles.MAPPER.name} |
| 141 | + ) |
| 142 | + response_body = response.get_json() |
| 143 | + self.assertEqual(response.status_code, 401) |
| 144 | + self.assertEqual(response_body["SubCode"], "InvalidToken") |
| 145 | + |
| 146 | + def test_update_team_role_by_non_admin_fails(self): |
| 147 | + """ |
| 148 | + Test that endpoint returns 403 when non admin updates project team role |
| 149 | + """ |
| 150 | + response = self.client.patch( |
| 151 | + self.single_project_team_url, |
| 152 | + json={"role": TeamRoles.MAPPER.name}, |
| 153 | + headers={"Authorization": self.test_user_session_token}, |
| 154 | + ) |
| 155 | + response_body = response.get_json() |
| 156 | + self.assertEqual(response.status_code, 403) |
| 157 | + self.assertEqual(response_body["Error"], "User is not a manager of the project") |
| 158 | + self.assertEqual(response_body["SubCode"], "UserPermissionError") |
| 159 | + |
| 160 | + def test_update_team_role_of_non_existent_project_fails(self): |
| 161 | + """ |
| 162 | + Test that endpoint returns 404 when admin updates non-existent project team role |
| 163 | + """ |
| 164 | + response = self.client.patch( |
| 165 | + self.non_existent_project_team_url, |
| 166 | + json={"role": TeamRoles.MAPPER.name}, |
| 167 | + headers={"Authorization": self.test_author_session_token}, |
| 168 | + ) |
| 169 | + response_body = response.get_json() |
| 170 | + self.assertEqual(response.status_code, 404) |
| 171 | + self.assertEqual(response_body["SubCode"], "NotFound") |
| 172 | + |
| 173 | + def test_update_team_role_by_admin_passes(self): |
| 174 | + """ |
| 175 | + Test that endpoint returns 200 when admin successfully updates project team role |
| 176 | + """ |
| 177 | + assign_team_to_project( |
| 178 | + self.test_project, self.test_team, TeamRoles.MAPPER.value |
| 179 | + ) |
| 180 | + response = self.client.patch( |
| 181 | + self.single_project_team_url, |
| 182 | + json={"role": TeamRoles.VALIDATOR.name}, |
| 183 | + headers={"Authorization": self.test_author_session_token}, |
| 184 | + ) |
| 185 | + response_body = response.get_json() |
| 186 | + self.assertEqual(response.status_code, 200) |
| 187 | + self.assertEqual(response_body["Status"], "Team role updated successfully") |
| 188 | + |
| 189 | + # delete |
| 190 | + def test_delete_project_team_by_unauthenticated_user_fails(self): |
| 191 | + """ |
| 192 | + Test that endpoint returns 401 when unauthenticated user deletes project team |
| 193 | + """ |
| 194 | + response = self.client.delete(self.single_project_team_url) |
| 195 | + response_body = response.get_json() |
| 196 | + self.assertEqual(response.status_code, 401) |
| 197 | + self.assertEqual(response_body["SubCode"], "InvalidToken") |
| 198 | + |
| 199 | + def test_delete_project_team_by_non_admin_fails(self): |
| 200 | + """ |
| 201 | + Test that endpoint returns 403 when non admin deletes project team |
| 202 | + """ |
| 203 | + response = self.client.delete( |
| 204 | + self.single_project_team_url, |
| 205 | + headers={"Authorization": self.test_user_session_token}, |
| 206 | + ) |
| 207 | + response_body = response.get_json() |
| 208 | + self.assertEqual(response.status_code, 403) |
| 209 | + self.assertEqual(response_body["Error"], "User is not a manager of the project") |
| 210 | + self.assertEqual(response_body["SubCode"], "UserPermissionError") |
| 211 | + |
| 212 | + def test_delete_non_existent_project_team_fails(self): |
| 213 | + """ |
| 214 | + Test that endpoint returns 404 when admin deletes non-existent project team |
| 215 | + """ |
| 216 | + response = self.client.delete( |
| 217 | + self.non_existent_project_team_url, |
| 218 | + headers={"Authorization": self.test_author_session_token}, |
| 219 | + ) |
| 220 | + response_body = response.get_json() |
| 221 | + self.assertEqual(response.status_code, 404) |
| 222 | + self.assertEqual(response_body["Error"], "No team found") |
| 223 | + self.assertEqual(response_body["SubCode"], "NotFound") |
| 224 | + |
| 225 | + def test_delete_project_team_by_admin_passes(self): |
| 226 | + """ |
| 227 | + Test that endpoint returns 200 when admin successfully deletes project team |
| 228 | + """ |
| 229 | + assign_team_to_project( |
| 230 | + self.test_project, self.test_team, TeamRoles.MAPPER.value |
| 231 | + ) |
| 232 | + response = self.client.delete( |
| 233 | + self.single_project_team_url, |
| 234 | + headers={"Authorization": self.test_author_session_token}, |
| 235 | + ) |
| 236 | + response_body = response.get_json() |
| 237 | + self.assertEqual(response.status_code, 200) |
| 238 | + self.assertEqual(response_body["Success"], True) |
0 commit comments