aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/Authentication
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-11-20 00:28:53 +0800
committercrupest <crupest@outlook.com>2019-11-20 00:28:53 +0800
commita72960e54a89bd31dcb8be8f52e097007dfd23e5 (patch)
tree55eaf43bda897148b0fa8a65406e7289aed3832e /Timeline.Tests/Helpers/Authentication
parentf3c7912caec2e9eee8a685d8751894f357528a71 (diff)
downloadtimeline-a72960e54a89bd31dcb8be8f52e097007dfd23e5.tar.gz
timeline-a72960e54a89bd31dcb8be8f52e097007dfd23e5.tar.bz2
timeline-a72960e54a89bd31dcb8be8f52e097007dfd23e5.zip
Clean and refactor tests.
Diffstat (limited to 'Timeline.Tests/Helpers/Authentication')
-rw-r--r--Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs75
-rw-r--r--Timeline.Tests/Helpers/Authentication/PrincipalHelper.cs23
2 files changed, 0 insertions, 98 deletions
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<CreateTokenResponse> 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<CreateTokenResponse>().Which;
- }
-
- public static async Task<HttpClient> CreateClientWithCredential<T>(this WebApplicationFactory<T> 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<HttpClient> CreateClientAs<T>(this WebApplicationFactory<T> factory, MockUser user) where T : class
- {
- return CreateClientWithCredential(factory, user.Username, user.Password);
- }
-
- public static Task<HttpClient> CreateClientAsUser<T>(this WebApplicationFactory<T> factory) where T : class
- {
- return factory.CreateClientAs(MockUser.User);
- }
-
- public static Task<HttpClient> CreateClientAsAdmin<T>(this WebApplicationFactory<T> factory) where T : class
- {
- return factory.CreateClientAs(MockUser.Admin);
- }
-
- public static Task<HttpClient> CreateClientAs<T>(this WebApplicationFactory<T> 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;
- }
-}
diff --git a/Timeline.Tests/Helpers/Authentication/PrincipalHelper.cs b/Timeline.Tests/Helpers/Authentication/PrincipalHelper.cs
deleted file mode 100644
index 214472a2..00000000
--- a/Timeline.Tests/Helpers/Authentication/PrincipalHelper.cs
+++ /dev/null
@@ -1,23 +0,0 @@
-using System.Linq;
-using System.Security.Claims;
-using Timeline.Models;
-
-namespace Timeline.Tests.Helpers.Authentication
-{
- public static class PrincipalHelper
- {
- internal const string AuthScheme = "TESTAUTH";
-
- internal static ClaimsPrincipal Create(string username, bool administrator)
- {
- var identity = new ClaimsIdentity(AuthScheme);
- identity.AddClaim(new Claim(identity.NameClaimType, username, ClaimValueTypes.String));
- identity.AddClaims(UserRoleConvert.ToArray(administrator).Select(role => new Claim(identity.RoleClaimType, role, ClaimValueTypes.String)));
-
- var principal = new ClaimsPrincipal();
- principal.AddIdentity(identity);
-
- return principal;
- }
- }
-}