mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-04 19:26:51 +00:00
Add tests for the Organization Members API Client
This commit is contained in:
@@ -0,0 +1,294 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NSubstitute;
|
||||
using Octokit.Clients;
|
||||
using Octokit.Internal;
|
||||
using Octokit.Tests.Helpers;
|
||||
using Xunit;
|
||||
using Xunit.Extensions;
|
||||
|
||||
namespace Octokit.Tests.Clients
|
||||
{
|
||||
/// <summary>
|
||||
/// Client tests mostly just need to make sure they call the IApiConnection with the correct
|
||||
/// relative Uri. No need to fake up the response. All *those* tests are in ApiConnectionTests.cs.
|
||||
/// </summary>
|
||||
public class OrganizationMembersClientTests
|
||||
{
|
||||
public class TheConstructor
|
||||
{
|
||||
[Fact]
|
||||
public void EnsureNonNullArguments()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => new OrganizationMembersClient(null));
|
||||
}
|
||||
}
|
||||
|
||||
public class TheGetAllMethod
|
||||
{
|
||||
[Fact]
|
||||
public void RequestsTheCorrectUrl()
|
||||
{
|
||||
var client = Substitute.For<IApiConnection>();
|
||||
var orgMembersClient = new OrganizationMembersClient(client);
|
||||
|
||||
orgMembersClient.GetAll("org");
|
||||
|
||||
client.Received().GetAll<User>(Arg.Is<Uri>(u => u.ToString() == "orgs/org/members"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnsureNonNullArguments()
|
||||
{
|
||||
var orgMembers = new OrganizationMembersClient(Substitute.For<IApiConnection>());
|
||||
|
||||
AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.GetAll(null));
|
||||
AssertEx.Throws<ArgumentException>(async () => await orgMembers.GetAll(""));
|
||||
}
|
||||
}
|
||||
|
||||
public class TheGetPublicMethod
|
||||
{
|
||||
[Fact]
|
||||
public void RequestsTheCorrectUrl()
|
||||
{
|
||||
var client = Substitute.For<IApiConnection>();
|
||||
var orgMembers = new OrganizationMembersClient(client);
|
||||
|
||||
orgMembers.GetPublic("org");
|
||||
|
||||
client.Received().GetAll<User>(Arg.Is<Uri>(u => u.ToString() == "orgs/org/public_members"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnsureNonNullArguments()
|
||||
{
|
||||
var orgMembers = new OrganizationMembersClient(Substitute.For<IApiConnection>());
|
||||
|
||||
AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.GetPublic(null));
|
||||
AssertEx.Throws<ArgumentException>(async () => await orgMembers.GetPublic(""));
|
||||
}
|
||||
}
|
||||
|
||||
public class TheCheckMemberMethod
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(HttpStatusCode.NoContent, true)]
|
||||
[InlineData(HttpStatusCode.NotFound, false)]
|
||||
[InlineData(HttpStatusCode.Found, false)]
|
||||
public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
|
||||
{
|
||||
var response = Task.Factory.StartNew<IResponse<object>>(() =>
|
||||
new ApiResponse<object> { StatusCode = status });
|
||||
var connection = Substitute.For<IConnection>();
|
||||
connection.GetAsync<object>(Arg.Is<Uri>(u => u.ToString() == "orgs/org/members/username"),
|
||||
null, null).Returns(response);
|
||||
var apiConnection = Substitute.For<IApiConnection>();
|
||||
apiConnection.Connection.Returns(connection);
|
||||
var client = new OrganizationMembersClient(apiConnection);
|
||||
|
||||
var result = await client.CheckMember("org", "username");
|
||||
|
||||
Assert.Equal(expected, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsExceptionForInvalidStatusCode()
|
||||
{
|
||||
var response = Task.Factory.StartNew<IResponse<object>>(() =>
|
||||
new ApiResponse<object> { StatusCode = HttpStatusCode.Conflict });
|
||||
var connection = Substitute.For<IConnection>();
|
||||
connection.GetAsync<object>(Arg.Is<Uri>(u => u.ToString() == "orgs/org/members/username"),
|
||||
null, null).Returns(response);
|
||||
var apiConnection = Substitute.For<IApiConnection>();
|
||||
apiConnection.Connection.Returns(connection);
|
||||
var client = new OrganizationMembersClient(apiConnection);
|
||||
|
||||
AssertEx.Throws<ApiException>(async () => await client.CheckMember("org", "username"));
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureNonNullArguments()
|
||||
{
|
||||
var orgMembers = new OrganizationMembersClient(Substitute.For<IApiConnection>());
|
||||
|
||||
await AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.CheckMember(null, "username"));
|
||||
await AssertEx.Throws<ArgumentException>(async () => await orgMembers.CheckMember(null, ""));
|
||||
await AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.CheckMember("org", null));
|
||||
await AssertEx.Throws<ArgumentException>(async () => await orgMembers.CheckMember("", null));
|
||||
}
|
||||
}
|
||||
|
||||
public class TheCheckMemberPublicMethod
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(HttpStatusCode.NoContent, true)]
|
||||
[InlineData(HttpStatusCode.NotFound, false)]
|
||||
public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
|
||||
{
|
||||
var response = Task.Factory.StartNew<IResponse<object>>(() =>
|
||||
new ApiResponse<object> { StatusCode = status });
|
||||
var connection = Substitute.For<IConnection>();
|
||||
connection.GetAsync<object>(Arg.Is<Uri>(u => u.ToString() == "orgs/org/public_members/username"),
|
||||
null, null).Returns(response);
|
||||
var apiConnection = Substitute.For<IApiConnection>();
|
||||
apiConnection.Connection.Returns(connection);
|
||||
var client = new OrganizationMembersClient(apiConnection);
|
||||
|
||||
var result = await client.CheckMemberPublic("org", "username");
|
||||
|
||||
Assert.Equal(expected, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsExceptionForInvalidStatusCode()
|
||||
{
|
||||
var response = Task.Factory.StartNew<IResponse<object>>(() =>
|
||||
new ApiResponse<object> { StatusCode = HttpStatusCode.Conflict });
|
||||
var connection = Substitute.For<IConnection>();
|
||||
connection.GetAsync<object>(Arg.Is<Uri>(u => u.ToString() == "orgs/org/public_members/username"),
|
||||
null, null).Returns(response);
|
||||
var apiConnection = Substitute.For<IApiConnection>();
|
||||
apiConnection.Connection.Returns(connection);
|
||||
var client = new OrganizationMembersClient(apiConnection);
|
||||
|
||||
AssertEx.Throws<ApiException>(async () => await client.CheckMemberPublic("org", "username"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureNonNullArguments()
|
||||
{
|
||||
var orgMembers = new OrganizationMembersClient(Substitute.For<IApiConnection>());
|
||||
|
||||
await AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.CheckMemberPublic(null, "username"));
|
||||
await AssertEx.Throws<ArgumentException>(async () => await orgMembers.CheckMemberPublic("", "username"));
|
||||
await AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.CheckMemberPublic("org", null));
|
||||
await AssertEx.Throws<ArgumentException>(async () => await orgMembers.CheckMemberPublic("org", ""));
|
||||
}
|
||||
}
|
||||
|
||||
public class TheDeleteMethod
|
||||
{
|
||||
[Fact]
|
||||
public void PostsToCorrectUrl()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new OrganizationMembersClient(connection);
|
||||
|
||||
client.Delete("org", "username");
|
||||
|
||||
connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "orgs/org/members/username"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureNonNullArguments()
|
||||
{
|
||||
var orgMembers = new OrganizationMembersClient(Substitute.For<IApiConnection>());
|
||||
|
||||
await AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.Delete(null, "username"));
|
||||
await AssertEx.Throws<ArgumentException>(async () => await orgMembers.Delete("", "username"));
|
||||
await AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.Delete("org", null));
|
||||
await AssertEx.Throws<ArgumentException>(async () => await orgMembers.Delete("org", ""));
|
||||
}
|
||||
}
|
||||
|
||||
public class ThePublicizeMethod
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(HttpStatusCode.NoContent, true)]
|
||||
public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
|
||||
{
|
||||
var response = Task.Factory.StartNew<IResponse<object>>(() =>
|
||||
new ApiResponse<object> { StatusCode = status });
|
||||
var connection = Substitute.For<IConnection>();
|
||||
connection.PutAsync<object>(Arg.Is<Uri>(u => u.ToString() == "orgs/org/public_members/username"),
|
||||
Args.Object).Returns(response);
|
||||
var apiConnection = Substitute.For<IApiConnection>();
|
||||
apiConnection.Connection.Returns(connection);
|
||||
var client = new OrganizationMembersClient(apiConnection);
|
||||
|
||||
var result = await client.Publicize("org", "username");
|
||||
|
||||
Assert.Equal(expected, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsExceptionForInvalidStatusCode()
|
||||
{
|
||||
var response = Task.Factory.StartNew<IResponse<object>>(() =>
|
||||
new ApiResponse<object> { StatusCode = HttpStatusCode.Conflict });
|
||||
var connection = Substitute.For<IConnection>();
|
||||
connection.PutAsync<object>(Arg.Is<Uri>(u => u.ToString() == "orgs/org/public_members/username"),
|
||||
new { }).Returns(response);
|
||||
var apiConnection = Substitute.For<IApiConnection>();
|
||||
apiConnection.Connection.Returns(connection);
|
||||
var client = new OrganizationMembersClient(apiConnection);
|
||||
|
||||
AssertEx.Throws<ApiException>(async () => await client.Publicize("org", "username"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureNonNullArguments()
|
||||
{
|
||||
var orgMembers = new OrganizationMembersClient(Substitute.For<IApiConnection>());
|
||||
|
||||
await AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.Publicize(null, "username"));
|
||||
await AssertEx.Throws<ArgumentException>(async () => await orgMembers.Publicize("", "username"));
|
||||
await AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.Publicize("org", null));
|
||||
await AssertEx.Throws<ArgumentException>(async () => await orgMembers.Publicize("org", ""));
|
||||
}
|
||||
}
|
||||
|
||||
public class TheConcealMethod
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(HttpStatusCode.NoContent, true)]
|
||||
public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
|
||||
{
|
||||
var response = Task.Factory.StartNew<IResponse<object>>(() =>
|
||||
new ApiResponse<object> { StatusCode = status });
|
||||
var connection = Substitute.For<IConnection>();
|
||||
connection.PutAsync<object>(Arg.Is<Uri>(u => u.ToString() == "orgs/org/public_members/username"),
|
||||
Args.Object).Returns(response);
|
||||
var apiConnection = Substitute.For<IApiConnection>();
|
||||
apiConnection.Connection.Returns(connection);
|
||||
var client = new OrganizationMembersClient(apiConnection);
|
||||
|
||||
var result = await client.Conceal("org", "username");
|
||||
|
||||
Assert.Equal(expected, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsExceptionForInvalidStatusCode()
|
||||
{
|
||||
var response = Task.Factory.StartNew<IResponse<object>>(() =>
|
||||
new ApiResponse<object> { StatusCode = HttpStatusCode.Conflict });
|
||||
var connection = Substitute.For<IConnection>();
|
||||
connection.PutAsync<object>(Arg.Is<Uri>(u => u.ToString() == "orgs/org/public_members/username"),
|
||||
new { }).Returns(response);
|
||||
var apiConnection = Substitute.For<IApiConnection>();
|
||||
apiConnection.Connection.Returns(connection);
|
||||
var client = new OrganizationMembersClient(apiConnection);
|
||||
|
||||
AssertEx.Throws<ApiException>(async () => await client.Conceal("org", "username"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureNonNullArguments()
|
||||
{
|
||||
var orgMembers = new OrganizationMembersClient(Substitute.For<IApiConnection>());
|
||||
|
||||
await AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.Conceal(null, "username"));
|
||||
await AssertEx.Throws<ArgumentException>(async () => await orgMembers.Conceal("", "username"));
|
||||
await AssertEx.Throws<ArgumentNullException>(async () => await orgMembers.Conceal("org", null));
|
||||
await AssertEx.Throws<ArgumentException>(async () => await orgMembers.Conceal("org", ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,7 @@
|
||||
<Compile Include="Clients\AssigneesClientTests.cs" />
|
||||
<Compile Include="Clients\CommitStatusClientTests.cs" />
|
||||
<Compile Include="Clients\GitDatabaseClientTests.cs" />
|
||||
<Compile Include="Clients\OrganizationMembersClientTests.cs" />
|
||||
<Compile Include="Clients\TagsClientTests.cs" />
|
||||
<Compile Include="Clients\IssuesEventsClientTests.cs" />
|
||||
<Compile Include="Clients\IssueCommentsClientTests.cs" />
|
||||
|
||||
Reference in New Issue
Block a user