Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CreateAsync to UserClient #641

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NGitLab.Mock/Clients/UserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public Models.User Create(UserUpsert user)
}
}

public async Task<Models.User> CreateAsync(UserUpsert user, CancellationToken cancellationToken = default)
{
await Task.Yield();
return Create(user);
}

public UserToken CreateToken(UserTokenCreate tokenRequest)
{
throw new NotSupportedException();
Expand Down
29 changes: 26 additions & 3 deletions NGitLab.Tests/UsersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ public async Task CreateUpdateDelete()
await GitLabTestContext.RetryUntilAsync(() => users.Get(addedUser.Username).ToList(), users => !users.Any(), TimeSpan.FromMinutes(2));
}

[Test]
[NGitLabRetry]
public async Task CreateAsync()
{
using var context = await GitLabTestContext.CreateAsync();
var users = context.AdminClient.Users;

var addedUser = await CreateNewUserAsync(context);
Assert.That(addedUser.Bio, Is.EqualTo("bio"));

await GitLabTestContext.RetryUntilAsync(() => users.Get(addedUser.Username).ToList(), users => users.Any(), TimeSpan.FromMinutes(2));
}

[Test]
[NGitLabRetry]
public async Task GetUserByEmailDoesNotWorkOnNonAdminClient()
Expand Down Expand Up @@ -182,10 +195,10 @@ public async Task GetLastActivityDates_UsingNonAdminCredentials_ThrowsForbidden(
Assert.That(exception.StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
}

private static User CreateNewUser(GitLabTestContext context)
private static UserUpsert CreateNewUserUpsert(GitLabTestContext context)
{
var randomNumber = context.GetRandomNumber().ToString(CultureInfo.InvariantCulture);
return context.AdminClient.Users.Create(new UserUpsert
return new UserUpsert
{
Email = $"test{randomNumber}@test.pl",
Bio = "bio",
Expand All @@ -201,7 +214,17 @@ private static User CreateNewUser(GitLabTestContext context)
Twitter = "twitter",
Username = $"ngitlabtestuser{randomNumber}",
WebsiteURL = "https://www.example.com",
});
};
}

private static User CreateNewUser(GitLabTestContext context)
{
return context.AdminClient.Users.Create(CreateNewUserUpsert(context));
}

private static Task<User> CreateNewUserAsync(GitLabTestContext context)
{
return context.AdminClient.Users.CreateAsync(CreateNewUserUpsert(context));
}

// Comes from https://github.com/meziantou/Meziantou.GitLabClient/blob/main/test/Meziantou.GitLabClient.Tests/Internals/RsaSshKey.cs
Expand Down
2 changes: 2 additions & 0 deletions NGitLab/IUserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface IUserClient

User Create(UserUpsert user);

Task<User> CreateAsync(UserUpsert user, CancellationToken cancellationToken = default);

User Update(int id, UserUpsert user);

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions NGitLab/Impl/UserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public IEnumerable<User> Get(UserQuery query)

public User Create(UserUpsert user) => _api.Post().With(user).To<User>(User.Url);

public Task<User> CreateAsync(UserUpsert user, CancellationToken cancellationToken = default) => _api.Post().With(user).ToAsync<User>(User.Url, cancellationToken);

public UserToken CreateToken(UserTokenCreate tokenRequest) => _api.Post().With(tokenRequest).To<UserToken>(User.Url + "/" + tokenRequest.UserId.ToStringInvariant() + "/impersonation_tokens");

public User Update(int id, UserUpsert user) => _api.Put().With(user).To<User>(User.Url + "/" + id.ToStringInvariant());
Expand Down
2 changes: 2 additions & 0 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ NGitLab.Impl.UserClient
NGitLab.Impl.UserClient.Activate(int userId) -> void
NGitLab.Impl.UserClient.All.get -> System.Collections.Generic.IEnumerable<NGitLab.Models.User>
NGitLab.Impl.UserClient.Create(NGitLab.Models.UserUpsert user) -> NGitLab.Models.User
NGitLab.Impl.UserClient.CreateAsync(NGitLab.Models.UserUpsert user, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<NGitLab.Models.User>
NGitLab.Impl.UserClient.CreateToken(NGitLab.Models.UserTokenCreate tokenRequest) -> NGitLab.Models.UserToken
NGitLab.Impl.UserClient.Current.get -> NGitLab.Models.Session
NGitLab.Impl.UserClient.CurrentUserSShKeys.get -> NGitLab.ISshKeyClient
Expand Down Expand Up @@ -1131,6 +1132,7 @@ NGitLab.IUserClient
NGitLab.IUserClient.Activate(int id) -> void
NGitLab.IUserClient.All.get -> System.Collections.Generic.IEnumerable<NGitLab.Models.User>
NGitLab.IUserClient.Create(NGitLab.Models.UserUpsert user) -> NGitLab.Models.User
NGitLab.IUserClient.CreateAsync(NGitLab.Models.UserUpsert user, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<NGitLab.Models.User>
NGitLab.IUserClient.CreateToken(NGitLab.Models.UserTokenCreate tokenRequest) -> NGitLab.Models.UserToken
NGitLab.IUserClient.Current.get -> NGitLab.Models.Session
NGitLab.IUserClient.CurrentUserSShKeys.get -> NGitLab.ISshKeyClient
Expand Down