Skip to content

Commit

Permalink
tests validator
Browse files Browse the repository at this point in the history
  • Loading branch information
joaovitorsilvestre committed Jul 29, 2017
1 parent 59ee495 commit 4622911
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions graphene_mongo/mutation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def mutate(root, args, context, info):
def user_mutate(root, args, context, info):
if validator:
validator(model, args, {}, {})

obj = mutate_func(args, context)
if not isinstance(obj, model):
raise TypeError('Failed to resolve mutation of the schema {}'
Expand All @@ -36,6 +37,7 @@ def user_mutate(root, args, context, info):
def generic_mutate(root, args, context, info):
if validator:
validator(model, args, {}, {})

obj = model(**args)
obj.save()
graphene_obj = mongo_to_graphene(obj, graphene_schema, fields_mutation)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,25 @@ class Test(Document):
assert str(e_info.value) == "It was not possible to generate schema for {} because the field {} is a " \
"ReferenceField to self and this is not supported yet."\
.format("TestSchema", 'parent')


def test_validator_wrong():
from mongoengine import Document, StringField

class Test(Document):
parent = StringField()

with pytest.raises(Exception) as e_info:
Options('TestSchema', {'model': Test, 'validator': True})

assert str(e_info.value) == "'validator' attribute must be callable."

with pytest.raises(Exception) as e_info:
Options('TestSchema', {'model': Test, 'validator': lambda x: x})

assert str(e_info.value) == ("The 'validator' attribute must be a callable that accepts four arguments: "
"model, fields, query, special_params. \n"
"model: mongoengine.Document that the opration is to be made, \n"
"fields: list of fields that was requested, \n"
"query: dict with the query parameters, \n"
"special_params: dict with params used to improve query, as 'limit' and 'skip'")
98 changes: 98 additions & 0 deletions tests/test_validator.py
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

0 comments on commit 4622911

Please sign in to comment.