33
33
34
34
import dev_appserver
35
35
dev_appserver .fix_sys_path ()
36
- import mox
36
+ import mock
37
37
import webapp2
38
38
39
39
from google .appengine .api import apiproxy_stub
@@ -159,13 +159,9 @@ def test_raise_correct_type_of_exception(self):
159
159
'memcache' , memcache_stub .MemcacheServiceStub ())
160
160
161
161
scope = 'http://www.googleapis.com/scope'
162
- try :
163
- credentials = AppAssertionCredentials (scope )
164
- http = httplib2 .Http ()
165
- credentials .refresh (http )
166
- self .fail ('Should have raised an AccessTokenRefreshError' )
167
- except AccessTokenRefreshError :
168
- pass
162
+ credentials = AppAssertionCredentials (scope )
163
+ http = httplib2 .Http ()
164
+ self .assertRaises (AccessTokenRefreshError , credentials .refresh , http )
169
165
170
166
def test_get_access_token_on_refresh (self ):
171
167
app_identity_stub = self .AppIdentityStubImpl ()
@@ -201,19 +197,19 @@ def test_get_access_token_on_refresh(self):
201
197
def test_custom_service_account (self ):
202
198
scope = "http://www.googleapis.com/scope"
203
199
204
- m = mox .Mox ()
205
- m .StubOutWithMock (app_identity , 'get_access_token' )
206
- app_identity .get_access_token (
207
- [scope ], service_account_id = account_id ).AndReturn (('a_token_456' , None ))
208
- m .ReplayAll ()
209
200
210
- credentials = AppAssertionCredentials (scope , service_account_id = account_id )
211
- http = httplib2 .Http ()
212
- credentials .refresh (http )
213
- m .VerifyAll ()
214
- m .UnsetStubs ()
215
- self .assertEqual ('a_token_456' , credentials .access_token )
216
- self .assertEqual (scope , credentials .scope )
201
+ with mock .patch .object (app_identity , 'get_access_token' ,
202
+ return_value = ('a_token_456' , None ),
203
+ autospec = True ) as get_access_token :
204
+ credentials = AppAssertionCredentials (
205
+ scope , service_account_id = account_id )
206
+ http = httplib2 .Http ()
207
+ credentials .refresh (http )
208
+
209
+ self .assertEqual ('a_token_456' , credentials .access_token )
210
+ self .assertEqual (scope , credentials .scope )
211
+ get_access_token .assert_called_once_with (
212
+ [scope ], service_account_id = account_id )
217
213
218
214
def test_create_scoped_required_without_scopes (self ):
219
215
credentials = AppAssertionCredentials ([])
@@ -538,7 +534,8 @@ def get(self, *args, **kwargs):
538
534
'wsgi.url_scheme' : 'http' ,
539
535
'HTTP_HOST' : 'localhost' ,
540
536
})
541
- users .get_current_user = user_mock ()
537
+ self .current_user = user_mock ()
538
+ users .get_current_user = self .current_user
542
539
self .httplib2_orig = httplib2 .Http
543
540
httplib2 .Http = Http2Mock
544
541
@@ -562,30 +559,28 @@ def test_required(self):
562
559
self .assertEqual ('code' , q ['response_type' ][0 ])
563
560
self .assertEqual (False , self .decorator .has_credentials ())
564
561
565
- m = mox .Mox ()
566
- m .StubOutWithMock (appengine , '_parse_state_value' )
567
- appengine ._parse_state_value ('foo_path:xsrfkey123' ,
568
- mox .IgnoreArg ()).AndReturn ('foo_path' )
569
- m .ReplayAll ()
570
-
571
- # Now simulate the callback to /oauth2callback.
572
- response = self .app .get ('/oauth2callback' , {
573
- 'code' : 'foo_access_code' ,
574
- 'state' : 'foo_path:xsrfkey123' ,
575
- })
576
- parts = response .headers ['Location' ].split ('?' , 1 )
577
- self .assertEqual ('http://localhost/foo_path' , parts [0 ])
578
- self .assertEqual (None , self .decorator .credentials )
579
- if self .decorator ._token_response_param :
580
- response = urlparse .parse_qs (
581
- parts [1 ])[self .decorator ._token_response_param ][0 ]
582
- self .assertEqual (Http2Mock .content , json .loads (urllib .unquote (response )))
583
- self .assertEqual (self .decorator .flow , self .decorator ._tls .flow )
584
- self .assertEqual (self .decorator .credentials ,
585
- self .decorator ._tls .credentials )
586
-
587
- m .UnsetStubs ()
588
- m .VerifyAll ()
562
+ with mock .patch .object (appengine , '_parse_state_value' ,
563
+ return_value = 'foo_path' ,
564
+ autospec = True ) as parse_state_value :
565
+ # Now simulate the callback to /oauth2callback.
566
+ response = self .app .get ('/oauth2callback' , {
567
+ 'code' : 'foo_access_code' ,
568
+ 'state' : 'foo_path:xsrfkey123' ,
569
+ })
570
+ parts = response .headers ['Location' ].split ('?' , 1 )
571
+ self .assertEqual ('http://localhost/foo_path' , parts [0 ])
572
+ self .assertEqual (None , self .decorator .credentials )
573
+ if self .decorator ._token_response_param :
574
+ response_query = urlparse .parse_qs (parts [1 ])
575
+ response = response_query [self .decorator ._token_response_param ][0 ]
576
+ self .assertEqual (Http2Mock .content ,
577
+ json .loads (urllib .unquote (response )))
578
+ self .assertEqual (self .decorator .flow , self .decorator ._tls .flow )
579
+ self .assertEqual (self .decorator .credentials ,
580
+ self .decorator ._tls .credentials )
581
+
582
+ parse_state_value .assert_called_once_with (
583
+ 'foo_path:xsrfkey123' , self .current_user )
589
584
590
585
# Now requesting the decorated path should work.
591
586
response = self .app .get ('/foo_path' )
@@ -599,13 +594,9 @@ def test_required(self):
599
594
600
595
# Raising an exception still clears the Credentials.
601
596
self .should_raise = True
602
- try :
603
- response = self .app .get ('/foo_path' )
604
- self .fail ('Should have raised an exception.' )
605
- except Exception :
606
- pass
607
- self .assertEqual (None , self .decorator .credentials )
597
+ self .assertRaises (Exception , self .app .get , '/foo_path' )
608
598
self .should_raise = False
599
+ self .assertEqual (None , self .decorator .credentials )
609
600
610
601
# Invalidate the stored Credentials.
611
602
self .found_credentials .invalid = True
@@ -623,37 +614,34 @@ def test_storage_delete(self):
623
614
response = self .app .get ('/foo_path' )
624
615
self .assertTrue (response .status .startswith ('302' ))
625
616
626
- m = mox .Mox ()
627
- m .StubOutWithMock (appengine , '_parse_state_value' )
628
- appengine ._parse_state_value ('foo_path:xsrfkey123' ,
629
- mox .IgnoreArg ()).AndReturn ('foo_path' )
630
- m .ReplayAll ()
631
-
632
- # Now simulate the callback to /oauth2callback.
633
- response = self .app .get ('/oauth2callback' , {
634
- 'code' : 'foo_access_code' ,
635
- 'state' : 'foo_path:xsrfkey123' ,
636
- })
637
- self .assertEqual ('http://localhost/foo_path' , response .headers ['Location' ])
638
- self .assertEqual (None , self .decorator .credentials )
639
-
640
- # Now requesting the decorated path should work.
641
- response = self .app .get ('/foo_path' )
617
+ with mock .patch .object (appengine , '_parse_state_value' ,
618
+ return_value = 'foo_path' ,
619
+ autospec = True ) as parse_state_value :
620
+ # Now simulate the callback to /oauth2callback.
621
+ response = self .app .get ('/oauth2callback' , {
622
+ 'code' : 'foo_access_code' ,
623
+ 'state' : 'foo_path:xsrfkey123' ,
624
+ })
625
+ self .assertEqual ('http://localhost/foo_path' , response .headers ['Location' ])
626
+ self .assertEqual (None , self .decorator .credentials )
627
+
628
+ # Now requesting the decorated path should work.
629
+ response = self .app .get ('/foo_path' )
642
630
643
- self .assertTrue (self .had_credentials )
631
+ self .assertTrue (self .had_credentials )
644
632
645
- # Credentials should be cleared after each call.
646
- self .assertEqual (None , self .decorator .credentials )
633
+ # Credentials should be cleared after each call.
634
+ self .assertEqual (None , self .decorator .credentials )
647
635
648
- # Invalidate the stored Credentials.
649
- self .found_credentials .store .delete ()
636
+ # Invalidate the stored Credentials.
637
+ self .found_credentials .store .delete ()
650
638
651
- # Invalid Credentials should start the OAuth dance again.
652
- response = self .app .get ('/foo_path' )
653
- self .assertTrue (response .status .startswith ('302' ))
639
+ # Invalid Credentials should start the OAuth dance again.
640
+ response = self .app .get ('/foo_path' )
641
+ self .assertTrue (response .status .startswith ('302' ))
654
642
655
- m . UnsetStubs ()
656
- m . VerifyAll ( )
643
+ parse_state_value . assert_called_once_with (
644
+ 'foo_path:xsrfkey123' , self . current_user )
657
645
658
646
def test_aware (self ):
659
647
# An initial request to an oauth_aware decorated path should not redirect.
@@ -670,23 +658,20 @@ def test_aware(self):
670
658
q ['state' ][0 ].rsplit (':' , 1 )[0 ])
671
659
self .assertEqual ('code' , q ['response_type' ][0 ])
672
660
673
- m = mox .Mox ()
674
- m .StubOutWithMock (appengine , '_parse_state_value' )
675
- appengine ._parse_state_value ('bar_path:xsrfkey456' ,
676
- mox .IgnoreArg ()).AndReturn ('bar_path' )
677
- m .ReplayAll ()
678
-
679
- # Now simulate the callback to /oauth2callback.
680
- url = self .decorator .authorize_url ()
681
- response = self .app .get ('/oauth2callback' , {
682
- 'code' : 'foo_access_code' ,
683
- 'state' : 'bar_path:xsrfkey456' ,
684
- })
685
- self .assertEqual ('http://localhost/bar_path' , response .headers ['Location' ])
686
- self .assertEqual (False , self .decorator .has_credentials ())
687
-
688
- m .UnsetStubs ()
689
- m .VerifyAll ()
661
+ with mock .patch .object (appengine , '_parse_state_value' ,
662
+ return_value = 'bar_path' ,
663
+ autospec = True ) as parse_state_value :
664
+ # Now simulate the callback to /oauth2callback.
665
+ url = self .decorator .authorize_url ()
666
+ response = self .app .get ('/oauth2callback' , {
667
+ 'code' : 'foo_access_code' ,
668
+ 'state' : 'bar_path:xsrfkey456' ,
669
+ })
670
+
671
+ self .assertEqual ('http://localhost/bar_path' , response .headers ['Location' ])
672
+ self .assertEqual (False , self .decorator .has_credentials ())
673
+ parse_state_value .assert_called_once_with (
674
+ 'bar_path:xsrfkey456' , self .current_user )
690
675
691
676
# Now requesting the decorated path will have credentials.
692
677
response = self .app .get ('/bar_path/2012/01' )
@@ -703,13 +688,9 @@ def test_aware(self):
703
688
704
689
# Raising an exception still clears the Credentials.
705
690
self .should_raise = True
706
- try :
707
- response = self .app .get ('/bar_path/2012/01' )
708
- self .fail ('Should have raised an exception.' )
709
- except Exception :
710
- pass
711
- self .assertEqual (None , self .decorator .credentials )
691
+ self .assertRaises (Exception , self .app .get , '/bar_path/2012/01' )
712
692
self .should_raise = False
693
+ self .assertEqual (None , self .decorator .credentials )
713
694
714
695
715
696
def test_error_in_step2 (self ):
0 commit comments