Files
notifications-admin/tests/notifications_utils/test_url_safe_tokens.py
Carlo Costino 57e9fc9ba1 Localize notification_utils to the Admin!
This changeset copies everything in notifications_utils to turn into a local project module.  It includes dependency changes to account for this adjustment and the notifications_utils tests.  It also fixes all of the import statements.

Signed-off-by: Carlo Costino <carlo.costino@gsa.gov>
2024-04-05 12:51:31 -04:00

37 lines
1.1 KiB
Python

import urllib
from itsdangerous import BadSignature, SignatureExpired
from pytest import fail
from notifications_utils.url_safe_token import check_token, generate_token
def test_should_return_payload_from_signed_token():
payload = "email@something.com"
token = generate_token(payload, "secret-key", "dangerous-salt")
token = urllib.parse.unquote(token)
assert payload == check_token(token, "secret-key", "dangerous-salt", 30)
def test_should_throw_exception_when_token_is_tampered_with():
import uuid
token = generate_token(str(uuid.uuid4()), "secret-key", "dangerous-salt")
try:
check_token(token + "qerqwer", "secret-key", "dangerous-salt", 30)
fail()
except BadSignature:
pass
def test_return_none_when_token_is_expired():
max_age = -1000
payload = "some_payload"
token = generate_token(payload, "secret-key", "dangerous-salt")
token = urllib.parse.unquote(token)
try:
assert check_token(token, "secret-key", "dangerous-salt", max_age) is None
fail("Expected a SignatureExpired exception")
except SignatureExpired:
pass