Files
octokit.net/Octokit.Tests/Clients/WatchedClientTests.cs
2015-11-04 13:38:51 -08:00

136 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Internal;
using Xunit;
namespace Octokit.Tests.Clients
{
public class WatchedClientTests
{
public class TheGetAllForCurrentMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var endpoint = new Uri("user/subscriptions", UriKind.Relative);
var connection = Substitute.For<IApiConnection>();
var client = new WatchedClient(connection);
client.GetAllForCurrent();
connection.Received().GetAll<Repository>(endpoint);
}
}
public class TheGetAllForUserMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var endpoint = new Uri("users/banana/subscriptions", UriKind.Relative);
var connection = Substitute.For<IApiConnection>();
var client = new WatchedClient(connection);
client.GetAllForUser("banana");
connection.Received().GetAll<Repository>(endpoint);
}
}
public class TheGetAllWatchersForRepoMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var endpoint = new Uri("repos/fight/club/subscribers", UriKind.Relative);
var connection = Substitute.For<IApiConnection>();
var client = new WatchedClient(connection);
client.GetAllWatchers("fight", "club");
connection.Received().GetAll<User>(endpoint);
}
}
public class TheCheckWatchedMethod
{
[Fact]
public async Task ReturnsTrueOnValidResult()
{
var endpoint = new Uri("repos/fight/club/subscription", UriKind.Relative);
var connection = Substitute.For<IApiConnection>();
connection.Get<Subscription>(endpoint).Returns(Task.FromResult(new Subscription(false, false, null, default(DateTimeOffset), null, null)));
var client = new WatchedClient(connection);
var watched = await client.CheckWatched("fight", "club");
Assert.True(watched);
}
[Fact]
public async Task ReturnsFalseOnNotFoundException()
{
var endpoint = new Uri("repos/fight/club/subscription", UriKind.Relative);
var connection = Substitute.For<IApiConnection>();
var response = new Response(HttpStatusCode.NotFound, null, new Dictionary<string, string>(), "application/json");
connection.Get<Subscription>(endpoint).Returns(x =>
{
throw new NotFoundException(response);
});
var client = new WatchedClient(connection);
var watched = await client.CheckWatched("fight", "club");
Assert.False(watched);
}
}
public class TheWatchRepoMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var endpoint = new Uri("repos/fight/club/subscription", UriKind.Relative);
var connection = Substitute.For<IApiConnection>();
var client = new WatchedClient(connection);
var newSubscription = new NewSubscription();
client.WatchRepo("fight", "club", newSubscription);
connection.Received().Put<Subscription>(endpoint, newSubscription);
}
}
public class TheUnwatchRepoMethod
{
[Theory]
[InlineData(HttpStatusCode.NoContent, true)]
[InlineData(HttpStatusCode.NotFound, false)]
[InlineData(HttpStatusCode.OK, false)]
public async Task ReturnsCorrectResultBasedOnStatus(HttpStatusCode status, bool expected)
{
var response = Task.Factory.StartNew(() => status);
var connection = Substitute.For<IConnection>();
connection.Delete(Arg.Is<Uri>(u => u.ToString() == "repos/yes/no/subscription"))
.Returns(response);
var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new WatchedClient(apiConnection);
var result = await client.UnwatchRepo("yes", "no");
Assert.Equal(expected, result);
}
}
}
}