aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/Authentication
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-09 21:48:12 +0800
committerGitHub <noreply@github.com>2019-08-09 21:48:12 +0800
commit38ff45fcc0b58a95ad52ba43a8be4ff466694269 (patch)
treef1cf455b758e8bf0265d4db0e42a404e9877b321 /Timeline.Tests/Helpers/Authentication
parente283a3e745bad05a55c572646d7b20fbaaeb522d (diff)
parentea8397bafe24b5c9ab814891eb3a293a07ca217e (diff)
downloadtimeline-38ff45fcc0b58a95ad52ba43a8be4ff466694269.tar.gz
timeline-38ff45fcc0b58a95ad52ba43a8be4ff466694269.tar.bz2
timeline-38ff45fcc0b58a95ad52ba43a8be4ff466694269.zip
Merge pull request #38 from crupest/null-request-field
Do 3 things.
Diffstat (limited to 'Timeline.Tests/Helpers/Authentication')
-rw-r--r--Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs18
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);
+ }
}
}