-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59ee495
commit 4622911
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,98 @@ | ||
from graphene_mongo import MongoSchema | ||
|
||
|
||
def test_validator_raises_error_query(mock_person, schema_builder): | ||
def verify_permission(model, fields, query, special_params): | ||
if 'name' in fields: | ||
raise Exception('Unauthorized Access') | ||
|
||
class PersonSchema(MongoSchema): | ||
model = mock_person | ||
validator = verify_permission | ||
|
||
def mutate(args, context): | ||
u = mock_person(**args) | ||
u.save() | ||
return u | ||
|
||
mock_person(name="Test").save() | ||
|
||
schema = schema_builder([(PersonSchema, PersonSchema.single)], [PersonSchema]) | ||
result = schema.execute("""query testQuery { | ||
person(name:"Test") { | ||
name | ||
} | ||
}""") | ||
|
||
|
||
def test_validator_raises_error_mutation(mock_person, schema_builder): | ||
def verify_permission(model, fields, query, special_params): | ||
if 'name' in fields: | ||
raise Exception('Unauthorized Access') | ||
|
||
# tests without user defined mutate | ||
class PersonSchema(MongoSchema): | ||
model = mock_person | ||
validator = verify_permission | ||
|
||
schema = schema_builder([(PersonSchema, PersonSchema.single)], [PersonSchema]) | ||
|
||
result = schema.execute("""mutation testMutation { | ||
createPerson(name:"Test") { | ||
person { | ||
name | ||
} | ||
} | ||
}""") | ||
|
||
assert result.errors | ||
assert str(result.errors[0]) == 'Unauthorized Access' | ||
|
||
# tests with user defined mutate | ||
class PersonSchema(MongoSchema): | ||
model = mock_person | ||
validator = verify_permission | ||
|
||
@staticmethod | ||
def mutate(args, context): | ||
u = mock_person(**args) | ||
u.save() | ||
return u | ||
|
||
schema = schema_builder([(PersonSchema, PersonSchema.single)], [PersonSchema]) | ||
|
||
result = schema.execute("""mutation testMutation { | ||
createPerson(name:"Test") { | ||
person { | ||
name | ||
} | ||
} | ||
}""") | ||
|
||
assert result.errors | ||
assert str(result.errors[0]) == 'Unauthorized Access' | ||
|
||
|
||
def test_validator_right_params_types(mock_person, schema_builder): | ||
def verify_permission(model, fields, query, special_params): | ||
assert model == mock_person | ||
assert sorted(fields) == ['id', 'name'] | ||
assert query == {'name__contains': 'Joe'} | ||
assert special_params == {'skip': 1, 'limit': 2} | ||
|
||
for i in range(5): | ||
mock_person(name="Joe" + str(i)).save() | ||
|
||
class PersonSchema(MongoSchema): | ||
model = mock_person | ||
validator = verify_permission | ||
|
||
schema = schema_builder([(PersonSchema, PersonSchema.list)]) | ||
result = schema.execute(""" query testQuery { | ||
person(name_Contains: "Joe", skip: 1, limit: 2) { | ||
id | ||
name | ||
} | ||
}""") | ||
|
||
assert not result.errors |