diff options
author | crupest <crupest@outlook.com> | 2019-11-19 23:18:45 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-11-19 23:18:45 +0800 |
commit | f3c7912caec2e9eee8a685d8751894f357528a71 (patch) | |
tree | 66e1bcfe602877e4f72ea1b68cbf33e06557c222 /Timeline.Tests/Helpers | |
parent | 2238568fdfc891d69e8174060e54845c7a3d5e95 (diff) | |
download | timeline-f3c7912caec2e9eee8a685d8751894f357528a71.tar.gz timeline-f3c7912caec2e9eee8a685d8751894f357528a71.tar.bz2 timeline-f3c7912caec2e9eee8a685d8751894f357528a71.zip |
Complete integrated tests??? Fix bugs.
Diffstat (limited to 'Timeline.Tests/Helpers')
-rw-r--r-- | Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs | 28 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/TestApplication.cs | 22 |
2 files changed, 25 insertions, 25 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs index 6a78be7a..4048bb73 100644 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs @@ -1,5 +1,4 @@ using Microsoft.AspNetCore.Mvc.Testing;
-using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;
@@ -22,9 +21,8 @@ namespace Timeline.Tests.Helpers.Authentication public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password, int? expireOffset = null)
{
var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, Expire = expireOffset });
- response.Should().HaveStatusCode(200);
- var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
- return result;
+ return response.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<CreateTokenResponse>().Which;
}
public static async Task<HttpClient> CreateClientWithCredential<T>(this WebApplicationFactory<T> factory, string username, string password) where T : class
@@ -35,14 +33,19 @@ namespace Timeline.Tests.Helpers.Authentication return client;
}
+ public static Task<HttpClient> CreateClientAs<T>(this WebApplicationFactory<T> factory, MockUser user) where T : class
+ {
+ return CreateClientWithCredential(factory, user.Username, user.Password);
+ }
+
public static Task<HttpClient> CreateClientAsUser<T>(this WebApplicationFactory<T> factory) where T : class
{
- return factory.CreateClientWithCredential(MockUser.User.Username, MockUser.User.Password);
+ return factory.CreateClientAs(MockUser.User);
}
public static Task<HttpClient> CreateClientAsAdmin<T>(this WebApplicationFactory<T> factory) where T : class
{
- return factory.CreateClientWithCredential(MockUser.Admin.Username, MockUser.Admin.Password);
+ return factory.CreateClientAs(MockUser.Admin);
}
public static Task<HttpClient> CreateClientAs<T>(this WebApplicationFactory<T> factory, AuthType authType) where T : class
@@ -55,5 +58,18 @@ namespace Timeline.Tests.Helpers.Authentication _ => throw new InvalidOperationException("Unknown auth type.")
};
}
+
+ 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;
}
}
diff --git a/Timeline.Tests/Helpers/TestApplication.cs b/Timeline.Tests/Helpers/TestApplication.cs index b0187a30..5862f452 100644 --- a/Timeline.Tests/Helpers/TestApplication.cs +++ b/Timeline.Tests/Helpers/TestApplication.cs @@ -10,26 +10,11 @@ namespace Timeline.Tests.Helpers {
public class TestApplication : IDisposable
{
- public SqliteConnection DatabaseConnection { get; } = new SqliteConnection("Data Source=:memory:;");
+ public TestDatabase Database { get; } = new TestDatabase();
public WebApplicationFactory<Startup> Factory { get; }
public TestApplication(WebApplicationFactory<Startup> factory)
{
- // 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 .
- DatabaseConnection.Open();
-
- {
- var options = new DbContextOptionsBuilder<DatabaseContext>()
- .UseSqlite(DatabaseConnection)
- .Options;
-
- using (var context = new DatabaseContext(options))
- {
- TestDatabase.InitDatabase(context);
- };
- }
-
Factory = factory.WithWebHostBuilder(builder =>
{
builder.ConfigureServices(services =>
@@ -37,7 +22,7 @@ namespace Timeline.Tests.Helpers services.AddEntityFrameworkSqlite();
services.AddDbContext<DatabaseContext>(options =>
{
- options.UseSqlite(DatabaseConnection);
+ options.UseSqlite(Database.Connection);
});
});
});
@@ -45,8 +30,7 @@ namespace Timeline.Tests.Helpers public void Dispose()
{
- DatabaseConnection.Close();
- DatabaseConnection.Dispose();
+ Database.Dispose();
}
}
}
|