aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline.Tests/Helpers')
-rw-r--r--Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs16
-rw-r--r--Timeline.Tests/Helpers/MyWebApplicationFactory.cs83
-rw-r--r--Timeline.Tests/Helpers/ResponseExtensions.cs14
-rw-r--r--Timeline.Tests/Helpers/TestClock.cs25
-rw-r--r--Timeline.Tests/Helpers/TestUsers.cs8
-rw-r--r--Timeline.Tests/Helpers/UserInfoComparers.cs2
-rw-r--r--Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs53
7 files changed, 129 insertions, 72 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
index f4e2e45a..27362ac3 100644
--- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
+++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Newtonsoft.Json;
-using System;
using System.Net.Http;
using System.Threading.Tasks;
using Timeline.Entities.Http;
@@ -11,9 +10,9 @@ namespace Timeline.Tests.Helpers.Authentication
{
private const string CreateTokenUrl = "/token/create";
- public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password)
+ public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password, double? expireOffset = null)
{
- var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password });
+ var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, ExpireOffset = expireOffset });
var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
return result;
}
@@ -25,16 +24,5 @@ namespace Timeline.Tests.Helpers.Authentication
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
return client;
}
-
- public static async Task<HttpResponseMessage> SendWithAuthenticationAsync(this HttpClient client, string token, string path, Action<HttpRequestMessage> requestBuilder = null)
- {
- var request = new HttpRequestMessage
- {
- RequestUri = new Uri(client.BaseAddress, path),
- };
- request.Headers.Add("Authorization", "Bearer " + token);
- requestBuilder?.Invoke(request);
- return await client.SendAsync(request);
- }
}
}
diff --git a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
new file mode 100644
index 00000000..903cd670
--- /dev/null
+++ b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
@@ -0,0 +1,83 @@
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.AspNetCore.TestHost;
+using Microsoft.Data.Sqlite;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
+using Timeline.Models;
+using Timeline.Services;
+using Xunit.Abstractions;
+
+namespace Timeline.Tests.Helpers
+{
+ public class MyWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
+ {
+ // We should keep the connection, so the database is persisted but not recreate every time.
+ // See https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/sqlite#writing-tests .
+ private readonly SqliteConnection _databaseConnection;
+
+ public MyWebApplicationFactory() : base()
+ {
+ _databaseConnection = new SqliteConnection("Data Source=:memory:;");
+ _databaseConnection.Open();
+
+ InitDatabase();
+ }
+
+ private void InitDatabase()
+ {
+ var options = new DbContextOptionsBuilder<DatabaseContext>()
+ .UseSqlite(_databaseConnection)
+ .Options;
+
+ using (var context = new DatabaseContext(options))
+ {
+ context.Database.EnsureCreated();
+ context.Users.AddRange(TestMockUsers.MockUsers);
+ context.SaveChanges();
+ }
+ }
+
+ protected override void ConfigureWebHost(IWebHostBuilder builder)
+ {
+ builder.ConfigureServices(services =>
+ {
+ services.AddEntityFrameworkSqlite();
+ services.AddDbContext<DatabaseContext>(options =>
+ {
+ options.UseSqlite(_databaseConnection);
+ });
+ })
+ .ConfigureTestServices(services =>
+ {
+ services.AddSingleton<IClock, TestClock>();
+ });
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ _databaseConnection.Close();
+ _databaseConnection.Dispose();
+ }
+
+ base.Dispose(disposing);
+ }
+ }
+
+ public static class WebApplicationFactoryExtensions
+ {
+ public static WebApplicationFactory<TEntry> WithTestLogging<TEntry>(this WebApplicationFactory<TEntry> factory, ITestOutputHelper outputHelper) where TEntry : class
+ {
+ return factory.WithWebHostBuilder(builder =>
+ {
+ builder.ConfigureLogging(logging =>
+ {
+ logging.AddXunit(outputHelper);
+ });
+ });
+ }
+ }
+}
diff --git a/Timeline.Tests/Helpers/ResponseExtensions.cs b/Timeline.Tests/Helpers/ResponseExtensions.cs
new file mode 100644
index 00000000..86ac1c88
--- /dev/null
+++ b/Timeline.Tests/Helpers/ResponseExtensions.cs
@@ -0,0 +1,14 @@
+using Newtonsoft.Json;
+using System.Net.Http;
+using System.Threading.Tasks;
+
+namespace Timeline.Tests.Helpers
+{
+ public static class ResponseExtensions
+ {
+ public static async Task<T> ReadBodyAsJson<T>(this HttpResponseMessage response)
+ {
+ return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
+ }
+ }
+}
diff --git a/Timeline.Tests/Helpers/TestClock.cs b/Timeline.Tests/Helpers/TestClock.cs
new file mode 100644
index 00000000..91523f2b
--- /dev/null
+++ b/Timeline.Tests/Helpers/TestClock.cs
@@ -0,0 +1,25 @@
+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<T>(this WebApplicationFactory<T> factory) where T : class
+ {
+ return factory.Server.Host.Services.GetRequiredService<IClock>() as TestClock;
+ }
+ }
+}
diff --git a/Timeline.Tests/Helpers/TestUsers.cs b/Timeline.Tests/Helpers/TestUsers.cs
index dd00e38d..41dd83a9 100644
--- a/Timeline.Tests/Helpers/TestUsers.cs
+++ b/Timeline.Tests/Helpers/TestUsers.cs
@@ -11,19 +11,21 @@ namespace Timeline.Tests.Helpers
static TestMockUsers()
{
var mockUsers = new List<User>();
- var passwordService = new PasswordService(null);
+ var passwordService = new PasswordService();
mockUsers.Add(new User
{
Name = "user",
EncryptedPassword = passwordService.HashPassword("user"),
- RoleString = "user"
+ RoleString = UserUtility.IsAdminToRoleString(false),
+ Version = 0,
});
mockUsers.Add(new User
{
Name = "admin",
EncryptedPassword = passwordService.HashPassword("admin"),
- RoleString = "user,admin"
+ RoleString = UserUtility.IsAdminToRoleString(true),
+ Version = 0,
});
MockUsers = mockUsers;
diff --git a/Timeline.Tests/Helpers/UserInfoComparers.cs b/Timeline.Tests/Helpers/UserInfoComparers.cs
index 0d91efe3..fcf37e5c 100644
--- a/Timeline.Tests/Helpers/UserInfoComparers.cs
+++ b/Timeline.Tests/Helpers/UserInfoComparers.cs
@@ -1,6 +1,4 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Timeline.Entities;
namespace Timeline.Tests.Helpers
diff --git a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
deleted file mode 100644
index a7616b41..00000000
--- a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
+++ /dev/null
@@ -1,53 +0,0 @@
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.AspNetCore.Mvc.Testing;
-using Microsoft.EntityFrameworkCore;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Logging;
-using Timeline.Models;
-using Xunit.Abstractions;
-
-namespace Timeline.Tests.Helpers
-{
- public static class WebApplicationFactoryExtensions
- {
- public static WebApplicationFactory<TEntry> WithTestConfig<TEntry>(this WebApplicationFactory<TEntry> factory, ITestOutputHelper outputHelper) where TEntry : class
- {
- return factory.WithWebHostBuilder(builder =>
- {
- builder
- .ConfigureLogging(logging =>
- {
- logging.AddXunit(outputHelper);
- })
- .ConfigureServices(services =>
- {
- var serviceProvider = new ServiceCollection()
- .AddEntityFrameworkInMemoryDatabase()
- .BuildServiceProvider();
-
- services.AddDbContext<DatabaseContext>(options =>
- {
- options.UseInMemoryDatabase("timeline");
- options.UseInternalServiceProvider(serviceProvider);
- });
-
- var sp = services.BuildServiceProvider();
-
- // Create a scope to obtain a reference to the database
- // context (ApplicationDbContext).
- using (var scope = sp.CreateScope())
- {
- var scopedServices = scope.ServiceProvider;
- var db = scopedServices.GetRequiredService<DatabaseContext>();
-
- // Ensure the database is created.
- db.Database.EnsureCreated();
-
- db.Users.AddRange(TestMockUsers.MockUsers);
- db.SaveChanges();
- }
- });
- });
- }
- }
-}