aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-11-17 23:04:21 +0800
committercrupest <crupest@outlook.com>2019-11-17 23:04:21 +0800
commit59c10650fc36e8c79c1def088240fd90a5151250 (patch)
tree46282455cd6bdf43bf2215d3918f5803b2a31674 /Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
parentc2b107eda8a14f836d663638641a3e3f17373b5d (diff)
downloadtimeline-59c10650fc36e8c79c1def088240fd90a5151250.tar.gz
timeline-59c10650fc36e8c79c1def088240fd90a5151250.tar.bz2
timeline-59c10650fc36e8c79c1def088240fd90a5151250.zip
Fix typo in path of personal timeline controller actions. Add timeline permission test.
Diffstat (limited to 'Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs')
-rw-r--r--Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs19
1 files changed, 19 insertions, 0 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
index 34d7e460..6a78be7a 100644
--- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
+++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Newtonsoft.Json;
+using System;
using System.Net.Http;
using System.Threading.Tasks;
using Timeline.Models.Http;
@@ -7,6 +8,13 @@ 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";
@@ -36,5 +44,16 @@ namespace Timeline.Tests.Helpers.Authentication
{
return factory.CreateClientWithCredential(MockUser.Admin.Username, MockUser.Admin.Password);
}
+
+ 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.")
+ };
+ }
}
}