def setUp(self): self.mock_request = self.create_patch( 'yahoo_fantasy.api.OAuth1Service.get_request_token', autospec=True, side_effect=mock_request_token ) self.mock_auth = self.create_patch( 'yahoo_fantasy.api.OAuth1Service.get_auth_session', autospec=True, side_effect=mock_auth_session ) self.api = API('my_key', 'my_secret')
class APITest(unittest.TestCase): def create_patch(self, name, autospec=None, side_effect=None): patcher = patch( name, autospec=autospec, side_effect=side_effect ) mocked = patcher.start() self.addCleanup(patcher.stop) return mocked def setUp(self): self.mock_request = self.create_patch( 'yahoo_fantasy.api.OAuth1Service.get_request_token', autospec=True, side_effect=mock_request_token ) self.mock_auth = self.create_patch( 'yahoo_fantasy.api.OAuth1Service.get_auth_session', autospec=True, side_effect=mock_auth_session ) self.api = API('my_key', 'my_secret') def test_creates_oauth_service(self): self.assertIsInstance(self.api.service, OAuth1Service) self.assertEqual(self.api.service.consumer_key, 'my_key') self.assertEqual(self.api.service.consumer_secret, 'my_secret') def test_refuses_bad_consumer_key(self): with self.assertRaises(AuthenticationError): self.api.service.consumer_key = 'bad_key' self.api.get_request_token() def test_refuses_bad_consumer_secret(self): with self.assertRaises(AuthenticationError): self.api.service.consumer_secret = 'bad_secret' self.api.get_request_token() def test_includes_callback_method(self): self.api.get_request_token() args, kwargs = self.mock_request.call_args self.assertEqual(kwargs['params'], {'oauth_callback': 'oob'}) def test_returns_request_token(self): self.api.get_request_token() self.assertEqual(self.api.request_token, 'request_token') self.assertEqual(self.api.request_secret, 'request_secret') def test_generates_authorize_url(self): url = self.api.get_authorize_url() self.assertEqual( url, 'https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=request_token' ) def test_creates_session_with_pin(self): self.api.get_request_token() self.api.create_session('pin') self.mock_auth.assert_called_with( self.api.service, 'request_token', 'request_secret', method='POST', data={'oauth_verifier': 'pin'} ) self.assertEqual(self.api.pin, 'pin') def test_refuses_bad_pin(self): self.api.get_request_token() with self.assertRaises(AuthenticationError): self.api.create_session('bad_pin')