Look up major database resources in a before_request
hook.
A `before_request` hook queries the database for portfolios, requests, and task orders based on the route arguments. The resources are added as attributes on `g`. The portfolio context processor and the access decorator now rely on those resources being available on `g`. WIP: find major resources in before_request hook, apply to g WIP: use g.portfolio for portfolio context processor WIP: the access decorator should rely on the resources being available on g
This commit is contained in:
@@ -153,7 +153,7 @@ def test_user_can_access_decorator_atat_level(set_current_user):
|
||||
_access_activity_log()
|
||||
|
||||
|
||||
def test_user_can_access_decorator_portfolio_level(set_current_user):
|
||||
def test_user_can_access_decorator_portfolio_level(set_current_user, request_ctx):
|
||||
ccpo = UserFactory.create_ccpo()
|
||||
edit_admin = UserFactory.create()
|
||||
view_admin = UserFactory.create()
|
||||
@@ -162,6 +162,9 @@ def test_user_can_access_decorator_portfolio_level(set_current_user):
|
||||
# factory gives view perms by default
|
||||
PortfolioRoleFactory.create(user=view_admin, portfolio=portfolio)
|
||||
|
||||
request_ctx.g.portfolio = portfolio
|
||||
request_ctx.g.application = None
|
||||
|
||||
@user_can_access_decorator(Permissions.EDIT_PORTFOLIO_NAME)
|
||||
def _edit_portfolio_name(*args, **kwargs):
|
||||
return True
|
||||
@@ -177,7 +180,7 @@ def test_user_can_access_decorator_portfolio_level(set_current_user):
|
||||
_edit_portfolio_name(portfolio_id=portfolio.id)
|
||||
|
||||
|
||||
def test_user_can_access_decorator_application_level(set_current_user):
|
||||
def test_user_can_access_decorator_application_level(set_current_user, request_ctx):
|
||||
ccpo = UserFactory.create_ccpo()
|
||||
port_admin = UserFactory.create()
|
||||
app_user = UserFactory.create()
|
||||
@@ -189,6 +192,9 @@ def test_user_can_access_decorator_application_level(set_current_user):
|
||||
app = portfolio.applications[0]
|
||||
ApplicationRoleFactory.create(application=app, user=app_user)
|
||||
|
||||
request_ctx.g.portfolio = portfolio
|
||||
request_ctx.g.application = app
|
||||
|
||||
@user_can_access_decorator(Permissions.VIEW_APPLICATION)
|
||||
def _stroll_into_mos_eisley(*args, **kwargs):
|
||||
return True
|
||||
|
@@ -78,9 +78,7 @@ def test_all_protected_routes_have_access_control(
|
||||
monkeypatch.setattr(
|
||||
"atst.domain.invitations.PortfolioInvitations._get", lambda *a: Mock()
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"atst.utils.context_processors.get_portfolio_from_context", lambda *a: None
|
||||
)
|
||||
monkeypatch.setattr("atst.app.assign_resources", lambda *a: None)
|
||||
|
||||
# patch the internal function the access decorator uses so that
|
||||
# we can check that it was called
|
||||
|
24
tests/utils/test_context_processors.py
Normal file
24
tests/utils/test_context_processors.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from atst.utils.context_processors import get_resources_from_context
|
||||
|
||||
from tests.factories import *
|
||||
|
||||
|
||||
def test_get_resources_from_context():
|
||||
portfolio = PortfolioFactory.create()
|
||||
task_order = TaskOrderFactory.create(portfolio=portfolio)
|
||||
application = ApplicationFactory.create(portfolio=portfolio)
|
||||
environment = EnvironmentFactory.create(application=application)
|
||||
|
||||
assert get_resources_from_context({"portfolio_id": portfolio.id}) == (portfolio,)
|
||||
assert get_resources_from_context({"application_id": application.id}) == (
|
||||
portfolio,
|
||||
application,
|
||||
)
|
||||
assert get_resources_from_context({"environment_id": environment.id}) == (
|
||||
portfolio,
|
||||
application,
|
||||
)
|
||||
assert get_resources_from_context({"task_order_id": task_order.id}) == (
|
||||
portfolio,
|
||||
task_order,
|
||||
)
|
Reference in New Issue
Block a user