Skip to content

Commit

Permalink
fix and test: clear "www." cache too
Browse files Browse the repository at this point in the history
  • Loading branch information
artoonie committed Jul 27, 2024
1 parent dfc88af commit afbd51b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions common/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def purge_paths_cache(cls, paths):
apiUrl = f"https://api.cloudflare.com/client/v4/zones/{zoneId}/purge_cache"

# Absolute URLs
domain = Site.objects.get_current().domain
rcvisUrls = [f'https://{domain}{path}' for path in paths]
data = {'files': rcvisUrls}
data = {'files': get_purge_paths_for(paths)}

# Send it off
response = requests.post(
Expand All @@ -76,3 +74,11 @@ def purge_paths_cache(cls, paths):
logger.info("Cleared cloudflare cache for %s: %s", info, response.json())
else:
logger.error("Received bad response from cloudflare for %s: %s", info, response.json())

@classmethod
def get_purge_paths_for(cls, paths):
domain = Site.objects.get_current().domain
rcvisUrls = [f'https://{domain}{path}' for path in paths]
if not domain.startswith("www"):
rcvisUrls.extend([f'https://www.{domain}{path}' for path in paths])
return rcvisUrls
11 changes: 11 additions & 0 deletions visualizer/tests/testRestApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
from mock import patch

from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from rest_framework_tracking.models import APIRequestLog

from common.cloudflare import CloudflareAPI
from common.testUtils import TestHelpers
from visualizer.tests import filenames

Expand Down Expand Up @@ -424,3 +426,12 @@ def test_update_clears_cloudflare_cache(self, purgeMock):

# Ensure purge is called once edited
purgeMock.assert_called_once()
purgeMock.assert_called_with('one-round')

def test_purge_paths(self):
""" Test that cloudflare cache clears both www and non-www addresses """
paths = CloudflareAPI.get_purge_paths_for('/v/one-round')
firstPath = paths[0]
firstPathWithWWW = firstPath.replace("://", "://www.")
self.assertNotIn('www', Site.objects.get_current().domain)
self.assertIn(firstPathWithWWW, paths)

0 comments on commit afbd51b

Please sign in to comment.