Skip to content

Commit

Permalink
Add check for current user not authenticated
Browse files Browse the repository at this point in the history
  • Loading branch information
imdadahad committed Oct 24, 2016
1 parent 76a61ff commit 2b5894b
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions tests/app/main/test_permissions.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import pytest
from app.utils import user_has_permissions
from app.main.views.index import index
from werkzeug.exceptions import Forbidden
from werkzeug.exceptions import Forbidden, Unauthorized
from flask import request


def _test_permissions(app_, usr, permissions, service_id, will_succeed, any_=False, admin_override=False):
with app_.test_request_context() as ctx:
request.view_args.update({'service_id': service_id})
with app_.test_client() as client:
client.login(usr)
if usr:
client.login(usr)
decorator = user_has_permissions(*permissions, any_=any_, admin_override=admin_override)
decorated_index = decorator(index)
if will_succeed:
response = decorated_index()
else:
try:
response = decorated_index()
pytest.fail("Failed to throw a forbidden exception")
except Forbidden:
pytest.fail("Failed to throw a forbidden or unauthorised exception")
except (Forbidden, Unauthorized):
pass


Expand Down Expand Up @@ -107,6 +108,17 @@ def test_platform_admin_user_can_not_access_page(app_,
admin_override=False)


def test_user_with_permissions_returns_401_unauthenticated_user(app_):
from flask_login import current_user
assert not current_user
_test_permissions(
app_,
None,
[],
'',
will_succeed=False)


def _user_with_permissions():
from app.notify_client.user_api_client import User

Expand Down

0 comments on commit 2b5894b

Please sign in to comment.