Skip to content

Commit

Permalink
šŸ› Source Facebook Marketing: Removed validation that blocked personalā€¦
Browse files Browse the repository at this point in the history
ā€¦ ad accounts during `check` (#32731)
  • Loading branch information
tolik0 authored and git-phu committed Nov 28, 2023
1 parent 09f88c6 commit 56a876e
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data:
connectorSubtype: api
connectorType: source
definitionId: e7778cfc-e97c-4458-9ecb-b4f2bba8946c
dockerImageTag: 1.2.0
dockerImageTag: 1.2.1
dockerRepository: airbyte/source-facebook-marketing
documentationUrl: https://docs.airbyte.com/integrations/sources/facebook-marketing
githubIssueLabel: source-facebook-marketing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@


class SourceFacebookMarketing(AbstractSource):

# Skip exceptions on missing streams
raise_exception_on_missing_stream = False

Expand Down Expand Up @@ -96,15 +95,9 @@ def check_connection(self, logger: logging.Logger, config: Mapping[str, Any]) ->

api = API(account_id=config.account_id, access_token=config.access_token, page_size=config.page_size)

record_iterator = AdAccount(api=api).read_records(sync_mode=SyncMode.full_refresh, stream_state={})
account_info = list(record_iterator)[0]

if account_info.get("is_personal"):
message = (
"The personal ad account you're currently using is not eligible "
"for this operation. Please switch to a business ad account."
)
return False, message
# Get Ad Account to check creds
ad_account = api.account
logger.info(f"Select account {ad_account}")

except AirbyteTracedException as e:
return False, f"{e.message}. Full error: {e.internal_message}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def api_fixture(mocker):
return api_mock


@pytest.fixture(name="api_find_account")
def api_fixture_find_account(mocker):
api_mock = mocker.patch("source_facebook_marketing.source.API._find_account")
api_mock.return_value = "1234"
return api_mock


@pytest.fixture(name="logger_mock")
def logger_mock_fixture(mocker):
return mocker.patch("source_facebook_marketing.source.logger")
Expand All @@ -71,6 +78,15 @@ def test_check_connection_ok(self, config, logger_mock, fb_marketing):
assert ok
assert not error_msg

def test_check_connection_find_account_was_called(self, api_find_account, config, logger_mock, fb_marketing):
"""Check if _find_account was called to validate credentials"""
ok, error_msg = fb_marketing.check_connection(logger_mock, config=config)

api_find_account.assert_called_once_with(config["account_id"])
logger_mock.info.assert_called_once_with("Select account 1234")
assert ok
assert not error_msg

def test_check_connection_future_date_range(self, api, config, logger_mock, fb_marketing):
config["start_date"] = "2219-10-10T00:00:00"
config["end_date"] = "2219-10-11T00:00:00"
Expand All @@ -94,12 +110,12 @@ def test_check_connection_empty_config(self, api, logger_mock, fb_marketing):
assert not ok
assert error_msg

def test_check_connection_invalid_config(self, api, config, logger_mock, fb_marketing):
def test_check_connection_config_no_start_date(self, api, config, logger_mock, fb_marketing):
config.pop("start_date")
ok, error_msg = fb_marketing.check_connection(logger_mock, config=config)

assert not ok
assert error_msg
assert ok
assert not error_msg

def test_check_connection_exception(self, api, config, logger_mock, fb_marketing):
api.side_effect = RuntimeError("Something went wrong!")
Expand Down Expand Up @@ -180,18 +196,3 @@ def test_check_config(config_gen, requests_mock, fb_marketing):

assert command_check(fb_marketing, config_gen(end_date=...)) == AirbyteConnectionStatus(status=Status.SUCCEEDED, message=None)
assert command_check(fb_marketing, config_gen(end_date="")) == AirbyteConnectionStatus(status=Status.SUCCEEDED, message=None)


def test_check_connection_account_type_exception(mocker, fb_marketing, config, logger_mock, requests_mock):
account_id = "123"
ad_account_response = {"json": {"account_id": account_id, "id": f"act_{account_id}", "is_personal": 1}}
requests_mock.reset_mock()
requests_mock.register_uri("GET", f"{FacebookSession.GRAPH}/{FacebookAdsApi.API_VERSION}/act_123/", [ad_account_response])

result, error = fb_marketing.check_connection(logger=logger_mock, config=config)

assert not result
assert (
error
== "The personal ad account you're currently using is not eligible for this operation. Please switch to a business ad account."
)
Loading

0 comments on commit 56a876e

Please sign in to comment.