|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
| 5 | +from website_content.api import purge_content_on_save |
| 6 | +from website_content.constants import WebsiteContentType |
| 7 | +from website_content.factories import WebsiteContentFactory |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.django_db |
| 11 | +@pytest.mark.parametrize( |
| 12 | + ("slug", "is_published", "expect_purge"), |
| 13 | + [ |
| 14 | + ("test-article", True, True), |
| 15 | + ("test-article", False, False), |
| 16 | + ("", True, False), |
| 17 | + ("", False, False), |
| 18 | + ], |
| 19 | +) |
| 20 | +def test_purge_content_on_save(mocker, slug, is_published, expect_purge): |
| 21 | + """ |
| 22 | + CDN purge should only fire when the content is published AND has a slug. |
| 23 | + Asserts on call_fastly_purge_api (the lowest plumbing before the HTTP call) |
| 24 | + so the test is independent of FASTLY_API_KEY being set in the test env. |
| 25 | + @single_task uses a Redis lock so fastly_purge_website_content_list.delay |
| 26 | + is mocked to avoid that dependency in unit tests. |
| 27 | + """ |
| 28 | + mock_purge_api = mocker.patch("website_content.tasks.call_fastly_purge_api") |
| 29 | + mock_purge_api.return_value = {"status": "ok", "id": "abc"} |
| 30 | + mocker.patch("website_content.tasks.fastly_purge_website_content_list.delay") |
| 31 | + |
| 32 | + content = WebsiteContentFactory.build( |
| 33 | + is_published=is_published, |
| 34 | + slug=slug or None, |
| 35 | + content_type=WebsiteContentType.news.name, |
| 36 | + ) |
| 37 | + |
| 38 | + purge_content_on_save(content) |
| 39 | + |
| 40 | + if expect_purge: |
| 41 | + assert mock_purge_api.called |
| 42 | + else: |
| 43 | + mock_purge_api.assert_not_called() |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.django_db |
| 47 | +@pytest.mark.parametrize( |
| 48 | + ("content_type", "expected_listing"), |
| 49 | + [ |
| 50 | + (WebsiteContentType.news.name, "/news"), |
| 51 | + (WebsiteContentType.article.name, "/articles"), |
| 52 | + ], |
| 53 | +) |
| 54 | +def test_purge_content_on_save_listing_url(mocker, content_type, expected_listing): |
| 55 | + """ |
| 56 | + The listing-page purge task should be enqueued with the URL matching |
| 57 | + the content's content_type. |
| 58 | + """ |
| 59 | + mocker.patch("website_content.tasks.call_fastly_purge_api").return_value = { |
| 60 | + "status": "ok", |
| 61 | + "id": "abc", |
| 62 | + } |
| 63 | + mock_purge_list = mocker.patch( |
| 64 | + "website_content.tasks.fastly_purge_website_content_list.delay" |
| 65 | + ) |
| 66 | + |
| 67 | + content = WebsiteContentFactory.build( |
| 68 | + is_published=True, |
| 69 | + slug="some-slug", |
| 70 | + content_type=content_type, |
| 71 | + ) |
| 72 | + mocker.patch.object( |
| 73 | + type(content), "get_url", return_value=f"/{content_type}/some-slug" |
| 74 | + ) |
| 75 | + |
| 76 | + purge_content_on_save(content) |
| 77 | + |
| 78 | + mock_purge_list.assert_called_once_with(expected_listing) |
| 79 | + |
5 | 80 |
|
6 | 81 | @pytest.mark.django_db |
7 | 82 | def test_content_published_actions_triggers_hook(mocker, user): |
|
0 commit comments