diff options
author | crupest <crupest@outlook.com> | 2019-11-19 23:18:45 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-11-19 23:18:45 +0800 |
commit | 0ff98dd9274485994b11d9d00f4b647a5f2d42ac (patch) | |
tree | 66e1bcfe602877e4f72ea1b68cbf33e06557c222 /Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs | |
parent | 0ff55237330279e5df6cd2838e4b90507d3542a6 (diff) | |
download | timeline-0ff98dd9274485994b11d9d00f4b647a5f2d42ac.tar.gz timeline-0ff98dd9274485994b11d9d00f4b647a5f2d42ac.tar.bz2 timeline-0ff98dd9274485994b11d9d00f4b647a5f2d42ac.zip |
Complete integrated tests??? Fix bugs.
Diffstat (limited to 'Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs')
-rw-r--r-- | Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs | 28 |
1 files changed, 22 insertions, 6 deletions
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<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 });
- response.Should().HaveStatusCode(200);
- var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
- return result;
+ 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
@@ -35,14 +33,19 @@ namespace Timeline.Tests.Helpers.Authentication 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.CreateClientWithCredential(MockUser.User.Username, MockUser.User.Password);
+ return factory.CreateClientAs(MockUser.User);
}
public static Task<HttpClient> CreateClientAsAdmin<T>(this WebApplicationFactory<T> factory) where T : class
{
- return factory.CreateClientWithCredential(MockUser.Admin.Username, MockUser.Admin.Password);
+ return factory.CreateClientAs(MockUser.Admin);
}
public static Task<HttpClient> CreateClientAs<T>(this WebApplicationFactory<T> 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;
}
}
|