|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import json |
| 6 | +import logging |
6 | 7 | import random |
7 | 8 | import re |
8 | 9 |
|
9 | 10 | import pytest |
10 | 11 | import responses |
| 12 | +from rest_framework import status |
11 | 13 | from rest_framework.test import APIClient |
12 | 14 |
|
13 | | -from core import factories, models |
| 15 | +from core import enums, factories, models |
| 16 | +from core.tests.fixtures import matrix |
14 | 17 |
|
15 | 18 | pytestmark = pytest.mark.django_db |
16 | 19 |
|
@@ -171,14 +174,17 @@ def test_api_team_accesses_create_authenticated_owner(): |
171 | 174 | } |
172 | 175 |
|
173 | 176 |
|
174 | | -def test_api_team_accesses_create_webhook(): |
| 177 | +def test_api_team_accesses_create__with_scim_webhook(): |
175 | 178 | """ |
176 | | - When the team has a webhook, creating a team access should fire a call. |
| 179 | + If a team has a SCIM webhook, creating a team access should fire a call |
| 180 | + with the expected payload. |
177 | 181 | """ |
178 | 182 | user, other_user = factories.UserFactory.create_batch(2) |
179 | 183 |
|
180 | 184 | team = factories.TeamFactory(users=[(user, "owner")]) |
181 | | - webhook = factories.TeamWebhookFactory(team=team) |
| 185 | + webhook = factories.TeamWebhookFactory( |
| 186 | + team=team, protocol=enums.WebhookProtocolChoices.SCIM |
| 187 | + ) |
182 | 188 |
|
183 | 189 | role = random.choice([role[0] for role in models.RoleChoices.choices]) |
184 | 190 |
|
@@ -226,3 +232,139 @@ def test_api_team_accesses_create_webhook(): |
226 | 232 | } |
227 | 233 | ], |
228 | 234 | } |
| 235 | + |
| 236 | + assert models.TeamAccess.objects.filter(user=other_user, team=team).exists() |
| 237 | + |
| 238 | + |
| 239 | +def test_api_team_accesses_create__multiple_webhooks_success(caplog): |
| 240 | + """ |
| 241 | + When the team has multiple webhooks, creating a team access should fire all the expected calls. |
| 242 | + If all responses are positive, proceeds to add the user to the team. |
| 243 | + """ |
| 244 | + caplog.set_level(logging.INFO) |
| 245 | + |
| 246 | + user, other_user = factories.UserFactory.create_batch(2) |
| 247 | + |
| 248 | + team = factories.TeamFactory(users=[(user, "owner")]) |
| 249 | + webhook_scim = factories.TeamWebhookFactory( |
| 250 | + team=team, protocol=enums.WebhookProtocolChoices.SCIM, secret="wesh" |
| 251 | + ) |
| 252 | + webhook_matrix = factories.TeamWebhookFactory( |
| 253 | + team=team, |
| 254 | + url="https://www.webhookserver.fr/#/room/room_id:home_server/", |
| 255 | + protocol=enums.WebhookProtocolChoices.MATRIX, |
| 256 | + secret="yo", |
| 257 | + ) |
| 258 | + |
| 259 | + role = random.choice([role[0] for role in models.RoleChoices.choices]) |
| 260 | + |
| 261 | + client = APIClient() |
| 262 | + client.force_login(user) |
| 263 | + |
| 264 | + with responses.RequestsMock() as rsps: |
| 265 | + # Ensure successful response by scim provider using "responses": |
| 266 | + rsps.add( |
| 267 | + rsps.PATCH, |
| 268 | + re.compile(r".*/Groups/.*"), |
| 269 | + body="{}", |
| 270 | + status=200, |
| 271 | + content_type="application/json", |
| 272 | + ) |
| 273 | + rsps.add( |
| 274 | + rsps.POST, |
| 275 | + re.compile(r".*/join"), |
| 276 | + body=str(matrix.mock_join_room_successful), |
| 277 | + status=status.HTTP_200_OK, |
| 278 | + content_type="application/json", |
| 279 | + ) |
| 280 | + rsps.add( |
| 281 | + rsps.POST, |
| 282 | + re.compile(r".*/invite"), |
| 283 | + body=str(matrix.mock_invite_successful()["message"]), |
| 284 | + status=matrix.mock_invite_successful()["status_code"], |
| 285 | + content_type="application/json", |
| 286 | + ) |
| 287 | + |
| 288 | + response = client.post( |
| 289 | + f"/api/v1.0/teams/{team.id!s}/accesses/", |
| 290 | + { |
| 291 | + "user": str(other_user.id), |
| 292 | + "role": role, |
| 293 | + }, |
| 294 | + format="json", |
| 295 | + ) |
| 296 | + assert response.status_code == 201 |
| 297 | + |
| 298 | + # Logger |
| 299 | + log_messages = [msg.message for msg in caplog.records] |
| 300 | + for webhook in [webhook_scim, webhook_matrix]: |
| 301 | + assert ( |
| 302 | + f"add_user_to_group synchronization succeeded with {webhook.url}" |
| 303 | + in log_messages |
| 304 | + ) |
| 305 | + |
| 306 | + # Status |
| 307 | + for webhook in [webhook_scim, webhook_matrix]: |
| 308 | + webhook.refresh_from_db() |
| 309 | + assert webhook.status == "success" |
| 310 | + assert models.TeamAccess.objects.filter(user=other_user, team=team).exists() |
| 311 | + |
| 312 | + |
| 313 | +@responses.activate |
| 314 | +def test_api_team_accesses_create__multiple_webhooks_failure(caplog): |
| 315 | + """When a webhook fails, user should still be added to the team.""" |
| 316 | + caplog.set_level(logging.INFO) |
| 317 | + |
| 318 | + user, other_user = factories.UserFactory.create_batch(2) |
| 319 | + |
| 320 | + team = factories.TeamFactory(users=[(user, "owner")]) |
| 321 | + webhook_scim = factories.TeamWebhookFactory( |
| 322 | + team=team, protocol=enums.WebhookProtocolChoices.SCIM, secret="wesh" |
| 323 | + ) |
| 324 | + webhook_matrix = factories.TeamWebhookFactory( |
| 325 | + team=team, |
| 326 | + url="https://www.webhookserver.fr/#/room/room_id:home_server/", |
| 327 | + protocol=enums.WebhookProtocolChoices.MATRIX, |
| 328 | + secret="secret", |
| 329 | + ) |
| 330 | + |
| 331 | + role = random.choice([role[0] for role in models.RoleChoices.choices]) |
| 332 | + client = APIClient() |
| 333 | + client.force_login(user) |
| 334 | + |
| 335 | + responses.patch( |
| 336 | + re.compile(r".*/Groups/.*"), |
| 337 | + body="{}", |
| 338 | + status=200, |
| 339 | + ) |
| 340 | + responses.post( |
| 341 | + re.compile(r".*/join"), |
| 342 | + body=str(matrix.mock_join_room_forbidden()["message"]), |
| 343 | + status=str(matrix.mock_join_room_forbidden()["status_code"]), |
| 344 | + ) |
| 345 | + |
| 346 | + response = client.post( |
| 347 | + f"/api/v1.0/teams/{team.id!s}/accesses/", |
| 348 | + { |
| 349 | + "user": str(other_user.id), |
| 350 | + "role": role, |
| 351 | + }, |
| 352 | + format="json", |
| 353 | + ) |
| 354 | + assert response.status_code == status.HTTP_201_CREATED |
| 355 | + |
| 356 | + # Logger |
| 357 | + log_messages = [msg.message for msg in caplog.records] |
| 358 | + assert ( |
| 359 | + f"add_user_to_group synchronization succeeded with {webhook_scim.url}" |
| 360 | + in log_messages |
| 361 | + ) |
| 362 | + assert ( |
| 363 | + f"add_user_to_group synchronization failed with {webhook_matrix.url}" |
| 364 | + in log_messages |
| 365 | + ) |
| 366 | + |
| 367 | + # Status |
| 368 | + webhook_scim.status = "success" |
| 369 | + webhook_matrix.status = "failure" |
| 370 | + assert models.TeamAccess.objects.filter(user=other_user, team=team).exists() |
0 commit comments