Use new API for blog

This commit is contained in:
Robert Marshall 2020-04-11 13:37:14 +01:00
parent e389b2404a
commit 25c320bf6b
17 changed files with 356 additions and 314 deletions

View file

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

View file

@ -1,55 +0,0 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using FluentAssertions;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Website.Data;
using Website.Data.States;
using Website.Models;
using Xunit;
namespace Website.Tests.Data
{
public class BlogRepositoryTests
{
// [Fact]
// public void GetPostAsync_WithValidId_ReturnsBlogPost() {
// var blogPostState = new BlogPostState
// {
// Post_Id = 1,
// Post_Title = "title",
// Post_Content = "content",
// Post_Timestamp = new DateTime(),
// Post_Deleted = false,
// Post_Draft = "draft",
// Post_Url = "url",
// User_Id = 1
// };
// var connection = Substitute.For<IDbConnection>();
// connection.QueryAsync(null, null, (Func<object[], BlogPostState>)null, null, null, false, null, null, null).ReturnsForAnyArgs(new[]{ blogPostState });
// var provider = Substitute.For<IDatabaseProvider>();
// provider.NewConnection().Returns(connection);
// var repo = new BlogRepository(provider);
// var post = repo.GetPostAsync(1);
// var expected = new BlogPost
// {
// Id = 1,
// Title = "title",
// Content = "content",
// Timestamp = new DateTime(),
// Draft = "draft",
// Url = "url",
// UserId = 1
// }; ;
// post.Should().BeEquivalentTo(expected);
// }
//public async Task GetPostAsync_WithInvalidId_ReturnsNull()
//public async Task GetPostAsync_WithDeletedPost_ReturnsNull()
}
}

View file

@ -1,42 +0,0 @@
using FluentAssertions;
using Website.Models;
using Xunit;
namespace Website.Tests.Models {
public class BlogPostTests {
[Fact]
public void UpdateTitle_WithNewTitle_UpdatesTitlePropertyAndRegeneratesUrl() {
var post = new BlogPost();
post.UpdateTitle("new title");
post.Title.Should().Be("new title");
post.Url.Should().Be("new-title");
}
[Theory]
[InlineData("new:title", "new-title")]
[InlineData("new: title", "new-title")]
[InlineData("new$title", "new-title")]
[InlineData("new TITle", "new-title")]
public void UpdateTitle_WithUnsafeCharsInTitle_RegeneratesSafeUrl(string title, string url) {
var post = new BlogPost();
post.UpdateTitle(title);
post.Url.Should().Be(url);
}
[Fact]
public void UpdateDraft_WithContent_UpdatesDraftProperty() {
var post = new BlogPost();
post.UpdateDraft("content");
post.Draft.Should().Be("content");
}
[Fact]
public void Publish_SetsContentToDraft() {
var post = new BlogPost();
post.UpdateDraft("content");
post.Publish();
post.Content.Should().Be("content");
}
}
}

View file

@ -1,5 +1,4 @@
using FluentAssertions;
using Website.Data.States;
using FluentAssertions;
using Website.Models;
using Website.ViewModels;
using Xunit;
@ -10,22 +9,16 @@ namespace Website.Tests.VIewModels
{
[Fact]
public void Constructor_WithContentOver1000Characters_LimitsContentTo1000Chars() {
var state = new BlogPostState {
Post_Content = new string('a', 1001)
};
var post = new BlogPost(state);
var post = new BlogPost {Content = new string('a', 1001)};
var vm = new BlogPostSnippetViewModel(post);
vm.Content.Length.Should().Be(1000);
}
[Fact]
public void Constructor_WithContentUnder1000Characters_ContentIsIdenticalLength() {
var state = new BlogPostState {
Post_Content = new string('a', 900)
};
var post = new BlogPost(state);
var post = new BlogPost{Content = new string('a', 900)};
var vm = new BlogPostSnippetViewModel(post);
vm.Content.Length.Should().Be(900);
}
}
}
}

View file

@ -1,55 +1,48 @@
using System;
using System;
using FluentAssertions;
using Website.Data.States;
using Website.Models;
using Website.ViewModels;
using Xunit;
namespace Website.Tests.VIewModels
{
public class BlogPostViewModelTests
{
namespace Website.Tests.VIewModels {
public class BlogPostViewModelTests {
[Fact]
public void ContentHtml_WithMarkdownContent_ReturnsHtml() {
var state = new BlogPostState {
Post_Content="# header"
var post = new BlogPost {
Content = "# header"
};
var post = new BlogPost(state);
var vm = new BlogPostViewModel(post, false);
vm.ContentHtml.Should().Be("<h1>header</h1>");
}
[Fact]
public void Timestamp_OnThe1st_IsFriendlyString() {
var state = new BlogPostState {
Post_Content = "",
Post_Timestamp = new DateTime(2018, 10, 01, 15, 1, 25)
var post = new BlogPost {
Content = "",
Timestamp = new DateTime(2018, 10, 01, 15, 1, 25)
};
var post = new BlogPost(state);
var vm = new BlogPostViewModel(post, false);
vm.Timestamp.Should().Be("Monday the 1st of October 2018");
}
[Fact]
public void Timestamp_OnThe2nd_IsFriendlyString() {
var state = new BlogPostState {
Post_Content = "",
Post_Timestamp = new DateTime(2018, 10, 02, 15, 1, 25)
var post = new BlogPost {
Content = "",
Timestamp = new DateTime(2018, 10, 02, 15, 1, 25)
};
var post = new BlogPost(state);
var vm = new BlogPostViewModel(post, false);
vm.Timestamp.Should().Be("Tuesday the 2nd of October 2018");
}
[Fact]
public void Timestamp_OnThe3rd_IsFriendlyString() {
var state = new BlogPostState {
Post_Content = "",
Post_Timestamp = new DateTime(2018, 10, 03, 15, 1, 25)
var post = new BlogPost {
Content = "",
Timestamp = new DateTime(2018, 10, 03, 15, 1, 25)
};
var post = new BlogPost(state);
var vm = new BlogPostViewModel(post, false);
vm.Timestamp.Should().Be("Wednesday the 3rd of October 2018");
}
}
}
}