-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix url extraction with slugs that may contain user input.
* Add slug to schedule's test factory. * Add tests for most auth dependencies.
- Loading branch information
1 parent
8c32443
commit 641c603
Showing
4 changed files
with
81 additions
and
4 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
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 |
---|---|---|
|
@@ -4,9 +4,11 @@ | |
import pytest | ||
from freezegun import freeze_time | ||
|
||
from appointment.controller.auth import signed_url_by_subscriber | ||
from appointment.database import repo | ||
from appointment.dependencies.auth import get_user_from_token | ||
from appointment.exceptions.validation import InvalidTokenException | ||
from appointment.dependencies.auth import get_user_from_token, get_subscriber, get_admin_subscriber, \ | ||
get_subscriber_from_schedule_or_signed_url | ||
from appointment.exceptions.validation import InvalidTokenException, InvalidPermissionLevelException | ||
from appointment.routes.auth import create_access_token | ||
|
||
|
||
|
@@ -61,3 +63,77 @@ def test_get_user_from_token(self, with_db, with_l10n, make_pro_subscriber): | |
# Internally raises ExpiredSignatureError, but we catch it and send a HTTPException instead. | ||
with pytest.raises(InvalidTokenException): | ||
get_user_from_token(db, access_token) | ||
|
||
def test_get_subscriber(self, with_db, with_l10n, make_pro_subscriber): | ||
subscriber = make_pro_subscriber() | ||
access_token = create_access_token(data={"sub": f"uid-{subscriber.id}"}) | ||
|
||
with with_db() as db: | ||
retrieved_subscriber = get_subscriber(access_token, db) | ||
|
||
assert retrieved_subscriber.id == subscriber.id | ||
assert retrieved_subscriber.email == subscriber.email | ||
|
||
def test_get_subscriber_with_invalid_token(self, with_db, with_l10n, make_pro_subscriber): | ||
subscriber = make_pro_subscriber() | ||
|
||
with with_db() as db: | ||
with pytest.raises(InvalidTokenException): | ||
# Use a nonsense value, like the subscriber id! | ||
get_subscriber(subscriber.id, db) | ||
|
||
def test_get_admin_subscriber(self, with_db, with_l10n, make_pro_subscriber): | ||
subscriber = make_pro_subscriber() | ||
access_token = create_access_token(data={"sub": f"uid-{subscriber.id}"}) | ||
|
||
os.environ['APP_ADMIN_ALLOW_LIST'] = subscriber.email | ||
|
||
with with_db() as db: | ||
retrieved_subscriber = get_admin_subscriber(get_subscriber(access_token, db)) | ||
|
||
assert retrieved_subscriber.id == subscriber.id | ||
assert retrieved_subscriber.email == subscriber.email | ||
|
||
def test_get_admin_subscriber_fails_with_no_allow_list(self, with_db, with_l10n, make_pro_subscriber): | ||
subscriber = make_pro_subscriber() | ||
access_token = create_access_token(data={"sub": f"uid-{subscriber.id}"}) | ||
|
||
os.environ['APP_ADMIN_ALLOW_LIST'] = '' | ||
|
||
with with_db() as db: | ||
with pytest.raises(InvalidPermissionLevelException): | ||
get_admin_subscriber(get_subscriber(access_token, db)) | ||
|
||
def test_get_admin_subscriber_fails_not_in_allow_list(self, with_db, with_l10n, make_pro_subscriber): | ||
subscriber = make_pro_subscriber(email='[email protected]') | ||
access_token = create_access_token(data={"sub": f"uid-{subscriber.id}"}) | ||
|
||
os.environ['APP_ADMIN_ALLOW_LIST'] = '@notexample.org' | ||
|
||
with with_db() as db: | ||
with pytest.raises(InvalidPermissionLevelException): | ||
get_admin_subscriber(get_subscriber(access_token, db)) | ||
|
||
def test_get_subscriber_from_schedule_or_signed_url_with_signed_url(self, with_db, with_l10n, make_pro_subscriber): | ||
subscriber = make_pro_subscriber() | ||
|
||
with with_db() as db: | ||
signed_url = signed_url_by_subscriber(subscriber) | ||
retrieved_subscriber = get_subscriber_from_schedule_or_signed_url(signed_url, db) | ||
|
||
assert retrieved_subscriber.id == subscriber.id | ||
assert retrieved_subscriber.email == subscriber.email | ||
|
||
def test_get_subscriber_from_schedule_or_signed_url_with_schedule_slug(self, with_db, with_l10n, | ||
make_pro_subscriber, make_schedule, | ||
make_caldav_calendar): | ||
subscriber = make_pro_subscriber() | ||
calendar = make_caldav_calendar(subscriber_id=subscriber.id) | ||
schedule = make_schedule(calendar_id=calendar.id) | ||
|
||
with with_db() as db: | ||
url = f"https://apmt.day/{subscriber.username}/{schedule.slug}/" | ||
retrieved_subscriber = get_subscriber_from_schedule_or_signed_url(url, db) | ||
|
||
assert retrieved_subscriber.id == subscriber.id | ||
assert retrieved_subscriber.email == subscriber.email |