aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-04-21 00:08:59 +0800
committercrupest <crupest@outlook.com>2019-04-21 00:08:59 +0800
commit076e0131dff71a9f76fff13c92fffa0ef408935f (patch)
tree9fa853690866f452a571b45fb062ac692d298a2e /Timeline.Tests/Helpers
parent325d4c7dbfba45e9c5a7518279831f54c4690d20 (diff)
downloadtimeline-076e0131dff71a9f76fff13c92fffa0ef408935f.tar.gz
timeline-076e0131dff71a9f76fff13c92fffa0ef408935f.tar.bz2
timeline-076e0131dff71a9f76fff13c92fffa0ef408935f.zip
Reorgnize api. Add basic unit test.
Diffstat (limited to 'Timeline.Tests/Helpers')
-rw-r--r--Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs (renamed from Timeline.Tests/Helpers/Authentication/AuthenticationHttpClientExtensions.cs)15
-rw-r--r--Timeline.Tests/Helpers/TestUsers.cs40
-rw-r--r--Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs21
3 files changed, 53 insertions, 23 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationHttpClientExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
index c0051c53..40191009 100644
--- a/Timeline.Tests/Helpers/Authentication/AuthenticationHttpClientExtensions.cs
+++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
@@ -1,4 +1,5 @@
-using Newtonsoft.Json;
+using Microsoft.AspNetCore.Mvc.Testing;
+using Newtonsoft.Json;
using System;
using System.Net;
using System.Net.Http;
@@ -8,9 +9,9 @@ using Xunit;
namespace Timeline.Tests.Helpers.Authentication
{
- public static class AuthenticationHttpClientExtensions
+ public static class AuthenticationExtensions
{
- private const string CreateTokenUrl = "/User/CreateToken";
+ private const string CreateTokenUrl = "/token/create";
public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password, bool assertSuccess = true)
{
@@ -24,6 +25,14 @@ namespace Timeline.Tests.Helpers.Authentication
return result;
}
+ public static async Task<HttpClient> CreateClientWithUser<T>(this WebApplicationFactory<T> factory, string username, string password) where T : class
+ {
+ var client = factory.CreateDefaultClient();
+ var token = (await client.CreateUserTokenAsync(username, password)).Token;
+ 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
diff --git a/Timeline.Tests/Helpers/TestUsers.cs b/Timeline.Tests/Helpers/TestUsers.cs
new file mode 100644
index 00000000..b7005d54
--- /dev/null
+++ b/Timeline.Tests/Helpers/TestUsers.cs
@@ -0,0 +1,40 @@
+using System.Collections.Generic;
+using System.Linq;
+using Timeline.Entities;
+using Timeline.Models;
+using Timeline.Services;
+
+namespace Timeline.Tests.Helpers
+{
+ public static class TestMockUsers
+ {
+ static TestMockUsers()
+ {
+ var mockUsers = new List<User>();
+ var passwordService = new PasswordService(null);
+
+ mockUsers.Add(new User
+ {
+ Name = "user",
+ EncryptedPassword = passwordService.HashPassword("user"),
+ RoleString = "user"
+ });
+ mockUsers.Add(new User
+ {
+ Name = "admin",
+ EncryptedPassword = passwordService.HashPassword("admin"),
+ RoleString = "user,admin"
+ });
+
+ MockUsers = mockUsers;
+
+ var mockUserInfos = mockUsers.Select(u => new UserInfo(u)).ToList();
+ mockUserInfos.Sort(UserInfo.Comparer);
+ MockUserInfos = mockUserInfos;
+ }
+
+ public static List<User> MockUsers { get; }
+
+ public static IReadOnlyList<UserInfo> MockUserInfos { get; }
+ }
+}
diff --git a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
index 4a7f87fb..a34217f4 100644
--- a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
+++ b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
@@ -4,7 +4,6 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Timeline.Models;
-using Timeline.Services;
using Xunit.Abstractions;
namespace Timeline.Tests.Helpers
@@ -42,28 +41,10 @@ namespace Timeline.Tests.Helpers
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<DatabaseContext>();
- var passwordService = new PasswordService(null);
-
// Ensure the database is created.
db.Database.EnsureCreated();
- db.Users.AddRange(new User[] {
- new User
- {
- Id = 0,
- Name = "user",
- EncryptedPassword = passwordService.HashPassword("user"),
- RoleString = "user"
- },
- new User
- {
- Id = 0,
- Name = "admin",
- EncryptedPassword = passwordService.HashPassword("admin"),
- RoleString = "user,admin"
- }
- });
-
+ db.Users.AddRange(TestMockUsers.MockUsers);
db.SaveChanges();
}
});