def test_handle_rate_limit(self): client_id = 'abc123' client_secret = '123abc' code = 'hellosecret' context = 'stores/abc' scope = 'store_v2_products' redirect_uri = 'http://localhost/callback' result = {'access_token': '12345abcdef'} connection = OAuthConnection( client_id, store_hash='abc', rate_limiting_management={ 'wait': True, 'autoretry': True } ) connection._run_method = MagicMock() connection._run_method.return_value = MagicMock( status_code=429, reason='foo', headers={ 'X-Rate-Limit-Time-Reset-Ms': '300', 'X-Rate-Limit-Time-Window-Ms': '5000', 'X-Rate-Limit-Requests-Left': '6', 'X-Rate-Limit-Requests-Quota': '25' }, content='' ) with self.assertRaises(RateLimitingException ): connection.make_request('POST', 'wathever') self.assertEqual(connection._run_method.call_count, 2)
def test_fetch_token(self): client_id = 'abc123' client_secret = '123abc' code = 'hellosecret' context = 'stores/abc' scope = 'store_v2_products' redirect_uri = 'http://localhost/callback' result = {'access_token': '12345abcdef'} with patch('bigcommerce.connection.OAuthConnection') as mock: connection = OAuthConnection(client_id, store_hash='abc') connection.post = MagicMock() connection.post.return_value = result res = connection.fetch_token(client_secret, code, context, scope, redirect_uri) self.assertEqual(res, result) self.assertDictEqual(connection._session.headers, {'X-Auth-Client': 'abc123', 'X-Auth-Token': '12345abcdef', 'Accept': 'application/json'}) connection.post.assert_called_once_with('https://login.bigcommerce.com/oauth2/token', { 'client_id': client_id, 'client_secret': client_secret, 'code': code, 'context': context, 'scope': scope, 'grant_type': 'authorization_code', 'redirect_uri': redirect_uri }, headers={'Content-Type': 'application/x-www-form-urlencoded'} )
def test_fetch_token(self): client_id = 'abc123' client_secret = '123abc' code = 'hellosecret' context = 'stores/abc' scope = 'store_v2_products' redirect_uri = 'http://localhost/callback' result = {'access_token': '12345abcdef'} connection = OAuthConnection(client_id, store_hash='abc') connection.post = MagicMock() connection.post.return_value = result res = connection.fetch_token(client_secret, code, context, scope, redirect_uri) self.assertEqual(res, result) self.assertDictEqual(connection._session.headers, {'X-Auth-Client': 'abc123', 'X-Auth-Token': '12345abcdef', 'Accept': 'application/json', 'Accept-Encoding': 'gzip'}) connection.post.assert_called_once_with('https://login.bigcommerce.com/oauth2/token', { 'client_id': client_id, 'client_secret': client_secret, 'code': code, 'context': context, 'scope': scope, 'grant_type': 'authorization_code', 'redirect_uri': redirect_uri }, headers={'Content-Type': 'application/x-www-form-urlencoded'} )
def test_verify_payload(self): """Decode and verify signed payload.""" payload = "eyJ1c2VyIjp7ImlkIjo3MiwiZW1haWwiOiJqYWNraWUuaHV5bmh" \ "AYmlnY29tbWVyY2UuY29tIn0sInN0b3JlX2hhc2giOiJsY3R2aD" \ "V3bSIsInRpbWVzdGFtcCI6MTM4OTA1MDMyNy42NTc5NjI2fQ==." \ "ZTViYzAzNTM2MGFjM2M2YTVkZjFmNzFlYTk4NTY1ODZiMzkxODZmZDExZTdjZGFmOGEzN2E3YTEzNGQ0MmExYw==" client_secret = 'ntb1kcxa1do55wf0h25ps7h94fnsoi6' user_data = OAuthConnection.verify_payload(payload, client_secret) self.assertTrue(user_data) # otherwise verification has failed self.assertEqual(user_data['user']['id'], 72) self.assertEqual(user_data['user']['email'], "*****@*****.**") # Try again with a fake payload payload = "notevenreal7ImlkIjo3MiwiZW1haWwiOiJqYWNraWUuaHV5bmh" \ "AYmlnY29tbWVyY2UuY29tIn0sInN0b3JlX2hhc2giOiJsY3R2aD" \ "V3bSIsInRpbWVzdGFtcCI6MTM4OTA1MDMyNy42NTc5NjI2fQ==." \ "quitefakeTM2MGFjM2M2YTVkZjFmNzFlYTk4NTY1ODZiMzkxODZmZDExZTdjZGFmOGEzN2E3YTEzNGQ0MmExYw==" user_data = OAuthConnection.verify_payload(payload, client_secret) self.assertFalse(user_data)
def test_fetch_token(self): client_id = "abc123" client_secret = "123abc" code = "hellosecret" context = "stores/abc" scope = "store_v2_products" redirect_uri = "http://localhost/callback" result = {"access_token": "12345abcdef"} with patch("bigcommerce.connection.OAuthConnection") as mock: connection = OAuthConnection(client_id, store_hash="abc") connection.post = MagicMock() connection.post.return_value = result res = connection.fetch_token(client_secret, code, context, scope, redirect_uri) self.assertEqual(res, result) self.assertDictEqual( connection._session.headers, { "X-Auth-Client": "abc123", "X-Auth-Token": "12345abcdef", "Accept": "application/json", "Accept-Encoding": "gzip", }, ) connection.post.assert_called_once_with( "https://login.bigcommerce.com/oauth2/token", { "client_id": client_id, "client_secret": client_secret, "code": code, "context": context, "scope": scope, "grant_type": "authorization_code", "redirect_uri": redirect_uri, }, headers={"Content-Type": "application/x-www-form-urlencoded"}, )
def test_alternate_api_endpoint(self): connection = OAuthConnection(client_id='123', store_hash='abcdef', host='barbaz.com') self.assertEqual(connection.full_path('time'), 'https://barbaz.com/stores/abcdef/v2/time')
def test_full_path(self): connection = OAuthConnection(client_id='123', store_hash='abcdef') self.assertEqual(connection.full_path('time'), 'https://api.bigcommerce.com/stores/abcdef/v2/time')
def test_alternate_api_endpoint(self): connection = OAuthConnection(client_id="123", store_hash="abcdef", host="barbaz.com") self.assertEqual(connection.full_path("time"), "https://barbaz.com/stores/abcdef/v2/time")
def test_full_path(self): connection = OAuthConnection(client_id="123", store_hash="abcdef") self.assertEqual(connection.full_path("time"), "https://api.bigcommerce.com/stores/abcdef/v2/time")