From 29bd71cd93b03248254f341aff9252374abc74ec Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Aug 2019 17:38:56 +0800 Subject: Add script to convert encoding and eof. And of course run it. --- .../Authentication/AuthenticationExtensions.cs | 56 ++++++------- Timeline.Tests/Helpers/MyWebApplicationFactory.cs | 46 +++++------ Timeline.Tests/Helpers/ResponseExtensions.cs | 28 +++---- Timeline.Tests/Helpers/TestClock.cs | 2 +- Timeline.Tests/Helpers/TestUsers.cs | 84 +++++++++---------- Timeline.Tests/Helpers/UserInfoComparers.cs | 94 +++++++++++----------- 6 files changed, 155 insertions(+), 155 deletions(-) (limited to 'Timeline.Tests/Helpers') 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 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(await response.Content.ReadAsStringAsync()); - return result; - } - - public static async Task CreateClientWithUser(this WebApplicationFactory 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 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(await response.Content.ReadAsStringAsync()); + return result; + } + + public static async Task CreateClientWithUser(this WebApplicationFactory 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 : WebApplicationFactory 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 WithTestLogging(this WebApplicationFactory factory, ITestOutputHelper outputHelper) where TEntry : class - { - return factory.WithWebHostBuilder(builder => - { + } + + public static class WebApplicationFactoryExtensions + { + public static WebApplicationFactory WithTestLogging(this WebApplicationFactory 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 ReadBodyAsJson(this HttpResponseMessage response) - { - return JsonConvert.DeserializeObject(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 ReadBodyAsJson(this HttpResponseMessage response) + { + return JsonConvert.DeserializeObject(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(); - 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 MockUsers { get; } - - public static IReadOnlyList 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(); + 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 MockUsers { get; } + + public static IReadOnlyList 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 EqualityComparer { get; } = new EqualityComparerImpl(); - public static IComparer Comparer { get; } = Comparer.Create(Compare); - - - private class EqualityComparerImpl : IEqualityComparer - { - bool IEqualityComparer.Equals(UserInfo x, UserInfo y) - { - return Compare(x, y) == 0; - } - - int IEqualityComparer.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 EqualityComparer { get; } = new EqualityComparerImpl(); + public static IComparer Comparer { get; } = Comparer.Create(Compare); + + + private class EqualityComparerImpl : IEqualityComparer + { + bool IEqualityComparer.Equals(UserInfo x, UserInfo y) + { + return Compare(x, y) == 0; + } + + int IEqualityComparer.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; + } + } +} -- cgit v1.2.3