Skip to content

Instantly share code, notes, and snippets.

@o3o3o
Last active July 9, 2019 02:11
Show Gist options
  • Select an option

  • Save o3o3o/65ab48f86b4fd2d32c69587fe17958c2 to your computer and use it in GitHub Desktop.

Select an option

Save o3o3o/65ab48f86b4fd2d32c69587fe17958c2 to your computer and use it in GitHub Desktop.
django graphql jwt test with sessionMiddleware
from django.test import TestCase
from django.conf import settings
from django.contrib.sessions.middleware import SessionMiddleware
from graphql_jwt.testcases import JSONWebTokenClient
class MYJSONWebTokenClient(JSONWebTokenClient):
def execute(self, query, variables=None, **extra):
extra.update(self._credentials)
context = self.post("/", **extra)
if hasattr(self, "session_key"):
# Fake cookie saving action just like SessionMiddleware does
context.COOKIES = {settings.SESSION_COOKIE_NAME: self.session_key}
# Init session from cookies
middleware = SessionMiddleware()
middleware.process_request(context)
context.session.save()
self.session_key = context.session.session_key
res = super(JSONWebTokenClient, self).execute(
query, context=context, variables=variables
)
# Save the session after invoking the api wihout SessionMiddleware
context.session.save()
return res
class MyTests(TestCase):
def setUp(self):
self.client = MYJSONWebTokenClient()
def test_sth(self):
# ....
# gql = ' mutation { ....}'
# variables = {}
data = self.client.execute(gql, variables)
data = self.client.execute(gql, variables)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment