|
| 1 | +# Copyright (c) 2023 Airbyte, Inc., all rights reserved. |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +import requests |
| 7 | + |
| 8 | +from airbyte_cdk.sources.declarative.auth.token import BasicHttpAuthenticator, BearerAuthenticator |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "response_data, expected_records", |
| 13 | + [ |
| 14 | + # Test cases for IVRMenusRecordExtractor |
| 15 | + ( |
| 16 | + { |
| 17 | + "ivrs": [ |
| 18 | + {"id": "ivr_1", "menus": [{"id": "menu_1a", "name": "Menu 1A"}, {"id": "menu_1b", "name": "Menu 1B"}]}, |
| 19 | + {"id": "ivr_2", "menus": [{"id": "menu_2a", "name": "Menu 2A"}]}, |
| 20 | + ] |
| 21 | + }, |
| 22 | + [ |
| 23 | + {"ivr_id": "ivr_1", "id": "menu_1a", "name": "Menu 1A"}, |
| 24 | + {"ivr_id": "ivr_1", "id": "menu_1b", "name": "Menu 1B"}, |
| 25 | + {"ivr_id": "ivr_2", "id": "menu_2a", "name": "Menu 2A"}, |
| 26 | + ], |
| 27 | + ), |
| 28 | + ({"ivrs": []}, []), |
| 29 | + ({"ivrs": [{"id": "ivr_1", "menus": []}]}, []), |
| 30 | + ], |
| 31 | +) |
| 32 | +def test_ivr_menus_record_extractor(components_module, response_data, expected_records): |
| 33 | + IVRMenusRecordExtractor = components_module.IVRMenusRecordExtractor |
| 34 | + with patch("requests.get") as mock_get: |
| 35 | + mock_response = MagicMock() |
| 36 | + mock_response.json.return_value = response_data |
| 37 | + mock_get.return_value = mock_response |
| 38 | + response = requests.get("https://not-the-real.api/ivrs") |
| 39 | + |
| 40 | + extractor = IVRMenusRecordExtractor() |
| 41 | + records = extractor.extract_records(response) |
| 42 | + |
| 43 | + assert records == expected_records |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize( |
| 47 | + "response_data, expected_records", |
| 48 | + [ |
| 49 | + # Test cases for IVRRoutesRecordExtractor |
| 50 | + ( |
| 51 | + { |
| 52 | + "ivrs": [ |
| 53 | + { |
| 54 | + "id": "ivr_1", |
| 55 | + "menus": [ |
| 56 | + { |
| 57 | + "id": "menu_1a", |
| 58 | + "routes": [{"id": "route_1a1", "name": "Route 1A1"}, {"id": "route_1a2", "name": "Route 1A2"}], |
| 59 | + } |
| 60 | + ], |
| 61 | + } |
| 62 | + ] |
| 63 | + }, |
| 64 | + [ |
| 65 | + {"ivr_id": "ivr_1", "ivr_menu_id": "menu_1a", "id": "route_1a1", "name": "Route 1A1"}, |
| 66 | + {"ivr_id": "ivr_1", "ivr_menu_id": "menu_1a", "id": "route_1a2", "name": "Route 1A2"}, |
| 67 | + ], |
| 68 | + ), |
| 69 | + ({"ivrs": [{"id": "ivr_1", "menus": [{"id": "menu_1a", "routes": []}]}]}, []), |
| 70 | + ], |
| 71 | +) |
| 72 | +def test_ivr_routes_record_extractor(components_module, response_data, expected_records): |
| 73 | + IVRRoutesRecordExtractor = components_module.IVRRoutesRecordExtractor |
| 74 | + with patch("requests.get") as mock_get: |
| 75 | + mock_response = MagicMock() |
| 76 | + mock_response.json.return_value = response_data |
| 77 | + mock_get.return_value = mock_response |
| 78 | + response = requests.get("https://not-the-real.api/ivrs") |
| 79 | + |
| 80 | + extractor = IVRRoutesRecordExtractor() |
| 81 | + records = extractor.extract_records(response) |
| 82 | + |
| 83 | + assert records == expected_records |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize( |
| 87 | + "config, authenticator_type", |
| 88 | + [ |
| 89 | + ({"access_token": "dummy_token", "email": "[email protected]"}, BasicHttpAuthenticator), |
| 90 | + ({"credentials": {"auth_type": "api_token"}}, BasicHttpAuthenticator), |
| 91 | + ({"credentials": {"auth_type": "oauth2.0"}}, BearerAuthenticator), |
| 92 | + ], |
| 93 | +) |
| 94 | +def test_zendesk_talk_authenticator(components_module, config, authenticator_type): |
| 95 | + ZendeskTalkAuthenticator = components_module.ZendeskTalkAuthenticator |
| 96 | + legacy_basic_auth = MagicMock(spec=BasicHttpAuthenticator) |
| 97 | + basic_auth = MagicMock(spec=BasicHttpAuthenticator) |
| 98 | + oauth = MagicMock(spec=BearerAuthenticator) |
| 99 | + |
| 100 | + authenticator = ZendeskTalkAuthenticator(legacy_basic_auth, basic_auth, oauth, config) |
| 101 | + assert isinstance(authenticator, authenticator_type) |
| 102 | + |
| 103 | + |
| 104 | +def test_zendesk_talk_authenticator_invalid(components_module): |
| 105 | + ZendeskTalkAuthenticator = components_module.ZendeskTalkAuthenticator |
| 106 | + with pytest.raises(Exception) as excinfo: |
| 107 | + config = {"credentials": {"auth_type": "invalid"}} |
| 108 | + ZendeskTalkAuthenticator(None, None, None, config) |
| 109 | + assert "Missing valid authenticator" in str(excinfo.value) |
0 commit comments