diff options
author | 杨宇千 <crupest@outlook.com> | 2019-11-20 18:21:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-20 18:21:17 +0800 |
commit | ae5bb5cbef2aec94673e26712d7937fca3996f5b (patch) | |
tree | 788b8acdf1141c757cb3226d3cd5f64594386b8f /Timeline.Tests/IntegratedTests/IntegratedTestBase.cs | |
parent | 37a2e6340ab20de1f9e847d795c0cbec9846de97 (diff) | |
parent | ca87f6781a5b0e80989a66be338a699846c40f8d (diff) | |
download | timeline-ae5bb5cbef2aec94673e26712d7937fca3996f5b.tar.gz timeline-ae5bb5cbef2aec94673e26712d7937fca3996f5b.tar.bz2 timeline-ae5bb5cbef2aec94673e26712d7937fca3996f5b.zip |
Merge pull request #54 from crupest/timeline
Add core feature Timeline (currently only personal timeline)
Diffstat (limited to 'Timeline.Tests/IntegratedTests/IntegratedTestBase.cs')
-rw-r--r-- | Timeline.Tests/IntegratedTests/IntegratedTestBase.cs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs new file mode 100644 index 00000000..242a452d --- /dev/null +++ b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs @@ -0,0 +1,94 @@ +using Microsoft.AspNetCore.Mvc.Testing; +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; +using Timeline.Models.Http; +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<WebApplicationFactory<Startup>>, IDisposable + { + protected TestApplication TestApp { get; } + + protected WebApplicationFactory<Startup> Factory => TestApp.Factory; + + public IntegratedTestBase(WebApplicationFactory<Startup> factory) + { + TestApp = new TestApplication(factory); + } + + protected virtual void OnDispose() + { + + } + + public void Dispose() + { + OnDispose(); + TestApp.Dispose(); + } + + protected void CreateExtraMockUsers(int count) + { + TestApp.Database.CreateExtraMockUsers(count); + } + + 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); + + } +} |