Skip to content

Commit

Permalink
Add test for magic link generator
Browse files Browse the repository at this point in the history
  • Loading branch information
GregKaleka committed Jan 14, 2025
1 parent cd15c85 commit 69717f6
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions news/tests/test_notifications.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from datetime import date

import pytest
from django.conf import settings
from django.core import mail
from django.utils.html import escape
from django.urls import reverse
from itsdangerous import URLSafeTimedSerializer

from ..models import NEWS_MODELS
from ..notifications import (
send_email_news_approved,
send_email_news_needs_moderation,
send_email_news_posted,
generate_magic_approval_link,
NEWS_APPROVAL_SALT,
)
from users.models import Preferences

Expand Down Expand Up @@ -118,6 +122,28 @@ def test_send_email_news_needs_moderation(
}


def test_generate_magic_approval_link(make_entry, make_user):
entry = make_entry()
# entry = make_entry(NEWS_MODELS[0])
moderator = make_user(groups={"moderator": ["news.*"]}, email="[email protected]")
url = generate_magic_approval_link(entry.slug, moderator.id)

dummy_token = "dummy-token"
expected_base_url = (
reverse("news-magic-approve", kwargs={"token": dummy_token})
.replace(dummy_token, "")
.rstrip("/")
)
assert url.startswith(expected_base_url)

token = url.split(expected_base_url)[-1].strip("/")
serializer = URLSafeTimedSerializer(settings.SECRET_KEY)
data = serializer.loads(token, salt=NEWS_APPROVAL_SALT)

assert data["entry_slug"] == entry.slug
assert data["moderator_id"] == moderator.id


@pytest.mark.parametrize("model_class", NEWS_MODELS)
def test_send_email_news_needs_moderation_no_moderator_match(
rf, tp, make_entry, model_class, moderator_user
Expand Down

0 comments on commit 69717f6

Please sign in to comment.