2017-03-16 21:05:26 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
from auth.scopes import (
|
|
|
|
scopes_from_scope_string, validate_scope_string, ALL_SCOPES, is_subset_string)
|
2017-03-16 21:05:26 +00:00
|
|
|
|
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'scopes_string, expected',
|
|
|
|
[
|
|
|
|
# Valid single scopes.
|
|
|
|
('repo:read', ['repo:read']),
|
|
|
|
('repo:admin', ['repo:admin']),
|
2017-03-16 21:05:26 +00:00
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
# Invalid scopes.
|
|
|
|
('not:valid', []),
|
|
|
|
('repo:admins', []),
|
2017-03-16 21:05:26 +00:00
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
# Valid scope strings.
|
|
|
|
('repo:read repo:admin', ['repo:read', 'repo:admin']),
|
|
|
|
('repo:read,repo:admin', ['repo:read', 'repo:admin']),
|
|
|
|
('repo:read,repo:admin repo:write', ['repo:read', 'repo:admin', 'repo:write']),
|
2017-03-16 21:05:26 +00:00
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
# Partially invalid scopes.
|
|
|
|
('repo:read,not:valid', []),
|
|
|
|
('repo:read repo:admins', []),
|
2017-03-16 21:05:26 +00:00
|
|
|
|
2017-07-05 19:45:07 +00:00
|
|
|
# Invalid scope strings.
|
|
|
|
('repo:read|repo:admin', []),
|
|
|
|
|
|
|
|
# Mixture of delimiters.
|
|
|
|
('repo:read, repo:admin', []),])
|
2017-03-16 21:05:26 +00:00
|
|
|
def test_parsing(scopes_string, expected):
|
|
|
|
expected_scope_set = {ALL_SCOPES[scope_name] for scope_name in expected}
|
|
|
|
parsed_scope_set = scopes_from_scope_string(scopes_string)
|
|
|
|
assert parsed_scope_set == expected_scope_set
|
|
|
|
assert validate_scope_string(scopes_string) == bool(expected)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('superset, subset, result', [
|
|
|
|
('repo:read', 'repo:read', True),
|
|
|
|
('repo:read repo:admin', 'repo:read', True),
|
|
|
|
('repo:read,repo:admin', 'repo:read', True),
|
|
|
|
('repo:read,repo:admin', 'repo:admin', True),
|
|
|
|
('repo:read,repo:admin', 'repo:admin repo:read', True),
|
|
|
|
('', 'repo:read', False),
|
|
|
|
('unknown:tag', 'repo:read', False),
|
|
|
|
('repo:read unknown:tag', 'repo:read', False),
|
2017-07-05 19:45:07 +00:00
|
|
|
('repo:read,unknown:tag', 'repo:read', False),])
|
2017-03-16 21:05:26 +00:00
|
|
|
def test_subset_string(superset, subset, result):
|
|
|
|
assert is_subset_string(superset, subset) == result
|