From c7254816cf782932647c8aee436bd43653ef808c Mon Sep 17 00:00:00 2001 From: prijendev Date: Wed, 24 Aug 2022 14:53:12 +0530 Subject: [PATCH 1/4] Added warning message for pro account plan. --- tap_freshdesk/client.py | 7 ++- tests/unittests/test_check_access_token.py | 56 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/unittests/test_check_access_token.py diff --git a/tap_freshdesk/client.py b/tap_freshdesk/client.py index 2800295..fd463a2 100644 --- a/tap_freshdesk/client.py +++ b/tap_freshdesk/client.py @@ -134,7 +134,12 @@ def check_access_token(self): """ Check if the access token is valid. """ - self.request(self.base_url+"/api/v2/roles", {"per_page": 1, "page": 1}) + try: + self.request(self.base_url+"/api/v2/tickets/1/time_entries", {"per_page": 1, "page": 1}) + except FresdeskAccessDeniedError: + LOGGER.warning("The `Surveys` and the `Timesheets` features are not supported in your current plan. "\ + "So, Data collection cannot be initiated for satisfaction_ratings and time_entries streams. "\ + "Please upgrade your account to `Pro` plan to use it.") @backoff.on_exception(backoff.expo, (TimeoutError, ConnectionError, Server5xxError), diff --git a/tests/unittests/test_check_access_token.py b/tests/unittests/test_check_access_token.py new file mode 100644 index 0000000..bbbab2d --- /dev/null +++ b/tests/unittests/test_check_access_token.py @@ -0,0 +1,56 @@ +import unittest +from unittest import mock + +from tap_freshdesk import client +from parameterized import parameterized + + +@mock.patch("tap_freshdesk.client.FreshdeskClient.request") +class TestAccessToken(unittest.TestCase): + """ + Test case to verify the `check_access_token` method. + """ + @parameterized.expand([ + ('free_account_with_invalid_token', client.FresdeskAuthenticationError, 'The Authorization header is either missing or incorrect.'), + ('free_account_with_invalid_domain', client.FresdeskNotFoundError, 'The request contains invalid ID/Freshdesk domain in the URL or an invalid URL itself.') + ]) + def test_invalid_domain_or_token(self, mock_request, name, error, err_message): + """ + Verify that tap raise error for an invalid token or invalid domain name. + """ + config = {} + mock_request.side_effect = error(err_message) + _client = client.FreshdeskClient(config) + with self.assertRaises(error) as e: + _client.check_access_token() + + # Verify that an error message is expected + self.assertEqual(str(e.exception), err_message) + + @mock.patch("tap_freshdesk.client.LOGGER.warning") + def test_free_account(self, mock_request, mock_logger): + """ + Verify that tap provide a warning message to upgrade the current free account to the pro account. + Because `satisfaction_ratings` and `time_entries` streams are accessible only for the pro account. + """ + config = {} + mock_request.side_effect = client.FresdeskAccessDeniedError + _client = client.FreshdeskClient(config) + + _client.check_access_token() + + # Verify that `LOGGER.warning` is called for 1 time. + self.assertEqual(mock_logger.call_count, 1) + + @mock.patch("tap_freshdesk.client.LOGGER.warning") + def test_pro_account_plan(self, mock_logger, mock_request): + """ + Verify that tap does not raise any error or provide any warning message for the pro account plan. + """ + config = {} + _client = client.FreshdeskClient(config) + + _client.check_access_token() + + # Verify that `LOGGER.warning` is not called. + self.assertEqual(mock_logger.call_count, 0) From a93a5cfe2bc6ad53d5c657668642305b28ce3455 Mon Sep 17 00:00:00 2001 From: prijendev Date: Thu, 25 Aug 2022 14:23:32 +0530 Subject: [PATCH 2/4] Added assertion to verify logger message. --- tap_freshdesk/client.py | 2 +- tests/unittests/test_check_access_token.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tap_freshdesk/client.py b/tap_freshdesk/client.py index 3396459..1ea1c9d 100644 --- a/tap_freshdesk/client.py +++ b/tap_freshdesk/client.py @@ -150,7 +150,7 @@ def check_access_token(self): """ try: self.request(self.base_url+"/api/v2/tickets/1/time_entries", {"per_page": 1, "page": 1}) - except FresdeskAccessDeniedError: + except FreshdeskAccessDeniedError: LOGGER.warning("The `Surveys` and the `Timesheets` features are not supported in your current plan. "\ "So, Data collection cannot be initiated for satisfaction_ratings and time_entries streams. "\ "Please upgrade your account to `Pro` plan to use it.") diff --git a/tests/unittests/test_check_access_token.py b/tests/unittests/test_check_access_token.py index bbbab2d..799966f 100644 --- a/tests/unittests/test_check_access_token.py +++ b/tests/unittests/test_check_access_token.py @@ -11,8 +11,8 @@ class TestAccessToken(unittest.TestCase): Test case to verify the `check_access_token` method. """ @parameterized.expand([ - ('free_account_with_invalid_token', client.FresdeskAuthenticationError, 'The Authorization header is either missing or incorrect.'), - ('free_account_with_invalid_domain', client.FresdeskNotFoundError, 'The request contains invalid ID/Freshdesk domain in the URL or an invalid URL itself.') + ('free_account_with_invalid_token', client.FreshdeskAuthenticationError, 'The Authorization header is either missing or incorrect.'), + ('free_account_with_invalid_domain', client.FreshdeskNotFoundError, 'The request contains invalid ID/Freshdesk domain in the URL or an invalid URL itself.') ]) def test_invalid_domain_or_token(self, mock_request, name, error, err_message): """ @@ -28,13 +28,13 @@ def test_invalid_domain_or_token(self, mock_request, name, error, err_message): self.assertEqual(str(e.exception), err_message) @mock.patch("tap_freshdesk.client.LOGGER.warning") - def test_free_account(self, mock_request, mock_logger): + def test_free_account(self, mock_logger, mock_request): """ Verify that tap provide a warning message to upgrade the current free account to the pro account. Because `satisfaction_ratings` and `time_entries` streams are accessible only for the pro account. """ config = {} - mock_request.side_effect = client.FresdeskAccessDeniedError + mock_request.side_effect = client.FreshdeskAccessDeniedError _client = client.FreshdeskClient(config) _client.check_access_token() @@ -42,6 +42,11 @@ def test_free_account(self, mock_request, mock_logger): # Verify that `LOGGER.warning` is called for 1 time. self.assertEqual(mock_logger.call_count, 1) + # Verify expected warning message. + mock_logger.assert_called_with('The `Surveys` and the `Timesheets` features are not supported in your current plan. '\ + 'So, Data collection cannot be initiated for satisfaction_ratings and time_entries streams. Please upgrade your '\ + 'account to `Pro` plan to use it.') + @mock.patch("tap_freshdesk.client.LOGGER.warning") def test_pro_account_plan(self, mock_logger, mock_request): """ From 180a499515ad748eb54d51274c6cf5bb738d27f0 Mon Sep 17 00:00:00 2001 From: prijendev Date: Thu, 25 Aug 2022 15:04:40 +0530 Subject: [PATCH 3/4] Updated warning message. --- tap_freshdesk/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tap_freshdesk/client.py b/tap_freshdesk/client.py index 1ea1c9d..165b6fd 100644 --- a/tap_freshdesk/client.py +++ b/tap_freshdesk/client.py @@ -151,9 +151,9 @@ def check_access_token(self): try: self.request(self.base_url+"/api/v2/tickets/1/time_entries", {"per_page": 1, "page": 1}) except FreshdeskAccessDeniedError: - LOGGER.warning("The `Surveys` and the `Timesheets` features are not supported in your current plan. "\ + LOGGER.warning("The `Surveys` and the `Timesheets` features might not be supported in your current plan. "\ "So, Data collection cannot be initiated for satisfaction_ratings and time_entries streams. "\ - "Please upgrade your account to `Pro` plan to use it.") + "Upgrade the current account to the Pro plan will work if this is the case. So, please check your account's plan.") @backoff.on_exception(backoff.expo, (TimeoutError, ConnectionError, Server5xxError), From ef36eb52ea1aea52ae59c75aa8afc0e8680ba63c Mon Sep 17 00:00:00 2001 From: prijendev Date: Wed, 21 Sep 2022 12:15:33 +0530 Subject: [PATCH 4/4] Updated unit tests. --- tests/unittests/test_check_access_token.py | 7 ++++--- tests/unittests/test_client.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/unittests/test_check_access_token.py b/tests/unittests/test_check_access_token.py index 799966f..c58c3fe 100644 --- a/tests/unittests/test_check_access_token.py +++ b/tests/unittests/test_check_access_token.py @@ -43,9 +43,10 @@ def test_free_account(self, mock_logger, mock_request): self.assertEqual(mock_logger.call_count, 1) # Verify expected warning message. - mock_logger.assert_called_with('The `Surveys` and the `Timesheets` features are not supported in your current plan. '\ - 'So, Data collection cannot be initiated for satisfaction_ratings and time_entries streams. Please upgrade your '\ - 'account to `Pro` plan to use it.') + mock_logger.assert_called_with("The `Surveys` and the `Timesheets` features might not be supported in your current plan. "\ + "So, Data collection cannot be initiated for satisfaction_ratings and time_entries streams. "\ + "Upgrade the current account to the Pro plan will work if this is the case. "\ + "So, please check your account's plan.") @mock.patch("tap_freshdesk.client.LOGGER.warning") def test_pro_account_plan(self, mock_logger, mock_request): diff --git a/tests/unittests/test_client.py b/tests/unittests/test_client.py index de677a8..7b0913f 100644 --- a/tests/unittests/test_client.py +++ b/tests/unittests/test_client.py @@ -33,4 +33,4 @@ def test_access_token(self, mock_request): # Verify that for check access token, `request` method was called self.assertTrue(mock_request.called) - mock_request.assert_called_with("https://sampleDomain.freshdesk.com/api/v2/roles", mock.ANY) + mock_request.assert_called_with('https://sampleDomain.freshdesk.com/api/v2/tickets/1/time_entries', {'per_page': 1, 'page': 1})