using System; using System.Net.Http; using System.Threading.Tasks; using FluentAssertions; using Website.Data; using Website.Models.Git; using Xunit; namespace Website.Tests.Data { public class GitApiTests { [Fact] public async Task GetRepositories_WithUser_ReturnsRepositoryCollection() { const string json = @"[{""name"":""name"",""url"":""url""}]"; var httpClient = new HttpClientBuilder() .WithMethod(HttpMethod.Get) .WithUrl("/repositories") .WithQueryString("user", "test") .WithResponse(json) .Build(); var expectation = new[] {new Repository {Url = "url", Name = "name"}}; var api = new GitApi(httpClient); (await api.GetRepositories("test")).Should().BeEquivalentTo(expectation); } [Fact] public async Task GetBranches_WithUserAndRepository_ReturnsBranchCollection() { const string json = @"[{""name"":""master"",""commit"":{""id"":""0923b554309ef562fca978c7e981b3812bc4af40"",""message"":""message"",""timestamp"":""2020-04-11T14:09:29+01:00"",""url"":null}}]"; var httpClient = new HttpClientBuilder() .WithMethod(HttpMethod.Get) .WithUrl("/branches") .WithQueryString("user", "test") .WithQueryString("repository", "repo") .WithResponse(json) .Build(); var expectation = new[] { new Branch { Name = "master", Commit = new Commit { Id = "0923b554309ef562fca978c7e981b3812bc4af40", Message = "message", Timestamp = new DateTimeOffset(2020, 04, 11, 14, 09, 29, TimeSpan.FromHours(1)) } } }; var api = new GitApi(httpClient); (await api.GetBranches("test", "repo")).Should().BeEquivalentTo(expectation); } [Fact] public async Task GetCommit_WithUserAndRepositoryAndHash_ReturnsCommit() { const string json = @"{""id"":""0923b554309ef562fca978c7e981b3812bc4af40"",""message"":""message"",""timestamp"":""2020-04-11T14:09:29+01:00"",""url"":""https://test.com/test/repo/commit/0923b554309ef562fca978c7e981b3812bc4af40""}"; var httpClient = new HttpClientBuilder() .WithMethod(HttpMethod.Get) .WithUrl("/commit") .WithQueryString("user", "test") .WithQueryString("repository", "repo") .WithQueryString("hash", "hash") .WithResponse(json) .Build(); var expectation = new Commit { Id = "0923b554309ef562fca978c7e981b3812bc4af40", Message = "message", Timestamp = new DateTimeOffset(2020, 04, 11, 14, 09, 29, TimeSpan.FromHours(1)), Url = "https://test.com/test/repo/commit/0923b554309ef562fca978c7e981b3812bc4af40" }; var api = new GitApi(httpClient); (await api.GetCommit("test", "repo", "hash")).Should().BeEquivalentTo(expectation); } } }