| 1 |
from django.core.exceptions import ValidationError |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from allauth.idp.oidc.internal.resources import is_resources_subset, validate_resources |
| 6 |
|
| 7 |
|
| 8 |
@pytest.mark.parametrize( |
| 9 |
"requested,granted,expected", |
| 10 |
[ |
| 11 |
# No granted resources means anything is allowed. |
| 12 |
(["https://api.example.com/a"], [], True), |
| 13 |
# No requested resources is always fine. |
| 14 |
([], ["https://api.example.com"], True), |
| 15 |
# Exact match. |
| 16 |
(["https://api.example.com/a"], ["https://api.example.com/a"], True), |
| 17 |
# Sub-path is allowed. |
| 18 |
(["https://api.example.com/a/b"], ["https://api.example.com/a"], True), |
| 19 |
# Different path is not allowed. |
| 20 |
(["https://api.example.com/b"], ["https://api.example.com/a"], False), |
| 21 |
# Prefix match must respect path boundaries — /ninja does not grant /ninja-admin. |
| 22 |
( |
| 23 |
["https://api.example.com/ninja-admin"], |
| 24 |
["https://api.example.com/ninja"], |
| 25 |
False, |
| 26 |
), |
| 27 |
# But /ninja does grant /ninja/sub. |
| 28 |
( |
| 29 |
["https://api.example.com/ninja/sub"], |
| 30 |
["https://api.example.com/ninja"], |
| 31 |
True, |
| 32 |
), |
| 33 |
# Different scheme. |
| 34 |
(["http://api.example.com/a"], ["https://api.example.com/a"], False), |
| 35 |
# Different host. |
| 36 |
(["https://other.example.com/a"], ["https://api.example.com/a"], False), |
| 37 |
], |
| 38 |
) |
| 39 |
def test_is_resources_subset(requested, granted, expected): |
| 40 |
assert is_resources_subset(requested, granted) == expected |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.parametrize( |
| 44 |
"resource", |
| 45 |
[ |
| 46 |
# Valid absolute URI. |
| 47 |
"https://api.example.com/a", |
| 48 |
# Valid with no path. |
| 49 |
"https://api.example.com", |
| 50 |
], |
| 51 |
) |
| 52 |
def test_validate_resources(resource): |
| 53 |
validate_resources([resource]) |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.parametrize( |
| 57 |
"resource", |
| 58 |
[ |
| 59 |
# Missing scheme — not absolute. |
| 60 |
"api.example.com/a", |
| 61 |
# Missing netloc — not absolute. |
| 62 |
"/a/b/c", |
| 63 |
# Fragment not allowed. |
| 64 |
"https://api.example.com/a#frag", |
| 65 |
# Directory traversal. |
| 66 |
"https://api.example.com/a/../b", |
| 67 |
# Non-normalized path with double slash. |
| 68 |
"https://api.example.com//a", |
| 69 |
# Non-normalized path with dot segment. |
| 70 |
"https://api.example.com/./a", |
| 71 |
# Trailing dot-dot. |
| 72 |
"https://api.example.com/a/..", |
| 73 |
], |
| 74 |
) |
| 75 |
def test_validate_resources_invalid(resource): |
| 76 |
with pytest.raises(ValidationError): |
| 77 |
validate_resources([resource]) |