Skip to content

Commit

Permalink
Merge pull request #855 from Red-Folder/master
Browse files Browse the repository at this point in the history
Access to the last ApiInfo object
  • Loading branch information
haacked committed Aug 28, 2015
2 parents 3c034c0 + 07d6a8a commit 5a117fe
Show file tree
Hide file tree
Showing 26 changed files with 537 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Octokit.Reactive/IObservableGitHubClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Octokit.Reactive
{
public interface IObservableGitHubClient
public interface IObservableGitHubClient : IApiInfoProvider
{
IConnection Connection { get; }

Expand Down
9 changes: 9 additions & 0 deletions Octokit.Reactive/ObservableGitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,14 @@ public IConnection Connection
public IObservableNotificationsClient Notification { get; private set; }
public IObservableGitDatabaseClient GitDatabase { get; private set; }
public IObservableSearchClient Search { get; private set; }

/// <summary>
/// Gets the latest API Info - this will be null if no API calls have been made
/// </summary>
/// <returns><seealso cref="ApiInfo"/> representing the information returned as part of an Api call</returns>
public ApiInfo GetLastApiInfo()
{
return _gitHubClient.Connection.GetLastApiInfo();
}
}
}
83 changes: 83 additions & 0 deletions Octokit.Tests.Integration/Clients/GitHubClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Octokit;
using Octokit.Tests.Integration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

public class GitHubClientTests
{
public class TheLastApiInfoProperty
{
[IntegrationTest]
public async Task CanRetrieveLastApiInfoWithEtag()
{
// To check for etag, I'm using a new repository
// As per suggestion here -> https://github.com/octokit/octokit.net/pull/855#issuecomment-126966532
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");

var createdRepository = await github.Repository.Create(new NewRepository(repoName));

try
{
var result = github.GetLastApiInfo();

Assert.True(result.Links.Count == 0);
Assert.True(result.AcceptedOauthScopes.Count > -1);
Assert.True(result.OauthScopes.Count > -1);
Assert.False(String.IsNullOrEmpty(result.Etag));
Assert.True(result.RateLimit.Limit > 0);
Assert.True(result.RateLimit.Remaining > -1);
Assert.NotNull(result.RateLimit.Reset);
}
finally
{
Helper.DeleteRepo(createdRepository);
}
}

[IntegrationTest]
public async Task CanRetrieveLastApiInfoWithLinks()
{
// To check for links, I'm doing a list of all contributors to the octokit.net project
// Adapted from suggestion here -> https://github.com/octokit/octokit.net/pull/855#issuecomment-126966532
var github = Helper.GetAuthenticatedClient();

await github.Repository.GetAllContributors("octokit", "octokit.net");

var result = github.GetLastApiInfo();

Assert.True(result.Links.Count > 0);
Assert.True(result.AcceptedOauthScopes.Count > -1);
Assert.True(result.OauthScopes.Count > -1);
Assert.False(String.IsNullOrEmpty(result.Etag));
Assert.True(result.RateLimit.Limit > 0);
Assert.True(result.RateLimit.Remaining > -1);
Assert.NotNull(result.RateLimit.Reset);
}

[PersonalAccessTokenTest]
public async Task CanRetrieveLastApiInfoAcceptedOauth()
{
// To check for OAuth & AcceptedOAuth I'm getting the octokit user
// Adapted from suggestion here -> https://github.com/octokit/octokit.net/pull/855#issuecomment-126966532
var github = Helper.GetAuthenticatedClient();

await github.User.Get("octokit");

var result = github.GetLastApiInfo();

Assert.True(result.Links.Count == 0);
Assert.True(result.AcceptedOauthScopes.Count > 0);
Assert.True(result.OauthScopes.Count > 0);
Assert.False(String.IsNullOrEmpty(result.Etag));
Assert.True(result.RateLimit.Limit > 0);
Assert.True(result.RateLimit.Remaining > -1);
Assert.NotNull(result.RateLimit.Reset);
}

}
}
10 changes: 9 additions & 1 deletion Octokit.Tests.Integration/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class Helper
var githubUsername = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME");
UserName = githubUsername;
Organization = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBORGANIZATION");

