Skip to content

Commit

Permalink
migrator: add support for gh connect action
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Panero committed Sep 20, 2023
1 parent 9eb6cdb commit e87a6b2
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 77 deletions.
211 changes: 163 additions & 48 deletions migrator/tests/actions/oauth/test_oauth_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,40 +660,87 @@ def connect_orcid_oauth_application_tx():
return {"tx_id": 1, "operations": ops}


@pytest.fixture()
def connect_gh_oauth_application_tx():
"""Transaction data to connect an OAuth ORCID account.
As it would be after the extraction step.
"""
datafile = (
Path(__file__).parent / "testdata" / "linked_accounts" / "connect_gh.jsonl"
)
with open(datafile, "rb") as reader:
ops = [orjson.loads(line)["value"] for line in reader]

return {"tx_id": 1, "operations": ops}


class TestOAuthLinkedAccountConnectAction:
"""Connect an OAuth account action tests."""

def test_matches_with_valid_data(self):
assert (
OAuthLinkedAccountConnectAction.matches_action(
Tx(
id=1,
operations=[
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remotetoken"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_useridentity"},
"after": {},
},
{
"op": OperationType.UPDATE,
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
],
full = [
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remotetoken"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_useridentity"},
"after": {},
},
{
"op": OperationType.UPDATE,
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauth2server_client"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauth2server_token"},
"after": {},
},
{
"op": OperationType.UPDATE,
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
]
minimal = [
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remotetoken"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_useridentity"},
"after": {},
},
]

for valid_ops in [minimal]:
assert (
OAuthLinkedAccountConnectAction.matches_action(
Tx(id=1, operations=valid_ops)
)
is True
)
is True
)

def test_matches_with_invalid_data(self):
empty = []
Expand All @@ -709,14 +756,35 @@ def test_matches_with_invalid_data(self):
"source": {"table": "oauthclient_useridentity"},
"after": {},
},
]

no_token = [
{
"op": OperationType.UPDATE,
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_useridentity"},
"after": {},
},
]

no_account_update = [
no_user_identity = [
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remotetoken"},
"after": {},
},
]

no_server_token = [
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
Expand All @@ -732,9 +800,14 @@ def test_matches_with_invalid_data(self):
"source": {"table": "oauthclient_useridentity"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauth2server_client"},
"after": {},
},
]

double_insert = [
no_server_client = [
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
Expand All @@ -752,14 +825,14 @@ def test_matches_with_invalid_data(self):
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
"source": {"table": "oauth2server_token"},
"after": {},
},
]

double_update = [
wrong_update_op = [
{
"op": OperationType.UPDATE,
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
Expand All @@ -778,14 +851,34 @@ def test_matches_with_invalid_data(self):
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauth2server_client"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauth2server_token"},
"after": {},
},
{
"op": OperationType.UPDATE, # wrong
"source": {"table": "oauth2server_token"},
"after": {},
},
]

no_token = [
extra_update = [
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remotetoken"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_useridentity"},
Expand All @@ -796,27 +889,24 @@ def test_matches_with_invalid_data(self):
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
]

no_user_identity = [
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
"source": {"table": "oauth2server_client"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remotetoken"},
"source": {"table": "oauth2server_token"},
"after": {},
},
{
"op": OperationType.UPDATE,
"source": {"table": "oauthclient_remoteaccount"},
"source": {"table": "another"},
"after": {},
},
]

wrong_op = [
extra_insert = [
{
"op": OperationType.INSERT,
"source": {"table": "oauthclient_remoteaccount"},
Expand All @@ -828,7 +918,7 @@ def test_matches_with_invalid_data(self):
"after": {},
},
{
"op": OperationType.UPDATE,
"op": OperationType.INSERT,
"source": {"table": "oauthclient_useridentity"},
"after": {},
},
Expand All @@ -837,17 +927,33 @@ def test_matches_with_invalid_data(self):
"source": {"table": "oauthclient_remoteaccount"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauth2server_client"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "oauth2server_token"},
"after": {},
},
{
"op": OperationType.INSERT,
"source": {"table": "another"},
"after": {},
},
]

for invalid_ops in [
empty,
no_account,
no_account_update,
double_insert,
double_update,
no_token,
no_user_identity,
wrong_op,
no_server_token,
no_server_client,
wrong_update_op,
extra_update,
extra_insert,
]:
assert (
OAuthLinkedAccountConnectAction.matches_action(
Expand All @@ -856,7 +962,7 @@ def test_matches_with_invalid_data(self):
is False
)

def test_transform_with_valid_data(self, connect_orcid_oauth_application_tx):
def test_transform_with_valid_orcid_data(self, connect_orcid_oauth_application_tx):
action = OAuthLinkedAccountConnectAction(
Tx(
id=connect_orcid_oauth_application_tx["tx_id"],
Expand All @@ -865,6 +971,15 @@ def test_transform_with_valid_data(self, connect_orcid_oauth_application_tx):
)
assert isinstance(action.transform(), load.OAuthLinkedAccountConnectAction)

def test_transform_with_valid_gh_data(self, connect_gh_oauth_application_tx):
action = OAuthLinkedAccountConnectAction(
Tx(
id=connect_gh_oauth_application_tx["tx_id"],
operations=connect_gh_oauth_application_tx["operations"],
)
)
assert isinstance(action.transform(), load.OAuthLinkedAccountConnectAction)


@pytest.fixture()
def disconnect_orcid_oauth_application_tx():
Expand Down
30 changes: 16 additions & 14 deletions migrator/tests/actions/oauth/test_oauth_actions_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,22 @@ def test_oauth_linked_app_disconnect_orcid_action_stream(
#


# def test_oauth_linked_app_connect_gh_action_stream(
# db_client_server, pg_tx_load, test_extract_cls, tx_files_linked_accounts
# ):
# stream = Stream(
# name="action",
# extract=test_extract_cls(tx_files_linked_accounts["connect_gh"]),
# transform=ZenodoTxTransform(),
# load=pg_tx_load,
# )
# stream.run()

# assert db_client_server.scalars(sa.select(RemoteAccount)).one()
# assert db_client_server.scalars(sa.select(RemoteToken)).one()
# assert db_client_server.scalars(sa.select(UserIdentity)).one()
def test_oauth_linked_app_connect_gh_action_stream(
db_client_server, pg_tx_load, test_extract_cls, tx_files_linked_accounts
):
stream = Stream(
name="action",
extract=test_extract_cls(tx_files_linked_accounts["connect_gh"]),
transform=ZenodoTxTransform(),
load=pg_tx_load,
)
stream.run()

assert db_client_server.scalars(sa.select(RemoteAccount)).one()
assert db_client_server.scalars(sa.select(RemoteToken)).one()
assert db_client_server.scalars(sa.select(UserIdentity)).one()
assert db_client_server.scalars(sa.select(ServerClient)).one()
assert db_client_server.scalars(sa.select(ServerToken)).one()


@pytest.fixture(scope="function")
Expand Down

Large diffs are not rendered by default.

Empty file.
Loading

0 comments on commit e87a6b2

Please sign in to comment.