website/Website.Tests/Data/BlogApiTests.cs

249 lines
8 KiB
C#

using System;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using Website.Data;
using Website.Models;
using Xunit;
namespace Website.Tests.Data {
public class BlogApiTests {
[Fact]
public async Task GetPostByUrlAsync_WithUrl_ReturnsBlogPost() {
const string json = @"{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/get/test")
.WithResponse(json)
.Build();
var expectation = new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
};
var api = new BlogApi(httpClient);
(await api.GetPostByUrlAsync("test")).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetLatestPostsAsync_WithNoParameters_ReturnsBlogPostCollection() {
const string json = @"[{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}]";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getlatestposts")
.WithResponse(json)
.Build();
var expectation = new[] {
new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
}
};
var api = new BlogApi(httpClient);
(await api.GetLatestPostsAsync()).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetLatestPostsAsync_WithCount_ReturnsBlogPostCollection() {
const string json = @"[{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}]";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getlatestposts?count=1")
.WithResponse(json)
.Build();
var expectation = new[] {
new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
}
};
var api = new BlogApi(httpClient);
(await api.GetLatestPostsAsync(1)).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetLatestPostsAsync_WithCountAndOffset_ReturnsBlogPostCollection() {
const string json = @"[{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}]";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getlatestposts")
.WithQueryString("count", "1")
.WithQueryString("offset", "1")
.WithResponse(json)
.Build();
var expectation = new[] {
new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
}
};
var api = new BlogApi(httpClient);
(await api.GetLatestPostsAsync(1, 1)).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetLatestPostAsync_ReturnsBlogPost() {
const string json = @"{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getlatestpost")
.WithResponse(json)
.Build();
var expectation = new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
};
var api = new BlogApi(httpClient);
(await api.GetLatestPostAsync()).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetCountAsync_ReturnsCount() {
const string json = @"23";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getcount")
.WithResponse(json)
.Build();
var api = new BlogApi(httpClient);
(await api.GetCountAsync()).Should().Be(23);
}
[Fact]
public async Task GetPostByIdAsync_WithUrl_ReturnsBlogPost() {
const string json = @"{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/get/1")
.WithResponse(json)
.Build();
var expectation = new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
};
var api = new BlogApi(httpClient);
(await api.GetPostByIdAsync(1)).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task SavePost_WithPost_ReturnsNothing() {
const string requestJson = @"{""Id"":1,""Title"":""title"",""Content"":""content""}";
const string responseJson = @"{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Post)
.WithUrl("/savepost")
.WithPostBody(requestJson)
.WithResponse(responseJson)
.Build();
var post = new BlogPostSubmission {Id = 1, Title = "title", Content = "content"};
var expectation = new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
};
var api = new BlogApi(httpClient);
(await api.SavePost(post)).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetAllPostsAsync_ReturnsBlogPostCollection() {
const string json =
@"[{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}]";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getallposts")
.WithResponse(json)
.Build();
var expectation = new[] {
new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
}
};
var api = new BlogApi(httpClient);
(await api.GetAllPostsAsync()).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task DeletePost_WithId_ReturnsNothing() {
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Post)
.WithUrl("/deletepost")
.WithPostBody("1")
.Build(out var mockHttpMessageHandler);
var api = new BlogApi(httpClient);
await api.DeletePostAsync(1);
mockHttpMessageHandler.VerifyNoOutstandingExpectation();
}
[Fact]
public async Task PublishPost_WithId_ReturnsNothing() {
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Post)
.WithUrl("/publishpost")
.WithPostBody("1")
.Build(out var mockHttpMessageHandler);
var api = new BlogApi(httpClient);
await api.PublishPostAsync(1);
mockHttpMessageHandler.VerifyNoOutstandingExpectation();
}
}
}