var githubToken = Environment.GetEnvironmentVariable("OCTOKIT_OAUTHTOKEN");

if (githubToken != null)
Expand Down Expand Up @@ -52,6 +52,14 @@ static Helper()

public static Credentials ApplicationCredentials { get { return _oauthApplicationCredentials.Value; } }

public static bool IsUsingToken
{
get
{
return !String.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OCTOKIT_OAUTHTOKEN"));
}
}

public static bool IsPaidAccount
{
get
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Linq;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Octokit.Tests.Integration
{
public class PersonalAccessTokenTestDiscoverer : IXunitTestCaseDiscoverer
{
readonly IMessageSink diagnosticMessageSink;

public PersonalAccessTokenTestDiscoverer(IMessageSink diagnosticMessageSink)
{
this.diagnosticMessageSink = diagnosticMessageSink;
}

public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
{
return Helper.IsUsingToken
? new[] { new XunitTestCase(diagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod) }
: Enumerable.Empty<IXunitTestCase>();
}
}

[XunitTestCaseDiscoverer("Octokit.Tests.Integration.PersonalAccessTokenTestDiscoverer", "Octokit.Tests.Integration")]
public class PersonalAccessTokenTestAttribute : FactAttribute
{
}
}
2 changes: 2 additions & 0 deletions Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Compile Include="Clients\AuthorizationClientTests.cs" />
<Compile Include="Clients\BlobClientTests.cs" />
<Compile Include="Clients\BranchesClientTests.cs" />
<Compile Include="Clients\GitHubClientTests.cs" />
<Compile Include="Clients\MergingClientTests.cs" />
<Compile Include="Clients\CommitsClientTests.cs" />
<Compile Include="Clients\CommitStatusClientTests.cs" />
Expand All @@ -101,6 +102,7 @@
<Compile Include="fixtures\RepositoriesHooksCollection.cs" />
<Compile Include="fixtures\RepositoriesHooksFixture.cs" />
<Compile Include="Helpers\ApplicationTestAttribute.cs" />
<Compile Include="Helpers\PersonalAccessTokenTestAttribute.cs" />
<Compile Include="Helpers\PaidAccountTestAttribute.cs" />
<Compile Include="Helpers\RepositorySetupHelper.cs" />
<Compile Include="HttpClientAdapterTests.cs" />
Expand Down
67 changes: 67 additions & 0 deletions Octokit.Tests/GitHubClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Octokit.Internal;
using Xunit;
using Xunit.Extensions;
using System.Collections.Generic;

namespace Octokit.Tests
{
Expand Down Expand Up @@ -104,5 +105,71 @@ public void IsRetrievedFromCredentialStore()
Assert.Equal("bar", client.Credentials.Password);
}
}

