Skip to content

Commit

Permalink
TDL-17879 added authentication before discovery mode and unittests (#128
Browse files Browse the repository at this point in the history
)

* added authentication before discovery mode and unittests

* resolved comments

* fixed spelling error

* added schema missing fields

* added comment for pagination test

* added detailed comment

* added ticket it

* fixed circleci errors
  • Loading branch information
namrata270998 authored May 27, 2022
1 parent 6ab28c8 commit acacd4d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
7 changes: 4 additions & 3 deletions tap_stripe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,10 @@ def sync():
def main():
# Parse command line arguments
args = utils.parse_args(REQUIRED_CONFIG_KEYS)
# set the config and state in prior to check the authentication in the discovery mode itself.
Context.config = args.config
Context.state = args.state
configure_stripe_client()

# If discover flag was passed, run discovery mode and dump output to stdout
if args.discover:
Expand All @@ -924,9 +928,6 @@ def main():
else:
Context.catalog = discover()

Context.config = args.config
Context.state = args.state
configure_stripe_client()
validate_dependencies()
try:
sync()
Expand Down
4 changes: 4 additions & 0 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def test_run(self):

incremental_streams = {key for key, value in self.expected_replication_method().items()
if value == self.INCREMENTAL}
# We cannot determine if the child stream is
# returning a page of data due to the duplicacy in the data due to normal parent records
# as well as event updates of the parents. And hence the ticket: https://jira.talendforge.org/browse/TDL-10005
# is a blocker. Hence skipping the child streams from this test.
direct_streams = self.child_streams().union({
# Data is generated automatically for 'balance_transactions' when 'charges' is created
'balance_transactions',
Expand Down
56 changes: 56 additions & 0 deletions tests/unittests/test_auth_in_discovery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from unittest import mock
from tap_stripe import stripe, Context
import tap_stripe
import unittest
import requests
import json

# Mock args
class Args():
def __init__(self):
self.discover = True
self.catalog = False
self.config = {"client_secret": "test_secret", "account_id": "test_account", "start_date": "test_start_date"}
self.state = False

# Mock response
def get_mock_http_response(status_code, content={}):
contents = json.dumps(content)
response = requests.Response()
response.status_code = status_code
response.headers = {}
response._content = contents.encode()
return response

@mock.patch('tap_stripe.utils.parse_args')
class TestBasicAuthInDiscoverMode(unittest.TestCase):
@mock.patch('tap_stripe.discover')
@mock.patch('stripe.http_client.requests.Session.request')
def test_basic_auth_no_access_401(self, mock_request, mocked_discover, mocked_args):
'''
Verify exception is raised for no access(401) error code for authentication through sdk
and discover is called zero times for setup Context.
'''
mock_request.return_value = get_mock_http_response(401, {'error': {'message': 'Invalid API Key provided: test_secret', 'type': 'invalid_request_error'}})
mocked_args.return_value = Args()
try:
tap_stripe.main()
except stripe.error.AuthenticationError as e:
expected_error_message = 'Invalid API Key provided: test_secret'
# Verifying the message formed for the custom exception
self.assertEquals(str(e), expected_error_message)
# Verify that the discover is not called when incorrect credentials are passed
self.assertEqual(mocked_discover.call_count, 0)

@mock.patch('tap_stripe.discover', return_value = {})
@mock.patch('stripe.http_client.requests.Session.request')
def test_basic_auth_access_200(self, mock_retrieve, mocked_discover, mocked_args):
'''
Verify discover mode is called if credentials are valid by setting up the client and calling the sdk function
and discover function is called once for setup Context and discover mode.
'''
mock_retrieve.return_value = get_mock_http_response(200, {"settings":{"dashboard": {"display_name": "Stitch"}}})
mocked_args.return_value = Args()
tap_stripe.main()
# Verify that the discover is called once when correct credentials are passed
self.assertEqual(mocked_discover.call_count, 1)

0 comments on commit acacd4d

Please sign in to comment.