Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Add tests
support70fe4b15edf4
diff --git a/src/Tests/Grand.Business.Authentication.Tests/ExternalAuthenticationProviderTest.cs b/src/Tests/Grand.Business.Authentication.Tests/ExternalAuthenticationProviderTest.csnew file mode 100644index 000000000..c89e677e4--- /dev/null+++ b/src/Tests/Grand.Business.Authentication.Tests/ExternalAuthenticationProviderTest.cs@@ -0,0 +1,24 @@+using Grand.Business.Core.Interfaces.Authentication;++namespace Grand.Business.Authentication.Tests+{+ public class ExternalAuthenticationProviderTest : IExternalAuthenticationProvider+ {+ public string ConfigurationUrl => "";++ public string SystemName => nameof(ExternalAuthenticationProviderTest);++ public string FriendlyName => nameof(ExternalAuthenticationProviderTest);++ public int Priority => 0;++ public IList<string> LimitedToStores => new List<string>();++ public IList<string> LimitedToGroups => new List<string>();++ public Task<string> GetPublicViewComponentName()+ {+ throw new NotImplementedException();+ }+ }+}diff --git a/src/Tests/Grand.Business.Authentication.Tests/Grand.Business.Authentication.Tests.csproj b/src/Tests/Grand.Business.Authentication.Tests/Grand.Business.Authentication.Tests.csprojindex 851076b7f..7f3413ec4 100644--- a/src/Tests/Grand.Business.Authentication.Tests/Grand.Business.Authentication.Tests.csproj+++ b/src/Tests/Grand.Business.Authentication.Tests/Grand.Business.Authentication.Tests.csproj@@ -18,6 +18,7 @@<ItemGroup><ProjectReference Include="..\..\Business\Grand.Business.Authentication\Grand.Business.Authentication.csproj" /><ProjectReference Include="..\..\Business\Grand.Business.Core\Grand.Business.Core.csproj" />+ <ProjectReference Include="..\Grand.Data.Tests\Grand.Data.Tests.csproj" /></ItemGroup></Project>diff --git a/src/Tests/Grand.Business.Authentication.Tests/ApiAuthenticationServiceTests.cs b/src/Tests/Grand.Business.Authentication.Tests/Services/ApiAuthenticationServiceTests.cssimilarity index 99%rename from src/Tests/Grand.Business.Authentication.Tests/ApiAuthenticationServiceTests.csrename to src/Tests/Grand.Business.Authentication.Tests/Services/ApiAuthenticationServiceTests.csindex 8628b9242..33b382939 100644--- a/src/Tests/Grand.Business.Authentication.Tests/ApiAuthenticationServiceTests.cs+++ b/src/Tests/Grand.Business.Authentication.Tests/Services/ApiAuthenticationServiceTests.cs@@ -10,7 +10,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;using Moq;using System.Security.Claims;-namespace Grand.Business.Authentication.Tests+namespace Grand.Business.Authentication.Tests.Services{[TestClass()]public class ApiAuthenticationServiceTestsdiff --git a/src/Tests/Grand.Business.Authentication.Tests/CookieAuthenticationServiceTests.cs b/src/Tests/Grand.Business.Authentication.Tests/Services/CookieAuthenticationServiceTests.cssimilarity index 95%rename from src/Tests/Grand.Business.Authentication.Tests/CookieAuthenticationServiceTests.csrename to src/Tests/Grand.Business.Authentication.Tests/Services/CookieAuthenticationServiceTests.csindex ec33248cf..b47f49c26 100644--- a/src/Tests/Grand.Business.Authentication.Tests/CookieAuthenticationServiceTests.cs+++ b/src/Tests/Grand.Business.Authentication.Tests/Services/CookieAuthenticationServiceTests.cs@@ -10,7 +10,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;using Moq;using System.Security.Claims;-namespace Grand.Business.Authentication.Tests+namespace Grand.Business.Authentication.Tests.Services{[TestClass()]public class CookieAuthenticationServiceTests@@ -98,7 +98,7 @@ namespace Grand.Business.Authentication.Tests};var principals = new ClaimsPrincipal(new ClaimsIdentity(claims, GrandCookieAuthenticationDefaults.AuthenticationScheme));_authServiceMock.Setup(c => c.AuthenticateAsync(It.IsAny<HttpContext>(), It.IsAny<string>()))- .Returns(() => Task.FromResult<AuthenticateResult>(AuthenticateResult.Success(new AuthenticationTicket(principals, ""))));+ .Returns(() => Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(principals, ""))));_customerServiceMock.Setup(c => c.GetCustomerByUsername(It.IsAny<string>())).Returns(() => Task.FromResult(expectedCustomer));_groupServiceMock.Setup(c => c.IsRegistered(It.IsAny<Customer>())).Returns(() => Task.FromResult(true));@@ -118,7 +118,7 @@ namespace Grand.Business.Authentication.Tests};var principals = new ClaimsPrincipal(new ClaimsIdentity(claims, GrandCookieAuthenticationDefaults.AuthenticationScheme));_authServiceMock.Setup(c => c.AuthenticateAsync(It.IsAny<HttpContext>(), It.IsAny<string>()))- .Returns(() => Task.FromResult<AuthenticateResult>(AuthenticateResult.Success(new AuthenticationTicket(principals, ""))));+ .Returns(() => Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(principals, ""))));_customerServiceMock.Setup(c => c.GetCustomerByUsername(It.IsAny<string>())).Returns(() => Task.FromResult(expectedCustomer));//guest_groupServiceMock.Setup(c => c.IsRegistered(It.IsAny<Customer>())).Returns(() => Task.FromResult(false));diff --git a/src/Tests/Grand.Business.Authentication.Tests/Services/ExternalAuthenticationServiceTests.cs b/src/Tests/Grand.Business.Authentication.Tests/Services/ExternalAuthenticationServiceTests.csnew file mode 100644index 000000000..3a81ab82f--- /dev/null+++ b/src/Tests/Grand.Business.Authentication.Tests/Services/ExternalAuthenticationServiceTests.cs@@ -0,0 +1,144 @@+using Grand.Business.Authentication.Tests;+using Grand.Business.Core.Interfaces.Authentication;+using Grand.Business.Core.Interfaces.Common.Directory;+using Grand.Business.Core.Interfaces.Customers;+using Grand.Data.Tests.MongoDb;+using Grand.Domain.Customers;+using Grand.Domain.Data;+using Grand.Infrastructure;+using MediatR;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Business.Authentication.Services.Tests+{+ [TestClass()]+ public class ExternalAuthenticationServiceTests+ {+ private Mock<ICustomerService> _customerServiceMock;+ private Mock<IGroupService> _groupServiceMock;++ private Mock<IGrandAuthenticationService> _authenticationServiceMock;+ private Mock<ICustomerManagerService> _customerManagerServiceMock;+ private Mock<IMediator> _mediatorMock;+ private IRepository<ExternalAuthentication> _externalAuthenticationRecordRepository;+ private Mock<IWorkContext> _workContextMock;+ private IEnumerable<IExternalAuthenticationProvider> _externalAuthenticationProviders;++ ExternalAuthenticationService _externalAuthenticationService;++ [TestInitialize()]+ public void Init()+ {+ _customerServiceMock = new Mock<ICustomerService>();+ _groupServiceMock = new Mock<IGroupService>();+ _authenticationServiceMock = new Mock<IGrandAuthenticationService>();+ _customerManagerServiceMock = new Mock<ICustomerManagerService>();+ _mediatorMock = new Mock<IMediator>();+ _externalAuthenticationRecordRepository = new MongoDBRepositoryTest<ExternalAuthentication>();+ _workContextMock = new Mock<IWorkContext>();+ _externalAuthenticationProviders = new List<IExternalAuthenticationProvider> { new ExternalAuthenticationProviderTest() };++ var externalAuthenticationSetting = new ExternalAuthenticationSettings();+ externalAuthenticationSetting.ActiveAuthenticationMethodSystemNames = new List<string>();+ externalAuthenticationSetting.ActiveAuthenticationMethodSystemNames.Add("ExternalAuthenticationProviderTest");++ _externalAuthenticationService = new ExternalAuthenticationService(_authenticationServiceMock.Object, _customerManagerServiceMock.Object, _customerServiceMock.Object,+ _groupServiceMock.Object, _mediatorMock.Object, _externalAuthenticationRecordRepository, _workContextMock.Object, _externalAuthenticationProviders,+ new CustomerSettings(), externalAuthenticationSetting);+ }+++ [TestMethod()]+ public void LoadActiveAuthenticationProvidersTest()+ {+ //Act+ var result = _externalAuthenticationService.LoadActiveAuthenticationProviders();+ //Assert+ Assert.IsTrue(result.Any());+ }++ [TestMethod()]+ public void LoadAuthenticationProviderBySystemNameTest()+ {+ //Act+ var result = _externalAuthenticationService.LoadAuthenticationProviderBySystemName("ExternalAuthenticationProviderTest");+ //Assert+ Assert.IsNotNull(result);+ }++ [TestMethod()]+ public void LoadAllAuthenticationProvidersTest()+ {+ //Act+ var result = _externalAuthenticationService.LoadAllAuthenticationProviders();+ //Assert+ Assert.IsTrue(result.Any());+ }++ [TestMethod()]+ public void AuthenticationProviderIsAvailableTest_Available()+ {+ //Act+ var result = _externalAuthenticationService.AuthenticationProviderIsAvailable("ExternalAuthenticationProviderTest");+ //Assert+ Assert.IsTrue(result);+ }+ [TestMethod()]+ public void AuthenticationProviderIsAvailableTest_NotAvailable()+ {+ //Act+ var result = _externalAuthenticationService.AuthenticationProviderIsAvailable("123");+ //Assert+ Assert.IsFalse(result);+ }++ [TestMethod()]+ public async Task AssociateCustomerTest()+ {+ //Act+ await _externalAuthenticationService.AssociateCustomer(new Customer(), new Core.Utilities.Authentication.ExternalAuthParam() { ProviderSystemName = "ExternalAuthenticationProviderTest" });+ //Assert+ Assert.IsTrue(_externalAuthenticationRecordRepository.Table.Any());+ }++ [TestMethod()]+ public async Task GetCustomerTest()+ {+ //Arrange+ var expectedCustomer = new Customer() { Username = "John", Active = true };+ _customerServiceMock.Setup(c => c.GetCustomerById(It.IsAny<string>())).Returns(() => Task.FromResult(expectedCustomer));+ await _externalAuthenticationService.AssociateCustomer(expectedCustomer, new Core.Utilities.Authentication.ExternalAuthParam() { ProviderSystemName = "ExternalAuthenticationProviderTest", Identifier = "1" });+ //Act+ var customer = await _externalAuthenticationService.GetCustomer(new Core.Utilities.Authentication.ExternalAuthParam() { ProviderSystemName = "ExternalAuthenticationProviderTest", Identifier = "1" });+ //Assert+ Assert.IsNotNull(customer);+ Assert.AreEqual(customer.Username, expectedCustomer.Username);+ }++ [TestMethod()]+ public async Task GetExternalIdentifiersTest()+ {+ //Arrange+ var customer = new Customer() { Username = "John", Active = true };+ await _externalAuthenticationService.AssociateCustomer(customer, new Core.Utilities.Authentication.ExternalAuthParam() { ProviderSystemName = "ExternalAuthenticationProviderTest", Identifier = "1" });+ //Act+ var result = await _externalAuthenticationService.GetExternalIdentifiers(customer);+ //Assert+ Assert.IsTrue(result.Any());+ }++ [TestMethod()]+ public async Task DeleteExternalAuthenticationTest()+ {+ //Arrange+ var customer = new Customer() { Username = "John", Active = true };+ var externalAuthParam = new Core.Utilities.Authentication.ExternalAuthParam() { ProviderSystemName = "ExternalAuthenticationProviderTest", Identifier = "1" };+ await _externalAuthenticationService.AssociateCustomer(customer, externalAuthParam);+ //Act+ await _externalAuthenticationService.DeleteExternalAuthentication(_externalAuthenticationRecordRepository.Table.FirstOrDefault(x=>x.ExternalIdentifier == externalAuthParam.Identifier));+ //Assert+ Assert.IsFalse(_externalAuthenticationRecordRepository.Table.Any());+ }+ }+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Authentication.Tests/Services/JwtBearerCustomerAuthenticationServiceTests.cs b/src/Tests/Grand.Business.Authentication.Tests/Services/JwtBearerCustomerAuthenticationServiceTests.csnew file mode 100644index 000000000..9aea15f15--- /dev/null+++ b/src/Tests/Grand.Business.Authentication.Tests/Services/JwtBearerCustomerAuthenticationServiceTests.cs@@ -0,0 +1,139 @@+using Grand.Business.Core.Interfaces.Authentication;+using Grand.Business.Authentication.Services;+using Grand.Business.Core.Interfaces.Common.Directory;+using Grand.Business.Core.Interfaces.Customers;+using Grand.Domain.Customers;+using Microsoft.AspNetCore.Authentication;+using Microsoft.AspNetCore.Authentication.JwtBearer;+using Microsoft.AspNetCore.Http;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;+using System.Security.Claims;+using Grand.Business.Core.Interfaces.Common.Security;+using Grand.Business.Core.Utilities.Common.Security;+using Grand.Domain.Security;++namespace Grand.Business.Authentication.Tests.Services+{+ [TestClass()]+ public class JwtBearerCustomerAuthenticationServiceTests+ {+ private Mock<ICustomerService> _customerServiceMock;+ private Mock<IPermissionService> _permissionServiceMock;+ private Mock<HttpContext> _httpContextMock;+ private Mock<IGroupService> _groupService;+ private Mock<IRefreshTokenService> _refreshTokenServiceMock;++ private IJwtBearerCustomerAuthenticationService _jwtBearerCustomerAuthenticationService;++ [TestInitialize()]+ public void Init()+ {+ _customerServiceMock = new Mock<ICustomerService>();+ _groupService = new Mock<IGroupService>();+ _permissionServiceMock = new Mock<IPermissionService>();+ _httpContextMock = new Mock<HttpContext>();+ _refreshTokenServiceMock = new Mock<IRefreshTokenService>();+ _jwtBearerCustomerAuthenticationService = new JwtBearerCustomerAuthenticationService(_customerServiceMock.Object, _permissionServiceMock.Object, _groupService.Object, _refreshTokenServiceMock.Object);+ }++ [TestMethod()]+ public async Task Valid_NullEmail_ReturnFalse()+ {+ var httpContext = new Mock<HttpContext>();+ var context = new TokenValidatedContext(httpContext.Object, new AuthenticationScheme("", "", typeof(AuthSchemaMock)), new JwtBearerOptions());+ IList<Claim> claims = new List<Claim>+ {+ new Claim("Token", "123"),+ new Claim("Email", "[email protected]"),+ new Claim("RefreshId", "567"),+ };+ context.Principal = new ClaimsPrincipal(new ClaimsIdentity(claims, ""));+ var result = await _jwtBearerCustomerAuthenticationService.Valid(context);+ Assert.IsFalse(result);+ Assert.AreEqual(await _jwtBearerCustomerAuthenticationService.ErrorMessage(), "Not found customer");+ }++ [TestMethod()]+ public async Task Valid_NullToken_ReturnFalse()+ {+ var expectedCustomer = new Customer() { Username = "John", Active = true };+ _customerServiceMock.Setup(c => c.GetCustomerByEmail(It.IsAny<string>())).Returns(() => Task.FromResult(expectedCustomer));++ var httpContext = new Mock<HttpContext>();+ var context = new TokenValidatedContext(httpContext.Object, new AuthenticationScheme("", "", typeof(AuthSchemaMock)), new JwtBearerOptions());+ IList<Claim> claims = new List<Claim>+ {+ new Claim("Email", "[email protected]"),+ };+ context.Principal = new ClaimsPrincipal(new ClaimsIdentity(claims, ""));+ var result = await _jwtBearerCustomerAuthenticationService.Valid(context);+ Assert.IsFalse(result);+ Assert.AreEqual(await _jwtBearerCustomerAuthenticationService.ErrorMessage(), "Invalid token or cancel by refresh token");+ }+++ [TestMethod()]+ public async Task Valid_NotActiveCustomer_ReturnFalse()+ {+ var expectedCustomer = new Customer() { Username = "John", Active = true };+ _customerServiceMock.Setup(c => c.GetCustomerByEmail(It.IsAny<string>())).Returns(() => Task.FromResult(expectedCustomer));++ var httpContext = new Mock<HttpContext>();+ var context = new TokenValidatedContext(httpContext.Object, new AuthenticationScheme("", "", typeof(AuthSchemaMock)), new JwtBearerOptions());+ IList<Claim> claims = new List<Claim>+ {+ new Claim("Email", "[email protected]"),+ new Claim("Token", "123")+ };+ context.Principal = new ClaimsPrincipal(new ClaimsIdentity(claims, ""));+ _customerServiceMock.Setup(c => c.GetCustomerByEmail(It.IsAny<string>())).Returns(() => Task.FromResult(new Customer() { Active = false }));+ var result = await _jwtBearerCustomerAuthenticationService.Valid(context);+ Assert.IsFalse(result);+ Assert.AreEqual(await _jwtBearerCustomerAuthenticationService.ErrorMessage(), "Customer not exists/or not active in the customer table");+ }++ [TestMethod()]+ public async Task Valid_NoPermissions_Customer_ReturnFalse()+ {+ var customer = new Customer() { Username = "John", Active = true };+ _customerServiceMock.Setup(c => c.GetCustomerByEmail(It.IsAny<string>())).Returns(() => Task.FromResult(customer));+ _refreshTokenServiceMock.Setup(c => c.GetCustomerRefreshToken(customer)).Returns(() => Task.FromResult(new RefreshToken() { IsActive = true, RefreshId = "567", Token = "123" }));+ _permissionServiceMock.Setup(c => c.Authorize(StandardPermission.AllowUseApi, customer)).Returns(() => Task.FromResult(false));+ var httpContext = new Mock<HttpContext>();+ var context = new TokenValidatedContext(httpContext.Object, new AuthenticationScheme("", "", typeof(AuthSchemaMock)), new JwtBearerOptions());+ IList<Claim> claims = new List<Claim>+ {+ new Claim("Email", "[email protected]"),+ new Claim("Token", "123"),+ new Claim("RefreshId", "567"),+ };+ context.Principal = new ClaimsPrincipal(new ClaimsIdentity(claims, ""));++ var result = await _jwtBearerCustomerAuthenticationService.Valid(context);+ Assert.IsFalse(result);+ Assert.AreEqual(await _jwtBearerCustomerAuthenticationService.ErrorMessage(), "You do not have permission to use API operation (Customer group)");+ }+ [TestMethod()]+ public async Task Valid_Customer_ReturnTrue()+ {+ var customer = new Customer() { Username = "John", Active = true };+ customer.UserFields.Add(new Domain.Common.UserField() { Key = SystemCustomerFieldNames.PasswordToken, Value = "123", StoreId = "" });+ _customerServiceMock.Setup(c => c.GetCustomerByEmail(It.IsAny<string>())).Returns(() => Task.FromResult(customer));+ _refreshTokenServiceMock.Setup(c => c.GetCustomerRefreshToken(customer)).Returns(() => Task.FromResult(new RefreshToken() { IsActive = true, RefreshId = "567", Token = "123" }));+ _permissionServiceMock.Setup(c => c.Authorize(StandardPermission.AllowUseApi, customer)).Returns(() => Task.FromResult(true));+ var httpContext = new Mock<HttpContext>();+ var context = new TokenValidatedContext(httpContext.Object, new AuthenticationScheme("", "", typeof(AuthSchemaMock)), new JwtBearerOptions());+ IList<Claim> claims = new List<Claim>+ {+ new Claim("Email", "[email protected]"),+ new Claim("Token", "123"),+ new Claim("RefreshId", "567"),+ };+ context.Principal = new ClaimsPrincipal(new ClaimsIdentity(claims, ""));++ var result = await _jwtBearerCustomerAuthenticationService.Valid(context);+ Assert.IsTrue(result);+ }+ }+}\ No newline at end of file