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 +++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs') 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; } } -- cgit v1.2.3