test_apple_touch_icon.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import os
  2. import pytest
  3. from django.core.files.uploadedfile import SimpleUploadedFile
  4. from ...models import Icon
  5. @pytest.fixture
  6. def apple_touch_icon(db, image):
  7. return Icon.objects.create(
  8. type=Icon.TYPE_APPLE_TOUCH_ICON,
  9. image=SimpleUploadedFile("image.png", image, "image/png"),
  10. )
  11. def test_new_touch_icon_can_be_set(admin_client, admin_link, image):
  12. admin_client.post(
  13. admin_link,
  14. {"apple_touch_icon": SimpleUploadedFile("image.png", image, "image/png")},
  15. )
  16. Icon.objects.get(type=Icon.TYPE_APPLE_TOUCH_ICON)
  17. def test_setting_new_touch_icon_removes_old_one(
  18. admin_client, admin_link, image, apple_touch_icon
  19. ):
  20. admin_client.post(
  21. admin_link,
  22. {"apple_touch_icon": SimpleUploadedFile("image.png", image, "image/png")},
  23. )
  24. with pytest.raises(Icon.DoesNotExist):
  25. apple_touch_icon.refresh_from_db()
  26. def test_setting_new_touch_icon_removes_old_one_image_file(
  27. admin_client, admin_link, image, apple_touch_icon
  28. ):
  29. admin_client.post(
  30. admin_link,
  31. {"apple_touch_icon": SimpleUploadedFile("image.png", image, "image/png")},
  32. )
  33. assert not os.path.exists(apple_touch_icon.image.path)
  34. def test_submitting_form_without_new_icon_does_not_delete_old_one(
  35. admin_client, admin_link, apple_touch_icon
  36. ):
  37. admin_client.post(admin_link, {})
  38. apple_touch_icon.refresh_from_db()
  39. def test_icon_can_be_deleted_without_setting_new_one(
  40. admin_client, admin_link, apple_touch_icon
  41. ):
  42. admin_client.post(admin_link, {"apple_touch_icon_delete": "1"})
  43. with pytest.raises(Icon.DoesNotExist):
  44. apple_touch_icon.refresh_from_db()
  45. def test_deleting_icon_also_deletes_its_image_file(
  46. admin_client, admin_link, apple_touch_icon
  47. ):
  48. admin_client.post(admin_link, {"apple_touch_icon_delete": "1"})
  49. assert not os.path.exists(apple_touch_icon.image.path)
  50. def test_uploading_invalid_icon_does_not_remove_current_icon(
  51. admin_client, admin_link, apple_touch_icon, image_small
  52. ):
  53. admin_client.post(
  54. admin_link,
  55. {"apple_touch_icon": SimpleUploadedFile("image.png", image_small, "image/png")},
  56. )
  57. apple_touch_icon.refresh_from_db()
  58. def test_icon_is_not_set_because_it_was_not_square(
  59. admin_client, admin_link, image_non_square
  60. ):
  61. admin_client.post(
  62. admin_link,
  63. {
  64. "apple_touch_icon": SimpleUploadedFile(
  65. "image.png", image_non_square, "image/png"
  66. )
  67. },
  68. )
  69. assert not Icon.objects.exists()
  70. def test_icon_is_not_set_because_it_was_too_small(
  71. admin_client, admin_link, image_small
  72. ):
  73. admin_client.post(
  74. admin_link,
  75. {"apple_touch_icon": SimpleUploadedFile("image.png", image_small, "image/png")},
  76. )
  77. assert not Icon.objects.exists()