-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from colibris-framework/tests-permissions-and-…
…more Tests for permissions and more
- Loading branch information
Showing
19 changed files
with
378 additions
and
70 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
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
|
||
from .functional.fixtures import * | ||
from .unit.fixtures import * |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
import types | ||
|
||
|
||
DUMMY_PERMISSION = 'dummy_permission' | ||
ANOTHER_PERMISSION = 'another_permission' | ||
YET_ANOTHER_PERMISSION = 'yet_another_permission' | ||
|
||
DUMMY_ACCOUNT = types.SimpleNamespace(role=DUMMY_PERMISSION) | ||
ANOTHER_ACCOUNT = types.SimpleNamespace(role=ANOTHER_PERMISSION) |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
import pytest | ||
|
||
from colibris.authorization import permissions | ||
|
||
from .fixtures import DUMMY_PERMISSION, ANOTHER_PERMISSION, YET_ANOTHER_PERMISSION | ||
|
||
|
||
def test_conjunction(): | ||
p = permissions.Permissions(and_set={DUMMY_PERMISSION, ANOTHER_PERMISSION}) | ||
|
||
with pytest.raises(permissions.PermissionNotMet): | ||
p.verify(set()) | ||
|
||
with pytest.raises(permissions.PermissionNotMet): | ||
p.verify({DUMMY_PERMISSION}) | ||
|
||
with pytest.raises(permissions.PermissionNotMet): | ||
p.verify({ANOTHER_PERMISSION}) | ||
|
||
p.verify({DUMMY_PERMISSION, ANOTHER_PERMISSION}) | ||
|
||
|
||
def test_disjunction(): | ||
p = permissions.Permissions(or_set={DUMMY_PERMISSION, ANOTHER_PERMISSION}) | ||
|
||
with pytest.raises(permissions.PermissionNotMet): | ||
p.verify(set()) | ||
|
||
p.verify({DUMMY_PERMISSION}) | ||
p.verify({ANOTHER_PERMISSION}) | ||
p.verify({DUMMY_PERMISSION, ANOTHER_PERMISSION}) | ||
|
||
|
||
def test_conjunction_disjunction(): | ||
p = permissions.Permissions(and_set={DUMMY_PERMISSION, ANOTHER_PERMISSION}, | ||
or_set={DUMMY_PERMISSION, YET_ANOTHER_PERMISSION}) | ||
|
||
with pytest.raises(permissions.PermissionNotMet): | ||
p.verify(set()) | ||
|
||
with pytest.raises(permissions.PermissionNotMet): | ||
p.verify({DUMMY_PERMISSION}) | ||
|
||
with pytest.raises(permissions.PermissionNotMet): | ||
p.verify({ANOTHER_PERMISSION}) | ||
|
||
with pytest.raises(permissions.PermissionNotMet): | ||
p.verify({YET_ANOTHER_PERMISSION}) | ||
|
||
with pytest.raises(permissions.PermissionNotMet): | ||
p.verify({DUMMY_PERMISSION, YET_ANOTHER_PERMISSION}) | ||
|
||
with pytest.raises(permissions.PermissionNotMet): | ||
p.verify({ANOTHER_PERMISSION, YET_ANOTHER_PERMISSION}) | ||
|
||
p.verify({DUMMY_PERMISSION, ANOTHER_PERMISSION}) | ||
p.verify({DUMMY_PERMISSION, ANOTHER_PERMISSION, YET_ANOTHER_PERMISSION}) | ||
|
||
|
||
def test_combine(): | ||
p1 = permissions.Permissions(and_set={DUMMY_PERMISSION}, or_set={ANOTHER_PERMISSION}) | ||
p2 = permissions.Permissions(and_set={ANOTHER_PERMISSION}, or_set={YET_ANOTHER_PERMISSION}) | ||
|
||
c = p1.combine(p2) | ||
assert c.and_set == {DUMMY_PERMISSION, ANOTHER_PERMISSION} | ||
assert c.or_set == {ANOTHER_PERMISSION, YET_ANOTHER_PERMISSION} |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
import pytest | ||
|
||
from colibris.authorization.rights import RightsBackend | ||
from colibris import persist | ||
|
||
|
||
USERNAME1 = 'username1' | ||
USERNAME2 = 'username2' | ||
|
||
RESOURCE = 'resource' | ||
OPERATION = 'operation' | ||
PERMISSION = '{}:{}'.format(RESOURCE, OPERATION) | ||
|
||
|
||
class User(persist.Model): | ||
username = persist.CharField() | ||
|
||
|
||
class Right(persist.Model): | ||
user = persist.ForeignKeyField(User) | ||
resource = persist.CharField() | ||
operation = persist.CharField() | ||
|
||
|
||
@pytest.fixture | ||
def database(database_maker): | ||
return database_maker(models=[User, Right]) | ||
|
||
|
||
@pytest.fixture | ||
def user1(database): | ||
return User.create(username=USERNAME1) | ||
|
||
|
||
@pytest.fixture | ||
def user2(database): | ||
return User.create(username=USERNAME2) | ||
|
||
|
||
@pytest.fixture | ||
def right(user1): | ||
return Right.create(user=user1, resource=RESOURCE, operation=OPERATION) | ||
|
||
|
||
@pytest.fixture | ||
def backend(): | ||
return RightsBackend(model=Right, account_field='user', | ||
resource_field='resource', operation_field='operation') | ||
|
||
|
||
def test_allowed(backend, user1, right): | ||
assert backend.get_actual_permissions(account=user1, method='GET', path='/') == {PERMISSION} | ||
|
||
|
||
def test_forbidden(backend, user2, right): | ||
assert backend.get_actual_permissions(account=user2, method='GET', path='/') == set() |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
from colibris.authorization.role import RoleBackend | ||
|
||
from .fixtures import DUMMY_PERMISSION, ANOTHER_PERMISSION, YET_ANOTHER_PERMISSION, DUMMY_ACCOUNT, ANOTHER_ACCOUNT | ||
|
||
|
||
def test_extract_role(): | ||
backend = RoleBackend(role_field='role') | ||
assert backend.get_role(account=DUMMY_ACCOUNT) == DUMMY_PERMISSION | ||
|
||
|
||
def test_actual_permissions_simple(): | ||
backend = RoleBackend(role_field='role') | ||
assert backend.get_actual_permissions(account=DUMMY_ACCOUNT, method='GET', path='/') == {DUMMY_PERMISSION} | ||
|
||
|
||
def test_actual_permissions_order(): | ||
backend = RoleBackend(role_field='role', order=[DUMMY_PERMISSION, ANOTHER_PERMISSION, YET_ANOTHER_PERMISSION]) | ||
permissions = backend.get_actual_permissions(account=ANOTHER_ACCOUNT, method='GET', path='/') | ||
assert permissions == {DUMMY_PERMISSION, ANOTHER_PERMISSION} |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
|
||
from aiohttp import web | ||
|
||
from colibris import authorization | ||
from colibris import views | ||
|
||
from .fixtures import DUMMY_PERMISSION, ANOTHER_PERMISSION | ||
|
||
|
||
class DummyView(views.View): | ||
async def get(self): | ||
return web.json_response({'message': 'dummy'}) | ||
|
||
|
||
class DummyViewWithClassPermission(DummyView): | ||
permissions = {DUMMY_PERMISSION} | ||
|
||
|
||
class DummyViewWithMethodPermission(DummyView): | ||
@authorization.require_permission(DUMMY_PERMISSION) | ||
async def get(self): | ||
return await super().get() | ||
|
||
|
||
class PermissionAuthorizationBackend(authorization.AuthorizationBackend): | ||
def get_actual_permissions(self, account, method, path): | ||
return {DUMMY_PERMISSION} | ||
|
||
|
||
class NoPermissionAuthorizationBackend(authorization.AuthorizationBackend): | ||
def get_actual_permissions(self, account, method, path): | ||
return set() | ||
|
||
|
||
class WrongPermissionAuthorizationBackend(authorization.AuthorizationBackend): | ||
def get_actual_permissions(self, account, method, path): | ||
return {ANOTHER_PERMISSION} | ||
|
||
|
||
async def test_default_view_permissions(http_client_maker): | ||
client = await http_client_maker(routes=[('/dummy', DummyView)]) | ||
response = await client.get('/dummy') | ||
|
||
assert response.status == 200 | ||
|
||
|
||
async def test_class_required_permissions_fulfilled(http_client_maker): | ||
client = await http_client_maker(routes=[('/dummy', DummyViewWithClassPermission)], | ||
authorization_backend=PermissionAuthorizationBackend) | ||
response = await client.get('/dummy') | ||
|
||
assert response.status == 200 | ||
|
||
|
||
async def test_class_required_permissions_no_permissions(http_client_maker): | ||
client = await http_client_maker(routes=[('/dummy', DummyViewWithClassPermission)], | ||
authorization_backend=NoPermissionAuthorizationBackend) | ||
response = await client.get('/dummy') | ||
|
||
assert response.status == 403 | ||
|
||
|
||
async def test_class_required_permissions_wrong_permissions(http_client_maker): | ||
client = await http_client_maker(routes=[('/dummy', DummyViewWithClassPermission)], | ||
authorization_backend=WrongPermissionAuthorizationBackend) | ||
response = await client.get('/dummy') | ||
|
||
assert response.status == 403 | ||
|
||
|
||
async def test_method_required_permissions_fulfilled(http_client_maker): | ||
client = await http_client_maker(routes=[('/dummy', DummyViewWithMethodPermission)], | ||
authorization_backend=PermissionAuthorizationBackend) | ||
response = await client.get('/dummy') | ||
|
||
assert response.status == 200 | ||
|
||
|
||
async def test_method_required_permissions_no_permissions(http_client_maker): | ||
client = await http_client_maker(routes=[('/dummy', DummyViewWithMethodPermission)], | ||
authorization_backend=NoPermissionAuthorizationBackend) | ||
response = await client.get('/dummy') | ||
|
||
assert response.status == 403 | ||
|
||
|
||
async def test_method_required_permissions_wrong_permissions(http_client_maker): | ||
client = await http_client_maker(routes=[('/dummy', DummyViewWithMethodPermission)], | ||
authorization_backend=WrongPermissionAuthorizationBackend) | ||
response = await client.get('/dummy') | ||
|
||
assert response.status == 403 |
Oops, something went wrong.