diff --git a/src/Unosquare.Swan.AspNetCore.Sample/Controllers/ProductController.cs b/src/Unosquare.Swan.AspNetCore.Sample/Controllers/ProductController.cs index c09e52d..35c75f0 100644 --- a/src/Unosquare.Swan.AspNetCore.Sample/Controllers/ProductController.cs +++ b/src/Unosquare.Swan.AspNetCore.Sample/Controllers/ProductController.cs @@ -1,10 +1,8 @@ -using System; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; using Unosquare.Swan.AspNetCore.Sample.Database; -using Microsoft.AspNetCore.Authorization; namespace Unosquare.Swan.AspNetCore.Sample.Controllers { diff --git a/src/Unosquare.Swan.AspNetCore.Sample/Controllers/ValuesController.cs b/src/Unosquare.Swan.AspNetCore.Sample/Controllers/ValuesController.cs index bca4310..54f51ab 100644 --- a/src/Unosquare.Swan.AspNetCore.Sample/Controllers/ValuesController.cs +++ b/src/Unosquare.Swan.AspNetCore.Sample/Controllers/ValuesController.cs @@ -1,8 +1,5 @@ -using System; +using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; namespace Unosquare.Swan.AspNetCore.Sample.Controllers { diff --git a/src/Unosquare.Swan.AspNetCore.Sample/Database/AuditTrailEntry.cs b/src/Unosquare.Swan.AspNetCore.Sample/Database/AuditTrailEntry.cs index 327a591..9f13c73 100644 --- a/src/Unosquare.Swan.AspNetCore.Sample/Database/AuditTrailEntry.cs +++ b/src/Unosquare.Swan.AspNetCore.Sample/Database/AuditTrailEntry.cs @@ -1,12 +1,9 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Threading.Tasks; -using Unosquare.Swan.AspNetCore.Models; - -namespace Unosquare.Swan.AspNetCore.Sample.Database +namespace Unosquare.Swan.AspNetCore.Sample.Database { + using System; + using System.ComponentModel.DataAnnotations; + using Models; + public class AuditTrailEntry :IAuditTrailEntry { [Key] diff --git a/src/Unosquare.Swan.AspNetCore.Sample/Database/Product.cs b/src/Unosquare.Swan.AspNetCore.Sample/Database/Product.cs index d19fe84..ca99e4e 100644 --- a/src/Unosquare.Swan.AspNetCore.Sample/Database/Product.cs +++ b/src/Unosquare.Swan.AspNetCore.Sample/Database/Product.cs @@ -1,11 +1,7 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Threading.Tasks; - -namespace Unosquare.Swan.AspNetCore.Sample.Database +namespace Unosquare.Swan.AspNetCore.Sample.Database { + using System.ComponentModel.DataAnnotations; + public class Product { [Key] diff --git a/src/Unosquare.Swan.AspNetCore.Sample/Startup.cs b/src/Unosquare.Swan.AspNetCore.Sample/Startup.cs index 772d8e0..571f39b 100644 --- a/src/Unosquare.Swan.AspNetCore.Sample/Startup.cs +++ b/src/Unosquare.Swan.AspNetCore.Sample/Startup.cs @@ -1,6 +1,5 @@ namespace Unosquare.Swan.AspNetCore.Sample { - using AspNetCore; using Database; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -46,7 +45,7 @@ public Startup(IHostingEnvironment env) } public IConfigurationRoot Configuration { get; } - private TokenValidationParameters ValidationParameters { get; set; } + private TokenValidationParameters ValidationParameters { get; } // This method gets called by the runtime. Use this method to add services to the container public void ConfigureServices(IServiceCollection services) diff --git a/src/Unosquare.Swan.AspNetCore.Sample/Unosquare.Swan.AspNetCore.Sample.csproj b/src/Unosquare.Swan.AspNetCore.Sample/Unosquare.Swan.AspNetCore.Sample.csproj index 630515a..aa16cd0 100644 --- a/src/Unosquare.Swan.AspNetCore.Sample/Unosquare.Swan.AspNetCore.Sample.csproj +++ b/src/Unosquare.Swan.AspNetCore.Sample/Unosquare.Swan.AspNetCore.Sample.csproj @@ -17,14 +17,14 @@ - - - - - - - - + + + + + + + + @@ -32,7 +32,7 @@ - + diff --git a/src/Unosquare.Swan.AspNetCore/AuditTrailController.cs b/src/Unosquare.Swan.AspNetCore/AuditTrailController.cs index e65822c..99db028 100644 --- a/src/Unosquare.Swan.AspNetCore/AuditTrailController.cs +++ b/src/Unosquare.Swan.AspNetCore/AuditTrailController.cs @@ -4,8 +4,8 @@ using System; using System.Collections.Generic; using System.Linq; - using Unosquare.Swan.AspNetCore.Models; - using Unosquare.Swan.Formatters; + using Models; + using Formatters; /// /// Represents an AuditTrail controller to use with BusinessDbContext diff --git a/src/Unosquare.Swan.AspNetCore/BusinessRuleAttribute.cs b/src/Unosquare.Swan.AspNetCore/BusinessRuleAttribute.cs index a4b081f..e7ce8df 100644 --- a/src/Unosquare.Swan.AspNetCore/BusinessRuleAttribute.cs +++ b/src/Unosquare.Swan.AspNetCore/BusinessRuleAttribute.cs @@ -43,7 +43,7 @@ public class BusinessRuleAttribute : Attribute /// The action flags. public BusinessRuleAttribute(ActionFlags actionFlags) { - this.Action = actionFlags; + Action = actionFlags; } /// @@ -53,8 +53,8 @@ public BusinessRuleAttribute(ActionFlags actionFlags) /// The action flags. public BusinessRuleAttribute(Type[] entityTypes, ActionFlags actionFlags) { - this.EntityTypes = entityTypes; - this.Action = actionFlags; + EntityTypes = entityTypes; + Action = actionFlags; } /// diff --git a/src/Unosquare.Swan.AspNetCore/BusinessRulesController.cs b/src/Unosquare.Swan.AspNetCore/BusinessRulesController.cs index bf49ec3..161ba7c 100644 --- a/src/Unosquare.Swan.AspNetCore/BusinessRulesController.cs +++ b/src/Unosquare.Swan.AspNetCore/BusinessRulesController.cs @@ -92,7 +92,9 @@ public void RunBusinessRules() private void ExecuteBusinessRulesMethods(EntityState state, ActionFlags action, MethodInfo[] methodInfoSet) { - var selfTrackingEntries = Context.ChangeTracker.Entries().Where(x => x.State == state).ToList(); + var selfTrackingEntries = Context.ChangeTracker.Entries() + .Where(x => x.State == state) + .ToList(); foreach (var entry in selfTrackingEntries) { @@ -103,12 +105,12 @@ private void ExecuteBusinessRulesMethods(EntityState state, ActionFlags action, var methods = methodInfoSet.Where(m => m.GetCustomAttributes(typeof(BusinessRuleAttribute), true) .Select(a => a as BusinessRuleAttribute) .Any(b => b != null && (b.EntityTypes == null || - b.EntityTypes.Any(t => t == entityType)) && - b.Action == action)); + b.EntityTypes.Any(t => t == entityType)) && + b.Action == action)); foreach (var methodInfo in methods) { - methodInfo.Invoke(this, new[] { entity }); + methodInfo.Invoke(this, new[] {entity}); } } } diff --git a/src/Unosquare.Swan.AspNetCore/TokenProviderMiddleware.cs b/src/Unosquare.Swan.AspNetCore/TokenProviderMiddleware.cs index 1119270..8f38b87 100644 --- a/src/Unosquare.Swan.AspNetCore/TokenProviderMiddleware.cs +++ b/src/Unosquare.Swan.AspNetCore/TokenProviderMiddleware.cs @@ -85,6 +85,63 @@ public Task Invoke(HttpContext context) return _next(context); } + private static void ThrowIfInvalidOptions(TokenProviderOptions options) + { + if (string.IsNullOrEmpty(options.Path)) + { + throw new ArgumentNullException(nameof(TokenProviderOptions.Path)); + } + + if (string.IsNullOrEmpty(options.Issuer)) + { + throw new ArgumentNullException(nameof(TokenProviderOptions.Issuer)); + } + + if (string.IsNullOrEmpty(options.Audience)) + { + throw new ArgumentNullException(nameof(TokenProviderOptions.Audience)); + } + + if (options.Expiration == TimeSpan.Zero) + { + throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(TokenProviderOptions.Expiration)); + } + + if (options.IdentityResolver == null) + { + throw new ArgumentNullException(nameof(TokenProviderOptions.IdentityResolver)); + } + + if (options.SigningCredentials == null) + { + throw new ArgumentNullException(nameof(TokenProviderOptions.SigningCredentials)); + } + + if (options.NonceGenerator == null) + { + throw new ArgumentNullException(nameof(TokenProviderOptions.NonceGenerator)); + } + + if (options.IdentityResolver == null) + { + throw new ArgumentNullException(nameof(TokenProviderOptions.IdentityResolver)); + } + + if (options.BearerTokenResolver == null) + { + throw new ArgumentNullException(nameof(TokenProviderOptions.BearerTokenResolver)); + } + } + + private static string SerializeError(string description, string error = "invalid_grant") + { + return Json.Serialize(new + { + error, + error_description = description + }); + } + private async Task GenerateToken(HttpContext context) { JwtSecurityToken jwt; @@ -178,68 +235,5 @@ private async Task GenerateToken(HttpContext context) await context.Response.WriteAsync(Json.Serialize(await _options.BearerTokenResolver(identity, responseInfo))); } - - private static void ThrowIfInvalidOptions(TokenProviderOptions options) - { - if (string.IsNullOrEmpty(options.Path)) - { - throw new ArgumentNullException(nameof(TokenProviderOptions.Path)); - } - - if (string.IsNullOrEmpty(options.Issuer)) - { - throw new ArgumentNullException(nameof(TokenProviderOptions.Issuer)); - } - - if (string.IsNullOrEmpty(options.Audience)) - { - throw new ArgumentNullException(nameof(TokenProviderOptions.Audience)); - } - - if (options.Expiration == TimeSpan.Zero) - { - throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(TokenProviderOptions.Expiration)); - } - - if (options.IdentityResolver == null) - { - throw new ArgumentNullException(nameof(TokenProviderOptions.IdentityResolver)); - } - - if (options.SigningCredentials == null) - { - throw new ArgumentNullException(nameof(TokenProviderOptions.SigningCredentials)); - } - - if (options.NonceGenerator == null) - { - throw new ArgumentNullException(nameof(TokenProviderOptions.NonceGenerator)); - } - - if (options.IdentityResolver == null) - { - throw new ArgumentNullException(nameof(TokenProviderOptions.IdentityResolver)); - } - - if (options.BearerTokenResolver == null) - { - throw new ArgumentNullException(nameof(TokenProviderOptions.BearerTokenResolver)); - } - } - - /// - /// Serializes the error. - /// - /// The description. - /// The error. - /// The error in JSON format - private static string SerializeError(string description, string error = "invalid_grant") - { - return Json.Serialize(new - { - error, - error_description = description - }); - } } } \ No newline at end of file diff --git a/src/Unosquare.Swan.AspNetCore/Unosquare.Swan.AspNetCore.csproj b/src/Unosquare.Swan.AspNetCore/Unosquare.Swan.AspNetCore.csproj index 8024328..0998976 100644 --- a/src/Unosquare.Swan.AspNetCore/Unosquare.Swan.AspNetCore.csproj +++ b/src/Unosquare.Swan.AspNetCore/Unosquare.Swan.AspNetCore.csproj @@ -4,7 +4,7 @@ Bearer Token and logging to fast start any ASP.NET Core project Copyright (c) 2016-2017 - Unosquare Unosquare SWAN AspNet Core - 0.9.2 + 0.9.3 netstandard2.0 Full true @@ -17,16 +17,16 @@ All - - - - - - + + + + + + - + diff --git a/test/Unosquare.Swan.AspNetCore.Test/AuditTrailTest.cs b/test/Unosquare.Swan.AspNetCore.Test/AuditTrailTest.cs index 122cef3..2e82ee5 100644 --- a/test/Unosquare.Swan.AspNetCore.Test/AuditTrailTest.cs +++ b/test/Unosquare.Swan.AspNetCore.Test/AuditTrailTest.cs @@ -9,7 +9,7 @@ [TestFixture] class AuditTrailTest { - private ProductMock product; + private ProductMock _product; private BusinessDbContextMock SetupDatabase(string name) { @@ -22,7 +22,7 @@ private BusinessDbContextMock SetupDatabase(string name) [SetUp] public void SetUp() { - product = ProductMock.GetProduct(); + _product = ProductMock.GetProduct(); } [Test] @@ -30,7 +30,7 @@ public async Task SaveChangesEntityTestAsync() { using (var context = SetupDatabase(nameof(SaveChangesEntityTestAsync))) { - context.Add(product); + context.Add(_product); await context.SaveChangesAsync(); var audit = context.AuditTrailEntries.Last(); @@ -46,7 +46,7 @@ public void SaveChangesEntityTest() using (var context = SetupDatabase(nameof(SaveChangesEntityTest))) { - context.Add(product); + context.Add(_product); context.SaveChanges(); var audit = context.AuditTrailEntries.Last(); @@ -61,10 +61,10 @@ public void UpdatedChangesEntityTest() { using (var context = SetupDatabase(nameof(UpdatedChangesEntityTest))) { - context.Add(product); + context.Add(_product); context.SaveChanges(); - context.Update(product); + context.Update(_product); context.SaveChanges(); var audit = context.AuditTrailEntries.Last(); @@ -79,10 +79,10 @@ public void DeleteChangesEntityTest() { using (var context = SetupDatabase(nameof(DeleteChangesEntityTest))) { - context.Add(product); + context.Add(_product); context.SaveChanges(); - context.Remove(product); + context.Remove(_product); context.SaveChanges(); var audit = context.AuditTrailEntries.Last(); diff --git a/test/Unosquare.Swan.AspNetCore.Test/BasicUserStoreTest.cs b/test/Unosquare.Swan.AspNetCore.Test/BasicUserStoreTest.cs index 372dca4..7bbd96f 100644 --- a/test/Unosquare.Swan.AspNetCore.Test/BasicUserStoreTest.cs +++ b/test/Unosquare.Swan.AspNetCore.Test/BasicUserStoreTest.cs @@ -12,19 +12,20 @@ [TestFixture] public class BasicUserStoreTest { - private readonly CancellationToken ct = new CancellationToken(); - private static readonly ApplicationUserMock _mock = new ApplicationUserMock(); - private readonly List _users = _mock.GetUsers(); + private static readonly ApplicationUserMock Mock = new ApplicationUserMock(); + + private readonly CancellationToken _ct = new CancellationToken(); + private readonly List _users = Mock.GetUsers(); private readonly BasicUserStore _userStore = new BasicUserStore(); [Test] public async Task CreateAsyncTest() { - var user = _mock.GetUser(); + var user = Mock.GetUser(); var userId = user.UserId; - var result = await _userStore.CreateAsync(user, ct); - var data = await _userStore.FindByIdAsync(user.UserId, ct); + var result = await _userStore.CreateAsync(user, _ct); + var data = await _userStore.FindByIdAsync(user.UserId, _ct); Assert.IsTrue(result.Succeeded); Assert.AreEqual(user, data); @@ -34,12 +35,12 @@ public async Task CreateAsyncTest() [Test] public async Task UpdateAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); user.UserName = "Changed"; - var result = await _userStore.UpdateAsync(user, ct); - var userName = await _userStore.GetUserNameAsync(user, ct); + var result = await _userStore.UpdateAsync(user, _ct); + var userName = await _userStore.GetUserNameAsync(user, _ct); Assert.IsTrue(result.Succeeded); Assert.AreEqual(user.UserName, userName); @@ -48,11 +49,11 @@ public async Task UpdateAsyncTest() [Test] public async Task DeleteAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - var result = await _userStore.DeleteAsync(user, ct); - var data = await _userStore.FindByIdAsync(user.UserId, ct); + var result = await _userStore.DeleteAsync(user, _ct); + var data = await _userStore.FindByIdAsync(user.UserId, _ct); Assert.IsTrue(result.Succeeded); Assert.IsNull(data); @@ -61,12 +62,12 @@ public async Task DeleteAsyncTest() [Test] public async Task FindByIdAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); foreach (var u in _users) - await _userStore.CreateAsync(u, ct); + await _userStore.CreateAsync(u, _ct); - var userData = await _userStore.FindByIdAsync(user.UserId, ct); + var userData = await _userStore.FindByIdAsync(user.UserId, _ct); Assert.AreEqual(user, userData); } @@ -74,12 +75,12 @@ public async Task FindByIdAsyncTest() [Test] public async Task FindByNameAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); foreach (var u in _users) - await _userStore.CreateAsync(u, ct); + await _userStore.CreateAsync(u, _ct); - var userData = await _userStore.FindByNameAsync(user.UserName, ct); + var userData = await _userStore.FindByNameAsync(user.UserName, _ct); Assert.AreEqual(user.Name, userData.Name); } @@ -87,10 +88,10 @@ public async Task FindByNameAsyncTest() [Test] public async Task GetUserIdAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - var userId = await _userStore.GetUserIdAsync(user, ct); + var userId = await _userStore.GetUserIdAsync(user, _ct); Assert.AreEqual(user.UserId, userId); } @@ -98,10 +99,10 @@ public async Task GetUserIdAsyncTest() [Test] public async Task GetUserNameAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - var userName = await _userStore.GetUserNameAsync(user, ct); + var userName = await _userStore.GetUserNameAsync(user, _ct); Assert.AreEqual(user.UserName, userName); } @@ -109,10 +110,10 @@ public async Task GetUserNameAsyncTest() [Test] public async Task GetNormalizedUserNameAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - var userName = await _userStore.GetNormalizedUserNameAsync(user, ct); + var userName = await _userStore.GetNormalizedUserNameAsync(user, _ct); Assert.AreEqual(user.UserName, userName); } @@ -120,10 +121,10 @@ public async Task GetNormalizedUserNameAsyncTest() [Test] public async Task GetPasswordHashAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - var passwordHash = await _userStore.GetPasswordHashAsync(user, ct); + var passwordHash = await _userStore.GetPasswordHashAsync(user, _ct); Assert.AreEqual(user.PasswordHash, passwordHash); } @@ -131,10 +132,10 @@ public async Task GetPasswordHashAsyncTest() [Test] public async Task HasPasswordAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - var hasPassword = await _userStore.HasPasswordAsync(user, ct); + var hasPassword = await _userStore.HasPasswordAsync(user, _ct); Assert.IsTrue(hasPassword); } @@ -142,11 +143,11 @@ public async Task HasPasswordAsyncTest() [Test] public async Task SetUserNameAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - await _userStore.SetUserNameAsync(user, "Changed", ct); - var userName = await _userStore.GetUserNameAsync(user, ct); + await _userStore.SetUserNameAsync(user, "Changed", _ct); + var userName = await _userStore.GetUserNameAsync(user, _ct); Assert.AreEqual(user.UserName, userName); } @@ -154,11 +155,11 @@ public async Task SetUserNameAsyncTest() [Test] public async Task SetNormalizedUserNameAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - await _userStore.SetNormalizedUserNameAsync(user, "Changed", ct); - var userName = await _userStore.GetUserNameAsync(user, ct); + await _userStore.SetNormalizedUserNameAsync(user, "Changed", _ct); + var userName = await _userStore.GetUserNameAsync(user, _ct); Assert.AreEqual(user.UserName, userName); } @@ -166,11 +167,11 @@ public async Task SetNormalizedUserNameAsyncTest() [Test] public async Task SetPasswordHashAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - await _userStore.SetNormalizedUserNameAsync(user, "Changed", ct); - var userName = await _userStore.GetUserNameAsync(user, ct); + await _userStore.SetNormalizedUserNameAsync(user, "Changed", _ct); + var userName = await _userStore.GetUserNameAsync(user, _ct); Assert.AreEqual(user.UserName, userName); } @@ -178,10 +179,10 @@ public async Task SetPasswordHashAsyncTest() [Test] public async Task GetPhoneNumberAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - var userPhone = await _userStore.GetPhoneNumberAsync(user, ct); + var userPhone = await _userStore.GetPhoneNumberAsync(user, _ct); Assert.AreEqual(user.PhoneNumber, userPhone); } @@ -189,11 +190,11 @@ public async Task GetPhoneNumberAsyncTest() [Test] public async Task SetPhoneNumberAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - await _userStore.SetPhoneNumberAsync(user, "9876543210", ct); - var userPhone = await _userStore.GetPhoneNumberAsync(user, ct); + await _userStore.SetPhoneNumberAsync(user, "9876543210", _ct); + var userPhone = await _userStore.GetPhoneNumberAsync(user, _ct); Assert.AreEqual(user.PhoneNumber, userPhone); } @@ -201,60 +202,56 @@ public async Task SetPhoneNumberAsyncTest() [Test] public async Task GetPhoneNumberConfirmedAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - var userPhoneConfirmed = await _userStore.GetPhoneNumberConfirmedAsync(user, ct); + var userPhoneConfirmed = await _userStore.GetPhoneNumberConfirmedAsync(user, _ct); Assert.IsTrue(userPhoneConfirmed); - Assert.AreEqual(user.PhoneNumberConfirmed, userPhoneConfirmed); } [Test] public async Task SetPhoneNumberConfirmedAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - await _userStore.SetPhoneNumberConfirmedAsync(user, false, ct); - var userPhoneConfirmed = await _userStore.GetPhoneNumberConfirmedAsync(user, ct); + await _userStore.SetPhoneNumberConfirmedAsync(user, false, _ct); + var userPhoneConfirmed = await _userStore.GetPhoneNumberConfirmedAsync(user, _ct); Assert.IsFalse(userPhoneConfirmed); - Assert.AreEqual(user.PhoneNumberConfirmed, userPhoneConfirmed); } [Test] public async Task SetTwoFactorEnabledAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - await _userStore.SetTwoFactorEnabledAsync(user, true, ct); - var twoFactor = await _userStore.GetTwoFactorEnabledAsync(user, ct); + await _userStore.SetTwoFactorEnabledAsync(user, true, _ct); + var twoFactor = await _userStore.GetTwoFactorEnabledAsync(user, _ct); Assert.IsTrue(twoFactor); - Assert.AreEqual(user.TwoFactorEnabled, twoFactor); } [Test] public async Task GetTwoFactorEnabledAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - var twoFactor = await _userStore.GetTwoFactorEnabledAsync(user, ct); + var twoFactor = await _userStore.GetTwoFactorEnabledAsync(user, _ct); Assert.IsFalse(twoFactor); - Assert.AreEqual(user.TwoFactorEnabled, twoFactor); } [Test] public async Task GetLoginsAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); - var userLogin = await _userStore.GetLoginsAsync(user, ct); + var userLogin = await _userStore.GetLoginsAsync(user, _ct); // Assertion to 0 because the method return an empty list Assert.AreEqual(0, userLogin.Count); @@ -263,36 +260,28 @@ public async Task GetLoginsAsyncTest() [Test] public void FindByLoginAsyncTest() { - Assert.Throws(() => - { - _userStore.FindByLoginAsync("loginProvider", "providerkey", ct); - }); + _userStore.FindByLoginAsync("loginProvider", "providerkey", _ct)); } [Test] public async Task AddLoginAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); var userLoginInfo = new UserLoginInfo("loginProv", "providerkey", "Name"); - Assert.Throws(() => - { - _userStore.AddLoginAsync(user, userLoginInfo, ct); - }); + Assert.Throws(() => _userStore.AddLoginAsync(user, userLoginInfo, _ct)); } [Test] public async Task RemoveLoginAsyncTest() { - var user = _mock.GetUser(); - await _userStore.CreateAsync(user, ct); + var user = Mock.GetUser(); + await _userStore.CreateAsync(user, _ct); Assert.Throws(() => - { - _userStore.RemoveLoginAsync(user, "loginProvider", "providerKey", ct); - }); + _userStore.RemoveLoginAsync(user, "loginProvider", "providerKey", _ct)); } } -} +} \ No newline at end of file diff --git a/test/Unosquare.Swan.AspNetCore.Test/BearerTokenAuthTest.cs b/test/Unosquare.Swan.AspNetCore.Test/BearerTokenAuthTest.cs index 76ba6a9..1b3097e 100644 --- a/test/Unosquare.Swan.AspNetCore.Test/BearerTokenAuthTest.cs +++ b/test/Unosquare.Swan.AspNetCore.Test/BearerTokenAuthTest.cs @@ -16,7 +16,7 @@ class BearerTokenAuthTest { private readonly TestServer _server; private readonly HttpClient _client; - private readonly TokenValidationParameters _validationParameters = new TokenValidationParameters() + private readonly TokenValidationParameters _validationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("SETUPAVALIDSECURITYKEYHERE")), @@ -34,41 +34,43 @@ class BearerTokenAuthTest public BearerTokenAuthTest() { - _server = new TestServer(new WebHostBuilder() - .Configure(app => - { - app.UseBearerTokenAuthentication( - _validationParameters, - (username, password, grantType, clientId) => - { - if (username != "Admin" || password != "Pass.word") - return Task.FromResult(null); - - var claim = new ClaimsIdentity("Bearer"); - claim.AddClaim(new Claim(ClaimTypes.Name, username)); - - return Task.FromResult(claim); - }, (identity, obj) => - { - // This action is optional - obj["test"] = "OK"; - return Task.FromResult(obj); - }); - }) - .ConfigureServices(services => - { - services.AddBearerTokenAuthentication(_validationParameters); - })) + _server = new TestServer( + new WebHostBuilder() + .Configure(app => + { + app.UseBearerTokenAuthentication( + _validationParameters, + (username, password, grantType, clientId) => + { + if (username != "Admin" || password != "Pass.word") + return Task.FromResult(null); + + var claim = new ClaimsIdentity("Bearer"); + claim.AddClaim(new Claim(ClaimTypes.Name, username)); + + return Task.FromResult(claim); + }, (identity, obj) => + { + // This action is optional + obj["test"] = "OK"; + return Task.FromResult(obj); + }); + }) + .ConfigureServices(services => + { + services.AddBearerTokenAuthentication(_validationParameters); + })) { BaseAddress = new Uri("https://localhost/") }; + _client = _server.CreateClient(); } [Test] public async Task TokenTest() { - var body = new List>() + var body = new List> { new KeyValuePair("username","Admin"), new KeyValuePair("password","Pass.word"), @@ -89,7 +91,7 @@ public async Task TokenTest() [Test] public async Task UnauthorizedTokenTest() { - var body = new List>() + var body = new List> { new KeyValuePair("username","Admin"), new KeyValuePair("password","pass.word"), @@ -110,7 +112,7 @@ public async Task UnauthorizedTokenTest() [Test] public async Task InvalidRefreshTokenTest() { - var body = new List>() + var body = new List> { new KeyValuePair("username","Admin"), new KeyValuePair("password","Pass.word"), diff --git a/test/Unosquare.Swan.AspNetCore.Test/Mocks/BusinessDbContextMock.cs b/test/Unosquare.Swan.AspNetCore.Test/Mocks/BusinessDbContextMock.cs index 6f96420..c162acd 100644 --- a/test/Unosquare.Swan.AspNetCore.Test/Mocks/BusinessDbContextMock.cs +++ b/test/Unosquare.Swan.AspNetCore.Test/Mocks/BusinessDbContextMock.cs @@ -1,6 +1,5 @@ namespace Unosquare.Swan.AspNetCore.Test.Mocks { - using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Models; diff --git a/test/Unosquare.Swan.AspNetCore.Test/Unosquare.Swan.AspNetCore.Test.csproj b/test/Unosquare.Swan.AspNetCore.Test/Unosquare.Swan.AspNetCore.Test.csproj index cab6b19..9c70791 100644 --- a/test/Unosquare.Swan.AspNetCore.Test/Unosquare.Swan.AspNetCore.Test.csproj +++ b/test/Unosquare.Swan.AspNetCore.Test/Unosquare.Swan.AspNetCore.Test.csproj @@ -7,13 +7,13 @@ - - - - - + + + + + - +