22
33from typing import TYPE_CHECKING
44
5+ from infrahub .auth import AccountSession
6+ from infrahub .core .account import GlobalPermission
57from infrahub .core .branch import Branch
8+ from infrahub .core .constants import GlobalPermissions , PermissionDecision
69from infrahub .database import InfrahubDatabase
710from infrahub .events .node_action import NodeMutatedEvent
811from infrahub .graphql .initialization import prepare_graphql_params
912from infrahub .services import InfrahubServices
1013from tests .adapters .event import MemoryInfrahubEvent
1114from tests .helpers .graphql import graphql
15+ from tests .helpers .permissions import define_permissions
1216
1317if TYPE_CHECKING :
1418 from infrahub .auth import AccountSession
@@ -22,7 +26,19 @@ async def test_add_context_invalid_account(
2226 default_branch : Branch ,
2327 car_person_schema : None ,
2428 first_account : Node ,
29+ session_first_account : AccountSession ,
2530):
31+ await define_permissions (
32+ account = first_account ,
33+ db = db ,
34+ global_permissions = [
35+ GlobalPermission (
36+ action = GlobalPermissions .OVERRIDE_CONTEXT .value ,
37+ decision = PermissionDecision .ALLOW_ALL .value ,
38+ ),
39+ ],
40+ )
41+
2642 query = """
2743 mutation {
2844 TestPersonCreate(data: {name: { value: "John"}, height: {value: 182}}, context: { account: { id: "very-invalid" }}) {
@@ -33,7 +49,9 @@ async def test_add_context_invalid_account(
3349 }
3450 }
3551 """
36- gql_params = await prepare_graphql_params (db = db , include_subscription = False , branch = default_branch )
52+ gql_params = await prepare_graphql_params (
53+ db = db , include_subscription = False , branch = default_branch , account_session = session_first_account
54+ )
3755 result = await graphql (
3856 schema = gql_params .schema ,
3957 source = query ,
@@ -54,6 +72,17 @@ async def test_add_context_valid_account(
5472 first_account : Node ,
5573 second_account : Node ,
5674):
75+ await define_permissions (
76+ account = first_account ,
77+ db = db ,
78+ global_permissions = [
79+ GlobalPermission (
80+ action = GlobalPermissions .OVERRIDE_CONTEXT .value ,
81+ decision = PermissionDecision .ALLOW_ALL .value ,
82+ ),
83+ ],
84+ )
85+
5786 query = """
5887 mutation {
5988 TestPersonCreate(data: {name: { value: "John"}, height: {value: 182}}, context: { account: { id: "%s" }}) {
@@ -86,3 +115,39 @@ async def test_add_context_valid_account(
86115 node_event = memory_event .events [0 ]
87116 assert isinstance (node_event , NodeMutatedEvent )
88117 assert node_event .meta .account_id == second_account .id
118+
119+
120+ async def test_add_context_missing_permissions (
121+ db : InfrahubDatabase ,
122+ default_branch : Branch ,
123+ car_person_schema : None ,
124+ session_second_account : AccountSession ,
125+ first_account : Node ,
126+ second_account : Node ,
127+ ):
128+ query = """
129+ mutation {
130+ TestPersonCreate(data: {name: { value: "John"}, height: {value: 182}}, context: { account: { id: "%s" }}) {
131+ ok
132+ object {
133+ id
134+ }
135+ }
136+ }
137+ """ % (first_account .id )
138+
139+ gql_params = await prepare_graphql_params (
140+ db = db ,
141+ include_subscription = False ,
142+ branch = default_branch ,
143+ account_session = session_second_account ,
144+ )
145+ result = await graphql (
146+ schema = gql_params .schema ,
147+ source = query ,
148+ context_value = gql_params .context ,
149+ root_value = None ,
150+ variable_values = {},
151+ )
152+ assert result .errors
153+ assert "You do not have the following permission: global:override_context:allow_default" in str (result .errors )
0 commit comments