From f30ec27df90478eab7de549d860131228f682f56 Mon Sep 17 00:00:00 2001 From: pltaylor Date: Tue, 5 Nov 2013 08:53:36 -0500 Subject: [PATCH] Add tests for ActivityClient --- .../Clients/ActivitiesClientTests.cs | 225 ++++++++++++++++++ Octokit.Tests/Octokit.Tests.csproj | 1 + Octokit/Clients/ActivitiesClient.cs | 4 +- Octokit/Clients/IActivitiesClient.cs | 1 + Octokit/Helpers/ApiUrls.cs | 38 ++- Octokit/Models/Response/Activity.cs | 1 + 6 files changed, 265 insertions(+), 5 deletions(-) create mode 100644 Octokit.Tests/Clients/ActivitiesClientTests.cs diff --git a/Octokit.Tests/Clients/ActivitiesClientTests.cs b/Octokit.Tests/Clients/ActivitiesClientTests.cs new file mode 100644 index 00000000..09ad9341 --- /dev/null +++ b/Octokit.Tests/Clients/ActivitiesClientTests.cs @@ -0,0 +1,225 @@ + + +using System; +using System.Threading.Tasks; +using NSubstitute; +using Octokit.Tests.Helpers; +using Xunit; + +namespace Octokit.Tests.Clients +{ + public class ActivitiesClientTests + { + public class TheGetAllMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + client.GetAll(); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "events")); + } + } + + public class TheGetAllForRepositoryMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + client.GetAllForRepository("fake", "repo"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/fake/repo/issues/events")); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + await AssertEx.Throws(async () => await client.GetAllForRepository(null, "name")); + await AssertEx.Throws(async () => await client.GetAllForRepository("", "name")); + await AssertEx.Throws(async () => await client.GetAllForRepository("owner", null)); + await AssertEx.Throws(async () => await client.GetAllForRepository("owner", "")); + } + } + + public class TheGetAllForRepositoryNetworkMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + client.GetAllForRepositoryNetwork("fake", "repo"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "networks/fake/repo/events")); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + await AssertEx.Throws(async () => await client.GetAllForRepositoryNetwork(null, "name")); + await AssertEx.Throws(async () => await client.GetAllForRepositoryNetwork("", "name")); + await AssertEx.Throws(async () => await client.GetAllForRepositoryNetwork("owner", null)); + await AssertEx.Throws(async () => await client.GetAllForRepositoryNetwork("owner", "")); + } + } + + public class TheGetAllForOrganizationMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + client.GetAllForOrganization("fake"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "orgs/fake/events")); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + await AssertEx.Throws(async () => await client.GetAllForOrganization(null)); + await AssertEx.Throws(async () => await client.GetAllForOrganization("")); + } + } + + public class TheGetUserReceivedMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + client.GetUserReceived("fake"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "users/fake/received_events")); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + await AssertEx.Throws(async () => await client.GetUserReceived(null)); + await AssertEx.Throws(async () => await client.GetUserReceived("")); + } + } + + public class TheGetUserReceivedPublicMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + client.GetUserReceivedPublic("fake"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "users/fake/received_events/public")); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + await AssertEx.Throws(async () => await client.GetUserReceivedPublic(null)); + await AssertEx.Throws(async () => await client.GetUserReceivedPublic("")); + } + } + + public class TheGetUserPerformedMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + client.GetUserPerformed("fake"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "users/fake/events")); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + await AssertEx.Throws(async () => await client.GetUserPerformed(null)); + await AssertEx.Throws(async () => await client.GetUserPerformed("")); + } + } + + public class TheGetUserPerformedPublicMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + client.GetUserPerformedPublic("fake"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "users/fake/events/public")); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + await AssertEx.Throws(async () => await client.GetUserPerformedPublic(null)); + await AssertEx.Throws(async () => await client.GetUserPerformedPublic("")); + } + } + + public class TheGetForAnOrganizationMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + client.GetForAnOrganization("fake", "org"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "users/fake/events/orgs/org")); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new ActivitiesClient(connection); + + await AssertEx.Throws(async () => await client.GetForAnOrganization(null, "org")); + await AssertEx.Throws(async () => await client.GetForAnOrganization("", "org")); + await AssertEx.Throws(async () => await client.GetForAnOrganization("fake", null)); + await AssertEx.Throws(async () => await client.GetForAnOrganization("fake", "")); + } + } + } +} diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index 4cb79a7e..63c99c8f 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -62,6 +62,7 @@ + diff --git a/Octokit/Clients/ActivitiesClient.cs b/Octokit/Clients/ActivitiesClient.cs index 7ad58cf4..e7e109b9 100644 --- a/Octokit/Clients/ActivitiesClient.cs +++ b/Octokit/Clients/ActivitiesClient.cs @@ -99,7 +99,7 @@ namespace Octokit { Ensure.ArgumentNotNullOrEmptyString(user, "user"); - return ApiConnection.GetAll(ApiUrls.ReceivedEvents(user), null, "public"); + return ApiConnection.GetAll(ApiUrls.ReceivedEvents(user, true)); } /// @@ -129,7 +129,7 @@ namespace Octokit { Ensure.ArgumentNotNullOrEmptyString(user, "user"); - return ApiConnection.GetAll(ApiUrls.PerformedEvents(user), null, "public"); + return ApiConnection.GetAll(ApiUrls.PerformedEvents(user, true)); } /// diff --git a/Octokit/Clients/IActivitiesClient.cs b/Octokit/Clients/IActivitiesClient.cs index 15227792..38cdf676 100644 --- a/Octokit/Clients/IActivitiesClient.cs +++ b/Octokit/Clients/IActivitiesClient.cs @@ -12,6 +12,7 @@ namespace Octokit /// http://developer.github.com/v3/activity/events/#list-public-events /// /// All the public s for the particular user. + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] Task> GetAll(); /// diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index 49179f8f..ddfb3a49 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -357,7 +357,7 @@ namespace Octokit /// public static Uri NetworkEvents(string owner, string name) { - return "network/{0}/{1}/event".FormatUri(owner, name); + return "networks/{0}/{1}/events".FormatUri(owner, name); } /// @@ -377,7 +377,23 @@ namespace Octokit /// public static Uri ReceivedEvents(string user) { - return "users/{0}/received_events".FormatUri(user); + return ReceivedEvents(user, false); + } + + /// + /// Returns the for the received events for a user. + /// + /// The name of the user + /// Whether to return public events or not + /// + public static Uri ReceivedEvents(string user, bool isPublic) + { + string usersReceivedEvents = "users/{0}/received_events"; + if (isPublic) + { + usersReceivedEvents += "/public"; + } + return usersReceivedEvents.FormatUri(user); } /// @@ -387,7 +403,23 @@ namespace Octokit /// public static Uri PerformedEvents(string user) { - return "users/{0}/events".FormatUri(user); + return PerformedEvents(user, false); + } + + /// + /// Returns the for events performed by a user. + /// + /// The name of the user + /// Whether to return public events or not + /// + public static Uri PerformedEvents(string user, bool isPublic) + { + string usersEvents = "users/{0}/events"; + if (isPublic) + { + usersEvents += "/public"; + } + return usersEvents.FormatUri(user); } /// diff --git a/Octokit/Models/Response/Activity.cs b/Octokit/Models/Response/Activity.cs index 700942d2..5b7477dc 100644 --- a/Octokit/Models/Response/Activity.cs +++ b/Octokit/Models/Response/Activity.cs @@ -7,6 +7,7 @@ namespace Octokit /// /// The type of the activity. /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")] public string Type { get; set; } ///