Add ApiOptions overloads to methods on I(Observable)RepositoryDeployKeysClient (#1307)

This commit is contained in:
Alexander Efremov
2016-05-20 18:45:34 +07:00
committed by Brendan Forster
parent 289cae568b
commit c4520123bf
7 changed files with 264 additions and 6 deletions

View File

@@ -28,6 +28,17 @@ namespace Octokit.Reactive
/// <param name="name">The name of the repository.</param> /// <param name="name">The name of the repository.</param>
IObservable<DeployKey> GetAll(string owner, string name); IObservable<DeployKey> GetAll(string owner, string name);
/// <summary>
/// Get all deploy keys for a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#list"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="options">Options for changing the API response</param>
IObservable<DeployKey> GetAll(string owner, string name, ApiOptions options);
/// <summary> /// <summary>
/// Creates a new deploy key for a repository. /// Creates a new deploy key for a repository.
/// </summary> /// </summary>

View File

@@ -48,7 +48,25 @@ namespace Octokit.Reactive
Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(name, "name");
return _connection.GetAndFlattenAllPages<DeployKey>(ApiUrls.RepositoryDeployKeys(owner, name)); return GetAll(owner, name, ApiOptions.None);
}
/// <summary>
/// Get all deploy keys for a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#list"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="options">Options for changing the API response</param>
public IObservable<DeployKey> GetAll(string owner, string name, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(options, "options");
return _connection.GetAndFlattenAllPages<DeployKey>(ApiUrls.RepositoryDeployKeys(owner, name), options);
} }
/// <summary> /// <summary>

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Octokit; using Octokit;
using Octokit.Tests.Integration; using Octokit.Tests.Integration;
@@ -56,6 +57,115 @@ public class RepositoryDeployKeysClientTests : IDisposable
Assert.Equal(_keyTitle, deployKeys[0].Title); Assert.Equal(_keyTitle, deployKeys[0].Title);
} }
[IntegrationTest(Skip = "See https://github.com/octokit/octokit.net/issues/1003 for investigating this failing test")]
public async Task ReturnsCorrectCountOfHooksWithoutStart()
{
var deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName);
Assert.Equal(0, deployKeys.Count);
var list = new List<NewDeployKey>();
var hooksCount = 5;
for (int i = 0; i < hooksCount; i++)
{
var item = new NewDeployKey
{
Key = "ssh-rsa A" + i, // here we should genereate ssh-key some how
Title = "KeyTitle" + i
};
list.Add(item);
}
foreach (var key in list)
{
await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, key);
}
var options = new ApiOptions
{
PageSize = hooksCount,
PageCount = 1
};
deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName, options);
Assert.Equal(hooksCount, deployKeys.Count);
}
[IntegrationTest(Skip = "See https://github.com/octokit/octokit.net/issues/1003 for investigating this failing test")]
public async Task ReturnsCorrectCountOfHooksWithStart()
{
var deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName);
Assert.Equal(0, deployKeys.Count);
var list = new List<NewDeployKey>();
var hooksCount = 5;
for (int i = 0; i < hooksCount; i++)
{
var item = new NewDeployKey
{
Key = "ssh-rsa A" + i, // here we should genereate ssh-key some how
Title = "KeyTitle" + i
};
list.Add(item);
}
foreach (var key in list)
{
await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, key);
}
var options = new ApiOptions
{
PageSize = 2,
PageCount = 1,
StartPage = 3
};
deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName, options);
Assert.Equal(1, deployKeys.Count);
}
[IntegrationTest(Skip = "See https://github.com/octokit/octokit.net/issues/1003 for investigating this failing test")]
public async Task ReturnsDistinctResultsBasedOnStartPage()
{
var list = new List<NewDeployKey>();
var hooksCount = 5;
for (int i = 0; i < hooksCount; i++)
{
var item = new NewDeployKey
{
Key = "ssh-rsa A" + i, // here we should genereate ssh-key some how
Title = "KeyTitle" + i
};
list.Add(item);
}
foreach (var key in list)
{
await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, key);
}
var startOptions = new ApiOptions
{
PageSize = 2,
PageCount = 1
};
var firstPage = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName, startOptions);
var skipStartOptions = new ApiOptions
{
PageSize = 2,
PageCount = 1,
StartPage = 2
};
var secondPage = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName, skipStartOptions);
Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
}
[IntegrationTest(Skip = "see https://github.com/octokit/octokit.net/issues/533 for the resolution to this failing test")] [IntegrationTest(Skip = "see https://github.com/octokit/octokit.net/issues/533 for the resolution to this failing test")]
public async Task CanRetrieveADeployKey() public async Task CanRetrieveADeployKey()
{ {

View File

@@ -55,7 +55,27 @@ namespace Octokit.Tests.Clients
deployKeysClient.GetAll("user", "repo"); deployKeysClient.GetAll("user", "repo");
apiConnection.Received().GetAll<DeployKey>(Arg.Is<Uri>(u => u.ToString() == "repos/user/repo/keys")); apiConnection.Received().GetAll<DeployKey>(Arg.Is<Uri>(u => u.ToString() == "repos/user/repo/keys"), Args.ApiOptions);
}
[Fact]
public void GetsAListOfDeployKeysWithApiOptions()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryDeployKeysClient(connection);
var options = new ApiOptions
{
PageCount = 1,
PageSize = 1,
StartPage = 1
};
client.GetAll("user", "repo", options);
connection.Received(1)
.GetAll<DeployKey>(Arg.Is<Uri>(u => u.ToString() == "repos/user/repo/keys"),
options);
} }
[Fact] [Fact]
@@ -63,10 +83,22 @@ namespace Octokit.Tests.Clients
{ {
var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For<IApiConnection>()); var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For<IApiConnection>());
await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll(null, null));
await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll(null, "repo")); await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll(null, "repo"));
await Assert.ThrowsAsync<ArgumentException>(() => deployKeysClient.GetAll("", "repo"));
await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll("user", null)); await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll("user", null));
await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll(null, null, null));
await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll(null, null, ApiOptions.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll(null, "repo", null));
await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll("user", null, null));
await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll(null, "repo", ApiOptions.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll("user", null, ApiOptions.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => deployKeysClient.GetAll("user", "repo", null));
await Assert.ThrowsAsync<ArgumentException>(() => deployKeysClient.GetAll("user", "")); await Assert.ThrowsAsync<ArgumentException>(() => deployKeysClient.GetAll("user", ""));
await Assert.ThrowsAsync<ArgumentException>(() => deployKeysClient.GetAll("", "repo"));
} }
} }

