From f3c7912caec2e9eee8a685d8751894f357528a71 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 19 Nov 2019 23:18:45 +0800 Subject: Complete integrated tests??? Fix bugs. --- .../Authentication/AuthenticationExtensions.cs | 28 +++++++++++++++++----- Timeline.Tests/Helpers/TestApplication.cs | 22 +++-------------- 2 files changed, 25 insertions(+), 25 deletions(-) (limited to 'Timeline.Tests/Helpers') diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs index 6a78be7a..4048bb73 100644 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs @@ -1,5 +1,4 @@ using Microsoft.AspNetCore.Mvc.Testing; -using Newtonsoft.Json; using System; using System.Net.Http; using System.Threading.Tasks; @@ -22,9 +21,8 @@ namespace Timeline.Tests.Helpers.Authentication public static async Task CreateUserTokenAsync(this HttpClient client, string username, string password, int? expireOffset = null) { var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, Expire = expireOffset }); - response.Should().HaveStatusCode(200); - var result = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); - return result; + return response.Should().HaveStatusCode(200) + .And.HaveJsonBody().Which; } public static async Task CreateClientWithCredential(this WebApplicationFactory factory, string username, string password) where T : class @@ -35,14 +33,19 @@ namespace Timeline.Tests.Helpers.Authentication return client; } + public static Task CreateClientAs(this WebApplicationFactory factory, MockUser user) where T : class + { + return CreateClientWithCredential(factory, user.Username, user.Password); + } + public static Task CreateClientAsUser(this WebApplicationFactory factory) where T : class { - return factory.CreateClientWithCredential(MockUser.User.Username, MockUser.User.Password); + return factory.CreateClientAs(MockUser.User); } public static Task CreateClientAsAdmin(this WebApplicationFactory factory) where T : class { - return factory.CreateClientWithCredential(MockUser.Admin.Username, MockUser.Admin.Password); + return factory.CreateClientAs(MockUser.Admin); } public static Task CreateClientAs(this WebApplicationFactory factory, AuthType authType) where T : class @@ -55,5 +58,18 @@ namespace Timeline.Tests.Helpers.Authentication _ => throw new InvalidOperationException("Unknown auth type.") }; } + + public static MockUser GetMockUser(this AuthType authType) + { + return authType switch + { + AuthType.None => null, + AuthType.User => MockUser.User, + AuthType.Admin => MockUser.Admin, + _ => throw new InvalidOperationException("Unknown auth type.") + }; + } + + public static string GetUsername(this AuthType authType) => authType.GetMockUser().Username; } } diff --git a/Timeline.Tests/Helpers/TestApplication.cs b/Timeline.Tests/Helpers/TestApplication.cs index b0187a30..5862f452 100644 --- a/Timeline.Tests/Helpers/TestApplication.cs +++ b/Timeline.Tests/Helpers/TestApplication.cs @@ -10,26 +10,11 @@ namespace Timeline.Tests.Helpers { public class TestApplication : IDisposable { - public SqliteConnection DatabaseConnection { get; } = new SqliteConnection("Data Source=:memory:;"); + public TestDatabase Database { get; } = new TestDatabase(); public WebApplicationFactory Factory { get; } public TestApplication(WebApplicationFactory 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() - .UseSqlite(DatabaseConnection) - .Options; - - using (var context = new DatabaseContext(options)) - { - TestDatabase.InitDatabase(context); - }; - } - Factory = factory.WithWebHostBuilder(builder => { builder.ConfigureServices(services => @@ -37,7 +22,7 @@ namespace Timeline.Tests.Helpers services.AddEntityFrameworkSqlite(); services.AddDbContext(options => { - options.UseSqlite(DatabaseConnection); + options.UseSqlite(Database.Connection); }); }); }); @@ -45,8 +30,7 @@ namespace Timeline.Tests.Helpers public void Dispose() { - DatabaseConnection.Close(); - DatabaseConnection.Dispose(); + Database.Dispose(); } } } -- cgit v1.2.3