aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline.Tests/Helpers')
-rw-r--r--Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs13
-rw-r--r--Timeline.Tests/Helpers/MyWebApplicationFactory.cs4
-rw-r--r--Timeline.Tests/Helpers/TestClock.cs25
-rw-r--r--Timeline.Tests/Helpers/TestUsers.cs42
4 files changed, 15 insertions, 69 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
index de88fd35..e31bd51c 100644
--- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
+++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
@@ -3,6 +3,7 @@ using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
using Timeline.Models.Http;
+using Timeline.Tests.Mock.Data;
namespace Timeline.Tests.Helpers.Authentication
{
@@ -17,12 +18,22 @@ 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
+ 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);
+ }
}
}
diff --git a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
index d8da7168..b49756e4 100644
--- a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
+++ b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
@@ -7,6 +7,8 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Timeline.Entities;
using Timeline.Services;
+using Timeline.Tests.Mock.Data;
+using Timeline.Tests.Mock.Services;
using Xunit.Abstractions;
namespace Timeline.Tests.Helpers
@@ -34,7 +36,7 @@ namespace Timeline.Tests.Helpers
using (var context = new DatabaseContext(options))
{
context.Database.EnsureCreated();
- context.Users.AddRange(TestMockUsers.MockUsers);
+ context.Users.AddRange(MockUsers.Users);
context.SaveChanges();
}
}
diff --git a/Timeline.Tests/Helpers/TestClock.cs b/Timeline.Tests/Helpers/TestClock.cs
deleted file mode 100644
index ea90305f..00000000
--- a/Timeline.Tests/Helpers/TestClock.cs
+++ /dev/null
@@ -1,25 +0,0 @@
-using Microsoft.AspNetCore.Mvc.Testing;
-using Microsoft.Extensions.DependencyInjection;
-using System;
-using Timeline.Services;
-
-namespace Timeline.Tests.Helpers
-{
- public class TestClock : IClock
- {
- public DateTime? MockCurrentTime { get; set; } = null;
-
- public DateTime GetCurrentTime()
- {
- return MockCurrentTime.GetValueOrDefault(DateTime.Now);
- }
- }
-
- public static class TestClockWebApplicationFactoryExtensions
- {
- public static TestClock GetTestClock<T>(this WebApplicationFactory<T> factory) where T : class
- {
- return factory.Server.Host.Services.GetRequiredService<IClock>() as TestClock;
- }
- }
-}
diff --git a/Timeline.Tests/Helpers/TestUsers.cs b/Timeline.Tests/Helpers/TestUsers.cs
deleted file mode 100644
index 71de8237..00000000
--- a/Timeline.Tests/Helpers/TestUsers.cs
+++ /dev/null
@@ -1,42 +0,0 @@
-using System.Collections.Generic;
-using System.Linq;
-using Timeline.Entities;
-using Timeline.Models;
-using Timeline.Services;
-
-namespace Timeline.Tests.Helpers
-{
- public static class TestMockUsers
- {
- static TestMockUsers()
- {
- var mockUsers = new List<User>();
- var passwordService = new PasswordService();
-
- mockUsers.Add(new User
- {
- Name = "user",
- EncryptedPassword = passwordService.HashPassword("user"),
- RoleString = UserUtility.IsAdminToRoleString(false),
- Version = 0,
- });
- mockUsers.Add(new User
- {
- Name = "admin",
- EncryptedPassword = passwordService.HashPassword("admin"),
- RoleString = UserUtility.IsAdminToRoleString(true),
- Version = 0,
- });
-
- MockUsers = mockUsers;
-
- var mockUserInfos = mockUsers.Select(u => UserUtility.CreateUserInfo(u)).ToList();
- mockUserInfos.Sort(UserInfoComparers.Comparer);
- MockUserInfos = mockUserInfos;
- }
-
- public static List<User> MockUsers { get; }
-
- public static IReadOnlyList<UserInfo> MockUserInfos { get; }
- }
-}