From 6c50630c4d6576446fc2338714feaefbf28c4ed1 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 8 Aug 2019 17:41:59 +0800 Subject: Reorganize unit tests. --- .../Authentication/AuthenticationExtensions.cs | 13 ++++++- Timeline.Tests/Helpers/MyWebApplicationFactory.cs | 4 ++- Timeline.Tests/Helpers/TestClock.cs | 25 ------------- Timeline.Tests/Helpers/TestUsers.cs | 42 ---------------------- 4 files changed, 15 insertions(+), 69 deletions(-) delete mode 100644 Timeline.Tests/Helpers/TestClock.cs delete mode 100644 Timeline.Tests/Helpers/TestUsers.cs (limited to 'Timeline.Tests/Helpers') 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 CreateClientWithUser(this WebApplicationFactory factory, string username, string password) where T : class + public static async Task CreateClientWithCredential(this WebApplicationFactory 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 CreateClientAsUser(this WebApplicationFactory factory) where T : class + { + return factory.CreateClientWithCredential(MockUsers.UserUsername, MockUsers.UserPassword); + } + + public static Task CreateClientAsAdmin(this WebApplicationFactory 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(this WebApplicationFactory factory) where T : class - { - return factory.Server.Host.Services.GetRequiredService() 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(); - 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 MockUsers { get; } - - public static IReadOnlyList MockUserInfos { get; } - } -} -- cgit v1.2.3