Use new projects API

This commit is contained in:
Robert Marshall 2020-04-12 11:09:04 +01:00
parent 11bca4cf53
commit 8bf483b334
24 changed files with 141 additions and 222 deletions

View file

@ -0,0 +1,76 @@
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);
}
}
}