aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-10-24 20:15:58 +0800
committerGitHub <noreply@github.com>2019-10-24 20:15:58 +0800
commit4ab69665f26aaa59bad8684e6b801b4c4cf900cd (patch)
tree7ca5010a06829cc5fadea1ea17ae72d082fc344c /Timeline.Tests/Helpers
parent21de8da2feab19d3fbc392e71bf0dcec25ec8d6b (diff)
parent2bc4c701f9cdff1fdd11a5736c33a5818fbae3e9 (diff)
downloadtimeline-4ab69665f26aaa59bad8684e6b801b4c4cf900cd.tar.gz
timeline-4ab69665f26aaa59bad8684e6b801b4c4cf900cd.tar.bz2
timeline-4ab69665f26aaa59bad8684e6b801b4c4cf900cd.zip
Merge pull request #50 from crupest/refactor
Refactor : A Huge Step
Diffstat (limited to 'Timeline.Tests/Helpers')
-rw-r--r--Timeline.Tests/Helpers/AssertionResponseExtensions.cs105
-rw-r--r--Timeline.Tests/Helpers/AsyncFunctionAssertionsExtensions.cs16
-rw-r--r--Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs8
-rw-r--r--Timeline.Tests/Helpers/HttpClientExtensions.cs17
-rw-r--r--Timeline.Tests/Helpers/ImageHelper.cs24
-rw-r--r--Timeline.Tests/Helpers/InvalidModelTestHelpers.cs23
-rw-r--r--Timeline.Tests/Helpers/MyTestLoggerFactory.cs25
-rw-r--r--Timeline.Tests/Helpers/MyWebApplicationFactory.cs73
-rw-r--r--Timeline.Tests/Helpers/TestApplication.cs52
9 files changed, 150 insertions, 193 deletions
diff --git a/Timeline.Tests/Helpers/AssertionResponseExtensions.cs b/Timeline.Tests/Helpers/AssertionResponseExtensions.cs
index e67a172a..08f10b2b 100644
--- a/Timeline.Tests/Helpers/AssertionResponseExtensions.cs
+++ b/Timeline.Tests/Helpers/AssertionResponseExtensions.cs
@@ -6,6 +6,7 @@ using Newtonsoft.Json;
using System;
using System.Net;
using System.Net.Http;
+using System.Text;
using Timeline.Models.Http;
namespace Timeline.Tests.Helpers
@@ -23,8 +24,25 @@ namespace Timeline.Tests.Helpers
string padding = new string('\t', context.Depth);
var res = (HttpResponseMessage)value;
- var body = res.Content.ReadAsStringAsync().Result;
- return $"{newline}{padding} Status Code: {res.StatusCode} ; Body: {body.Substring(0, Math.Min(body.Length, 20))} ;";
+
+ var builder = new StringBuilder();
+ builder.Append($"{newline}{padding} Status Code: {res.StatusCode} ; Body: ");
+
+ try
+ {
+ var body = res.Content.ReadAsStringAsync().Result;
+ if (body.Length > 40)
+ {
+ body = body[0..40] + " ...";
+ }
+ builder.Append(body);
+ }
+ catch (AggregateException)
+ {
+ builder.Append("NOT A STRING.");
+ }
+
+ return builder.ToString();
}
}
@@ -43,15 +61,20 @@ namespace Timeline.Tests.Helpers
protected override string Identifier => "HttpResponseMessage";
+ public AndConstraint<HttpResponseMessage> HaveStatusCode(int expected, string because = "", params object[] becauseArgs)
+ {
+ return HaveStatusCode((HttpStatusCode)expected, because, becauseArgs);
+ }
+
public AndConstraint<HttpResponseMessage> HaveStatusCode(HttpStatusCode expected, string because = "", params object[] becauseArgs)
{
Execute.Assertion.BecauseOf(because, becauseArgs)
.ForCondition(Subject.StatusCode == expected)
- .FailWith("Expected status code of {context:HttpResponseMessage} to be {0}{reason}, but found {1}.\nResponse is {2}.", expected, Subject.StatusCode, Subject);
+ .FailWith("Expected status code of {context:HttpResponseMessage} to be {0}{reason}, but found {1}.", expected, Subject.StatusCode);
return new AndConstraint<HttpResponseMessage>(Subject);
}
- public AndWhichConstraint<HttpResponseMessage, T> HaveBodyAsJson<T>(string because = "", params object[] becauseArgs)
+ public AndWhichConstraint<HttpResponseMessage, T> HaveJsonBody<T>(string because = "", params object[] becauseArgs)
{
var a = Execute.Assertion.BecauseOf(because, becauseArgs);
string body;
@@ -59,22 +82,14 @@ namespace Timeline.Tests.Helpers
{
body = Subject.Content.ReadAsStringAsync().Result;
}
- catch (Exception e)
+ catch (AggregateException e)
{
- a.FailWith("Failed to read response body of {context:HttpResponseMessage}{reason}.\nException is {0}.", e);
+ a.FailWith("Expected response body of {context:HttpResponseMessage} to be json string{reason}, but failed to read it or it was not a string. Exception is {0}.", e.InnerExceptions);
return new AndWhichConstraint<HttpResponseMessage, T>(Subject, null);
}
- try
- {
- var result = JsonConvert.DeserializeObject<T>(body);
- return new AndWhichConstraint<HttpResponseMessage, T>(Subject, result);
- }
- catch (Exception e)
- {
- a.FailWith("Failed to convert response body of {context:HttpResponseMessage} to {0}{reason}.\nResponse is {1}.\nException is {2}.", typeof(T).FullName, Subject, e);
- return new AndWhichConstraint<HttpResponseMessage, T>(Subject, null);
- }
+ var result = JsonConvert.DeserializeObject<T>(body);
+ return new AndWhichConstraint<HttpResponseMessage, T>(Subject, result);
}
}
@@ -85,54 +100,42 @@ namespace Timeline.Tests.Helpers
return new HttpResponseMessageAssertions(instance);
}
- public static AndConstraint<HttpResponseMessage> HaveStatusCodeOk(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
- {
- return assertions.HaveStatusCode(HttpStatusCode.OK, because, becauseArgs);
- }
-
- public static AndConstraint<HttpResponseMessage> HaveStatusCodeCreated(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
- {
- return assertions.HaveStatusCode(HttpStatusCode.Created, because, becauseArgs);
- }
-
- public static AndConstraint<HttpResponseMessage> HaveStatusCodeBadRequest(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
- {
- return assertions.HaveStatusCode(HttpStatusCode.BadRequest, because, becauseArgs);
- }
-
- public static AndConstraint<HttpResponseMessage> HaveStatusCodeNotFound(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
- {
- return assertions.HaveStatusCode(HttpStatusCode.NotFound, because, becauseArgs);
- }
-
- public static AndWhichConstraint<HttpResponseMessage, CommonResponse> HaveBodyAsCommonResponse(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
- {
- return assertions.HaveBodyAsJson<CommonResponse>(because, becauseArgs);
- }
-
- public static void HaveBodyAsCommonResponseWithCode(this HttpResponseMessageAssertions assertions, int expected, string because = "", params object[] becauseArgs)
+ public static AndWhichConstraint<HttpResponseMessage, CommonResponse> HaveCommonBody(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
{
- assertions.HaveBodyAsCommonResponse(because, becauseArgs).Which.Code.Should().Be(expected, because, becauseArgs);
+ return assertions.HaveJsonBody<CommonResponse>(because, becauseArgs);
}
- public static void BePutCreated(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
+ public static AndWhichConstraint<HttpResponseMessage, CommonDataResponse<TData>> HaveCommonDataBody<TData>(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
{
- assertions.HaveStatusCodeCreated(because, becauseArgs).And.Should().HaveBodyAsCommonResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonPutResponse.Created, because, becauseArgs);
+ return assertions.HaveJsonBody<CommonDataResponse<TData>>(because, becauseArgs);
}
- public static void BePutModified(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
+ public static void BePut(this HttpResponseMessageAssertions assertions, bool create, string because = "", params object[] becauseArgs)
{
- assertions.HaveStatusCodeOk(because, becauseArgs).And.Should().HaveBodyAsCommonResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonPutResponse.Modified, because, becauseArgs);
+ var body = assertions.HaveStatusCode(create ? 201 : 200, because, becauseArgs)
+ .And.Should().HaveJsonBody<CommonPutResponse>(because, becauseArgs)
+ .Which;
+ body.Code.Should().Be(0);
+ body.Data.Create.Should().Be(create);
}
- public static void BeDeleteDeleted(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
+ public static void BeDelete(this HttpResponseMessageAssertions assertions, bool delete, string because = "", params object[] becauseArgs)
{
- assertions.HaveStatusCodeOk(because, becauseArgs).And.Should().HaveBodyAsCommonResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonDeleteResponse.Deleted, because, becauseArgs);
+ var body = assertions.HaveStatusCode(200, because, becauseArgs)
+ .And.Should().HaveJsonBody<CommonDeleteResponse>(because, becauseArgs)
+ .Which;
+ body.Code.Should().Be(0);
+ body.Data.Delete.Should().Be(delete);
}
- public static void BeDeleteNotExist(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
+ public static void BeInvalidModel(this HttpResponseMessageAssertions assertions, string message = null)
{
- assertions.HaveStatusCodeOk(because, becauseArgs).And.Should().HaveBodyAsCommonResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonDeleteResponse.NotExists, because, becauseArgs);
+ message = string.IsNullOrEmpty(message) ? "" : ", " + message;
+ assertions.HaveStatusCode(400, "Invalid Model Error must have 400 status code{0}", message)
+ .And.Should().HaveCommonBody("Invalid Model Error must have CommonResponse body{0}", message)
+ .Which.Code.Should().Be(ErrorCodes.Http.Common.InvalidModel,
+ "Invalid Model Error must have code {0} in body{1}",
+ ErrorCodes.Http.Common.InvalidModel, message);
}
}
}
diff --git a/Timeline.Tests/Helpers/AsyncFunctionAssertionsExtensions.cs b/Timeline.Tests/Helpers/AsyncFunctionAssertionsExtensions.cs
new file mode 100644
index 00000000..b78309c0
--- /dev/null
+++ b/Timeline.Tests/Helpers/AsyncFunctionAssertionsExtensions.cs
@@ -0,0 +1,16 @@
+using FluentAssertions;
+using FluentAssertions.Primitives;
+using FluentAssertions.Specialized;
+using System;
+using System.Threading.Tasks;
+
+namespace Timeline.Tests.Helpers
+{
+ public static class AsyncFunctionAssertionsExtensions
+ {
+ public static async Task<AndConstraint<ObjectAssertions>> ThrowAsync(this AsyncFunctionAssertions assertions, Type exceptionType, string because = "", params object[] becauseArgs)
+ {
+ return (await assertions.ThrowAsync<Exception>(because, becauseArgs)).Which.Should().BeAssignableTo(exceptionType);
+ }
+ }
+}
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
index c8a79e58..34d7e460 100644
--- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
+++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
@@ -13,8 +13,8 @@ namespace Timeline.Tests.Helpers.Authentication
public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password, int? expireOffset = null)
{
- var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, ExpireOffset = expireOffset });
- response.Should().HaveStatusCodeOk();
+ var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, Expire = expireOffset });
+ response.Should().HaveStatusCode(200);
var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
return result;
}
@@ -29,12 +29,12 @@ namespace Timeline.Tests.Helpers.Authentication
public static Task<HttpClient> CreateClientAsUser<T>(this WebApplicationFactory<T> factory) where T : class
{
- return factory.CreateClientWithCredential(MockUsers.UserUsername, MockUsers.UserPassword);
+ return factory.CreateClientWithCredential(MockUser.User.Username, MockUser.User.Password);
}
public static Task<HttpClient> CreateClientAsAdmin<T>(this WebApplicationFactory<T> factory) where T : class
{
- return factory.CreateClientWithCredential(MockUsers.AdminUsername, MockUsers.AdminPassword);
+ return factory.CreateClientWithCredential(MockUser.Admin.Username, MockUser.Admin.Password);
}
}
}
diff --git a/Timeline.Tests/Helpers/HttpClientExtensions.cs b/Timeline.Tests/Helpers/HttpClientExtensions.cs
index b9204fcc..38641f90 100644
--- a/Timeline.Tests/Helpers/HttpClientExtensions.cs
+++ b/Timeline.Tests/Helpers/HttpClientExtensions.cs
@@ -1,6 +1,8 @@
using Newtonsoft.Json;
+using System;
using System.Net.Http;
using System.Net.Http.Headers;
+using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
@@ -10,11 +12,24 @@ namespace Timeline.Tests.Helpers
{
public static Task<HttpResponseMessage> PatchAsJsonAsync<T>(this HttpClient client, string url, T body)
{
- return client.PatchAsync(url, new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"));
+ return client.PatchAsJsonAsync(new Uri(url, UriKind.RelativeOrAbsolute), body);
+ }
+
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
+ public static Task<HttpResponseMessage> PatchAsJsonAsync<T>(this HttpClient client, Uri url, T body)
+ {
+ return client.PatchAsync(url, new StringContent(
+ JsonConvert.SerializeObject(body), Encoding.UTF8, MediaTypeNames.Application.Json));
}
public static Task<HttpResponseMessage> PutByteArrayAsync(this HttpClient client, string url, byte[] body, string mimeType)
{
+ return client.PutByteArrayAsync(new Uri(url, UriKind.RelativeOrAbsolute), body, mimeType);
+ }
+
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
+ public static Task<HttpResponseMessage> PutByteArrayAsync(this HttpClient client, Uri url, byte[] body, string mimeType)
+ {
var content = new ByteArrayContent(body);
content.Headers.ContentLength = body.Length;
content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
diff --git a/Timeline.Tests/Helpers/ImageHelper.cs b/Timeline.Tests/Helpers/ImageHelper.cs
index 2a2f3870..9bed0917 100644
--- a/Timeline.Tests/Helpers/ImageHelper.cs
+++ b/Timeline.Tests/Helpers/ImageHelper.cs
@@ -9,26 +9,18 @@ namespace Timeline.Tests.Helpers
{
public static byte[] CreatePngWithSize(int width, int height)
{
- using (var image = new Image<Rgba32>(width, height))
- {
- using (var stream = new MemoryStream())
- {
- image.SaveAsPng(stream);
- return stream.ToArray();
- }
- }
+ using var image = new Image<Rgba32>(width, height);
+ using var stream = new MemoryStream();
+ image.SaveAsPng(stream);
+ return stream.ToArray();
}
public static byte[] CreateImageWithSize(int width, int height, IImageFormat format)
{
- using (var image = new Image<Rgba32>(width, height))
- {
- using (var stream = new MemoryStream())
- {
- image.Save(stream, format);
- return stream.ToArray();
- }
- }
+ using var image = new Image<Rgba32>(width, height);
+ using var stream = new MemoryStream();
+ image.Save(stream, format);
+ return stream.ToArray();
}
}
}
diff --git a/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs b/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs
deleted file mode 100644
index af432095..00000000
--- a/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs
+++ /dev/null
@@ -1,23 +0,0 @@
-using System.Net.Http;
-using System.Threading.Tasks;
-using Timeline.Models.Http;
-
-namespace Timeline.Tests.Helpers
-{
- public static class InvalidModelTestHelpers
- {
- public static async Task TestPostInvalidModel<T>(HttpClient client, string url, T body)
- {
- var response = await client.PostAsJsonAsync(url, body);
- response.Should().HaveStatusCodeBadRequest()
- .And.Should().HaveBodyAsCommonResponseWithCode(CommonResponse.ErrorCodes.InvalidModel);
- }
-
- public static async Task TestPutInvalidModel<T>(HttpClient client, string url, T body)
- {
- var response = await client.PutAsJsonAsync(url, body);
- response.Should().HaveStatusCodeBadRequest()
- .And.Should().HaveBodyAsCommonResponseWithCode(CommonResponse.ErrorCodes.InvalidModel);
- }
- }
-}
diff --git a/Timeline.Tests/Helpers/MyTestLoggerFactory.cs b/Timeline.Tests/Helpers/MyTestLoggerFactory.cs
deleted file mode 100644
index b9960378..00000000
--- a/Timeline.Tests/Helpers/MyTestLoggerFactory.cs
+++ /dev/null
@@ -1,25 +0,0 @@
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.Extensions.Logging;
-using Microsoft.Extensions.Logging.Abstractions;
-using Xunit.Abstractions;
-
-namespace Timeline.Tests.Helpers
-{
- public static class Logging
- {
- public static ILoggerFactory Create(ITestOutputHelper outputHelper)
- {
- // TODO: Use test output.
- return NullLoggerFactory.Instance;
- }
-
- public static IWebHostBuilder ConfigureTestLogging(this IWebHostBuilder builder)
- {
- builder.ConfigureLogging(logging =>
- {
- //logging.AddXunit(outputHelper);
- });
- return builder;
- }
- }
-}
diff --git a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
deleted file mode 100644
index dfbe6620..00000000
--- a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
+++ /dev/null
@@ -1,73 +0,0 @@
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.AspNetCore.Mvc.Testing;
-using Microsoft.AspNetCore.TestHost;
-using Microsoft.Data.Sqlite;
-using Microsoft.EntityFrameworkCore;
-using Microsoft.EntityFrameworkCore.Diagnostics;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Logging;
-using System;
-using Timeline.Entities;
-using Timeline.Services;
-using Timeline.Tests.Mock.Data;
-using Timeline.Tests.Mock.Services;
-using Xunit.Abstractions;
-
-namespace Timeline.Tests.Helpers
-{
- public class MyWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
- {
- protected override void ConfigureWebHost(IWebHostBuilder builder)
- {
- builder.ConfigureTestServices(services =>
- {
- services.AddSingleton<IClock, TestClock>();
- });
- }
- }
-
- public static class WebApplicationFactoryExtensions
- {
- public static WebApplicationFactory<TEntry> WithTestConfig<TEntry>(this WebApplicationFactory<TEntry> factory, ITestOutputHelper outputHelper, out Action disposeAction) where TEntry : class
- {
- // We should keep the connection, so the database is persisted but not recreate every time.
- // See https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/sqlite#writing-tests .
- SqliteConnection _databaseConnection = new SqliteConnection("Data Source=:memory:;");
- _databaseConnection.Open();
-
- {
- var options = new DbContextOptionsBuilder<DatabaseContext>()
- .UseSqlite(_databaseConnection)
- .ConfigureWarnings(builder =>
- {
- builder.Throw(RelationalEventId.QueryClientEvaluationWarning);
- })
- .Options;
-
- using (var context = new DatabaseContext(options))
- {
- TestDatabase.InitDatabase(context);
- };
- }
-
- disposeAction = () =>
- {
- _databaseConnection.Close();
- _databaseConnection.Dispose();
- };
-
- return factory.WithWebHostBuilder(builder =>
- {
- builder.ConfigureTestLogging()
- .ConfigureServices(services =>
- {
- services.AddEntityFrameworkSqlite();
- services.AddDbContext<DatabaseContext>(options =>
- {
- options.UseSqlite(_databaseConnection);
- });
- });
- });
- }
- }
-}
diff --git a/Timeline.Tests/Helpers/TestApplication.cs b/Timeline.Tests/Helpers/TestApplication.cs
new file mode 100644
index 00000000..b0187a30
--- /dev/null
+++ b/Timeline.Tests/Helpers/TestApplication.cs
@@ -0,0 +1,52 @@
+using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.Data.Sqlite;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.DependencyInjection;
+using System;
+using Timeline.Entities;
+using Timeline.Tests.Mock.Data;
+
+namespace Timeline.Tests.Helpers
+{
+ public class TestApplication : IDisposable
+ {
+ public SqliteConnection DatabaseConnection { get; } = new SqliteConnection("Data Source=:memory:;");
+ public WebApplicationFactory<Startup> Factory { get; }
+
+ public TestApplication(WebApplicationFactory<Startup> factory)
+ {
+ // We should keep the connection, so the database is persisted but not recreate every time.
+ // See https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/sqlite#writing-tests .
+ DatabaseConnection.Open();
+
+ {
+ var options = new DbContextOptionsBuilder<DatabaseContext>()
+ .UseSqlite(DatabaseConnection)
+ .Options;
+
+ using (var context = new DatabaseContext(options))
+ {
+ TestDatabase.InitDatabase(context);
+ };
+ }
+
+ Factory = factory.WithWebHostBuilder(builder =>
+ {
+ builder.ConfigureServices(services =>
+ {
+ services.AddEntityFrameworkSqlite();
+ services.AddDbContext<DatabaseContext>(options =>
+ {
+ options.UseSqlite(DatabaseConnection);
+ });
+ });
+ });
+ }
+
+ public void Dispose()
+ {
+ DatabaseConnection.Close();
+ DatabaseConnection.Dispose();
+ }
+ }
+}