Skip to content

Instantly share code, notes, and snippets.

@elena
Created August 13, 2020 05:36
Show Gist options
  • Save elena/84a623c30c76cf03e7dff618bdeb047c to your computer and use it in GitHub Desktop.
Save elena/84a623c30c76cf03e7dff618bdeb047c to your computer and use it in GitHub Desktop.
Test Django GraphQL graphene_django logged in user authenticated unittest
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=100)
import graphene
from graphql_jwt.decorators import login_required
from .models import Person
class Query(object):
person_list = graphene.List(PersonType)
@login_required
def resolve_person_list(self, info):
return Person.objects.all()
class Mutation(graphene.ObjectType):
person_create = PersonCreate.Field()
import json
from django.contrib.auth import get_user_model
from graphene_django.utils.testing import GraphQLTestCase
from graphql_jwt.shortcuts import get_token
from project.schema import schema
class TestSchemaPersonList(GraphQLTestCase):
GRAPHQL_SCHEMA = schema
def setUp(self):
self.user = get_user_model().objects.create_user("[email protected]", "1234")
token = get_token(get_user_model().objects.first())
self.headers = {"HTTP_AUTHORIZATION": f"JWT {token}"}
self.name_1 = "Test Name 1"
Person.objects.create(name=self.name_1)
def test_query_person_list_1_pass(self):
response = self.query(
"""
query {
personList {
id, name
}
}
""",
headers=self.headers,
)
content = json.loads(response.content)
self.assertResponseNoErrors(response)
person_list = content["data"]["personList"]
assert person_list[0]["id"] == "1"
assert person_list[0]["name"] == self.name_1
def test_query_person_list_2_pass(self):
name_2 = "Test Name 2"
Person.objects.create(name=name_2)
response = self.query(
"""
query {
personList {
id, name
}
}
""",
headers=self.headers,
)
content = json.loads(response.content)
self.assertResponseNoErrors(response)
person_list = content["data"]["personList"]
assert person_list[0]["id"] == "1"
assert person_list[0]["name"] == self.name_1
assert person_list[1]["id"] == "2"
assert person_list[1]["name"] == name_2
import graphene
from people.schema import Query as PersonQuery
from people.schema import Mutation as PersonMutation
class Query(PersonQuery):
pass
class Mutation(PersonMutation):
pass
schema = graphene.Schema(query=Query, mutation=Mutation)
# ...
INSTALLED_APPS = [
# ...
"graphene_django",
"corsheaders",
"people",
# ...
]
MIDDLEWARE = [
# ...
"django.contrib.sessions.middleware.SessionMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
# ...
]
AUTHENTICATION_BACKENDS = [
"graphql_jwt.backends.JSONWebTokenBackend",
"django.contrib.auth.backends.ModelBackend",
]
GRAPHENE = {
"SCHEMA": "appo.schema.schema",
"MIDDLEWARE": ["graphql_jwt.middleware.JSONWebTokenMiddleware"],
}
# Cors DO NOT IN PRODUCTION
CORS_ORIGIN_ALLOW_ALL = True
# ...
from django.urls import path
from graphene_django.views import GraphQLView
urlpatterns = [
# ...
path('graphql', GraphQLView.as_view(graphiql=True)),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment