Skip to content

Commit

Permalink
Remove unnecessary check for permissions in get_node
Browse files Browse the repository at this point in the history
  • Loading branch information
bellini666 committed Apr 25, 2020
1 parent 17108ac commit 3582a6f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
9 changes: 3 additions & 6 deletions graphene_django_plus/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ def get_queryset(cls, qs, info):
@classmethod
def get_node(cls, info, id):
"""Get the node instance given the relay global id."""
# NOTE: get_queryset will filter allowed models for the user so
# this will return None if he is not allowed to retrieve this

if gql_optimizer is not None:
# optimizer will ignore queryset so call the same as they call
# but passing objects from get_queryset to keep our preferences
Expand All @@ -156,12 +159,6 @@ def get_node(cls, info, id):
else:
instance = super().get_node(info, id)

if instance is None:
return None

if not cls.check_object_permissions(info.context.user, instance):
raise PermissionDenied()

return instance

@classmethod
Expand Down
40 changes: 25 additions & 15 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import json
from unittest import mock

from graphene_django import DjangoObjectType

from .base import BaseTestCase
from .schema import IssueType


class TestTypes(BaseTestCase):
Expand Down Expand Up @@ -402,18 +405,25 @@ def test_result_no_gql_optimizer(self):
}}},
)

# issue (not allowed)
p_id = base64.b64encode('IssueType:{}'.format(
self.unallowed_issues[0].id,
).encode()).decode()
r = self.query(
"""
query {
issue (id: "%s") {
name
}
}
""" % (p_id, ),
op_name='issue'
)
self.assertResponseHasErrors(r)
with mock.patch.object(
IssueType,
'get_node',
lambda info, id: DjangoObjectType.get_node.__func__(IssueType, info, id)):
# issue (not allowed)
p_id = base64.b64encode('IssueType:{}'.format(
self.unallowed_issues[0].id,
).encode()).decode()
r = self.query(
"""
query {
issue (id: "%s") {
name
}
}
""" % (p_id, ),
op_name='issue'
)
self.assertEqual(
json.loads(r.content),
{'data': {'issue': None}},
)

0 comments on commit 3582a6f

Please sign in to comment.