diff options
Diffstat (limited to 'Timeline.Tests')
-rw-r--r-- | Timeline.Tests/AuthorizationUnitTest.cs | 136 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs | 56 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/MyWebApplicationFactory.cs | 46 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/ResponseExtensions.cs | 28 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/TestClock.cs | 2 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/TestUsers.cs | 84 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/UserInfoComparers.cs | 94 | ||||
-rw-r--r-- | Timeline.Tests/Timeline.Tests.csproj | 2 | ||||
-rw-r--r-- | Timeline.Tests/TokenUnitTest.cs | 2 | ||||
-rw-r--r-- | Timeline.Tests/UserUnitTest.cs | 72 |
10 files changed, 261 insertions, 261 deletions
diff --git a/Timeline.Tests/AuthorizationUnitTest.cs b/Timeline.Tests/AuthorizationUnitTest.cs index a25a8f9b..8df23c45 100644 --- a/Timeline.Tests/AuthorizationUnitTest.cs +++ b/Timeline.Tests/AuthorizationUnitTest.cs @@ -1,68 +1,68 @@ -using Microsoft.AspNetCore.Mvc.Testing; -using System.Net; -using System.Threading.Tasks; -using Timeline.Tests.Helpers; -using Timeline.Tests.Helpers.Authentication; -using Xunit; -using Xunit.Abstractions; - -namespace Timeline.Tests -{ - public class AuthorizationUnitTest : IClassFixture<MyWebApplicationFactory<Startup>> - { - private const string AuthorizeUrl = "Test/User/Authorize"; - private const string UserUrl = "Test/User/User"; - private const string AdminUrl = "Test/User/Admin"; - - private readonly WebApplicationFactory<Startup> _factory; - - public AuthorizationUnitTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper) - { - _factory = factory.WithTestLogging(outputHelper); - } - - [Fact] - public async Task UnauthenticationTest() - { - using (var client = _factory.CreateDefaultClient()) - { - var response = await client.GetAsync(AuthorizeUrl); - Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); - } - } - - [Fact] - public async Task AuthenticationTest() - { - using (var client = await _factory.CreateClientWithUser("user", "user")) - { - var response = await client.GetAsync(AuthorizeUrl); - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - } - } - - [Fact] - public async Task UserAuthorizationTest() - { - using (var client = await _factory.CreateClientWithUser("user", "user")) - { - var response1 = await client.GetAsync(UserUrl); - Assert.Equal(HttpStatusCode.OK, response1.StatusCode); - var response2 = await client.GetAsync(AdminUrl); - Assert.Equal(HttpStatusCode.Forbidden, response2.StatusCode); - } - } - - [Fact] - public async Task AdminAuthorizationTest() - { - using (var client = await _factory.CreateClientWithUser("admin", "admin")) - { - var response1 = await client.GetAsync(UserUrl); - Assert.Equal(HttpStatusCode.OK, response1.StatusCode); - var response2 = await client.GetAsync(AdminUrl); - Assert.Equal(HttpStatusCode.OK, response2.StatusCode); - } - } - } -} +using Microsoft.AspNetCore.Mvc.Testing;
+using System.Net;
+using System.Threading.Tasks;
+using Timeline.Tests.Helpers;
+using Timeline.Tests.Helpers.Authentication;
+using Xunit;
+using Xunit.Abstractions;
+
+namespace Timeline.Tests
+{
+ public class AuthorizationUnitTest : IClassFixture<MyWebApplicationFactory<Startup>>
+ {
+ private const string AuthorizeUrl = "Test/User/Authorize";
+ private const string UserUrl = "Test/User/User";
+ private const string AdminUrl = "Test/User/Admin";
+
+ private readonly WebApplicationFactory<Startup> _factory;
+
+ public AuthorizationUnitTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
+ {
+ _factory = factory.WithTestLogging(outputHelper);
+ }
+
+ [Fact]
+ public async Task UnauthenticationTest()
+ {
+ using (var client = _factory.CreateDefaultClient())
+ {
+ var response = await client.GetAsync(AuthorizeUrl);
+ Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
+ }
+ }
+
+ [Fact]
+ public async Task AuthenticationTest()
+ {
+ using (var client = await _factory.CreateClientWithUser("user", "user"))
+ {
+ var response = await client.GetAsync(AuthorizeUrl);
+ Assert.Equal(HttpStatusCode.OK, response.StatusCode);
+ }
+ }
+
+ [Fact]
+ public async Task UserAuthorizationTest()
+ {
+ using (var client = await _factory.CreateClientWithUser("user", "user"))
+ {
+ var response1 = await client.GetAsync(UserUrl);
+ Assert.Equal(HttpStatusCode.OK, response1.StatusCode);
+ var response2 = await client.GetAsync(AdminUrl);
+ Assert.Equal(HttpStatusCode.Forbidden, response2.StatusCode);
+ }
+ }
+
+ [Fact]
+ public async Task AdminAuthorizationTest()
+ {
+ using (var client = await _factory.CreateClientWithUser("admin", "admin"))
+ {
+ var response1 = await client.GetAsync(UserUrl);
+ Assert.Equal(HttpStatusCode.OK, response1.StatusCode);
+ var response2 = await client.GetAsync(AdminUrl);
+ Assert.Equal(HttpStatusCode.OK, response2.StatusCode);
+ }
+ }
+ }
+}
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs index 27362ac3..c8bec266 100644 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs @@ -1,28 +1,28 @@ -using Microsoft.AspNetCore.Mvc.Testing; -using Newtonsoft.Json; -using System.Net.Http; -using System.Threading.Tasks; -using Timeline.Entities.Http; - -namespace Timeline.Tests.Helpers.Authentication -{ - public static class AuthenticationExtensions - { - private const string CreateTokenUrl = "/token/create"; - - 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, ExpireOffset = expireOffset }); - var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync()); - 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; - } - } -} +using Microsoft.AspNetCore.Mvc.Testing;
+using Newtonsoft.Json;
+using System.Net.Http;
+using System.Threading.Tasks;
+using Timeline.Entities.Http;
+
+namespace Timeline.Tests.Helpers.Authentication
+{
+ public static class AuthenticationExtensions
+ {
+ private const string CreateTokenUrl = "/token/create";
+
+ 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, ExpireOffset = expireOffset });
+ var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
+ 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;
+ }
+ }
+}
diff --git a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs index 903cd670..d9503526 100644 --- a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs +++ b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs @@ -1,16 +1,16 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Testing; +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 Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
+using Timeline.Models;
using Timeline.Services;
-using Xunit.Abstractions; - -namespace Timeline.Tests.Helpers -{ +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.
@@ -65,19 +65,19 @@ namespace Timeline.Tests.Helpers 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 => - { + }
+
+ 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 index 86ac1c88..155836fb 100644 --- a/Timeline.Tests/Helpers/ResponseExtensions.cs +++ b/Timeline.Tests/Helpers/ResponseExtensions.cs @@ -1,14 +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()); - } - } -} +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 index 91523f2b..ea90305f 100644 --- a/Timeline.Tests/Helpers/TestClock.cs +++ b/Timeline.Tests/Helpers/TestClock.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using System;
using Timeline.Services;
diff --git a/Timeline.Tests/Helpers/TestUsers.cs b/Timeline.Tests/Helpers/TestUsers.cs index 41dd83a9..71de8237 100644 --- a/Timeline.Tests/Helpers/TestUsers.cs +++ b/Timeline.Tests/Helpers/TestUsers.cs @@ -1,42 +1,42 @@ -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(); - - mockUsers.Add(new User - { - Name = "user", - EncryptedPassword = passwordService.HashPassword("user"), - RoleString = UserUtility.IsAdminToRoleString(false), - Version = 0, - }); - mockUsers.Add(new User - { - Name = "admin", - EncryptedPassword = passwordService.HashPassword("admin"), - RoleString = UserUtility.IsAdminToRoleString(true), - Version = 0, - }); - - MockUsers = mockUsers; - - var mockUserInfos = mockUsers.Select(u => UserUtility.CreateUserInfo(u)).ToList(); - mockUserInfos.Sort(UserInfoComparers.Comparer); - MockUserInfos = mockUserInfos; - } - - public static List<User> MockUsers { get; } - - public static IReadOnlyList<UserInfo> MockUserInfos { get; } - } -} +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();
+
+ mockUsers.Add(new User
+ {
+ Name = "user",
+ EncryptedPassword = passwordService.HashPassword("user"),
+ RoleString = UserUtility.IsAdminToRoleString(false),
+ Version = 0,
+ });
+ mockUsers.Add(new User
+ {
+ Name = "admin",
+ EncryptedPassword = passwordService.HashPassword("admin"),
+ RoleString = UserUtility.IsAdminToRoleString(true),
+ Version = 0,
+ });
+
+ MockUsers = mockUsers;
+
+ var mockUserInfos = mockUsers.Select(u => UserUtility.CreateUserInfo(u)).ToList();
+ mockUserInfos.Sort(UserInfoComparers.Comparer);
+ MockUserInfos = mockUserInfos;
+ }
+
+ public static List<User> MockUsers { get; }
+
+ public static IReadOnlyList<UserInfo> MockUserInfos { get; }
+ }
+}
diff --git a/Timeline.Tests/Helpers/UserInfoComparers.cs b/Timeline.Tests/Helpers/UserInfoComparers.cs index fcf37e5c..c7c584b7 100644 --- a/Timeline.Tests/Helpers/UserInfoComparers.cs +++ b/Timeline.Tests/Helpers/UserInfoComparers.cs @@ -1,47 +1,47 @@ -using System.Collections.Generic; -using Timeline.Entities; - -namespace Timeline.Tests.Helpers -{ - public static class UserInfoComparers - { - public static IEqualityComparer<UserInfo> EqualityComparer { get; } = new EqualityComparerImpl(); - public static IComparer<UserInfo> Comparer { get; } = Comparer<UserInfo>.Create(Compare); - - - private class EqualityComparerImpl : IEqualityComparer<UserInfo> - { - bool IEqualityComparer<UserInfo>.Equals(UserInfo x, UserInfo y) - { - return Compare(x, y) == 0; - } - - int IEqualityComparer<UserInfo>.GetHashCode(UserInfo obj) - { - return obj.Username.GetHashCode() ^ obj.Administrator.GetHashCode(); - } - } - - public static int Compare(UserInfo left, UserInfo right) - { - if (left == null) - { - if (right == null) - return 0; - return -1; - } - - if (right == null) - return 1; - - var uc = string.Compare(left.Username, right.Username); - if (uc != 0) - return uc; - - if (left.Administrator == right.Administrator) - return 0; - - return left.Administrator ? -1 : 1; - } - } -} +using System.Collections.Generic;
+using Timeline.Entities;
+
+namespace Timeline.Tests.Helpers
+{
+ public static class UserInfoComparers
+ {
+ public static IEqualityComparer<UserInfo> EqualityComparer { get; } = new EqualityComparerImpl();
+ public static IComparer<UserInfo> Comparer { get; } = Comparer<UserInfo>.Create(Compare);
+
+
+ private class EqualityComparerImpl : IEqualityComparer<UserInfo>
+ {
+ bool IEqualityComparer<UserInfo>.Equals(UserInfo x, UserInfo y)
+ {
+ return Compare(x, y) == 0;
+ }
+
+ int IEqualityComparer<UserInfo>.GetHashCode(UserInfo obj)
+ {
+ return obj.Username.GetHashCode() ^ obj.Administrator.GetHashCode();
+ }
+ }
+
+ public static int Compare(UserInfo left, UserInfo right)
+ {
+ if (left == null)
+ {
+ if (right == null)
+ return 0;
+ return -1;
+ }
+
+ if (right == null)
+ return 1;
+
+ var uc = string.Compare(left.Username, right.Username);
+ if (uc != 0)
+ return uc;
+
+ if (left.Administrator == right.Administrator)
+ return 0;
+
+ return left.Administrator ? -1 : 1;
+ }
+ }
+}
diff --git a/Timeline.Tests/Timeline.Tests.csproj b/Timeline.Tests/Timeline.Tests.csproj index 854c63ac..8cc304f4 100644 --- a/Timeline.Tests/Timeline.Tests.csproj +++ b/Timeline.Tests/Timeline.Tests.csproj @@ -1,4 +1,4 @@ -<Project Sdk="Microsoft.NET.Sdk.Web">
+<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
diff --git a/Timeline.Tests/TokenUnitTest.cs b/Timeline.Tests/TokenUnitTest.cs index 7b83cd13..f942767d 100644 --- a/Timeline.Tests/TokenUnitTest.cs +++ b/Timeline.Tests/TokenUnitTest.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
diff --git a/Timeline.Tests/UserUnitTest.cs b/Timeline.Tests/UserUnitTest.cs index b3377f7b..de429c7f 100644 --- a/Timeline.Tests/UserUnitTest.cs +++ b/Timeline.Tests/UserUnitTest.cs @@ -1,36 +1,36 @@ -using Microsoft.AspNetCore.Mvc.Testing; -using Newtonsoft.Json; -using System.Linq; -using System.Net; -using System.Threading.Tasks; -using Timeline.Entities; -using Timeline.Tests.Helpers; -using Timeline.Tests.Helpers.Authentication; -using Xunit; -using Xunit.Abstractions; - -namespace Timeline.Tests -{ - public class UserUnitTest : IClassFixture<MyWebApplicationFactory<Startup>> - { - private readonly WebApplicationFactory<Startup> _factory; - - public UserUnitTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper) - { - _factory = factory.WithTestLogging(outputHelper); - } - - [Fact] - public async Task UserTest() - { - using (var client = await _factory.CreateClientWithUser("admin", "admin")) - { - var res1 = await client.GetAsync("users"); - Assert.Equal(HttpStatusCode.OK, res1.StatusCode); - var users = JsonConvert.DeserializeObject<UserInfo[]>(await res1.Content.ReadAsStringAsync()).ToList(); - users.Sort(UserInfoComparers.Comparer); - Assert.Equal(TestMockUsers.MockUserInfos, users, UserInfoComparers.EqualityComparer); - } - } - } -} +using Microsoft.AspNetCore.Mvc.Testing;
+using Newtonsoft.Json;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+using Timeline.Entities;
+using Timeline.Tests.Helpers;
+using Timeline.Tests.Helpers.Authentication;
+using Xunit;
+using Xunit.Abstractions;
+
+namespace Timeline.Tests
+{
+ public class UserUnitTest : IClassFixture<MyWebApplicationFactory<Startup>>
+ {
+ private readonly WebApplicationFactory<Startup> _factory;
+
+ public UserUnitTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
+ {
+ _factory = factory.WithTestLogging(outputHelper);
+ }
+
+ [Fact]
+ public async Task UserTest()
+ {
+ using (var client = await _factory.CreateClientWithUser("admin", "admin"))
+ {
+ var res1 = await client.GetAsync("users");
+ Assert.Equal(HttpStatusCode.OK, res1.StatusCode);
+ var users = JsonConvert.DeserializeObject<UserInfo[]>(await res1.Content.ReadAsStringAsync()).ToList();
+ users.Sort(UserInfoComparers.Comparer);
+ Assert.Equal(TestMockUsers.MockUserInfos, users, UserInfoComparers.EqualityComparer);
+ }
+ }
+ }
+}
|