diff --git a/.circleci/config.yml b/.circleci/config.yml index 0cf1a65..7430186 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -14,12 +14,19 @@ jobs: virtualenv -p python3 /usr/local/share/virtualenvs/tap-zendesk source /usr/local/share/virtualenvs/tap-zendesk/bin/activate pip install .[test] + pip install coverage - run: name: 'pylint' command: | source /usr/local/share/virtualenvs/tap-zendesk/bin/activate - make test + pylint tap_zendesk -d missing-docstring,invalid-name,line-too-long,too-many-locals,too-few-public-methods,fixme,stop-iteration-return,too-many-branches,useless-import-alias,no-else-return,logging-not-lazy + nosetests --with-coverage --cover-erase --cover-package=tap_zendesk --cover-html-dir=htmlcov test/unittests + coverage html - add_ssh_keys + - store_test_results: + path: test_output/report.xml + - store_artifacts: + path: htmlcov - run: name: 'Integration Tests' command: | diff --git a/tap_zendesk/__init__.py b/tap_zendesk/__init__.py index 6982a0c..048ee55 100755 --- a/tap_zendesk/__init__.py +++ b/tap_zendesk/__init__.py @@ -15,6 +15,7 @@ LOGGER = singer.get_logger() + REQUIRED_CONFIG_KEYS = [ "start_date", "subdomain", @@ -46,9 +47,9 @@ def request_metrics_patch(self, method, url, **kwargs): Session.request = request_metrics_patch # end patch -def do_discover(client): +def do_discover(client, config): LOGGER.info("Starting discover") - catalog = {"streams": discover_streams(client)} + catalog = {"streams": discover_streams(client, config)} json.dump(catalog, sys.stdout, indent=2) LOGGER.info("Finished discover") @@ -199,7 +200,8 @@ def main(): LOGGER.error("""No suitable authentication keys provided.""") if parsed_args.discover: - do_discover(client) + # passing the config to check the authentication in the do_discover method + do_discover(client, parsed_args.config) elif parsed_args.catalog: state = parsed_args.state do_sync(client, parsed_args.catalog, state, parsed_args.config) diff --git a/tap_zendesk/discover.py b/tap_zendesk/discover.py index 0bac705..99f31b0 100644 --- a/tap_zendesk/discover.py +++ b/tap_zendesk/discover.py @@ -1,7 +1,11 @@ import os import json import singer +import zenpy from tap_zendesk.streams import STREAMS +from tap_zendesk.http import ZendeskForbiddenError + +LOGGER = singer.get_logger() def get_abs_path(path): return os.path.join(os.path.dirname(os.path.realpath(__file__)), path) @@ -20,12 +24,44 @@ def load_shared_schema_refs(): return shared_schema_refs -def discover_streams(client): +def discover_streams(client, config): streams = [] + error_list = [] refs = load_shared_schema_refs() - for s in STREAMS.values(): - s = s(client) - schema = singer.resolve_schema_references(s.load_schema(), refs) - streams.append({'stream': s.name, 'tap_stream_id': s.name, 'schema': schema, 'metadata': s.load_metadata()}) + + for stream in STREAMS.values(): + # for each stream in the `STREAMS` check if the user has the permission to access the data of that stream + stream = stream(client, config) + schema = singer.resolve_schema_references(stream.load_schema(), refs) + try: + # Here it call the check_access method to check whether stream have read permission or not. + # If stream does not have read permission then append that stream name to list and at the end of all streams + # raise forbidden error with proper message containing stream names. + stream.check_access() + except ZendeskForbiddenError as e: + error_list.append(stream.name) # Append stream name to the error_list + except zenpy.lib.exception.APIException as e: + args0 = json.loads(e.args[0]) + err = args0.get('error') + + # check if the error is of type dictionary and the message retrieved from the dictionary + # is the expected message. If so, only then print the logger message and return the schema + if isinstance(err, dict): + if err.get('message', None) == "You do not have access to this page. Please contact the account owner of this help desk for further help.": + error_list.append(stream.name) + elif args0.get('description') == "You are missing the following required scopes: read": + error_list.append(stream.name) + else: + raise e from None # raise error if it is other than 403 forbidden error + + streams.append({'stream': stream.name, 'tap_stream_id': stream.name, 'schema': schema, 'metadata': stream.load_metadata()}) + + if error_list: + streams_name = ", ".join(error_list) + message = "HTTP-error-code: 403, Error: You are missing the following required scopes: read. "\ + "The account credentials supplied do not have read access for the following stream(s): {}".format(streams_name) + raise ZendeskForbiddenError(message) + + return streams diff --git a/tap_zendesk/http.py b/tap_zendesk/http.py index 469e060..b4d2b4a 100644 --- a/tap_zendesk/http.py +++ b/tap_zendesk/http.py @@ -2,32 +2,154 @@ import backoff import requests import singer +from requests.exceptions import Timeout, HTTPError + LOGGER = singer.get_logger() +class ZendeskError(Exception): + def __init__(self, message=None, response=None): + super().__init__(message) + self.message = message + self.response = response + +class ZendeskBackoffError(ZendeskError): + pass + +class ZendeskBadRequestError(ZendeskError): + pass + +class ZendeskUnauthorizedError(ZendeskError): + pass + +class ZendeskForbiddenError(ZendeskError): + pass + +class ZendeskNotFoundError(ZendeskError): + pass + +class ZendeskConflictError(ZendeskError): + pass + +class ZendeskUnprocessableEntityError(ZendeskError): + pass + +class ZendeskRateLimitError(ZendeskBackoffError): + pass + +class ZendeskInternalServerError(ZendeskBackoffError): + pass + +class ZendeskNotImplementedError(ZendeskBackoffError): + pass + +class ZendeskBadGatewayError(ZendeskBackoffError): + pass + +class ZendeskServiceUnavailableError(ZendeskBackoffError): + pass + +ERROR_CODE_EXCEPTION_MAPPING = { + 400: { + "raise_exception": ZendeskBadRequestError, + "message": "A validation exception has occurred." + }, + 401: { + "raise_exception": ZendeskUnauthorizedError, + "message": "The access token provided is expired, revoked, malformed or invalid for other reasons." + }, + 403: { + "raise_exception": ZendeskForbiddenError, + "message": "You are missing the following required scopes: read" + }, + 404: { + "raise_exception": ZendeskNotFoundError, + "message": "The resource you have specified cannot be found." + }, + 409: { + "raise_exception": ZendeskConflictError, + "message": "The API request cannot be completed because the requested operation would conflict with an existing item." + }, + 422: { + "raise_exception": ZendeskUnprocessableEntityError, + "message": "The request content itself is not processable by the server." + }, + 429: { + "raise_exception": ZendeskRateLimitError, + "message": "The API rate limit for your organisation/application pairing has been exceeded." + }, + 500: { + "raise_exception": ZendeskInternalServerError, + "message": "The server encountered an unexpected condition which prevented" \ + " it from fulfilling the request." + }, + 501: { + "raise_exception": ZendeskNotImplementedError, + "message": "The server does not support the functionality required to fulfill the request." + }, + 502: { + "raise_exception": ZendeskBadGatewayError, + "message": "Server received an invalid response." + }, + 503: { + "raise_exception": ZendeskServiceUnavailableError, + "message": "API service is currently unavailable." + } +} def is_fatal(exception): status_code = exception.response.status_code - if status_code == 429: - sleep_time = int(exception.response.headers['Retry-After']) - LOGGER.info("Caught HTTP 429, retrying request in %s seconds", sleep_time) - sleep(sleep_time) - return False - - return 400 <= status_code < 500 + if status_code in [429, 503]: + # If status_code is 429 or 503 then checking whether response header has 'Retry-After' attribute or not. + # If response header has 'Retry-After' attribute then retry the error otherwise raise the error directly. + retry_after = exception.response.headers.get('Retry-After') + if retry_after: + sleep_time = int(retry_after) + LOGGER.info("Caught HTTP %s, retrying request in %s seconds", status_code, sleep_time) + sleep(sleep_time) + return False + else: + return True + + return 400 <=status_code < 500 + +def raise_for_error(response): + """ Error handling method which throws custom error. Class for each error defined above which extends `ZendeskError`. + This method map the status code with `ERROR_CODE_EXCEPTION_MAPPING` dictionary and accordingly raise the error. + If status_code is 200 then simply return json response. + """ + try: + response_json = response.json() + except Exception: # pylint: disable=broad-except + response_json = {} + if response.status_code != 200: + if response_json.get('error'): + message = "HTTP-error-code: {}, Error: {}".format(response.status_code, response_json.get('error')) + else: + message = "HTTP-error-code: {}, Error: {}".format( + response.status_code, + response_json.get("message", ERROR_CODE_EXCEPTION_MAPPING.get( + response.status_code, {}).get("message", "Unknown Error"))) + exc = ERROR_CODE_EXCEPTION_MAPPING.get( + response.status_code, {}).get("raise_exception", ZendeskError) + raise exc(message, response) from None @backoff.on_exception(backoff.expo, - requests.exceptions.HTTPError, + (HTTPError, ZendeskBackoffError), max_tries=10, giveup=is_fatal) -def call_api(url, params, headers): - response = requests.get(url, params=params, headers=headers) - response.raise_for_status() +@backoff.on_exception(backoff.expo, + (ConnectionError, Timeout),#As ConnectionError error and timeout error does not have attribute status_code, + max_tries=10, # here we added another backoff expression. + factor=2) +def call_api(url, request_timeout, params, headers): + response = requests.get(url, params=params, headers=headers, timeout=request_timeout) # Pass request timeout + raise_for_error(response) return response -def get_cursor_based(url, access_token, cursor=None, **kwargs): +def get_cursor_based(url, access_token, request_timeout, cursor=None, **kwargs): headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', @@ -43,7 +165,7 @@ def get_cursor_based(url, access_token, cursor=None, **kwargs): if cursor: params['page[after]'] = cursor - response = call_api(url, params=params, headers=headers) + response = call_api(url, request_timeout, params=params, headers=headers) response_json = response.json() yield response_json @@ -54,13 +176,13 @@ def get_cursor_based(url, access_token, cursor=None, **kwargs): cursor = response_json['meta']['after_cursor'] params['page[after]'] = cursor - response = call_api(url, params=params, headers=headers) + response = call_api(url, request_timeout, params=params, headers=headers) response_json = response.json() yield response_json has_more = response_json['meta']['has_more'] -def get_offset_based(url, access_token, **kwargs): +def get_offset_based(url, access_token, request_timeout, **kwargs): headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', @@ -73,7 +195,7 @@ def get_offset_based(url, access_token, **kwargs): **kwargs.get('params', {}) } - response = call_api(url, params=params, headers=headers) + response = call_api(url, request_timeout, params=params, headers=headers) response_json = response.json() yield response_json @@ -81,13 +203,13 @@ def get_offset_based(url, access_token, **kwargs): next_url = response_json.get('next_page') while next_url: - response = call_api(next_url, params=None, headers=headers) + response = call_api(next_url, request_timeout, params=None, headers=headers) response_json = response.json() yield response_json next_url = response_json.get('next_page') -def get_incremental_export(url, access_token, start_time): +def get_incremental_export(url, access_token, request_timeout, start_time): headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', @@ -96,7 +218,7 @@ def get_incremental_export(url, access_token, start_time): params = {'start_time': start_time.timestamp()} - response = call_api(url, params=params, headers=headers) + response = call_api(url, request_timeout, params=params, headers=headers) response_json = response.json() yield response_json @@ -107,8 +229,13 @@ def get_incremental_export(url, access_token, start_time): cursor = response_json['after_cursor'] params = {'cursor': cursor} - response = requests.get(url, params=params, headers=headers) - response.raise_for_status() + # Replaced below line of code with call_api method + # response = requests.get(url, params=params, headers=headers) + # response.raise_for_status() + # Because it doing the same as call_api. So, now error handling will work properly with backoff + # as earlier backoff was not possible + response = call_api(url, request_timeout, params=params, headers=headers) + response_json = response.json() yield response_json diff --git a/tap_zendesk/streams.py b/tap_zendesk/streams.py index c707d51..3476899 100644 --- a/tap_zendesk/streams.py +++ b/tap_zendesk/streams.py @@ -4,7 +4,6 @@ import time import pytz import zenpy -from requests.exceptions import HTTPError import singer from singer import metadata from singer import utils @@ -16,6 +15,12 @@ LOGGER = singer.get_logger() KEY_PROPERTIES = ['id'] +REQUEST_TIMEOUT = 300 +START_DATE_FORMAT = "%Y-%m-%dT%H:%M:%SZ" +HEADERS = { + 'Content-Type': 'application/json', + 'Accept': 'application/json', +} CUSTOM_TYPES = { 'text': 'string', 'textarea': 'string', @@ -59,10 +64,18 @@ class Stream(): replication_key = None key_properties = KEY_PROPERTIES stream = None + endpoint = None + request_timeout = None def __init__(self, client=None, config=None): self.client = client self.config = config + # Set and pass request timeout to config param `request_timeout` value. + config_request_timeout = self.config.get('request_timeout') + if config_request_timeout and float(config_request_timeout): + self.request_timeout = float(config_request_timeout) + else: + self.request_timeout = REQUEST_TIMEOUT # If value is 0,"0","" or not passed then it set default to 300 seconds. def get_bookmark(self, state): return utils.strptime_with_tz(singer.get_bookmark(state, self.name, self.replication_key)) @@ -103,6 +116,15 @@ def load_metadata(self): def is_selected(self): return self.stream is not None + def check_access(self): + ''' + Check whether the permission was given to access stream resources or not. + ''' + url = self.endpoint.format(self.config['subdomain']) + HEADERS['Authorization'] = 'Bearer {}'.format(self.config["access_token"]) + + http.call_api(url, self.request_timeout, params={'per_page': 1}, headers=HEADERS) + class CursorBasedStream(Stream): item_key = None endpoint = None @@ -112,8 +134,8 @@ def get_objects(self, **kwargs): Cursor based object retrieval ''' url = self.endpoint.format(self.config['subdomain']) - - for page in http.get_cursor_based(url, self.config['access_token'], **kwargs): + # Pass `request_timeout` parameter + for page in http.get_cursor_based(url, self.config['access_token'], self.request_timeout, **kwargs): yield from page[self.item_key] class CursorBasedExportStream(Stream): @@ -125,8 +147,8 @@ def get_objects(self, start_time): Retrieve objects from the incremental exports endpoint using cursor based pagination ''' url = self.endpoint.format(self.config['subdomain']) - - for page in http.get_incremental_export(url, self.config['access_token'], start_time): + # Pass `request_timeout` parameter + for page in http.get_incremental_export(url, self.config['access_token'], self.request_timeout, start_time): yield from page[self.item_key] @@ -137,6 +159,11 @@ def raise_or_log_zenpy_apiexception(schema, stream, e): # it doesn't have access. if not isinstance(e, zenpy.lib.exception.APIException): raise ValueError("Called with a bad exception type") from e + #If read permission is not available in OAuth access_token, then it returns the below error. + if json.loads(e.args[0]).get('description') == "You are missing the following required scopes: read": + LOGGER.warning("The account credentials supplied do not have access to `%s` custom fields.", + stream) + return schema if json.loads(e.args[0])['error']['message'] == "You do not have access to this page. Please contact the account owner of this help desk for further help.": LOGGER.warning("The account credentials supplied do not have access to `%s` custom fields.", stream) @@ -174,6 +201,14 @@ def sync(self, state): self.update_bookmark(state, organization.updated_at) yield (self.stream, organization) + def check_access(self): + ''' + Check whether the permission was given to access stream resources or not. + ''' + # Convert datetime object to standard format with timezone. Used utcnow to reduce API call burden at discovery time. + # Because API will return records from now which will be very less + start_time = datetime.datetime.utcnow().strftime(START_DATE_FORMAT) + self.client.organizations.incremental(start_time=start_time) class Users(Stream): name = "users" @@ -251,6 +286,14 @@ def sync(self, state): start = end - datetime.timedelta(seconds=1) end = start + datetime.timedelta(seconds=search_window_size) + def check_access(self): + ''' + Check whether the permission was given to access stream resources or not. + ''' + # Convert datetime object to standard format with timezone. Used utcnow to reduce API call burden at discovery time. + # Because API will return records from now which will be very less + start_time = datetime.datetime.utcnow().strftime(START_DATE_FORMAT) + self.client.search("", updated_after=start_time, updated_before='2000-01-02T00:00:00Z', type="user") class Tickets(CursorBasedExportStream): name = "tickets" @@ -259,32 +302,10 @@ class Tickets(CursorBasedExportStream): item_key = "tickets" endpoint = "https://{}.zendesk.com/api/v2/incremental/tickets/cursor.json" - last_record_emit = {} - buf = {} - buf_time = 60 - def _buffer_record(self, record): - stream_name = record[0].tap_stream_id - if self.last_record_emit.get(stream_name) is None: - self.last_record_emit[stream_name] = utils.now() - - if self.buf.get(stream_name) is None: - self.buf[stream_name] = [] - self.buf[stream_name].append(record) - - if (utils.now() - self.last_record_emit[stream_name]).total_seconds() > self.buf_time: - self.last_record_emit[stream_name] = utils.now() - return True - - return False - - def _empty_buffer(self): - for stream_name, stream_buf in self.buf.items(): - for rec in stream_buf: - yield rec - self.buf[stream_name] = [] - def sync(self, state): #pylint: disable=too-many-statements + bookmark = self.get_bookmark(state) + tickets = self.get_objects(bookmark) audits_stream = TicketAudits(self.client, self.config) @@ -304,59 +325,65 @@ def emit_sub_stream_metrics(sub_stream): for ticket in tickets: zendesk_metrics.capture('ticket') + generated_timestamp_dt = datetime.datetime.utcfromtimestamp(ticket.get('generated_timestamp')).replace(tzinfo=pytz.UTC) + self.update_bookmark(state, utils.strftime(generated_timestamp_dt)) ticket.pop('fields') # NB: Fields is a duplicate of custom_fields, remove before emitting - should_yield = self._buffer_record((self.stream, ticket)) + # yielding stream name with record in a tuple as it is used for obtaining only the parent records while sync + yield (self.stream, ticket) if audits_stream.is_selected(): try: for audit in audits_stream.sync(ticket["id"]): - self._buffer_record(audit) - except HTTPError as e: - if e.response.status_code == 404: - LOGGER.warning("Unable to retrieve audits for ticket (ID: %s), record not found", ticket['id']) - else: - raise e + yield audit + except http.ZendeskNotFoundError: + # Skip stream if ticket_audit does not found for particular ticekt_id. Earlier it throwing HTTPError + # but now as error handling updated, it throws ZendeskNotFoundError. + message = "Unable to retrieve audits for ticket (ID: {}), record not found".format(ticket['id']) + LOGGER.warning(message) if metrics_stream.is_selected(): try: for metric in metrics_stream.sync(ticket["id"]): - self._buffer_record(metric) - except HTTPError as e: - if e.response.status_code == 404: - LOGGER.warning("Unable to retrieve metrics for ticket (ID: %s), record not found", ticket['id']) - else: - raise e + yield metric + except http.ZendeskNotFoundError: + # Skip stream if ticket_metric does not found for particular ticekt_id. Earlier it throwing HTTPError + # but now as error handling updated, it throws ZendeskNotFoundError. + message = "Unable to retrieve metrics for ticket (ID: {}), record not found".format(ticket['id']) + LOGGER.warning(message) if comments_stream.is_selected(): try: # add ticket_id to ticket_comment so the comment can # be linked back to it's corresponding ticket for comment in comments_stream.sync(ticket["id"]): - self._buffer_record(comment) - except HTTPError as e: - if e.response.status_code == 404: - LOGGER.warning("Unable to retrieve comments for ticket (ID: %s), record not found", ticket['id']) - else: - raise e - - if should_yield: - for rec in self._empty_buffer(): - yield rec - emit_sub_stream_metrics(audits_stream) - emit_sub_stream_metrics(metrics_stream) - emit_sub_stream_metrics(comments_stream) - singer.write_state(state) - - for rec in self._empty_buffer(): - yield rec + yield comment + except http.ZendeskNotFoundError: + # Skip stream if ticket_comment does not found for particular ticekt_id. Earlier it throwing HTTPError + # but now as error handling updated, it throws ZendeskNotFoundError. + message = "Unable to retrieve comments for ticket (ID: {}), record not found".format(ticket['id']) + LOGGER.warning(message) + + singer.write_state(state) emit_sub_stream_metrics(audits_stream) emit_sub_stream_metrics(metrics_stream) emit_sub_stream_metrics(comments_stream) singer.write_state(state) + def check_access(self): + ''' + Check whether the permission was given to access stream resources or not. + ''' + url = self.endpoint.format(self.config['subdomain']) + # Convert start_date parameter to timestamp to pass with request param + start_time = datetime.datetime.strptime(self.config['start_date'], START_DATE_FORMAT).timestamp() + HEADERS['Authorization'] = 'Bearer {}'.format(self.config["access_token"]) + + http.call_api(url, self.request_timeout, params={'start_time': start_time, 'per_page': 1}, headers=HEADERS) + + class TicketAudits(Stream): name = "ticket_audits" replication_method = "INCREMENTAL" @@ -366,7 +393,8 @@ class TicketAudits(Stream): def get_objects(self, ticket_id): url = self.endpoint.format(self.config['subdomain'], ticket_id) - pages = http.get_offset_based(url, self.config['access_token']) + # Pass `request_timeout` parameter + pages = http.get_offset_based(url, self.config['access_token'], self.request_timeout) for page in pages: yield from page[self.item_key] @@ -377,6 +405,19 @@ def sync(self, ticket_id): self.count += 1 yield (self.stream, ticket_audit) + def check_access(self): + ''' + Check whether the permission was given to access stream resources or not. + ''' + + url = self.endpoint.format(self.config['subdomain'], '1') + HEADERS['Authorization'] = 'Bearer {}'.format(self.config["access_token"]) + try: + http.call_api(url, self.request_timeout, params={'per_page': 1}, headers=HEADERS) + except http.ZendeskNotFoundError: + #Skip 404 ZendeskNotFoundError error as goal is just to check whether TicketComments have read permission or not + pass + class TicketMetrics(CursorBasedStream): name = "ticket_metrics" replication_method = "INCREMENTAL" @@ -387,12 +428,25 @@ class TicketMetrics(CursorBasedStream): def sync(self, ticket_id): # Only 1 ticket metric per ticket url = self.endpoint.format(self.config['subdomain'], ticket_id) - pages = http.get_offset_based(url, self.config['access_token']) + # Pass `request_timeout` + pages = http.get_offset_based(url, self.config['access_token'], self.request_timeout) for page in pages: zendesk_metrics.capture('ticket_metric') self.count += 1 yield (self.stream, page[self.item_key]) + def check_access(self): + ''' + Check whether the permission was given to access stream resources or not. + ''' + url = self.endpoint.format(self.config['subdomain'], '1') + HEADERS['Authorization'] = 'Bearer {}'.format(self.config["access_token"]) + try: + http.call_api(url, self.request_timeout, params={'per_page': 1}, headers=HEADERS) + except http.ZendeskNotFoundError: + #Skip 404 ZendeskNotFoundError error as goal is just to check whether TicketComments have read permission or not + pass + class TicketComments(Stream): name = "ticket_comments" replication_method = "INCREMENTAL" @@ -402,7 +456,8 @@ class TicketComments(Stream): def get_objects(self, ticket_id): url = self.endpoint.format(self.config['subdomain'], ticket_id) - pages = http.get_offset_based(url, self.config['access_token']) + # Pass `request_timeout` parameter + pages = http.get_offset_based(url, self.config['access_token'], self.request_timeout) for page in pages: yield from page[self.item_key] @@ -414,6 +469,18 @@ def sync(self, ticket_id): ticket_comment['ticket_id'] = ticket_id yield (self.stream, ticket_comment) + def check_access(self): + ''' + Check whether the permission was given to access stream resources or not. + ''' + url = self.endpoint.format(self.config['subdomain'], '1') + HEADERS['Authorization'] = 'Bearer {}'.format(self.config["access_token"]) + try: + http.call_api(url, self.request_timeout, params={'per_page': 1}, headers=HEADERS) + except http.ZendeskNotFoundError: + #Skip 404 ZendeskNotFoundError error as goal is to just check to whether TicketComments have read permission or not + pass + class SatisfactionRatings(CursorBasedStream): name = "satisfaction_ratings" replication_method = "INCREMENTAL" @@ -519,6 +586,12 @@ def sync(self, state): self.update_bookmark(state, form.updated_at) yield (self.stream, form) + def check_access(self): + ''' + Check whether the permission was given to access stream resources or not. + ''' + self.client.ticket_forms() + class GroupMemberships(CursorBasedStream): name = "group_memberships" replication_method = "INCREMENTAL" @@ -556,6 +629,12 @@ def sync(self, state): # pylint: disable=unused-argument for policy in self.client.sla_policies(): yield (self.stream, policy) + def check_access(self): + ''' + Check whether the permission was given to access stream resources or not. + ''' + self.client.sla_policies() + STREAMS = { "tickets": Tickets, "groups": Groups, diff --git a/test/unittests/test_discovery_mode.py b/test/unittests/test_discovery_mode.py new file mode 100644 index 0000000..0c7c266 --- /dev/null +++ b/test/unittests/test_discovery_mode.py @@ -0,0 +1,276 @@ +import unittest +from unittest.mock import MagicMock, Mock, patch +from tap_zendesk import discover, http +import tap_zendesk +import requests +import zenpy + +ACCSESS_TOKEN_ERROR = '{"error": "Forbidden", "description": "You are missing the following required scopes: read"}' +API_TOKEN_ERROR = '{"error": {"title": "Forbidden",'\ + '"message": "You do not have access to this page. Please contact the account owner of this help desk for further help."}}' +AUTH_ERROR = '{"error": "Could not authenticate you"}' +START_DATE = "2021-10-30T00:00:00Z" + +def mocked_get(*args, **kwargs): + fake_response = requests.models.Response() + fake_response.headers.update(kwargs.get('headers', {})) + fake_response.status_code = kwargs['status_code'] + + # We can't set the content or text of the Response directly, so we mock a function + fake_response.json = Mock() + fake_response.json.side_effect = lambda:kwargs.get('json', {}) + + return fake_response + +class TestDiscovery(unittest.TestCase): + ''' + Test that we can call api for each stream in discovey mode and handle forbidden error. + ''' + @patch('tap_zendesk.streams.Organizations.check_access',side_effect=zenpy.lib.exception.APIException(ACCSESS_TOKEN_ERROR)) + @patch('tap_zendesk.streams.Users.check_access',side_effect=zenpy.lib.exception.APIException(ACCSESS_TOKEN_ERROR)) + @patch('tap_zendesk.streams.TicketForms.check_access',side_effect=zenpy.lib.exception.APIException(ACCSESS_TOKEN_ERROR)) + @patch('tap_zendesk.streams.SLAPolicies.check_access',side_effect=[mocked_get(status_code=200, json={"key1": "val1"})]) + @patch('tap_zendesk.discover.load_shared_schema_refs', return_value={}) + @patch('tap_zendesk.streams.Stream.load_metadata', return_value={}) + @patch('tap_zendesk.streams.Stream.load_schema', return_value={}) + @patch('singer.resolve_schema_references', return_value={}) + @patch('requests.get', + side_effect=[ + mocked_get(status_code=200, json={"tickets": [{"id": "t1"}]}), # Response of the 1st get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 2nd get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 3rd get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 4th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 5th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 6th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 7th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 8th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 9th get request call + mocked_get(status_code=403, json={"key1": "val1"}) # Response of the 10th get request call + ]) + def test_discovery_handles_403__raise_tap_zendesk_forbidden_error(self, mock_get, mock_resolve_schema_references, + mock_load_metadata, mock_load_schema,mock_load_shared_schema_refs, mocked_sla_policies, + mocked_ticket_forms, mock_users, mock_organizations): + ''' + Test that we handle forbidden error for child streams. discover_streams calls check_access for each stream to + check the read perission. discover_streams call many other methods including load_shared_schema_refs, load_metadata, + load_schema, resolve_schema_references also which we mock to test forbidden error. We mock check_access method of + some of stream method which call request of zenpy module and also mock get method of requests module with 200, 403 error. + + ''' + try: + responses = discover.discover_streams('dummy_client', {'subdomain': 'arp', 'access_token': 'dummy_token', 'start_date':START_DATE}) + except tap_zendesk.http.ZendeskForbiddenError as e: + expected_error_message = "HTTP-error-code: 403, Error: You are missing the following required scopes: read. "\ + "The account credentials supplied do not have read access for the following stream(s): groups, users, "\ + "organizations, ticket_audits, ticket_comments, ticket_fields, ticket_forms, group_memberships, macros, "\ + "satisfaction_ratings, tags, ticket_metrics" + + # Verifying the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + expected_call_count = 10 + actual_call_count = mock_get.call_count + self.assertEqual(expected_call_count, actual_call_count) + + + @patch('tap_zendesk.streams.Organizations.check_access',side_effect=zenpy.lib.exception.APIException(ACCSESS_TOKEN_ERROR)) + @patch('tap_zendesk.streams.Users.check_access',side_effect=zenpy.lib.exception.APIException(ACCSESS_TOKEN_ERROR)) + @patch('tap_zendesk.streams.TicketForms.check_access',side_effect=zenpy.lib.exception.APIException(ACCSESS_TOKEN_ERROR)) + @patch('tap_zendesk.streams.SLAPolicies.check_access',side_effect=zenpy.lib.exception.APIException(ACCSESS_TOKEN_ERROR)) + @patch('tap_zendesk.discover.load_shared_schema_refs', return_value={}) + @patch('tap_zendesk.streams.Stream.load_metadata', return_value={}) + @patch('tap_zendesk.streams.Stream.load_schema', return_value={}) + @patch('singer.resolve_schema_references', return_value={}) + @patch('requests.get', + side_effect=[ + mocked_get(status_code=200, json={"tickets": [{"id": "t1"}]}), # Response of the 1st get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 2nd get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 3rd get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 4th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 5th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 6th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 7th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 8th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 9th get request call + mocked_get(status_code=403, json={"key1": "val1"}) # Response of the 10th get request call + ]) + def test_discovery_handles_403_raise_zenpy_forbidden_error_for_access_token(self, mock_get, mock_resolve_schema_references, mock_load_metadata, + mock_load_schema,mock_load_shared_schema_refs, mocked_sla_policies, mocked_ticket_forms, + mock_users, mock_organizations): + ''' + Test that we handle forbidden error received from last failed request which we called from zenpy module and + raised zenpy.lib.exception.APIException. discover_streams calls check_access for each stream to check the + read perission. discover_streams call many other methods including load_shared_schema_refs, load_metadata, + load_schema, resolve_schema_references also which we mock to test forbidden error. We mock check_access method of + some of stream method which call request of zenpy module and also mock get method of requests module with 200, 403 error. + ''' + try: + responses = discover.discover_streams('dummy_client', {'subdomain': 'arp', 'access_token': 'dummy_token', 'start_date':START_DATE}) + except tap_zendesk.http.ZendeskForbiddenError as e: + expected_error_message = "HTTP-error-code: 403, Error: You are missing the following required scopes: read. "\ + "The account credentials supplied do not have read access for the following stream(s): groups, users, "\ + "organizations, ticket_audits, ticket_comments, ticket_fields, ticket_forms, group_memberships, macros, "\ + "satisfaction_ratings, tags, ticket_metrics, sla_policies" + + self.assertEqual(str(e), expected_error_message) + + expected_call_count = 10 + actual_call_count = mock_get.call_count + self.assertEqual(expected_call_count, actual_call_count) + + + @patch('tap_zendesk.streams.Organizations.check_access',side_effect=zenpy.lib.exception.APIException(API_TOKEN_ERROR)) + @patch('tap_zendesk.streams.Users.check_access',side_effect=zenpy.lib.exception.APIException(API_TOKEN_ERROR)) + @patch('tap_zendesk.streams.TicketForms.check_access',side_effect=zenpy.lib.exception.APIException(API_TOKEN_ERROR)) + @patch('tap_zendesk.streams.SLAPolicies.check_access',side_effect=[mocked_get(status_code=200, json={"key1": "val1"})]) + @patch('tap_zendesk.discover.load_shared_schema_refs', return_value={}) + @patch('tap_zendesk.streams.Stream.load_metadata', return_value={}) + @patch('tap_zendesk.streams.Stream.load_schema', return_value={}) + @patch('singer.resolve_schema_references', return_value={}) + @patch('requests.get', + side_effect=[ + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 2nd get request call + mocked_get(status_code=404, json={"key1": "val1"}), # Response of the 3rd get request call + mocked_get(status_code=404, json={"key1": "val1"}), # Response of the 4th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 5th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 6th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 7th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 8th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 9th get request call + mocked_get(status_code=404, json={"key1": "val1"}) # Response of the 10th get request call + ]) + def test_discovery_handles_403_raise_zenpy_forbidden_error_for_api_token(self, mock_get, mock_resolve_schema_references, + mock_load_metadata, mock_load_schema,mock_load_shared_schema_refs, mocked_sla_policies, + mocked_ticket_forms, mock_users, mock_organizations): + ''' + Test that we handle forbidden error received from last failed request which we called from zenpy module and + raised zenpy.lib.exception.APIException. discover_streams calls check_access for each stream to check the + read perission. discover_streams call many other methods including load_shared_schema_refs, load_metadata, + load_schema, resolve_schema_references also which we mock to test forbidden error. We mock check_access method of + some of stream method which call request of zenpy module and also mock get method of requests module with 200, 403 error. + ''' + try: + responses = discover.discover_streams('dummy_client', {'subdomain': 'arp', 'access_token': 'dummy_token', 'start_date':START_DATE}) + except tap_zendesk.http.ZendeskForbiddenError as e: + expected_error_message = "HTTP-error-code: 403, Error: You are missing the following required scopes: read. "\ + "The account credentials supplied do not have read access for the following stream(s): tickets, groups, users, "\ + "organizations, ticket_fields, ticket_forms, group_memberships, macros, satisfaction_ratings, tags" + + self.assertEqual(str(e), expected_error_message) + + expected_call_count = 10 + actual_call_count = mock_get.call_count + self.assertEqual(expected_call_count, actual_call_count) + + + @patch('tap_zendesk.streams.Organizations.check_access',side_effect=zenpy.lib.exception.APIException(ACCSESS_TOKEN_ERROR)) + @patch('tap_zendesk.streams.Users.check_access',side_effect=zenpy.lib.exception.APIException(ACCSESS_TOKEN_ERROR)) + @patch('tap_zendesk.streams.TicketForms.check_access',side_effect=zenpy.lib.exception.APIException(ACCSESS_TOKEN_ERROR)) + @patch('tap_zendesk.streams.SLAPolicies.check_access',side_effect=[mocked_get(status_code=200, json={"key1": "val1"})]) + @patch('tap_zendesk.discover.load_shared_schema_refs', return_value={}) + @patch('tap_zendesk.streams.Stream.load_metadata', return_value={}) + @patch('tap_zendesk.streams.Stream.load_schema', return_value={}) + @patch('singer.resolve_schema_references', return_value={}) + @patch('requests.get', + side_effect=[ + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 2nd get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 3rd get request call + mocked_get(status_code=400, json={"key1": "val1"}), # Response of the 4th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 5th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 6th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 7th get request call + ]) + def test_discovery_handles_except_403_error_requests_module(self, mock_get, mock_resolve_schema_references, + mock_load_metadata, mock_load_schema,mock_load_shared_schema_refs, mocked_sla_policies, + mocked_ticket_forms, mock_users, mock_organizations): + ''' + Test that function raises error directly if error code is other than 403. discover_streams calls check_access for each + stream to check the read perission. discover_streams call many other methods including load_shared_schema_refs, load_metadata, + load_schema, resolve_schema_references also which we mock to test forbidden error. We mock check_access method of + some of stream method which call request of zenpy module and also mock get method of requests module with 200, 403 error. + ''' + try: + responses = discover.discover_streams('dummy_client', {'subdomain': 'arp', 'access_token': 'dummy_token', 'start_date':START_DATE}) + except http.ZendeskBadRequestError as e: + expected_error_message = "HTTP-error-code: 400, Error: A validation exception has occurred." + # Verifying the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + expected_call_count = 4 + actual_call_count = mock_get.call_count + self.assertEqual(expected_call_count, actual_call_count) + + + @patch('tap_zendesk.streams.Organizations.check_access',side_effect=zenpy.lib.exception.APIException(AUTH_ERROR)) + @patch('tap_zendesk.streams.Users.check_access',side_effect=zenpy.lib.exception.APIException(AUTH_ERROR)) + @patch('tap_zendesk.streams.TicketForms.check_access',side_effect=zenpy.lib.exception.APIException(AUTH_ERROR)) + @patch('tap_zendesk.streams.SLAPolicies.check_access',side_effect=[mocked_get(status_code=200, json={"key1": "val1"})]) + @patch('tap_zendesk.discover.load_shared_schema_refs', return_value={}) + @patch('tap_zendesk.streams.Stream.load_metadata', return_value={}) + @patch('tap_zendesk.streams.Stream.load_schema', return_value={}) + @patch('singer.resolve_schema_references', return_value={}) + @patch('requests.get', + side_effect=[ + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 2nd get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 3rd get request call + mocked_get(status_code=400, json={"key1": "val1"}), # Response of the 4th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 5th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 6th get request call + mocked_get(status_code=403, json={"key1": "val1"}), # Response of the 7th get request call + ]) + def test_discovery_handles_except_403_error_zenpy_module(self, mock_get, mock_resolve_schema_references, + mock_load_metadata, mock_load_schema,mock_load_shared_schema_refs, mocked_sla_policies, + mocked_ticket_forms, mock_users, mock_organizations): + ''' + Test that discovery mode raise error direclty if it is rather than 403 for request zenpy module. discover_streams call + many other methods including load_shared_schema_refs, load_metadata, load_schema, resolve_schema_references + also which we mock to test forbidden error. We mock check_access method of some of stream method which + call request of zenpy module and also mock get method of requests module with 400, 403 error. + ''' + try: + responses = discover.discover_streams('dummy_client', {'subdomain': 'arp', 'access_token': 'dummy_token', 'start_date':START_DATE}) + except zenpy.lib.exception.APIException as e: + expected_error_message = AUTH_ERROR + # Verifying the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + expected_call_count = 2 + actual_call_count = mock_get.call_count + self.assertEqual(expected_call_count, actual_call_count) + + + @patch('tap_zendesk.streams.Organizations.check_access',side_effect=[mocked_get(status_code=200, json={"key1": "val1"})]) + @patch('tap_zendesk.streams.Users.check_access',side_effect=[mocked_get(status_code=200, json={"key1": "val1"})]) + @patch('tap_zendesk.streams.TicketForms.check_access',side_effect=[mocked_get(status_code=200, json={"key1": "val1"})]) + @patch('tap_zendesk.streams.SLAPolicies.check_access',side_effect=[mocked_get(status_code=200, json={"key1": "val1"})]) + @patch('tap_zendesk.discover.load_shared_schema_refs', return_value={}) + @patch('tap_zendesk.streams.Stream.load_metadata', return_value={}) + @patch('tap_zendesk.streams.Stream.load_schema', return_value={}) + @patch('singer.resolve_schema_references', return_value={}) + @patch('requests.get', + side_effect=[ + mocked_get(status_code=200, json={"tickets": [{"id": "t1"}]}), # Response of the 1st get request call + mocked_get(status_code=200, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=200, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=200, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=200, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=200, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=200, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=200, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=200, json={"key1": "val1"}), # Response of the 1st get request call + mocked_get(status_code=200, json={"key1": "val1"}) # Response of the 1st get request call + ]) + def test_discovery_handles_200_response(self, mock_get, mock_resolve_schema_references, + mock_load_metadata, mock_load_schema,mock_load_shared_schema_refs, mocked_sla_policies, + mocked_ticket_forms, mock_users, mock_organizations): + ''' + Test that discovery mode does not raise any error in case of all streams have read permission + ''' + discover.discover_streams('dummy_client', {'subdomain': 'arp', 'access_token': 'dummy_token', 'start_date':START_DATE}) + + expected_call_count = 10 + actual_call_count = mock_get.call_count + self.assertEqual(expected_call_count, actual_call_count) diff --git a/test/unittests/test_http.py b/test/unittests/test_http.py index 2328f55..00f0c36 100644 --- a/test/unittests/test_http.py +++ b/test/unittests/test_http.py @@ -1,8 +1,9 @@ import unittest from unittest.mock import MagicMock, Mock, patch -from tap_zendesk import http +from tap_zendesk import http, streams import requests +import zenpy SINGLE_RESPONSE = { 'meta': {'has_more': False} @@ -24,7 +25,7 @@ def mocked_get(*args, **kwargs): return fake_response - +@patch("time.sleep") class TestBackoff(unittest.TestCase): """Test that we can make single requests to the API and handle cursor based pagination. @@ -34,9 +35,9 @@ class TestBackoff(unittest.TestCase): @patch('requests.get', side_effect=[mocked_get(status_code=200, json=SINGLE_RESPONSE)]) - def test_get_cursor_based_gets_one_page(self, mock_get): + def test_get_cursor_based_gets_one_page(self, mock_get, mock_sleep): responses = [response for response in http.get_cursor_based(url='some_url', - access_token='some_token')] + access_token='some_token', request_timeout=300)] actual_response = responses[0] self.assertDictEqual(SINGLE_RESPONSE, actual_response) @@ -50,10 +51,10 @@ def test_get_cursor_based_gets_one_page(self, mock_get): mocked_get(status_code=200, json={"key1": "val1", **PAGINATE_RESPONSE}), mocked_get(status_code=200, json={"key2": "val2", **SINGLE_RESPONSE}), ]) - def test_get_cursor_based_can_paginate(self, mock_get): + def test_get_cursor_based_can_paginate(self, mock_get, mock_sleep): responses = [response for response in http.get_cursor_based(url='some_url', - access_token='some_token')] + access_token='some_token', request_timeout=300)] self.assertDictEqual({"key1": "val1", **PAGINATE_RESPONSE}, responses[0]) @@ -71,14 +72,14 @@ def test_get_cursor_based_can_paginate(self, mock_get): mocked_get(status_code=429, headers={'retry-after': 1}, json={"key2": "val2", **SINGLE_RESPONSE}), mocked_get(status_code=200, json={"key1": "val1", **SINGLE_RESPONSE}), ]) - def test_get_cursor_based_handles_429(self, mock_get): + def test_get_cursor_based_handles_429(self, mock_get, mock_sleep): """Test that the tap: - can handle 429s - requests uses a case insensitive dict for the `headers` - can handle either a string or an integer for the retry header """ responses = [response for response in http.get_cursor_based(url='some_url', - access_token='some_token')] + access_token='some_token', request_timeout=300)] actual_response = responses[0] self.assertDictEqual({"key1": "val1", **SINGLE_RESPONSE}, actual_response) @@ -86,3 +87,224 @@ def test_get_cursor_based_handles_429(self, mock_get): expected_call_count = 3 actual_call_count = mock_get.call_count self.assertEqual(expected_call_count, actual_call_count) + + @patch('requests.get',side_effect=[mocked_get(status_code=400, json={"key1": "val1"})]) + def test_get_cursor_based_handles_400(self,mock_get, mock_sleep): + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + + except http.ZendeskBadRequestError as e: + expected_error_message = "HTTP-error-code: 400, Error: A validation exception has occurred." + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + #Verify the request calls only 1 time + self.assertEqual(mock_get.call_count, 1) + + @patch('requests.get',side_effect=[mocked_get(status_code=400, json={"error": "Couldn't authenticate you"})]) + def test_get_cursor_based_handles_400_api_error_message(self,mock_get, mock_sleep): + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + + except http.ZendeskBadRequestError as e: + expected_error_message = "HTTP-error-code: 400, Error: Couldn't authenticate you" + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + #Verify the request calls only 1 time + self.assertEqual(mock_get.call_count, 1) + + @patch('requests.get',side_effect=[mocked_get(status_code=401, json={"key1": "val1"})]) + def test_get_cursor_based_handles_401(self,mock_get, mock_sleep): + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except http.ZendeskUnauthorizedError as e: + expected_error_message = "HTTP-error-code: 401, Error: The access token provided is expired, revoked,"\ + " malformed or invalid for other reasons." + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + #Verify the request calls only 1 time + self.assertEqual(mock_get.call_count, 1) + + @patch('requests.get',side_effect=[mocked_get(status_code=404, json={"key1": "val1"})]) + def test_get_cursor_based_handles_404(self,mock_get, mock_sleep): + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except http.ZendeskNotFoundError as e: + expected_error_message = "HTTP-error-code: 404, Error: The resource you have specified cannot be found." + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + #Verify the request calls only 1 time + self.assertEqual(mock_get.call_count, 1) + + + @patch('requests.get',side_effect=[mocked_get(status_code=409, json={"key1": "val1"})]) + def test_get_cursor_based_handles_409(self,mock_get, mock_sleep): + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except http.ZendeskConflictError as e: + expected_error_message = "HTTP-error-code: 409, Error: The API request cannot be completed because the requested operation would conflict with an existing item." + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + #Verify the request calls only 1 time + self.assertEqual(mock_get.call_count, 1) + + @patch('requests.get',side_effect=[mocked_get(status_code=422, json={"key1": "val1"})]) + def test_get_cursor_based_handles_422(self,mock_get, mock_sleep): + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except http.ZendeskUnprocessableEntityError as e: + expected_error_message = "HTTP-error-code: 422, Error: The request content itself is not processable by the server." + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + #Verify the request calls only 1 time + self.assertEqual(mock_get.call_count, 1) + + @patch('requests.get',side_effect=10*[mocked_get(status_code=500, json={"key1": "val1"})]) + def test_get_cursor_based_handles_500(self,mock_get, mock_sleep): + """ + Test that the tap can handle 500 error and retry it 10 times + """ + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except http.ZendeskInternalServerError as e: + expected_error_message = "HTTP-error-code: 500, Error: The server encountered an unexpected condition which prevented" \ + " it from fulfilling the request." + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + #Verify the request retry 10 times + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get',side_effect=10*[mocked_get(status_code=501, json={"key1": "val1"})]) + def test_get_cursor_based_handles_501(self,mock_get, mock_sleep): + """ + Test that the tap can handle 501 error and retry it 10 times + """ + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except http.ZendeskNotImplementedError as e: + expected_error_message = "HTTP-error-code: 501, Error: The server does not support the functionality required to fulfill the request." + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + #Verify the request retry 10 times + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get',side_effect=10*[mocked_get(status_code=502, json={"key1": "val1"})]) + def test_get_cursor_based_handles_502(self,mock_get, mock_sleep): + """ + Test that the tap can handle 502 error and retry it 10 times + """ + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except http.ZendeskBadGatewayError as e: + expected_error_message = "HTTP-error-code: 502, Error: Server received an invalid response." + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + #Verify the request retry 10 times + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get', + side_effect=[ + mocked_get(status_code=503, headers={'Retry-After': '1'}, json={"key3": "val3", **SINGLE_RESPONSE}), + mocked_get(status_code=503, headers={'retry-after': 1}, json={"key2": "val2", **SINGLE_RESPONSE}), + mocked_get(status_code=200, json={"key1": "val1", **SINGLE_RESPONSE}), + ]) + def test_get_cursor_based_handles_503(self,mock_get, mock_sleep): + """Test that the tap: + - can handle 503s + - requests uses a case insensitive dict for the `headers` + - can handle either a string or an integer for the retry header + """ + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + actual_response = responses[0] + self.assertDictEqual({"key1": "val1", **SINGLE_RESPONSE}, + actual_response) + + #Verify the request calls only 3 times + self.assertEqual(mock_get.call_count, 3) + + @patch('requests.get', + side_effect=[mocked_get(status_code=503)]) + def test_get_cursor_based_handles_503_without_retry_after(self,mock_get, mock_sleep): + """Test that the tap can handle 503 without retry-after headers + """ + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except http.ZendeskServiceUnavailableError as e: + expected_error_message = 'HTTP-error-code: 503, Error: API service is currently unavailable.' + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + #Verify the request calls only 1 time + self.assertEqual(mock_get.call_count, 1) + + @patch('requests.get') + def test_get_cursor_based_handles_444(self,mock_get, mock_sleep): + fake_response = requests.models.Response() + fake_response.status_code = 444 + + mock_get.side_effect = [fake_response] + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except http.ZendeskError as e: + expected_error_message = 'HTTP-error-code: 444, Error: Unknown Error' + # Verify the message formed for the custom exception + self.assertEqual(str(e), expected_error_message) + + self.assertEqual(mock_get.call_count, 1) + + @patch("tap_zendesk.streams.LOGGER.warning") + def test_raise_or_log_zenpy_apiexception(self, mocked_logger, mock_sleep): + schema = {} + stream = 'test_stream' + error_string = '{"error": "Forbidden", "description": "You are missing the following required scopes: read"}' + e = zenpy.lib.exception.APIException(error_string) + streams.raise_or_log_zenpy_apiexception(schema, stream, e) + # Verify the raise_or_log_zenpy_apiexception Log expected message + mocked_logger.assert_called_with( + "The account credentials supplied do not have access to `%s` custom fields.", + stream) + + @patch('requests.get') + def test_call_api_handles_timeout_error(self,mock_get, mock_sleep): + mock_get.side_effect = requests.exceptions.Timeout + + try: + responses = http.call_api(url='some_url', request_timeout=300, params={}, headers={}) + except requests.exceptions.Timeout as e: + pass + + # Verify the request retry 5 times on timeout + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get') + def test_call_api_handles_connection_error(self,mock_get, mock_sleep): + mock_get.side_effect = ConnectionError + + try: + responses = http.call_api(url='some_url', request_timeout=300, params={}, headers={}) + except ConnectionError as e: + pass + + # Verify the request retry 5 times on timeout + self.assertEqual(mock_get.call_count, 10) + diff --git a/test/unittests/test_request_timeout.py b/test/unittests/test_request_timeout.py new file mode 100644 index 0000000..8f3778b --- /dev/null +++ b/test/unittests/test_request_timeout.py @@ -0,0 +1,194 @@ +import unittest +from unittest.mock import MagicMock, Mock, patch +from tap_zendesk import http, streams +import requests +import datetime + +PAGINATE_RESPONSE = { + 'meta': {'has_more': True, + 'after_cursor': 'some_cursor'}, + 'end_of_stream': False, + 'after_cursor': 'some_cursor', + 'next_page': '3' +} + +SINGLE_RESPONSE = { + 'meta': {'has_more': False} +} +START_TIME = datetime.datetime.strptime("2021-10-30T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ") +def mocked_get(*args, **kwargs): + fake_response = requests.models.Response() + fake_response.headers.update(kwargs.get('headers', {})) + fake_response.status_code = kwargs['status_code'] + + # We can't set the content or text of the Response directly, so we mock a function + fake_response.json = Mock() + fake_response.json.side_effect = lambda:kwargs.get('json', {}) + + return fake_response + +@patch("time.sleep") +class TestRequestTimeoutBackoff(unittest.TestCase): + """ + A set of unit tests to ensure that requests are retrying properly for Timeout Error. + """ + + @patch('requests.get') + def test_call_api_handles_timeout_error(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout` and expect the tap to retry this up to 10 times, + """ + mock_get.side_effect = requests.exceptions.Timeout + + try: + responses = http.call_api(url='some_url', request_timeout=300, params={}, headers={}) + except requests.exceptions.Timeout as e: + pass + + # Verify the request retry 10 times on timeout + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get', side_effect=10*[requests.exceptions.Timeout]) + def test_get_cursor_based_handles_timeout_error(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout` and expect the tap to retry this up to 10 times, + """ + + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except requests.exceptions.Timeout as e: + pass + + # Verify the request retry 10 times on timeout + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get', side_effect=[mocked_get(status_code=200, json={"key1": "val1", **PAGINATE_RESPONSE}), + requests.exceptions.Timeout, requests.exceptions.Timeout, + mocked_get(status_code=200, json={"key1": "val1", **SINGLE_RESPONSE})]) + def test_get_cursor_based_handles_timeout_error_in_pagination_call(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout`. In next page call the tap should retry request timeout error. + """ + + try: + responses = [response for response in http.get_cursor_based(url='some_url', + access_token='some_token', request_timeout=300)] + except requests.exceptions.Timeout as e: + pass + + # Verify the request call total 4 times(2 time retry call, 2 time 200 call) + self.assertEqual(mock_get.call_count, 4) + + @patch('requests.get', side_effect=10*[requests.exceptions.Timeout]) + def test_get_offset_based_handles_timeout_error(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout` and expect the tap to retry this up to 10 times, + """ + + try: + responses = [response for response in http.get_offset_based(url='some_url', + access_token='some_token', request_timeout=300)] + except requests.exceptions.Timeout as e: + pass + + # Verify the request retry 10 times on timeout + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get', side_effect=[mocked_get(status_code=200, json={"key1": "val1", **PAGINATE_RESPONSE}), + requests.exceptions.Timeout, requests.exceptions.Timeout, + mocked_get(status_code=200, json={"key1": "val1", **SINGLE_RESPONSE})]) + def test_get_offset_based_handles_timeout_error_in_pagination_call(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout`. In next page call the tap should retry request timeout error. + """ + + try: + responses = [response for response in http.get_offset_based(url='some_url', + access_token='some_token', request_timeout=300)] + except requests.exceptions.Timeout as e: + pass + + # Verify the request call total 4 times(2 time retry call, 2 time 200 call) + self.assertEqual(mock_get.call_count, 4) + + @patch('requests.get', side_effect=10*[requests.exceptions.Timeout]) + def test_get_incremental_export_handles_timeout_error(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout` and expect the tap to retry this up to 10 times, + """ + + try: + responses = [response for response in http.get_incremental_export(url='some_url',access_token='some_token', + request_timeout=300, start_time= START_TIME)] + except requests.exceptions.Timeout as e: + pass + + # Verify the request retry 10 times on timeout + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get') + def test_cursor_based_stream_timeout_error(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout` and expect the tap to retry this up to 10 times, + """ + mock_get.side_effect = requests.exceptions.Timeout + cursor_based_stream = streams.CursorBasedStream(config={'subdomain': '34', 'access_token': 'df'}) + cursor_based_stream.endpoint = 'https://{}' + try: + responses = list(cursor_based_stream.get_objects()) + except requests.exceptions.Timeout as e: + pass + + # Verify the request retry 10 times on timeout + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get') + def test_cursor_based_export_stream_timeout_error(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout` and expect the tap to retry this up to 10 times, + """ + mock_get.side_effect = requests.exceptions.Timeout + cursor_based_export_stream = streams.CursorBasedExportStream(config={'subdomain': '34', 'access_token': 'df'}) + cursor_based_export_stream.endpoint = 'https://{}' + try: + responses = list(cursor_based_export_stream.get_objects(START_TIME)) + except requests.exceptions.Timeout as e: + pass + + # Verify the request retry 10 times on timeout + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get') + def test_ticket_audits_timeout_error(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout` and expect the tap to retry this up to 10 times, + """ + mock_get.side_effect = requests.exceptions.Timeout + ticket_audits = streams.TicketAudits(config={'subdomain': '34', 'access_token': 'df'}) + try: + responses = list(ticket_audits.get_objects('i1')) + except requests.exceptions.Timeout as e: + pass + + # Verify the request retry 10 times on timeout + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get') + def test_ticket_metrics_timeout_error(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout` and expect the tap to retry this up to 10 times, + """ + mock_get.side_effect = requests.exceptions.Timeout + ticket_metrics = streams.TicketMetrics(config={'subdomain': '34', 'access_token': 'df'}) + try: + responses = list(ticket_metrics.sync('i1')) + except requests.exceptions.Timeout as e: + pass + + # Verify the request retry 10 times on timeout + self.assertEqual(mock_get.call_count, 10) + + @patch('requests.get') + def test_ticket_comments_timeout_error(self, mock_get, mock_sleep): + """We mock request method to raise a `Timeout` and expect the tap to retry this up to 10 times, + """ + mock_get.side_effect = requests.exceptions.Timeout + ticket_comments = streams.TicketComments(config={'subdomain': '34', 'access_token': 'df'}) + try: + responses = list(ticket_comments.get_objects('i1')) + except requests.exceptions.Timeout as e: + pass + + # Verify the request retry 10 times on timeout + self.assertEqual(mock_get.call_count, 10) \ No newline at end of file diff --git a/test/unittests/test_tickets_child_stream_skip_404_error.py b/test/unittests/test_tickets_child_stream_skip_404_error.py new file mode 100644 index 0000000..360173e --- /dev/null +++ b/test/unittests/test_tickets_child_stream_skip_404_error.py @@ -0,0 +1,90 @@ +import unittest +from unittest.mock import patch +from tap_zendesk import streams, http + + +class MockClass: + def is_selected(self): + return True + + def sync(self, ticket_id): + raise http.ZendeskNotFoundError + +@patch("tap_zendesk.streams.Tickets._empty_buffer") +@patch("tap_zendesk.streams.LOGGER.warning") +@patch('tap_zendesk.streams.Tickets._buffer_record') +@patch('tap_zendesk.streams.Stream.update_bookmark') +@patch('tap_zendesk.metrics.capture') +@patch('tap_zendesk.streams.CursorBasedExportStream.get_objects') +@patch('tap_zendesk.streams.Stream.get_bookmark') +class TestSkip404Error(unittest.TestCase): + """ + Test that child stream of tickets - ticket_audits, ticket_metrics, ticket_comments skip the 404 error. + To raise the 404 error some of the method including _empty_buffer, LOGGER.warning, _buffer_record, update_bookmark, + metrics.capture, get_objects, get_bookmark mocked. + """ + @patch('tap_zendesk.streams.TicketAudits') + def test_ticket_audits_skip_404_error(self, mock_ticket_audits, mock_get_bookmark, mock_get_object, mock_metrics, + mock_update_bookmark, mock_buffer_record, mock_logger, mock_empty_buffer): + + ''' + Test that ticket_audits stream skip the 404 error + ''' + mock_ticket_audits.return_value = MockClass() + mock_empty_buffer.return_value = [] + mock_buffer_record.return_value = False + mock_get_object.return_value = [{'generated_timestamp': 12457845, 'fields': {}, 'id': 'i1'}] + tickets = streams.Tickets(config={'subdomain': '34', 'access_token': 'df'}) + + try: + responses = list(tickets.sync(state={})) + except AttributeError: + pass + + # verify if the LOGGER.warning was called and verify the message + mock_logger.assert_called_with("Unable to retrieve audits for ticket (ID: i1), record not found") + + @patch('tap_zendesk.streams.TicketComments') + def test_ticket_comments_skip_404_error(self, mock_ticket_comments, mock_get_bookmark, mock_get_object, mock_metrics, + mock_update_bookmark, mock_buffer_record, mock_logger, mock_empty_buffer): + + ''' + Test that ticket_audits stream skip the 404 error + ''' + mock_ticket_comments.return_value = MockClass() + mock_empty_buffer.return_value = [] + mock_buffer_record.return_value = False + mock_get_object.return_value = [{'generated_timestamp': 12457845, 'fields': {}, 'id': 'i1'}] + tickets = streams.Tickets(config={'subdomain': '34', 'access_token': 'df'}) + + try: + responses = list(tickets.sync(state={})) + except AttributeError: + pass + + # verify if the LOGGER.warning was called and verify the message + mock_logger.assert_called_with("Unable to retrieve comments for ticket (ID: i1), record not found") + + @patch('tap_zendesk.streams.TicketMetrics') + def test_ticket_metrics_skip_404_error(self, mock_ticket_metrics, mock_get_bookmark, mock_get_object, mock_metrics, + mock_update_bookmark, mock_buffer_record, mock_logger, mock_empty_buffer): + + ''' + Test that ticket_audits stream skip the 404 error + ''' + mock_ticket_metrics.return_value = MockClass() + mock_empty_buffer.return_value = [] + mock_buffer_record.return_value = False + mock_get_object.return_value = [{'generated_timestamp': 12457845, 'fields': {}, 'id': 'i1'}] + tickets = streams.Tickets(config={'subdomain': '34', 'access_token': 'df'}) + + try: + responses = list(tickets.sync(state={})) + self.assertEqual(responses, 1) + except AttributeError: + pass + + # verify if the LOGGER.warning was called and verify the message + mock_logger.assert_called_with("Unable to retrieve metrics for ticket (ID: i1), record not found") + + diff --git a/test/unittests/test_yield_records.py b/test/unittests/test_yield_records.py new file mode 100644 index 0000000..b9dfb6e --- /dev/null +++ b/test/unittests/test_yield_records.py @@ -0,0 +1,193 @@ +from tap_zendesk import LOGGER, oauth_auth +import unittest +from unittest import mock +from unittest.mock import patch +from tap_zendesk.streams import Stream, TicketAudits, Tickets, zendesk_metrics +from tap_zendesk.sync import sync_stream +from tap_zendesk import Zenpy +import json + +class Zenpy(): + def __init__(self) -> None: + pass + +def mocked_sync_audits(ticket_id=None): + """ + Mock the audit records which are retrieved in the sync function of the Audits stream + """ + ticket_audits = [ + { + "author_id":387494208358, + "created_at":"2021-10-11T12:23:20.000000Z", + "id":910518732098, + "ticket_id":2 + }, + { + "author_id":387494208358, + "created_at":"2021-10-11T12:24:05.000000Z", + "id":910519204898, + "ticket_id":2, + } + ] + for audit in ticket_audits: + yield ('ticket_audits', audit) + +def mocked_sync_metrics(ticket_id=None): + """ + Mock the metric records which are retrieved in the sync function of the Audits stream + """ + ticket_metrics = [ + { + "author_id":387494208358, + "created_at":"2021-10-11T12:23:20.000000Z", + "id":910518732090, + "ticket_id":2 + }, + { + "author_id":387494208358, + "created_at":"2021-10-11T12:24:05.000000Z", + "id":910519204892, + "ticket_id":2, + } + ] + for metric in ticket_metrics: + yield ('ticket_metrics', metric) + +def mocked_sync_comments(ticket_id=None): + """ + Mock the comment records which are retrieved in the sync function of the Audits stream + """ + ticket_comments = [ + { + "author_id":387494208356, + "created_at":"2021-10-11T12:23:20.000000Z", + "id":910518732090, + "ticket_id":2 + }, + { + "author_id":387494208354, + "created_at":"2021-10-11T12:24:05.000000Z", + "id":910519204892, + "ticket_id":2, + } + ] + for comment in ticket_comments: + yield ('ticket_comments', comment) + +def logger(logger, point): + return "test stream" + +@mock.patch('tap_zendesk.streams.Stream.update_bookmark') +@mock.patch('tap_zendesk.streams.Stream.get_bookmark') +@mock.patch('tap_zendesk.streams.TicketAudits.is_selected') +@mock.patch('tap_zendesk.streams.TicketMetrics.is_selected') +@mock.patch('tap_zendesk.streams.TicketComments.is_selected') +@mock.patch('tap_zendesk.streams.TicketAudits.sync') +@mock.patch('tap_zendesk.streams.TicketMetrics.sync') +@mock.patch('tap_zendesk.streams.TicketComments.sync') +@mock.patch('tap_zendesk.streams.CursorBasedExportStream.get_objects') +@mock.patch('tap_zendesk.streams.TicketAudits.stream') +@mock.patch('tap_zendesk.streams.TicketComments.stream') +@mock.patch('tap_zendesk.streams.TicketMetrics.stream') +@mock.patch('singer.metrics.log') +def test_yield_records(mocked_log, mocked_audits_stream, mocked_comments_stream, mocked_metrics_stream, mock_objects, mock_comments_sync, mock_metrics_sync, mock_audits_sync, mock_comments, mock_metrics, mock_audits, mock_get_bookmark, mock_update_bookmark): + """ + This function tests that the Tickets and its substreams' records are yielded properly. + """ + ticket_stream = Tickets(Zenpy(), {}) + # mocked ticket record for get_objects() function + tickets = [{ + "url":"https://talend1234.zendesk.com/api/v2/tickets/1.json", + "id":2, + "external_id":"None", + "created_at":"2021-10-11T12:12:31Z", + "updated_at":"2021-10-12T08:37:28Z", + "requester_id":387331462257, + "submitter_id":387494208358, + "assignee_id":387494208358, + "organization_id":"None", + "group_id":360010350357, + "due_at":"None", + "ticket_form_id":360003740737, + "brand_id":360004806057, + "generated_timestamp":1634027848, + "fields": [] + }] + mock_objects.return_value = tickets + + # expected audit records after yield + expected_audits = [ + { + "author_id":387494208358, + "created_at":"2021-10-11T12:23:20.000000Z", + "id":910518732098, + "ticket_id":2 + }, + { + "author_id":387494208358, + "created_at":"2021-10-11T12:24:05.000000Z", + "id":910519204898, + "ticket_id":2, + } + ] + + # expected metric records after yield + expected_metrics = [ + { + "author_id":387494208358, + "created_at":"2021-10-11T12:23:20.000000Z", + "id":910518732090, + "ticket_id":2 + }, + { + "author_id":387494208358, + "created_at":"2021-10-11T12:24:05.000000Z", + "id":910519204892, + "ticket_id":2, + } + ] + + # expected comment records after yield + expected_comments = [ + { + "author_id":387494208356, + "created_at":"2021-10-11T12:23:20.000000Z", + "id":910518732090, + "ticket_id":2 + }, + { + "author_id":387494208354, + "created_at":"2021-10-11T12:24:05.000000Z", + "id":910519204892, + "ticket_id":2, + } + ] + mock_metrics.return_value = True + mock_audits.return_value = True + mock_comments.return_value = True + mock_update_bookmark.side_effect = None + mock_metrics_sync.side_effect = mocked_sync_metrics + mock_audits_sync.side_effect = mocked_sync_audits + mock_comments_sync.side_effect = mocked_sync_comments + + expected_tickets = list(ticket_stream.sync(state={})) + audits = [] + metrics = [] + comments = [] + + # the yield returns a list with the first element as the parent stream tickets record + # and other elements as a tuple with the first element as the name of the stream and the second element + # as the record of that stream. Hence we are checking if each element of the stream and appending in our + # custom list and asserting all the lists at last. + for count, each in enumerate(expected_tickets): + if count == 0: + continue + if each[0] == 'ticket_audits': + audits.append(each[1]) + if each[0] == 'ticket_metrics': + metrics.append(each[1]) + if each[0] == 'ticket_comments': + comments.append(each[1]) + assert expected_audits == audits + assert expected_metrics == metrics + assert expected_comments == comments