6262
6363
6464class OAuth1SessionTest (unittest .TestCase ):
65-
66- def setUp (self ):
67- # For python 2.6
68- if not hasattr (self , 'assertIn' ):
69- self .assertIn = lambda a , b : self .assertTrue (a in b )
70-
7165 def test_signature_types (self ):
7266 def verify_signature (getter ):
7367 def fake_send (r , ** kwargs ):
@@ -204,14 +198,9 @@ def _test_fetch_access_token_raises_error(self, auth):
204198 passed in to the client.
205199 """
206200 auth .send = self .fake_body ('oauth_token=foo' )
207-
208- # Use a try-except block so that we can assert on the exception message
209- # being raised and also keep the Python2.6 compatibility where
210- # assertRaises is not a context manager.
211- try :
201+ with self .assertRaises (ValueError ) as cm :
212202 auth .fetch_access_token ('https://example.com/token' )
213- except ValueError as exc :
214- self .assertEqual ('No client verifier has been set.' , str (exc ))
203+ self .assertEqual ('No client verifier has been set.' , str (cm .exception ))
215204
216205 def test_fetch_token_invalid_response (self ):
217206 auth = OAuth1Session ('foo' )
@@ -221,15 +210,10 @@ def test_fetch_token_invalid_response(self):
221210
222211 for code in (400 , 401 , 403 ):
223212 auth .send = self .fake_body ('valid=response' , code )
224- # use try/catch rather than self.assertRaises, so we can
225- # assert on the properties of the exception
226- try :
213+ with self .assertRaises (ValueError ) as cm :
227214 auth .fetch_request_token ('https://example.com/token' )
228- except ValueError as err :
229- self .assertEqual (err .status_code , code )
230- self .assertTrue (isinstance (err .response , requests .Response ))
231- else : # no exception raised
232- self .fail ("ValueError not raised" )
215+ self .assertEqual (cm .exception .status_code , code )
216+ self .assertIsInstance (cm .exception .response , requests .Response )
233217
234218 def test_fetch_access_token_missing_verifier (self ):
235219 self ._test_fetch_access_token_raises_error (OAuth1Session ('foo' ))
0 commit comments