-
Notifications
You must be signed in to change notification settings - Fork 822
[WIP] Add support for permissions on fields #846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
patrick91
wants to merge
1
commit into
graphql-python:master
from
patrick91:feature/field-permissions
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from functools import partial | ||
|
||
import pytest | ||
from graphql.error import GraphQLError | ||
|
||
from ..argument import Argument | ||
from ..field import Field | ||
from ..scalars import String | ||
from ..structures import NonNull | ||
from .utils import MyLazyType | ||
|
||
|
||
class MyInstance(object): | ||
value = "value" | ||
value_func = staticmethod(lambda: "value_func") | ||
|
||
def value_method(self): | ||
return "value_method" | ||
|
||
|
||
class AlwaysFalsePermission(object): | ||
def has_permission(self, info, field): | ||
return False | ||
|
||
class AlwaysTruePermission(object): | ||
def has_permission(self, info, field): | ||
return True | ||
|
||
|
||
def test_raises_error(): | ||
MyType = object() | ||
field = Field(MyType, source="value", permission_classes=[AlwaysFalsePermission]) | ||
|
||
with pytest.raises(GraphQLError): | ||
|
||
field.get_resolver(None)(MyInstance(), None) | ||
|
||
# TODO: test error message | ||
|
||
def test_does_not_raise_error(): | ||
MyType = object() | ||
field = Field(MyType, source="value", permission_classes=[AlwaysTruePermission]) | ||
|
||
field.get_resolver(None)(MyInstance(), None) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't kwargs be passed as well?, I have cases where i need to check permissions based on input params
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe not it the same place but yes. You often times need the context (request) and all the inputs to validate. When you have inputs that depend on each other you need that. Although you shouldn't need inputs to determine field access, just to validate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I think we should pass all the arguments to the permission class, I was a bit lazy with the comment 😅