public class TheLastApiInfoProperty
{
[Fact]
public async Task ReturnsNullIfNew()
{
var connection = Substitute.For<IConnection>();
connection.GetLastApiInfo().Returns((ApiInfo)null);
var client = new GitHubClient(connection);

var result = client.GetLastApiInfo();

Assert.Null(result);

var temp = connection.Received(1).GetLastApiInfo();
}

[Fact]
public async Task ReturnsObjectIfNotNew()
{
var apiInfo = new ApiInfo(
new Dictionary<string, Uri>
{
{
"next",
new Uri("https://api.github.com/repos/rails/rails/issues?page=4&per_page=5")
},
{
"last",
new Uri("https://api.github.com/repos/rails/rails/issues?page=131&per_page=5")
},
{
"first",
new Uri("https://api.github.com/repos/rails/rails/issues?page=1&per_page=5")
},
{
"prev",
new Uri("https://api.github.com/repos/rails/rails/issues?page=2&per_page=5")
}
},
new List<string>
{
"user",
},
new List<string>
{
"user",
"public_repo",
"repo",
"gist"
},
"5634b0b187fd2e91e3126a75006cc4fa",
new RateLimit(100, 75, 1372700873)
);
var connection = Substitute.For<IConnection>();
connection.GetLastApiInfo().Returns(apiInfo);
var client = new GitHubClient(connection);

var result = client.GetLastApiInfo();

Assert.NotNull(result);

var temp = connection.Received(1).GetLastApiInfo();
}
}

}
}
100 changes: 100 additions & 0 deletions Octokit.Tests/Http/ApiInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Octokit.Tests.Http
{
public class ApiInfoTests
{
public class TheMethods
{
[Fact]
public void CanClone()
{
var original = new ApiInfo(
new Dictionary<string, Uri>
{
{
"next",
new Uri("https://api.github.com/repos/rails/rails/issues?page=4&per_page=5")
},
{
"last",
new Uri("https://api.github.com/repos/rails/rails/issues?page=131&per_page=5")
},
{
"first",
new Uri("https://api.github.com/repos/rails/rails/issues?page=1&per_page=5")
},
{
"prev",
new Uri("https://api.github.com/repos/rails/rails/issues?page=2&per_page=5")
}
},
new List<string>
{
"user",
},
new List<string>
{
"user",
"public_repo",
"repo",
"gist"
},
"5634b0b187fd2e91e3126a75006cc4fa",
new RateLimit(100, 75, 1372700873)
);

var clone = original.Clone();

// Note the use of Assert.NotSame tests for value types - this should continue to test should the underlying
// model are changed to Object types
Assert.NotSame(original, clone);

Assert.Equal(original.Etag, clone.Etag);
Assert.NotSame(original.Etag, clone.Etag);

Assert.Equal(original.AcceptedOauthScopes.Count, clone.AcceptedOauthScopes.Count);
Assert.NotSame(original.AcceptedOauthScopes, clone.AcceptedOauthScopes);
for (int i = 0; i < original.AcceptedOauthScopes.Count; i++)
{
Assert.Equal(original.AcceptedOauthScopes[i], clone.AcceptedOauthScopes[i]);
Assert.NotSame(original.AcceptedOauthScopes[i], clone.AcceptedOauthScopes[i]);
}

Assert.Equal(original.Links.Count, clone.Links.Count);
Assert.NotSame(original.Links, clone.Links);
for (int i = 0; i < original.Links.Count; i++)
{
Assert.Equal(original.Links.Keys.ToArray()[i], clone.Links.Keys.ToArray()[i]);
Assert.NotSame(original.Links.Keys.ToArray()[i], clone.Links.Keys.ToArray()[i]);
Assert.Equal(original.Links.Values.ToArray()[i].ToString(), clone.Links.Values.ToArray()[i].ToString());
Assert.NotSame(original.Links.Values.ToArray()[i], clone.Links.Values.ToArray()[i]);
}

Assert.Equal(original.OauthScopes.Count, clone.OauthScopes.Count);
Assert.NotSame(original.OauthScopes, clone.OauthScopes);
for (int i = 0; i < original.OauthScopes.Count; i++)
{
Assert.Equal(original.OauthScopes[i], clone.OauthScopes[i]);
Assert.NotSame(original.OauthScopes[i], clone.OauthScopes[i]);
}

Assert.NotSame(original.RateLimit, clone.RateLimit);
Assert.Equal(original.RateLimit.Limit, clone.RateLimit.Limit);
Assert.NotSame(original.RateLimit.Limit, clone.RateLimit.Limit);
Assert.Equal(original.RateLimit.Remaining, clone.RateLimit.Remaining);
Assert.NotSame(original.RateLimit.Remaining, clone.RateLimit.Remaining);
Assert.Equal(original.RateLimit.ResetAsUtcEpochSeconds, clone.RateLimit.ResetAsUtcEpochSeconds);
Assert.NotSame(original.RateLimit.ResetAsUtcEpochSeconds, clone.RateLimit.ResetAsUtcEpochSeconds);
Assert.Equal(original.RateLimit.Reset, clone.RateLimit.Reset);
Assert.NotSame(original.RateLimit.Reset, clone.RateLimit.Reset);
}
}

}
}
Loading

0 comments on commit 5a117fe

Please sign in to comment.