View File

@@ -53,7 +53,53 @@ namespace Octokit.Tests.Reactive
deployKeysClient.GetAll("user", "repo"); deployKeysClient.GetAll("user", "repo");
githubClient.Connection.Received(1).Get<List<DeployKey>>( githubClient.Connection.Received(1).Get<List<DeployKey>>(
new Uri("repos/user/repo/keys", UriKind.Relative), null, null); new Uri("repos/user/repo/keys", UriKind.Relative), Arg.Is<Dictionary<string, string>>(dictionary => dictionary.Count == 0), null);
}
[Fact]
public void GetsCorrectUrlWithApiOptions()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var deployKeysClient = new ObservableRepositoryDeployKeysClient(gitHubClient);
var expectedUrl = string.Format("repos/{0}/{1}/keys", "user", "repo");
// all properties are setted => only 2 options (StartPage, PageSize) in dictionary
var options = new ApiOptions
{
StartPage = 1,
PageCount = 1,
PageSize = 1
};
deployKeysClient.GetAll("user", "repo", options);
gitHubClient.Connection.Received(1)
.Get<List<DeployKey>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 2),
null);
// StartPage is setted => only 1 option (StartPage) in dictionary
options = new ApiOptions
{
StartPage = 1
};
deployKeysClient.GetAll("user", "repo", options);
gitHubClient.Connection.Received(1)
.Get<List<DeployKey>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 1),
null);
// PageCount is setted => none of options in dictionary
options = new ApiOptions
{
PageCount = 1
};
deployKeysClient.GetAll("user", "repo", options);
gitHubClient.Connection.Received(1)
.Get<List<DeployKey>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 0),
null);
} }
[Fact] [Fact]
@@ -61,10 +107,22 @@ namespace Octokit.Tests.Reactive
{ {
var deployKeysClient = new ObservableRepositoryDeployKeysClient(Substitute.For<IGitHubClient>()); var deployKeysClient = new ObservableRepositoryDeployKeysClient(Substitute.For<IGitHubClient>());
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll(null, null));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll(null, "repo")); Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll(null, "repo"));
Assert.Throws<ArgumentException>(() => deployKeysClient.GetAll("", "repo"));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll("user", null)); Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll("user", null));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll(null, null, null));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll(null, null, ApiOptions.None));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll(null, "repo", null));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll("user", null, null));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll(null, "repo", ApiOptions.None));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll("user", null, ApiOptions.None));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll("user", "repo", null));
Assert.Throws<ArgumentException>(() => deployKeysClient.GetAll("user", "")); Assert.Throws<ArgumentException>(() => deployKeysClient.GetAll("user", ""));
Assert.Throws<ArgumentException>(() => deployKeysClient.GetAll("", "repo"));
} }
} }

View File

@@ -34,6 +34,17 @@ namespace Octokit
/// <param name="name">The name of the repository.</param> /// <param name="name">The name of the repository.</param>
Task<IReadOnlyList<DeployKey>> GetAll(string owner, string name); Task<IReadOnlyList<DeployKey>> GetAll(string owner, string name);
/// <summary>
/// Get all deploy keys for a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#list"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="options">Options for changing the API response</param>
Task<IReadOnlyList<DeployKey>> GetAll(string owner, string name, ApiOptions options);
/// <summary> /// <summary>
/// Creates a new deploy key for a repository. /// Creates a new deploy key for a repository.
/// </summary> /// </summary>

View File

@@ -53,7 +53,25 @@ namespace Octokit
Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.GetAll<DeployKey>(ApiUrls.RepositoryDeployKeys(owner, name)); return GetAll(owner, name, ApiOptions.None);
}
/// <summary>
/// Get all deploy keys for a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/keys/#list"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="options">Options for changing the API response</param>
public Task<IReadOnlyList<DeployKey>> GetAll(string owner, string name, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(options, "options");
return ApiConnection.GetAll<DeployKey>(ApiUrls.RepositoryDeployKeys(owner, name), options);
} }
/// <summary> /// <summary>