diff options
author | crupest <crupest@outlook.com> | 2019-11-20 00:28:53 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-11-20 00:28:53 +0800 |
commit | a72960e54a89bd31dcb8be8f52e097007dfd23e5 (patch) | |
tree | 55eaf43bda897148b0fa8a65406e7289aed3832e /Timeline.Tests/IntegratedTests/IntegratedTestBase.cs | |
parent | f3c7912caec2e9eee8a685d8751894f357528a71 (diff) | |
download | timeline-a72960e54a89bd31dcb8be8f52e097007dfd23e5.tar.gz timeline-a72960e54a89bd31dcb8be8f52e097007dfd23e5.tar.bz2 timeline-a72960e54a89bd31dcb8be8f52e097007dfd23e5.zip |
Clean and refactor tests.
Diffstat (limited to 'Timeline.Tests/IntegratedTests/IntegratedTestBase.cs')
-rw-r--r-- | Timeline.Tests/IntegratedTests/IntegratedTestBase.cs | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs index 2dfaf82e..242a452d 100644 --- a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs +++ b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs @@ -1,14 +1,37 @@ using Microsoft.AspNetCore.Mvc.Testing; using System; using System.Collections.Generic; -using System.Linq; +using System.Net.Http; using System.Threading.Tasks; +using Timeline.Models.Http; using Timeline.Tests.Helpers; -using Timeline.Tests.Mock.Data; 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<WebApplicationFactory<Startup>>, IDisposable { protected TestApplication TestApp { get; } @@ -20,8 +43,14 @@ namespace Timeline.Tests.IntegratedTests TestApp = new TestApplication(factory); } - public virtual void Dispose() + protected virtual void OnDispose() + { + + } + + public void Dispose() { + OnDispose(); TestApp.Dispose(); } @@ -31,5 +60,35 @@ namespace Timeline.Tests.IntegratedTests } protected IReadOnlyList<MockUser> ExtraMockUsers => TestApp.Database.ExtraMockUsers; + + public Task<HttpClient> CreateClientWithNoAuth() + { + return Task.FromResult(Factory.CreateDefaultClient()); + } + + public async Task<HttpClient> CreateClientWithCredential(string username, string password) + { + var client = Factory.CreateDefaultClient(); + var response = await client.PostAsJsonAsync("/token/create", + new CreateTokenRequest { Username = username, Password = password }); + var token = response.Should().HaveStatusCode(200) + .And.HaveJsonBody<CreateTokenResponse>().Which.Token; + client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); + return client; + } + + public Task<HttpClient> CreateClientAs(MockUser user) + { + if (user == null) + return CreateClientWithNoAuth(); + return CreateClientWithCredential(user.Username, user.Password); + } + + public Task<HttpClient> CreateClientAs(AuthType authType) => CreateClientAs(authType.GetMockUser()); + + + public Task<HttpClient> CreateClientAsUser() => CreateClientAs(MockUser.User); + public Task<HttpClient> CreateClientAsAdmin() => CreateClientAs(MockUser.Admin); + } } |