|
| 1 | +# Copyright (c) 2023 Airbyte, Inc., all rights reserved. |
| 2 | + |
| 3 | +import copy |
| 4 | +from datetime import datetime, timedelta |
| 5 | +from typing import Any, Dict, List, Optional |
| 6 | + |
| 7 | +import freezegun |
| 8 | +import pytz |
| 9 | +from airbyte_cdk.test.catalog_builder import CatalogBuilder |
| 10 | +from airbyte_cdk.test.entrypoint_wrapper import EntrypointOutput, read |
| 11 | +from airbyte_cdk.test.mock_http import HttpMocker |
| 12 | +from airbyte_cdk.test.mock_http.response_builder import FieldPath, HttpResponseBuilder, RecordBuilder, create_record_builder, find_template |
| 13 | +from airbyte_protocol.models import AirbyteStateMessage, SyncMode |
| 14 | +from source_hubspot import SourceHubspot |
| 15 | + |
| 16 | +from .config_builder import ConfigBuilder |
| 17 | +from .request_builders.api import CustomObjectsRequestBuilder, OAuthRequestBuilder, PropertiesRequestBuilder, ScopesRequestBuilder |
| 18 | +from .request_builders.streams import CRMStreamRequestBuilder, IncrementalCRMStreamRequestBuilder, WebAnalyticsRequestBuilder |
| 19 | +from .response_builder.helpers import RootHttpResponseBuilder |
| 20 | +from .response_builder.api import ScopesResponseBuilder |
| 21 | +from .response_builder.streams import GenericResponseBuilder, HubspotStreamResponseBuilder |
| 22 | + |
| 23 | + |
| 24 | +@freezegun.freeze_time("2024-03-03T14:42:00Z") |
| 25 | +class HubspotTestCase: |
| 26 | + DT_FORMAT = '%Y-%m-%dT%H:%M:%SZ' |
| 27 | + OBJECT_ID = "testID" |
| 28 | + ACCESS_TOKEN = "new_access_token" |
| 29 | + CURSOR_FIELD = "occurredAt" |
| 30 | + PROPERTIES = { |
| 31 | + "closed_date": "datetime", |
| 32 | + "createdate": "datetime", |
| 33 | + } |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def now(cls): |
| 37 | + return datetime.now(pytz.utc) |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def start_date(cls): |
| 41 | + return cls.now() - timedelta(days=30) |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def updated_at(cls): |
| 45 | + return cls.now() - timedelta(days=1) |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def dt_str(cls, dt: datetime.date) -> str: |
| 49 | + return dt.strftime(cls.DT_FORMAT) |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def oauth_config(cls, start_date: Optional[str] = None) -> Dict[str, Any]: |
| 53 | + start_date = start_date or cls.dt_str(cls.start_date()) |
| 54 | + return ConfigBuilder().with_start_date(start_date).with_auth( |
| 55 | + { |
| 56 | + "credentials_title": "OAuth Credentials", |
| 57 | + "redirect_uri": "https://airbyte.io", |
| 58 | + "client_id": "client_id", |
| 59 | + "client_secret": "client_secret", |
| 60 | + "refresh_token": "refresh_token", |
| 61 | + } |
| 62 | + ).build() |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def private_token_config(cls, token: str, start_date: Optional[str] = None) -> Dict[str, Any]: |
| 66 | + start_date = start_date or cls.dt_str(cls.start_date()) |
| 67 | + return ConfigBuilder().with_start_date(start_date).with_auth( |
| 68 | + { |
| 69 | + "credentials_title": "Private App Credentials", |
| 70 | + "access_token": token, |
| 71 | + } |
| 72 | + ).build() |
| 73 | + |
| 74 | + @classmethod |
| 75 | + def mock_oauth(cls, http_mocker: HttpMocker, token: str): |
| 76 | + creds = cls.oauth_config()["credentials"] |
| 77 | + req = OAuthRequestBuilder().with_client_id( |
| 78 | + creds["client_id"] |
| 79 | + ).with_client_secret( |
| 80 | + creds["client_secret"] |
| 81 | + ).with_refresh_token( |
| 82 | + creds["refresh_token"] |
| 83 | + ).build() |
| 84 | + response = GenericResponseBuilder().with_value("access_token", token).with_value("expires_in", 7200).build() |
| 85 | + http_mocker.post(req, response) |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def mock_scopes(cls, http_mocker: HttpMocker, token: str, scopes: List[str]): |
| 89 | + http_mocker.get(ScopesRequestBuilder().with_access_token(token).build(), ScopesResponseBuilder(scopes).build()) |
| 90 | + |
| 91 | + @classmethod |
| 92 | + def mock_custom_objects(cls, http_mocker: HttpMocker): |
| 93 | + http_mocker.get( |
| 94 | + CustomObjectsRequestBuilder().build(), |
| 95 | + HttpResponseBuilder({}, records_path=FieldPath("results"), pagination_strategy=None).build() |
| 96 | + ) |
| 97 | + |
| 98 | + @classmethod |
| 99 | + def mock_properties(cls, http_mocker: HttpMocker, object_type: str, properties: Dict[str, str]): |
| 100 | + templates = find_template("properties", __file__) |
| 101 | + record_builder = lambda: RecordBuilder(copy.deepcopy(templates[0]), id_path=None, cursor_path=None) |
| 102 | + |
| 103 | + response_builder = RootHttpResponseBuilder(templates) |
| 104 | + for name, type in properties.items(): |
| 105 | + record = record_builder().with_field(FieldPath("name"), name).with_field(FieldPath("type"), type) |
| 106 | + response_builder = response_builder.with_record(record) |
| 107 | + |
| 108 | + http_mocker.get( |
| 109 | + PropertiesRequestBuilder().for_entity(object_type).build(), |
| 110 | + response_builder.build() |
| 111 | + ) |
| 112 | + |
| 113 | + @classmethod |
| 114 | + def mock_response(cls, http_mocker: HttpMocker, request, responses, method: str = "get"): |
| 115 | + if not isinstance(responses, (list, tuple)): |
| 116 | + responses = [responses] |
| 117 | + getattr(http_mocker, method)(request, responses) |
| 118 | + |
| 119 | + @classmethod |
| 120 | + def record_builder(cls, stream: str, record_cursor_path): |
| 121 | + return create_record_builder( |
| 122 | + find_template(stream, __file__), records_path=FieldPath("results"), record_id_path=None, record_cursor_path=record_cursor_path |
| 123 | + ) |
| 124 | + |
| 125 | + @classmethod |
| 126 | + def catalog(cls, stream: str, sync_mode: SyncMode): |
| 127 | + return CatalogBuilder().with_stream(stream, sync_mode).build() |
| 128 | + |
| 129 | + @classmethod |
| 130 | + def read_from_stream( |
| 131 | + cls, cfg, stream: str, sync_mode: SyncMode, state: Optional[List[AirbyteStateMessage]] = None, expecting_exception: bool = False |
| 132 | + ) -> EntrypointOutput: |
| 133 | + return read(SourceHubspot(), cfg, cls.catalog(stream, sync_mode), state, expecting_exception) |
0 commit comments