Skip to content

Commit

Permalink
Add ApiOptions overloads to methods on I(Observable)RepositoryDeployK…
Browse files Browse the repository at this point in the history
…eysClient (#1307)
  • Loading branch information
alexander-efremov authored and shiftkey committed May 20, 2016
1 parent 289cae5 commit c452012
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 6 deletions.
11 changes: 11 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepositoryDeployKeysClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public interface IObservableRepositoryDeployKeysClient
/// <param name="name">The name of the repository.</param>
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>
/// Creates a new deploy key for a repository.
/// </summary>
Expand Down
20 changes: 19 additions & 1 deletion Octokit.Reactive/Clients/ObservableRepositoryDeployKeysClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,25 @@ public IObservable<DeployKey> GetAll(string owner, string name)
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
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>
Expand Down
110 changes: 110 additions & 0 deletions Octokit.Tests.Integration/Clients/RepositoryDeployKeysClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Octokit;
using Octokit.Tests.Integration;
Expand Down Expand Up @@ -56,6 +57,115 @@ public async Task CanRetrieveAllDeployKeys()
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")]
public async Task CanRetrieveADeployKey()
{
Expand Down
36 changes: 34 additions & 2 deletions Octokit.Tests/Clients/RepositoryDeployKeysClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,50 @@ public void GetsAListOfDeployKeys()

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]
public async Task EnsuresNonNullArguments()
{
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<ArgumentException>(() => deployKeysClient.GetAll("", "repo"));
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("", "repo"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,76 @@ public void CallsIntoClient()
deployKeysClient.GetAll("user", "repo");

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]
public void EnsuresNonNullArguments()
{
var deployKeysClient = new ObservableRepositoryDeployKeysClient(Substitute.For<IGitHubClient>());

Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll(null, null));
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(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("", "repo"));
}
}

Expand Down
11 changes: 11 additions & 0 deletions Octokit/Clients/IRepositoryDeployKeysClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public interface IRepositoryDeployKeysClient
/// <param name="name">The name of the repository.</param>
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>
/// Creates a new deploy key for a repository.
/// </summary>
Expand Down
20 changes: 19 additions & 1 deletion Octokit/Clients/RepositoryDeployKeysClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,25 @@ public Task<IReadOnlyList<DeployKey>> GetAll(string owner, string name)
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
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>
Expand Down

0 comments on commit c452012

Please sign in to comment.