async def test_redirect_url_router( self, mock_user_db_oauth, test_app_client_redirect_url: httpx.AsyncClient, oauth_client, user_oauth, ): state_jwt = generate_state_token({"authentication_backend": "mock"}, "SECRET") with asynctest.patch.object( oauth_client, "get_access_token" ) as get_access_token_mock: get_access_token_mock.return_value = { "access_token": "TOKEN", "expires_at": 1579179542, } with asynctest.patch.object( oauth_client, "get_id_email" ) as get_id_email_mock: get_id_email_mock.return_value = ("user_oauth1", user_oauth.email) response = await test_app_client_redirect_url.get( "/callback", params={"code": "CODE", "state": state_jwt}, ) get_access_token_mock.assert_awaited_once_with( "CODE", "http://www.tintagel.bt/callback" ) data = cast(Dict[str, Any], response.json()) assert data["token"] == str(user_oauth.id)
async def test_existing_user_with_oauth( self, mock_user_db_oauth, test_app_client: httpx.AsyncClient, oauth_client, user_oauth, after_register, ): state_jwt = generate_state_token({"authentication_backend": "mock"}, "SECRET") with asynctest.patch.object( oauth_client, "get_access_token" ) as get_access_token_mock: get_access_token_mock.return_value = { "access_token": "TOKEN", "expires_at": 1579179542, } with asynctest.patch.object( oauth_client, "get_id_email" ) as get_id_email_mock: with asynctest.patch.object( mock_user_db_oauth, "update" ) as user_update_mock: get_id_email_mock.return_value = ("user_oauth1", user_oauth.email) response = await test_app_client.get( "/callback", params={"code": "CODE", "state": state_jwt}, ) get_id_email_mock.assert_awaited_once_with("TOKEN") user_update_mock.assert_awaited_once() data = cast(Dict[str, Any], response.json()) assert data["token"] == str(user_oauth.id) assert after_register.called is False
async def test_inactive_user( self, mock_user_db_oauth, test_app_client: httpx.AsyncClient, oauth_client, inactive_user_oauth, after_register, ): state_jwt = generate_state_token({"authentication_backend": "mock"}, "SECRET") with asynctest.patch.object( oauth_client, "get_access_token" ) as get_access_token_mock: get_access_token_mock.return_value = { "access_token": "TOKEN", "expires_at": 1579179542, } with asynctest.patch.object( oauth_client, "get_id_email" ) as get_id_email_mock: get_id_email_mock.return_value = ( "inactive_user_oauth1", inactive_user_oauth.email, ) response = await test_app_client.get( "/callback", params={"code": "CODE", "state": state_jwt}, ) assert response.status_code == status.HTTP_400_BAD_REQUEST data = cast(Dict[str, Any], response.json()) assert data["detail"] == ErrorCode.LOGIN_BAD_CREDENTIALS assert after_register.called is False
async def test_redirect_url_router( self, async_method_mocker: AsyncMethodMocker, test_app_client_redirect_url: httpx.AsyncClient, oauth_client: BaseOAuth2, user_oauth: UserDB, user_manager_oauth: UserManagerMock, access_token: str, ): state_jwt = generate_state_token({}, "SECRET") get_access_token_mock = async_method_mocker(oauth_client, "get_access_token", return_value=access_token) async_method_mocker(oauth_client, "get_id_email", return_value=("user_oauth1", user_oauth.email)) async_method_mocker(user_manager_oauth, "oauth_callback", return_value=user_oauth) response = await test_app_client_redirect_url.get( "/callback", params={ "code": "CODE", "state": state_jwt }, ) assert response.status_code == status.HTTP_200_OK get_access_token_mock.assert_called_once_with( "CODE", "http://www.tintagel.bt/callback") data = cast(Dict[str, Any], response.json()) assert data["access_token"] == str(user_oauth.id)
async def test_inactive_user( self, async_method_mocker: AsyncMethodMocker, test_app_client: httpx.AsyncClient, oauth_client: BaseOAuth2, inactive_user_oauth: UserDB, user_manager_oauth: UserManagerMock, access_token: str, ): state_jwt = generate_state_token({}, "SECRET") async_method_mocker(oauth_client, "get_access_token", return_value=access_token) async_method_mocker( oauth_client, "get_id_email", return_value=("user_oauth1", inactive_user_oauth.email), ) async_method_mocker(user_manager_oauth, "oauth_callback", return_value=inactive_user_oauth) response = await test_app_client.get( "/callback", params={ "code": "CODE", "state": state_jwt }, ) assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_unknown_user( self, mock_user_db_oauth, test_app_client: TestClient, oauth_client, event_handler, ): state_jwt = generate_state_token({"authentication_backend": "mock"}, "SECRET") with asynctest.patch.object( oauth_client, "get_access_token") as get_access_token_mock: get_access_token_mock.return_value = { "access_token": "TOKEN", "expires_at": 1579179542, } with asynctest.patch.object(oauth_client, "get_id_email") as get_id_email_mock: with asynctest.patch.object(mock_user_db_oauth, "create") as user_create_mock: get_id_email_mock.return_value = ( "unknown_user_oauth1", "*****@*****.**", ) response = test_app_client.get( "/callback", params={ "code": "CODE", "state": state_jwt }, ) get_id_email_mock.assert_awaited_once_with("TOKEN") user_create_mock.assert_awaited_once() data = response.json() assert "token" in data assert event_handler.called is True actual_user = event_handler.call_args[0][0] assert actual_user.id == data["token"] request = event_handler.call_args[0][1] assert isinstance(request, Request)
async def test_unknown_user( self, mock_user_db_oauth, test_app_client: httpx.AsyncClient, access_token, oauth_client, after_register, ): state_jwt = generate_state_token({"authentication_backend": "mock"}, "SECRET") with asynctest.patch.object( oauth_client, "get_access_token") as get_access_token_mock: get_access_token_mock.return_value = access_token with asynctest.patch.object(oauth_client, "get_id_email") as get_id_email_mock: with asynctest.patch.object(mock_user_db_oauth, "create") as user_create_mock: get_id_email_mock.return_value = ( "unknown_user_oauth1", "*****@*****.**", ) response = await test_app_client.get( "/callback", params={ "code": "CODE", "state": state_jwt }, ) get_id_email_mock.assert_awaited_once_with("TOKEN") user_create_mock.assert_awaited_once() data = cast(Dict[str, Any], response.json()) assert "token" in data assert after_register.called is True actual_user = after_register.call_args[0][0] assert str(actual_user.id) == data["token"] request = after_register.call_args[0][1] assert isinstance(request, Request)
def test_existing_user_without_oauth( self, mock_user_db_oauth, test_app_client: TestClient, oauth_client, superuser_oauth, event_handler, ): state_jwt = generate_state_token({"authentication_backend": "mock"}, "SECRET") with asynctest.patch.object( oauth_client, "get_access_token") as get_access_token_mock: get_access_token_mock.return_value = { "access_token": "TOKEN", "expires_at": 1579179542, } with asynctest.patch.object(oauth_client, "get_id_email") as get_id_email_mock: with asynctest.patch.object(mock_user_db_oauth, "update") as user_update_mock: get_id_email_mock.return_value = ( "superuser_oauth1", superuser_oauth.email, ) response = test_app_client.get( "/callback", params={ "code": "CODE", "state": state_jwt }, ) get_id_email_mock.assert_awaited_once_with("TOKEN") user_update_mock.assert_awaited_once() data = response.json() assert data["token"] == superuser_oauth.id assert event_handler.called is False