def test_sync_accounts_new_and_existing_accounts( user: User, successful_installation_response: object) -> None: user_account = Account.objects.create( github_installation_id=1066615, github_account_login=user.github_login, github_account_id=user.github_id, github_account_type="User", ) AccountMembership.objects.create(user=user, account=user_account, role="member") # the user should get removed from this account when we sync. This tests # that our membership removal of installations a user no longer has access # to works. acme_corp_account = Account.objects.create( github_installation_id=79233, github_account_login="******", github_account_id=33803, github_account_type="Organization", ) AccountMembership.objects.create(user=user, account=acme_corp_account, role="member") assert Account.objects.count() == 2 assert AccountMembership.objects.filter(user=user).count() == 2 user.sync_accounts() assert Account.objects.filter( github_account_login__in=["recipeyak", "chdsbd", "ghost", "acme-corp"]) assert (Account.objects.count() == 4 ), "we should have a new account for recipeyak and chdsbd." assert (AccountMembership.objects.filter(user=user).exclude( account__github_account_login__in=["recipeyak", "chdsbd", "ghost"]). count() == 0), "we should have removed acme-corp." assert (AccountMembership.objects.filter( user=user, role="member", account__github_account_login="******").count() == 1) assert (AccountMembership.objects.filter( user=user, role="admin", account__github_account_login="******").count() == 1) assert ( AccountMembership.objects.filter( user=user, account=acme_corp_account).exists() is False ), "the user should no longer be a member of the organization if is no longer returned from `/user/installations` endpoint." assert ( Account.objects.filter(id=acme_corp_account.id).exists() is True ), "account that we are no longer a member of should not be deleted."
def test_sync_accounts_failing_api_request_collaborator( user: User, failing_installation_response_membership_check: object) -> None: """ If the user is a collaborator of an organization they will get an API error when testing membership. We should ignore the error and not add them to that organization. """ assert Account.objects.count() == 0 assert AccountMembership.objects.count() == 0 assert User.objects.count() == 1 user.sync_accounts() assert (Account.objects.filter( github_account_login__in=["ghost", "chdsbd", "recipeyak"]).count() == Account.objects.count() == 3) assert AccountMembership.objects.count() == 3 assert User.objects.count() == 1
def test_sync_accounts_failing_api_request( user: User, failing_installation_response: object) -> None: with pytest.raises(SyncAccountsError): user.sync_accounts()