mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 10:53:28 -05:00
Makes uses of the additions to utils in https://github.com/alphagov/notifications-utils/pull/9 This commit strips out a lot of the complex stuff that the views and templates in this app were doing. There is now a cleaner separation of concerns: - utils returns the number and type of errors in the csv - `get_errors_for_csv` helper in this app maps the number and type of errors onto human-friendly error messages - the view and template just doing the glueing-together of all the pieces This is (hopefully) easier to understand, definitely makes the component parts easier to test in isolation, and makes it easier to give more specific error messages.
71 lines
2.8 KiB
Python
71 lines
2.8 KiB
Python
import pytest
|
|
from flask import url_for
|
|
|
|
from app.utils import user_has_permissions
|
|
from app.main.views.index import index
|
|
from werkzeug.exceptions import Forbidden
|
|
|
|
|
|
def test_user_has_permissions_on_endpoint_fail(app_,
|
|
api_user_active,
|
|
mock_login,
|
|
mock_get_user_with_permissions):
|
|
with app_.test_request_context():
|
|
with app_.test_client() as client:
|
|
client.login(api_user_active)
|
|
decorator = user_has_permissions('something')
|
|
decorated_index = decorator(index)
|
|
try:
|
|
response = decorated_index()
|
|
pytest.fail("Failed to throw a forbidden exception")
|
|
except Forbidden:
|
|
pass
|
|
|
|
|
|
def test_user_has_permissions_success(app_,
|
|
api_user_active,
|
|
mock_login,
|
|
mock_get_user_with_permissions):
|
|
with app_.test_request_context():
|
|
with app_.test_client() as client:
|
|
client.login(api_user_active)
|
|
decorator = user_has_permissions('manage_users')
|
|
decorated_index = decorator(index)
|
|
response = decorated_index()
|
|
|
|
|
|
def test_user_has_permissions_or(app_,
|
|
api_user_active,
|
|
mock_login,
|
|
mock_get_user_with_permissions):
|
|
with app_.test_request_context():
|
|
with app_.test_client() as client:
|
|
client.login(api_user_active)
|
|
decorator = user_has_permissions('something', 'manage_users', or_=True)
|
|
decorated_index = decorator(index)
|
|
response = decorated_index()
|
|
|
|
|
|
def test_user_has_permissions_multiple(app_,
|
|
api_user_active,
|
|
mock_login,
|
|
mock_get_user_with_permissions):
|
|
with app_.test_request_context():
|
|
with app_.test_client() as client:
|
|
client.login(api_user_active)
|
|
decorator = user_has_permissions('manage_templates', 'manage_users')
|
|
decorated_index = decorator(index)
|
|
response = decorated_index()
|
|
|
|
|
|
def test_exact_permissions(app_,
|
|
api_user_active,
|
|
mock_login,
|
|
mock_get_user_with_permissions):
|
|
with app_.test_request_context():
|
|
with app_.test_client() as client:
|
|
client.login(api_user_active)
|
|
decorator = user_has_permissions('manage_users', 'manage_templates', 'manage_settings')
|
|
decorated_index = decorator(index)
|
|
response = decorated_index()
|