From 52acf41e331ddbd66befed4692c804b754ba7d5c Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 30 Jan 2020 20:26:52 +0800 Subject: ... --- .../IntegratedTests/IntegratedTestBase.cs | 117 +++++++++++++-------- 1 file changed, 75 insertions(+), 42 deletions(-) (limited to 'Timeline.Tests/IntegratedTests/IntegratedTestBase.cs') diff --git a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs index 242a452d..721a25af 100644 --- a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs +++ b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs @@ -1,36 +1,17 @@ -using Microsoft.AspNetCore.Mvc.Testing; +using AutoMapper; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.DependencyInjection; using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; using Timeline.Models.Http; +using Timeline.Services; using Timeline.Tests.Helpers; using Xunit; namespace Timeline.Tests.IntegratedTests { - public enum AuthType - { - None, - User, - Admin - } - - public static class AuthTypeExtensions - { - public static MockUser GetMockUser(this AuthType authType) - { - return authType switch - { - AuthType.None => null, - AuthType.User => MockUser.User, - AuthType.Admin => MockUser.Admin, - _ => throw new InvalidOperationException("Unknown auth type.") - }; - } - - public static string GetUsername(this AuthType authType) => authType.GetMockUser().Username; - } public abstract class IntegratedTestBase : IClassFixture>, IDisposable { @@ -38,14 +19,62 @@ namespace Timeline.Tests.IntegratedTests protected WebApplicationFactory Factory => TestApp.Factory; - public IntegratedTestBase(WebApplicationFactory factory) + public IntegratedTestBase(WebApplicationFactory factory) : this(factory, 1) + { + + } + + public IntegratedTestBase(WebApplicationFactory factory, int userCount) { + if (userCount < 0) + throw new ArgumentOutOfRangeException(nameof(userCount), userCount, "User count can't be negative."); + TestApp = new TestApplication(factory); + + using (var scope = Factory.Services.CreateScope()) + { + var users = new List() + { + new User + { + Username = "admin", + Password = "adminpw", + Administrator = true, + Nickname = "administrator" + } + }; + + for (int i = 1; i <= userCount; i++) + { + users.Add(new User + { + Username = $"user{i}", + Password = $"user{i}pw", + Administrator = false, + Nickname = $"imuser{i}" + }); + } + + var userInfoList = new List(); + var userInfoForAdminList = new List(); + + var userService = scope.ServiceProvider.GetRequiredService(); + var mapper = scope.ServiceProvider.GetRequiredService(); + + foreach (var user in users) + { + userService.CreateUser(user); + userInfoList.Add(mapper.Map(user)); + userInfoForAdminList.Add(mapper.Map(user)); + } + + UserInfoList = userInfoList; + UserInfoForAdminList = userInfoForAdminList; + } } protected virtual void OnDispose() { - } public void Dispose() @@ -54,14 +83,11 @@ namespace Timeline.Tests.IntegratedTests TestApp.Dispose(); } - protected void CreateExtraMockUsers(int count) - { - TestApp.Database.CreateExtraMockUsers(count); - } + public IReadOnlyList UserInfoList { get; } - protected IReadOnlyList ExtraMockUsers => TestApp.Database.ExtraMockUsers; + public IReadOnlyList UserInfoForAdminList { get; } - public Task CreateClientWithNoAuth() + public Task CreateDefaultClient() { return Task.FromResult(Factory.CreateDefaultClient()); } @@ -77,18 +103,25 @@ namespace Timeline.Tests.IntegratedTests return client; } - public Task CreateClientAs(MockUser user) + public Task CreateClientAs(int userNumber) { - if (user == null) - return CreateClientWithNoAuth(); - return CreateClientWithCredential(user.Username, user.Password); - } - - public Task CreateClientAs(AuthType authType) => CreateClientAs(authType.GetMockUser()); - + if (userNumber < 0) + throw new ArgumentOutOfRangeException(nameof(userNumber), "User number can't be negative."); - public Task CreateClientAsUser() => CreateClientAs(MockUser.User); - public Task CreateClientAsAdmin() => CreateClientAs(MockUser.Admin); + if (userNumber == 0) + return CreateClientWithCredential("admin", "adminpw"); + else + return CreateClientWithCredential($"user{userNumber}", $"user{userNumber}pw"); + } + public Task CreateClientAsAdministrator() + { + return CreateClientAs(0); + } + + public Task CreateClientAsUser() + { + return CreateClientAs(1); + } } } -- cgit v1.2.3