forked from torchbox/wagtaildraftsharing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from mozmeao/refactor-link-generation-to-be-a-p…
…ython-api Move the link generation out of the view, to make it more reusable from Python
- Loading branch information
Showing
6 changed files
with
116 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,71 @@ | ||
import datetime | ||
from textwrap import dedent | ||
from unittest.mock import patch | ||
|
||
import wagtail | ||
from django.contrib.auth.models import User | ||
from django.test import TestCase | ||
from django.utils.timezone import is_aware | ||
from freezegun import freeze_time | ||
from wagtail_factories import PageFactory | ||
|
||
from ..models import WagtaildraftsharingLink | ||
import wagtaildraftsharing.models | ||
from wagtaildraftsharing.models import WagtaildraftsharingLink | ||
|
||
FROZEN_TIME_ISOFORMATTED = "2024-01-02 12:34:56.123456+00:00" | ||
|
||
|
||
class TestWagtaildraftsharingLinkManager(TestCase): | ||
def setUp(self): | ||
self.test_user = User.objects.create( | ||
username="test", email="[email protected]" | ||
) | ||
|
||
def create_revision(self): | ||
page = PageFactory() | ||
|
||
# create the first revision | ||
page.save_revision().publish() | ||
|
||
old_title = page.title | ||
new_title = f"New {old_title}" | ||
page.title = new_title | ||
|
||
# create the second revision with a new title | ||
page.save_revision().publish() | ||
|
||
page.refresh_from_db() | ||
earliest_revision = page.revisions.earliest("created_at") | ||
return earliest_revision | ||
|
||
@freeze_time(FROZEN_TIME_ISOFORMATTED) | ||
def test_create_sharing_link_view__max_age_from_settings(self): | ||
frozen_time = datetime.datetime.fromisoformat(FROZEN_TIME_ISOFORMATTED) | ||
|
||
# Ensure we've got a level playing field: that the time is TZ-aware | ||
if not is_aware(frozen_time): | ||
self.fail("frozen_time was a naive datetime but it should not be") | ||
|
||
max_ages_and_expected_expiries = ( | ||
(300, frozen_time + datetime.timedelta(seconds=300)), | ||
(1250000, frozen_time + datetime.timedelta(seconds=1250000)), | ||
(-1, None), | ||
) | ||
|
||
for max_age, expected_expiry in max_ages_and_expected_expiries: | ||
with self.subTest(max_age=max_age, expected_expiry=expected_expiry): | ||
with patch.object(wagtaildraftsharing.models, "max_age", max_age): | ||
revision = self.create_revision() | ||
|
||
link = WagtaildraftsharingLink.objects.get_or_create_for_revision( | ||
revision=revision, | ||
user=self.test_user, | ||
) | ||
|
||
assert link.active_until == expected_expiry, ( | ||
link.active_until, | ||
expected_expiry, | ||
) | ||
|
||
|
||
class TestWagtaildraftsharingLinkModel(TestCase): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from datetime import timezone | ||
|
||
from django.utils.timezone import is_aware, make_aware | ||
from django.utils.timezone import now as timezone_now | ||
|
||
|
||
def tz_aware_utc_now(): | ||
now = timezone_now() | ||
# Depending on your version of Django and/or setting.TZ_NOW, timezone_now() | ||
# may not actually be TZ aware, but we always want it to be for these links | ||
if not is_aware(now): | ||
now = make_aware(now, timezone.utc) | ||
return now |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters