diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-09 21:48:12 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-09 21:48:12 +0800 |
commit | d97185e6152a327f6ef3b1873bfd86f1a3aac3a1 (patch) | |
tree | f1cf455b758e8bf0265d4db0e42a404e9877b321 /Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs | |
parent | 29bd71cd93b03248254f341aff9252374abc74ec (diff) | |
parent | c964cbbb6c8f228bd4c3943025c6107ac4e42a13 (diff) | |
download | timeline-d97185e6152a327f6ef3b1873bfd86f1a3aac3a1.tar.gz timeline-d97185e6152a327f6ef3b1873bfd86f1a3aac3a1.tar.bz2 timeline-d97185e6152a327f6ef3b1873bfd86f1a3aac3a1.zip |
Merge pull request #38 from crupest/null-request-field
Do 3 things.
Diffstat (limited to 'Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs')
-rw-r--r-- | Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs index c8bec266..8a44c852 100644 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs @@ -2,7 +2,8 @@ using Microsoft.AspNetCore.Mvc.Testing; using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
-using Timeline.Entities.Http;
+using Timeline.Models.Http;
+using Timeline.Tests.Mock.Data;
namespace Timeline.Tests.Helpers.Authentication
{
@@ -10,19 +11,30 @@ namespace Timeline.Tests.Helpers.Authentication {
private const string CreateTokenUrl = "/token/create";
- public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password, double? expireOffset = null)
+ 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, ExpireOffset = expireOffset });
+ response.AssertOk();
var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
return result;
}
- public static async Task<HttpClient> CreateClientWithUser<T>(this WebApplicationFactory<T> factory, string username, string password) where T : class
+ 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> CreateClientAsUser<T>(this WebApplicationFactory<T> factory) where T : class
+ {
+ return factory.CreateClientWithCredential(MockUsers.UserUsername, MockUsers.UserPassword);
+ }
+
+ public static Task<HttpClient> CreateClientAsAdmin<T>(this WebApplicationFactory<T> factory) where T : class
+ {
+ return factory.CreateClientWithCredential(MockUsers.AdminUsername, MockUsers.AdminPassword);
+ }
}
}
|