示例#1
0
def test_get_user_field_headers():
    client = IdentityClient(
        credentials=credentials.AnonymousCredentials(),
  )

    # Any value that is part of the HTTP/1.1 URI should be sent as
    # a field header. Set these to a non-empty value.
    request = identity.GetUserRequest(
        name='name/value',
    )

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(
            type(client._transport.get_user),
            '__call__') as call:
        call.return_value = identity.User()
        response = client.get_user(request)

        # Establish that the underlying gRPC stub method was called.
        assert len(call.mock_calls) == 1
        _, args, _ = call.mock_calls[0]
        assert args[0] == request

    # Establish that the field header was sent.
    _, _, kw = call.mock_calls[0]
    assert (
        'x-goog-request-params',
        'name=name/value',
    ) in kw['metadata']
示例#2
0
def test_update_user(transport: str = 'grpc'):
    client = IdentityClient(
        credentials=credentials.AnonymousCredentials(),
        transport=transport,
    )

    # Everything is optional in proto3 as far as the runtime is concerned,
    # and we are mocking out the actual API, so just send an empty request.
    request = identity.UpdateUserRequest()

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client._transport.update_user),
                           '__call__') as call:
        # Designate an appropriate return value for the call.
        call.return_value = identity.User(
            name='name_value',
            display_name='display_name_value',
            email='email_value',
        )

        response = client.update_user(request)

        # Establish that the underlying gRPC stub method was called.
        assert len(call.mock_calls) == 1
        _, args, _ = call.mock_calls[0]

        assert args[0] == request

    # Establish that the response is the type that we expect.
    assert isinstance(response, identity.User)
    assert response.name == 'name_value'
    assert response.display_name == 'display_name_value'
    assert response.email == 'email_value'
示例#3
0
def test_create_user_flattened():
    client = IdentityClient(
        credentials=credentials.AnonymousCredentials(),
    )

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(
            type(client._transport.create_user),
            '__call__') as call:
        # Designate an appropriate return value for the call.
        call.return_value = identity.User()

        # Call the method with a truthy value for each flattened field,
        # using the keyword arguments to the method.
        response = client.create_user(
            display_name='display_name_value',
            email='email_value',
        )

        # Establish that the underlying call was made with the expected
        # request object values.
        assert len(call.mock_calls) == 1
        _, args, _ = call.mock_calls[0]
        assert args[0].user.display_name == 'display_name_value'
        assert args[0].user.email == 'email_value'
示例#4
0
def test_list_users_pages():
    client = IdentityClient(credentials=credentials.AnonymousCredentials, )

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client._transport.list_users),
                           '__call__') as call:
        # Set the response to a series of pages.
        call.side_effect = (
            identity.ListUsersResponse(
                users=[
                    identity.User(),
                    identity.User(),
                    identity.User(),
                ],
                next_page_token='abc',
            ),
            identity.ListUsersResponse(
                users=[],
                next_page_token='def',
            ),
            identity.ListUsersResponse(
                users=[
                    identity.User(),
                ],
                next_page_token='ghi',
            ),
            identity.ListUsersResponse(users=[
                identity.User(),
                identity.User(),
            ], ),
            RuntimeError,
        )
        pages = list(client.list_users(request={}).pages)
        for page, token in zip(pages, ['abc', 'def', 'ghi', '']):
            assert page.raw_page.next_page_token == token
示例#5
0
def test_list_users_pager():
    client = IdentityClient(credentials=credentials.AnonymousCredentials, )

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client._transport.list_users),
                           '__call__') as call:
        # Set the response to a series of pages.
        call.side_effect = (
            identity.ListUsersResponse(
                users=[
                    identity.User(),
                    identity.User(),
                    identity.User(),
                ],
                next_page_token='abc',
            ),
            identity.ListUsersResponse(
                users=[],
                next_page_token='def',
            ),
            identity.ListUsersResponse(
                users=[
                    identity.User(),
                ],
                next_page_token='ghi',
            ),
            identity.ListUsersResponse(users=[
                identity.User(),
                identity.User(),
            ], ),
            RuntimeError,
        )
        results = [i for i in client.list_users(request={}, )]
        assert len(results) == 6
        assert all(isinstance(i, identity.User) for i in results)