diff options
author | crupest <crupest@outlook.com> | 2019-04-21 00:08:59 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-04-21 00:08:59 +0800 |
commit | 076e0131dff71a9f76fff13c92fffa0ef408935f (patch) | |
tree | 9fa853690866f452a571b45fb062ac692d298a2e /Timeline.Tests/Helpers/Authentication | |
parent | 325d4c7dbfba45e9c5a7518279831f54c4690d20 (diff) | |
download | timeline-076e0131dff71a9f76fff13c92fffa0ef408935f.tar.gz timeline-076e0131dff71a9f76fff13c92fffa0ef408935f.tar.bz2 timeline-076e0131dff71a9f76fff13c92fffa0ef408935f.zip |
Reorgnize api. Add basic unit test.
Diffstat (limited to 'Timeline.Tests/Helpers/Authentication')
-rw-r--r-- | Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs (renamed from Timeline.Tests/Helpers/Authentication/AuthenticationHttpClientExtensions.cs) | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationHttpClientExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs index c0051c53..40191009 100644 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationHttpClientExtensions.cs +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using Microsoft.AspNetCore.Mvc.Testing; +using Newtonsoft.Json; using System; using System.Net; using System.Net.Http; @@ -8,9 +9,9 @@ using Xunit; namespace Timeline.Tests.Helpers.Authentication { - public static class AuthenticationHttpClientExtensions + public static class AuthenticationExtensions { - private const string CreateTokenUrl = "/User/CreateToken"; + private const string CreateTokenUrl = "/token/create"; public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password, bool assertSuccess = true) { @@ -24,6 +25,14 @@ namespace Timeline.Tests.Helpers.Authentication return result; } + public static async Task<HttpClient> CreateClientWithUser<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 async Task<HttpResponseMessage> SendWithAuthenticationAsync(this HttpClient client, string token, string path, Action<HttpRequestMessage> requestBuilder = null) { var request = new HttpRequestMessage |