aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-11-20 00:28:53 +0800
committercrupest <crupest@outlook.com>2019-11-20 00:28:53 +0800
commited8bae9cf7fd22300678d718cfee1913209f2cd0 (patch)
tree55eaf43bda897148b0fa8a65406e7289aed3832e /Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
parent0ff98dd9274485994b11d9d00f4b647a5f2d42ac (diff)
downloadtimeline-ed8bae9cf7fd22300678d718cfee1913209f2cd0.tar.gz
timeline-ed8bae9cf7fd22300678d718cfee1913209f2cd0.tar.bz2
timeline-ed8bae9cf7fd22300678d718cfee1913209f2cd0.zip
Clean and refactor tests.
Diffstat (limited to 'Timeline.Tests/IntegratedTests/IntegratedTestBase.cs')
-rw-r--r--Timeline.Tests/IntegratedTests/IntegratedTestBase.cs65
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);
+
}
}