From ed8bae9cf7fd22300678d718cfee1913209f2cd0 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 20 Nov 2019 00:28:53 +0800 Subject: Clean and refactor tests. --- .../Authentication/AuthenticationExtensions.cs | 75 ---------------------- 1 file changed, 75 deletions(-) delete mode 100644 Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs (limited to 'Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs') diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs deleted file mode 100644 index 4048bb73..00000000 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs +++ /dev/null @@ -1,75 +0,0 @@ -using Microsoft.AspNetCore.Mvc.Testing; -using System; -using System.Net.Http; -using System.Threading.Tasks; -using Timeline.Models.Http; -using Timeline.Tests.Mock.Data; - -namespace Timeline.Tests.Helpers.Authentication -{ - public enum AuthType - { - None, - User, - Admin - } - - public static class AuthenticationExtensions - { - private const string CreateTokenUrl = "/token/create"; - - 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 }); - return response.Should().HaveStatusCode(200) - .And.HaveJsonBody().Which; - } - - public static async Task CreateClientWithCredential(this WebApplicationFactory factory, string username, string password) where T : class - { - var client = factory.CreateDefaultClient(); - var token = (await client.CreateUserTokenAsync(username, password)).Token; - client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); - 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.CreateClientAs(MockUser.User); - } - - public static Task CreateClientAsAdmin(this WebApplicationFactory factory) where T : class - { - return factory.CreateClientAs(MockUser.Admin); - } - - public static Task CreateClientAs(this WebApplicationFactory factory, AuthType authType) where T : class - { - return authType switch - { - AuthType.None => Task.FromResult(factory.CreateDefaultClient()), - AuthType.User => factory.CreateClientAsUser(), - AuthType.Admin => factory.CreateClientAsAdmin(), - _ => 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