-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gsheets2): passed secrets as param
- Loading branch information
1 parent
35b7e72
commit 4ee2c12
Showing
5 changed files
with
110 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from functools import partial | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
@@ -8,6 +9,7 @@ | |
from toucan_connectors.google_sheets_2.google_sheets_2_connector import ( | ||
GoogleSheets2Connector, | ||
GoogleSheets2DataSource, | ||
NoCredentialsError, | ||
) | ||
|
||
import_path = 'toucan_connectors.google_sheets_2.google_sheets_2_connector' | ||
|
@@ -18,12 +20,6 @@ def con(): | |
return GoogleSheets2Connector(name='test_name') | ||
|
||
|
||
@fixture | ||
def con_with_secrets(con): | ||
con.set_secrets({'access_token': 'foo', 'refresh_token': None}) | ||
return con | ||
|
||
|
||
@fixture | ||
def ds(): | ||
return GoogleSheets2DataSource( | ||
|
@@ -43,6 +39,14 @@ def ds_without_sheet(): | |
) | ||
|
||
|
||
@fixture | ||
def fake_kwargs(): | ||
def fake_fetch_secrets(small_app_id, connector_type, auth_flow_id): | ||
return {'access_token': 'myaccesstoken'} | ||
|
||
return {'secrets': partial(fake_fetch_secrets, 'laputa', 'GoogleSheets2')} | ||
|
||
|
||
FAKE_SHEET = { | ||
'metadata': '...', | ||
'values': [['country', 'city'], ['France', 'Paris'], ['England', 'London']], | ||
|
@@ -85,13 +89,14 @@ def get_columns_in_schema(schema): | |
return None | ||
|
||
|
||
def test_get_form_with_secrets(mocker, con_with_secrets, ds): | ||
def test_get_form_with_secrets(mocker, con, ds, fake_kwargs): | ||
"""It should return a list of spreadsheet titles.""" | ||
mocker.patch.object(GoogleSheets2Connector, '_run_fetch', return_value=FAKE_SHEET_LIST_RESPONSE) | ||
|
||
result = ds.get_form( | ||
connector=con_with_secrets, | ||
connector=con, | ||
current_config={'spreadsheet_id': '1SMnhnmBm-Tup3SfhS03McCf6S4pS2xqjI6CAXSSBpHU'}, | ||
**fake_kwargs, | ||
) | ||
expected_results = ['Foo', 'Bar', 'Baz'] | ||
assert get_columns_in_schema(result) == expected_results | ||
|
@@ -107,58 +112,49 @@ def test_get_form_no_secrets(mocker, con, ds): | |
assert not get_columns_in_schema(result) | ||
|
||
|
||
def test_set_secrets(mocker, con): | ||
"""It should set secrets on the connector.""" | ||
spy = mocker.spy(GoogleSheets2Connector, 'set_secrets') | ||
fake_secrets = { | ||
'access_token': 'myaccesstoken', | ||
'refresh_token': None, | ||
} | ||
con.set_secrets(fake_secrets) | ||
|
||
assert con.secrets == fake_secrets | ||
spy.assert_called_once_with(con, fake_secrets) | ||
|
||
|
||
def test_spreadsheet_success(mocker, con_with_secrets, ds): | ||
def test_spreadsheet_success(mocker, con, ds, fake_kwargs): | ||
"""It should return a spreadsheet.""" | ||
mocker.patch.object(GoogleSheets2Connector, '_run_fetch', return_value=FAKE_SHEET) | ||
|
||
df = con_with_secrets.get_df(ds) | ||
df = con.get_df(ds, **fake_kwargs) | ||
|
||
assert df.shape == (2, 2) | ||
assert df.columns.tolist() == ['country', 'city'] | ||
|
||
ds.header_row = 1 | ||
df = con_with_secrets.get_df(ds) | ||
df = con.get_df(ds, **fake_kwargs) | ||
assert df.shape == (1, 2) | ||
assert df.columns.tolist() == ['France', 'Paris'] | ||
|
||
|
||
def test_spreadsheet_no_secrets(mocker, con, ds): | ||
"""It should raise an exception if there no secrets passed or no access token.""" | ||
"""It should raise an exception if there are no secrets returned or at all.""" | ||
mocker.patch.object(GoogleSheets2Connector, '_run_fetch', return_value=FAKE_SHEET) | ||
|
||
with pytest.raises(Exception) as err: | ||
con.get_df(ds) | ||
bogus_fake_kwargs = {'secrets': None} | ||
with pytest.raises(NoCredentialsError) as err: | ||
con.get_df(ds, **bogus_fake_kwargs) | ||
|
||
assert str(err.value) == 'No credentials' | ||
|
||
con.set_secrets({'refresh_token': None}) | ||
# Function that returns an empty dict, as if when no document is found in database | ||
def fake_fetch_secrets(small_app_id, connector_type, auth_flow_id): | ||
return {} | ||
|
||
empty_secrets_kwargs = {'secrets': partial(fake_fetch_secrets, 'laputa', 'GoogleSheets2')} | ||
|
||
with pytest.raises(KeyError): | ||
con.get_df(ds) | ||
with pytest.raises(NoCredentialsError): | ||
con.get_df(ds, **empty_secrets_kwargs) | ||
|
||
|
||
def test_set_columns(mocker, con_with_secrets, ds): | ||
def test_set_columns(mocker, con, ds, fake_kwargs): | ||
"""It should return a well-formed column set.""" | ||
fake_results = { | ||
'metadata': '...', | ||
'values': [['Animateur', '', '', 'Week'], ['pika', '', 'a', 'W1'], ['bulbi', '', '', 'W2']], | ||
} | ||
mocker.patch.object(GoogleSheets2Connector, '_run_fetch', return_value=fake_results) | ||
|
||
df = con_with_secrets.get_df(ds) | ||
df = con.get_df(ds, **fake_kwargs) | ||
assert df.to_dict() == { | ||
'Animateur': {1: 'pika', 2: 'bulbi'}, | ||
1: {1: '', 2: ''}, | ||
|
@@ -178,7 +174,7 @@ def test__run_fetch(mocker, con): | |
assert result == FAKE_SHEET | ||
|
||
|
||
def test_spreadsheet_without_sheet(mocker, con_with_secrets, ds_without_sheet): | ||
def test_spreadsheet_without_sheet(mocker, con, ds_without_sheet, fake_kwargs): | ||
""" | ||
It should retrieve the first sheet of the spreadsheet if no sheet has been indicated | ||
""" | ||
|
@@ -192,7 +188,7 @@ def mock_api_responses(uri: str, _token): | |
fetch_mock: Mock = mocker.patch.object( | ||
GoogleSheets2Connector, '_run_fetch', side_effect=mock_api_responses | ||
) | ||
df = con_with_secrets.get_df(ds_without_sheet) | ||
df = con.get_df(ds_without_sheet, **fake_kwargs) | ||
|
||
assert fetch_mock.call_count == 2 | ||
assert ( | ||
|
@@ -215,27 +211,27 @@ def test_get_status_no_secrets(mocker, con): | |
assert con.get_status().status is False | ||
|
||
|
||
def test_get_status_success(mocker, con_with_secrets): | ||
def test_get_status_success(mocker, con, fake_kwargs): | ||
""" | ||
It should fail if no secrets are provided | ||
""" | ||
fetch_mock: Mock = mocker.patch.object( | ||
GoogleSheets2Connector, '_run_fetch', return_value={'email': '[email protected]'} | ||
) | ||
|
||
connector_status = con_with_secrets.get_status() | ||
connector_status = con.get_status(**fake_kwargs) | ||
assert connector_status.status is True | ||
assert '[email protected]' in connector_status.message | ||
|
||
fetch_mock.assert_called_once_with( | ||
'https://www.googleapis.com/oauth2/v2/userinfo?alt=json', 'foo' | ||
'https://www.googleapis.com/oauth2/v2/userinfo?alt=json', 'myaccesstoken' | ||
) | ||
|
||
|
||
def test_get_status_api_down(mocker, con_with_secrets): | ||
def test_get_status_api_down(mocker, con, fake_kwargs): | ||
""" | ||
It should fail if no secrets are provided | ||
""" | ||
mocker.patch.object(GoogleSheets2Connector, '_run_fetch', side_effect=HttpError) | ||
|
||
assert con_with_secrets.get_status().status is False | ||
assert con.get_status(**fake_kwargs).status is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.