31
31
from google .appengine .ext import db
32
32
from google .appengine .ext import ndb
33
33
from google .appengine .ext import testbed
34
- import httplib2
35
34
import mock
36
35
from six .moves import urllib
37
36
import unittest2
42
41
from oauth2client import client
43
42
from oauth2client import clientsecrets
44
43
from oauth2client .contrib import appengine
45
- from ..http_mock import CacheMock
44
+ from .. import http_mock
46
45
47
46
__author__ = '[email protected] (Joe Gregorio)'
48
47
49
48
DATA_DIR = os .path .join (os .path .dirname (__file__ ), '..' , 'data' )
49
+ DEFAULT_RESP = """\
50
+ {
51
+ "access_token": "foo_access_token",
52
+ "expires_in": 3600,
53
+ "extra": "value",
54
+ "refresh_token": "foo_refresh_token"
55
+ }
56
+ """
50
57
51
58
52
59
def datafile (filename ):
@@ -75,22 +82,6 @@ def __call__(self):
75
82
return None
76
83
77
84
78
- class Http2Mock (object ):
79
- """Mock httplib2.Http"""
80
- status = 200
81
- content = {
82
- 'access_token' : 'foo_access_token' ,
83
- 'refresh_token' : 'foo_refresh_token' ,
84
- 'expires_in' : 3600 ,
85
- 'extra' : 'value' ,
86
- }
87
-
88
- def request (self , token_uri , method , body , headers , * args , ** kwargs ):
89
- self .body = body
90
- self .headers = headers
91
- return self , json .dumps (self .content )
92
-
93
-
94
85
class TestAppAssertionCredentials (unittest2 .TestCase ):
95
86
account_name = "[email protected] "
96
87
signature = "signature"
@@ -139,7 +130,7 @@ def test_raise_correct_type_of_exception(self):
139
130
140
131
scope = 'http://www.googleapis.com/scope'
141
132
credentials = appengine .AppAssertionCredentials (scope )
142
- http = httplib2 . Http ( )
133
+ http = http_mock . HttpMock ( data = DEFAULT_RESP )
143
134
with self .assertRaises (client .AccessTokenRefreshError ):
144
135
credentials .refresh (http )
145
136
@@ -155,7 +146,7 @@ def test_get_access_token_on_refresh(self):
155
146
"http://www.googleapis.com/scope" ,
156
147
"http://www.googleapis.com/scope2" ]
157
148
credentials = appengine .AppAssertionCredentials (scope )
158
- http = httplib2 . Http ( )
149
+ http = http_mock . HttpMock ( data = DEFAULT_RESP )
159
150
credentials .refresh (http )
160
151
self .assertEqual ('a_token_123' , credentials .access_token )
161
152
@@ -168,7 +159,7 @@ def test_get_access_token_on_refresh(self):
168
159
scope = ('http://www.googleapis.com/scope '
169
160
'http://www.googleapis.com/scope2' )
170
161
credentials = appengine .AppAssertionCredentials (scope )
171
- http = httplib2 . Http ( )
162
+ http = http_mock . HttpMock ( data = DEFAULT_RESP )
172
163
credentials .refresh (http )
173
164
self .assertEqual ('a_token_123' , credentials .access_token )
174
165
self .assertEqual (
@@ -184,7 +175,7 @@ def test_custom_service_account(self):
184
175
autospec = True ) as get_access_token :
185
176
credentials = appengine .AppAssertionCredentials (
186
177
scope , service_account_id = account_id )
187
- http = httplib2 . Http ( )
178
+ http = http_mock . HttpMock ( data = DEFAULT_RESP )
188
179
credentials .refresh (http )
189
180
190
181
self .assertEqual ('a_token_456' , credentials .access_token )
@@ -370,7 +361,7 @@ def test_validate(self):
370
361
371
362
372
363
def _http_request (* args , ** kwargs ):
373
- resp = httplib2 . Response ({ 'status' : '200' } )
364
+ resp = http_mock . ResponseMock ( )
374
365
content = json .dumps ({'access_token' : 'bar' })
375
366
376
367
return resp , content
@@ -630,12 +621,9 @@ def get(self, *args, **kwargs):
630
621
})
631
622
self .current_user = user_mock ()
632
623
users .get_current_user = self .current_user
633
- self .httplib2_orig = httplib2 .Http
634
- httplib2 .Http = Http2Mock
635
624
636
625
def tearDown (self ):
637
626
self .testbed .deactivate ()
638
- httplib2 .Http = self .httplib2_orig
639
627
640
628
def test_in_error (self ):
641
629
# NOTE: This branch is never reached. _in_error is not set by any code
@@ -655,7 +643,9 @@ def test_callback_application(self):
655
643
app .router .match_routes [0 ].handler .__name__ ,
656
644
'OAuth2Handler' )
657
645
658
- def test_required (self ):
646
+ @mock .patch ('oauth2client.transport.get_http_object' )
647
+ def test_required (self , new_http ):
648
+ new_http .return_value = http_mock .HttpMock (data = DEFAULT_RESP )
659
649
# An initial request to an oauth_required decorated path should be a
660
650
# redirect to start the OAuth dance.
661
651
self .assertEqual (self .decorator .flow , None )
@@ -688,7 +678,7 @@ def test_required(self):
688
678
response_query = urllib .parse .parse_qs (parts [1 ])
689
679
response = response_query [
690
680
self .decorator ._token_response_param ][0 ]
691
- self .assertEqual (Http2Mock . content ,
681
+ self .assertEqual (json . loads ( DEFAULT_RESP ) ,
692
682
json .loads (urllib .parse .unquote (response )))
693
683
self .assertEqual (self .decorator .flow , self .decorator ._tls .flow )
694
684
self .assertEqual (self .decorator .credentials ,
@@ -736,7 +726,12 @@ def test_required(self):
736
726
self .assertEqual ('http://localhost/oauth2callback' ,
737
727
query_params ['redirect_uri' ][0 ])
738
728
739
- def test_storage_delete (self ):
729
+ # Check the mocks were called.
730
+ new_http .assert_called_once_with ()
731
+
732
+ @mock .patch ('oauth2client.transport.get_http_object' )
733
+ def test_storage_delete (self , new_http ):
734
+ new_http .return_value = http_mock .HttpMock (data = DEFAULT_RESP )
740
735
# An initial request to an oauth_required decorated path should be a
741
736
# redirect to start the OAuth dance.
742
737
response = self .app .get ('/foo_path' )
@@ -772,7 +767,12 @@ def test_storage_delete(self):
772
767
parse_state_value .assert_called_once_with (
773
768
'foo_path:xsrfkey123' , self .current_user )
774
769
775
- def test_aware (self ):
770
+ # Check the mocks were called.
771
+ new_http .assert_called_once_with ()
772
+
773
+ @mock .patch ('oauth2client.transport.get_http_object' )
774
+ def test_aware (self , new_http ):
775
+ new_http .return_value = http_mock .HttpMock (data = DEFAULT_RESP )
776
776
# An initial request to an oauth_aware decorated path should
777
777
# not redirect.
778
778
response = self .app .get ('http://localhost/bar_path/2012/01' )
@@ -825,6 +825,9 @@ def test_aware(self):
825
825
self .should_raise = False
826
826
self .assertEqual (None , self .decorator .credentials )
827
827
828
+ # Check the mocks were called.
829
+ new_http .assert_called_once_with ()
830
+
828
831
def test_error_in_step2 (self ):
829
832
# An initial request to an oauth_aware decorated path should
830
833
# not redirect.
@@ -855,10 +858,14 @@ def test_kwargs_are_passed_to_underlying_flow(self):
855
858
self .assertEqual (decorator .flow , decorator ._tls .flow )
856
859
857
860
def test_token_response_param (self ):
861
+ # No need to set-up a mock since test_required() does.
858
862
self .decorator ._token_response_param = 'foobar'
859
863
self .test_required ()
860
864
861
- def test_decorator_from_client_secrets (self ):
865
+ @mock .patch ('oauth2client.transport.get_http_object' )
866
+ def test_decorator_from_client_secrets (self , new_http ):
867
+ new_http .return_value = http_mock .HttpMock (data = DEFAULT_RESP )
868
+ # Execute test after setting up mock.
862
869
decorator = appengine .OAuth2DecoratorFromClientSecrets (
863
870
datafile ('client_secrets.json' ),
864
871
scope = ['foo_scope' , 'bar_scope' ])
@@ -877,6 +884,9 @@ def test_decorator_from_client_secrets(self):
877
884
self .assertEqual (self .decorator ._revoke_uri ,
878
885
self .decorator .credentials .revoke_uri )
879
886
887
+ # Check the mocks were called.
888
+ new_http .assert_called_once_with ()
889
+
880
890
def test_decorator_from_client_secrets_toplevel (self ):
881
891
decorator_patch = mock .patch (
882
892
'oauth2client.contrib.appengine.OAuth2DecoratorFromClientSecrets' )
@@ -915,7 +925,7 @@ def test_decorator_from_client_secrets_kwargs(self):
915
925
self .assertIn ('prompt' , decorator ._kwargs )
916
926
917
927
def test_decorator_from_cached_client_secrets (self ):
918
- cache_mock = CacheMock ()
928
+ cache_mock = http_mock . CacheMock ()
919
929
load_and_cache ('client_secrets.json' , 'secret' , cache_mock )
920
930
decorator = appengine .OAuth2DecoratorFromClientSecrets (
921
931
# filename, scope, message=None, cache=None
@@ -991,7 +1001,10 @@ def test_decorator_from_client_secrets_with_optional_settings(self):
991
1001
# This is never set, but it's consistent with other tests.
992
1002
self .assertFalse (decorator ._in_error )
993
1003
994
- def test_invalid_state (self ):
1004
+ @mock .patch ('oauth2client.transport.get_http_object' )
1005
+ def test_invalid_state (self , new_http ):
1006
+ new_http .return_value = http_mock .HttpMock (data = DEFAULT_RESP )
1007
+ # Execute test after setting up mock.
995
1008
with mock .patch .object (appengine , '_parse_state_value' ,
996
1009
return_value = None , autospec = True ):
997
1010
# Now simulate the callback to /oauth2callback.
@@ -1002,6 +1015,9 @@ def test_invalid_state(self):
1002
1015
self .assertEqual ('200 OK' , response .status )
1003
1016
self .assertEqual ('The authorization request failed' , response .body )
1004
1017
1018
+ # Check the mocks were called.
1019
+ new_http .assert_called_once_with ()
1020
+
1005
1021
1006
1022
class DecoratorXsrfSecretTests (unittest2 .TestCase ):
1007
1023
"""Test xsrf_secret_key."""
0 commit comments