Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Switch from pickle to jsonpickle
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Prin committed Aug 11, 2016
1 parent b7f3eca commit 34672fa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
7 changes: 4 additions & 3 deletions oauth2client/contrib/django_util/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@
import hashlib
import json
import os
import pickle

from django import http
from django import shortcuts
from django.conf import settings
from django.core import urlresolvers
from django.shortcuts import redirect
import jsonpickle
from six.moves.urllib import parse

from oauth2client import client
from oauth2client.contrib import django_util
from oauth2client.contrib.django_util import get_storage
from oauth2client.contrib.django_util import signals


_CSRF_KEY = 'google_oauth2_csrf_token'
_FLOW_KEY = 'google_oauth2_flow_{0}'

Expand Down Expand Up @@ -71,7 +72,7 @@ def _make_flow(request, scopes, return_url=None):
urlresolvers.reverse("google_oauth:callback")))

flow_key = _FLOW_KEY.format(csrf_token)
request.session[flow_key] = pickle.dumps(flow)
request.session[flow_key] = jsonpickle.encode(flow)
return flow


Expand All @@ -89,7 +90,7 @@ def _get_flow_for_token(csrf_token, request):
CSRF token.
"""
flow_pickle = request.session.get(_FLOW_KEY.format(csrf_token), None)
return None if flow_pickle is None else pickle.loads(flow_pickle)
return None if flow_pickle is None else jsonpickle.decode(flow_pickle)


def oauth2_callback(request):
Expand Down
18 changes: 10 additions & 8 deletions tests/contrib/django_util/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ def setUp(self):
self.user = User.objects.create_user(
username='bill', email='bill@example.com', password='hunter2')

@mock.patch('oauth2client.contrib.django_util.views.pickle')
def test_callback_works(self, pickle):
@mock.patch('oauth2client.contrib.django_util.views.jsonpickle')
def test_callback_works(self, jsonpickle_mock):
request = self.factory.get('oauth2/oauth2callback', data={
'state': json.dumps(self.fake_state),
'code': 123
Expand All @@ -169,9 +169,9 @@ def test_callback_works(self, pickle):
redirect_uri=request.build_absolute_uri("oauth2/oauth2callback"))

name = 'google_oauth2_flow_{0}'.format(self.CSRF_TOKEN)
self.session[name] = pickle.dumps(flow)
self.session[name] = jsonpickle_mock.encode(flow)
flow.step2_exchange = mock.Mock()
pickle.loads.return_value = flow
jsonpickle_mock.decode.return_value = flow

request.session = self.session
request.user = self.user
Expand All @@ -180,9 +180,10 @@ def test_callback_works(self, pickle):
self.assertEqual(
response.status_code, django.http.HttpResponseRedirect.status_code)
self.assertEqual(response['Location'], self.RETURN_URL)
self.assertTrue(jsonpickle_mock.encode.called)

@mock.patch('oauth2client.contrib.django_util.views.pickle')
def test_callback_handles_bad_flow_exchange(self, pickle):
@mock.patch('oauth2client.contrib.django_util.views.jsonpickle')
def test_callback_handles_bad_flow_exchange(self, jsonpickle_mock):
request = self.factory.get('oauth2/oauth2callback', data={
"state": json.dumps(self.fake_state),
"code": 123
Expand All @@ -198,17 +199,18 @@ def test_callback_handles_bad_flow_exchange(self, pickle):
redirect_uri=request.build_absolute_uri('oauth2/oauth2callback'))

session_key = 'google_oauth2_flow_{0}'.format(self.CSRF_TOKEN)
self.session[session_key] = pickle.dumps(flow)
self.session[session_key] = jsonpickle_mock.encode(flow)

def local_throws(code):
raise FlowExchangeError('test')

flow.step2_exchange = local_throws
pickle.loads.return_value = flow
jsonpickle_mock.decode.return_value = flow

request.session = self.session
response = views.oauth2_callback(request)
self.assertIsInstance(response, http.HttpResponseBadRequest)
self.assertTrue(jsonpickle_mock.encode.called)

def test_error_returns_bad_request(self):
request = self.factory.get('oauth2/oauth2callback', data={
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ basedeps = mock>=1.3.0
deps = {[testenv]basedeps}
django
keyring
jsonpickle
setenv =
pypy: with_gmp=no
DJANGO_SETTINGS_MODULE=tests.contrib.django_util.settings
Expand Down

0 comments on commit 34672fa

Please sign in to comment.