|
1 | 1 | import copy |
2 | 2 | from unittest.mock import Mock |
3 | 3 |
|
| 4 | +from fastapi.encoders import jsonable_encoder |
4 | 5 | from starlette.testclient import TestClient |
5 | 6 |
|
6 | 7 | from database.model.agent.contact import Contact |
@@ -351,3 +352,82 @@ def test_organisation_delete_image( |
351 | 352 | ) |
352 | 353 | assert second_delete_response.status_code == HTTPStatus.NOT_FOUND |
353 | 354 | assert "No image with the name" in second_delete_response.json()["detail"] |
| 355 | + |
| 356 | + |
| 357 | +def test_organisation_put_without_media_keeps_media( |
| 358 | + client: TestClient, |
| 359 | + organisation: Organisation, |
| 360 | +): |
| 361 | + organisation.media = [] |
| 362 | + identifier = register_asset(organisation) |
| 363 | + |
| 364 | + fake_image = io.BytesIO(b"\x89PNG\r\n\x1a\n...") # fake PNG bytes |
| 365 | + fake_image.name = "logo.png" |
| 366 | + |
| 367 | + with logged_in_user(): |
| 368 | + response = client.post( |
| 369 | + f"/organisations/{identifier}/image", |
| 370 | + params={"name": "logo"}, |
| 371 | + files={"file": ("logo.png", fake_image, "image/png")}, |
| 372 | + headers={"Authorization": "Fake token"}, |
| 373 | + ) |
| 374 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 375 | + |
| 376 | + organisation.name = "new name" |
| 377 | + response = client.put( |
| 378 | + f"/organisations/{identifier}", |
| 379 | + json=jsonable_encoder(organisation.dict()), |
| 380 | + headers={"Authorization": "Fake token"}, |
| 381 | + ) |
| 382 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 383 | + response = client.get( |
| 384 | + f"/organisations/{identifier}", |
| 385 | + headers={"Authorization": "Fake token"}, |
| 386 | + ) |
| 387 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 388 | + assert response.json()["name"] == "new name", response.json() |
| 389 | + assert response.json()["media"], response.json() |
| 390 | + |
| 391 | + |
| 392 | +def test_organisation_put_with_media_keeps_media_if_no_new_binary( |
| 393 | + client: TestClient, |
| 394 | + organisation: Organisation, |
| 395 | +): |
| 396 | + organisation.media = [] |
| 397 | + identifier = register_asset(organisation) |
| 398 | + |
| 399 | + fake_image = io.BytesIO(b"\x89PNG\r\n\x1a\n...") # fake PNG bytes |
| 400 | + fake_image.name = "logo.png" |
| 401 | + |
| 402 | + with logged_in_user(): |
| 403 | + response = client.post( |
| 404 | + f"/organisations/{identifier}/image", |
| 405 | + params={"name": "logo"}, |
| 406 | + files={"file": ("logo.png", fake_image, "image/png")}, |
| 407 | + headers={"Authorization": "Fake token"}, |
| 408 | + ) |
| 409 | + assert response.status_code == HTTPStatus.OK, response.json() |
| 410 | + |
| 411 | + response = client.get( |
| 412 | + f"/organisations/{identifier}?get_image=true", |
| 413 | + headers={"Authorization": "Fake token"}, |
| 414 | + ) |
| 415 | + org = response.json() |
| 416 | + del org["aiod_entry"] |
| 417 | + org["media"].append( |
| 418 | + {"name": "foo", "binary_blob": "bar="}, |
| 419 | + ) |
| 420 | + response = client.put( |
| 421 | + f"/organisations/{identifier}", |
| 422 | + json=org, |
| 423 | + headers={"Authorization": "Fake token"}, |
| 424 | + ) |
| 425 | + assert response.status_code == HTTPStatus.BAD_REQUEST, "No new binary may be added through a PUT request" |
| 426 | + |
| 427 | + org["media"].pop() |
| 428 | + response = client.put( |
| 429 | + f"/organisations/{identifier}", |
| 430 | + json=org, |
| 431 | + headers={"Authorization": "Fake token"}, |
| 432 | + ) |
| 433 | + assert response.status_code == HTTPStatus.OK, response.json() |
0 commit comments