diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj
index 7d648fab..6ed273bf 100644
--- a/src/Web/Web.csproj
+++ b/src/Web/Web.csproj
@@ -1,8 +1,7 @@
-
- net7.0
- disable
+
+ enable
enable
Microsoft.eShopWeb.Web
aspnet-Web2-1FA3F72E-E7E3-4360-9E49-1CCCD7FE85F7
@@ -14,28 +13,27 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
-
-
-
+
+
diff --git a/src/Web/appsettings.Docker.json b/src/Web/appsettings.Docker.json
index bac53d64..07ea75ea 100644
--- a/src/Web/appsettings.Docker.json
+++ b/src/Web/appsettings.Docker.json
@@ -1,7 +1,7 @@
{
"ConnectionStrings": {
- "CatalogConnection": "Server=sqlserver,1433;Integrated Security=true;Initial Catalog=Microsoft.eShopOnWeb.CatalogDb;User Id=sa;Password=@someThingComplicated1234;Trusted_Connection=false;",
- "IdentityConnection": "Server=sqlserver,1433;Integrated Security=true;Initial Catalog=Microsoft.eShopOnWeb.Identity;User Id=sa;Password=@someThingComplicated1234;Trusted_Connection=false;"
+ "CatalogConnection": "Server=sqlserver,1433;Integrated Security=true;Initial Catalog=Microsoft.eShopOnWeb.CatalogDb;User Id=sa;Password=@someThingComplicated1234;Trusted_Connection=false;TrustServerCertificate=true;",
+ "IdentityConnection": "Server=sqlserver,1433;Integrated Security=true;Initial Catalog=Microsoft.eShopOnWeb.Identity;User Id=sa;Password=@someThingComplicated1234;Trusted_Connection=false;TrustServerCertificate=true;"
},
"baseUrls": {
"apiBase": "http://localhost:5200/api/",
diff --git a/src/Web/appsettings.json b/src/Web/appsettings.json
index d7d44636..c2bc6592 100644
--- a/src/Web/appsettings.json
+++ b/src/Web/appsettings.json
@@ -16,13 +16,5 @@
"System": "Warning"
},
"AllowedHosts": "*"
- },
-
- "eShopWeb": {
- "Settings": {
- "NoResultsMessage": "THERE ARE NO RESULTS THAT MATCH YOUR SEARCH"
- }
- },
- "UseAppConfig": false,
- "AppConfigEndpoint": "{appconfig-endpoint}"
-}
\ No newline at end of file
+ }
+}
diff --git a/src/Web/libman.json b/src/Web/libman.json
index ac8c0f95..1343cb4d 100644
--- a/src/Web/libman.json
+++ b/src/Web/libman.json
@@ -3,11 +3,11 @@
"defaultProvider": "cdnjs",
"libraries": [
{
- "library": "jquery@3.3.1",
+ "library": "jquery@3.6.3",
"destination": "wwwroot/lib/jquery/"
},
{
- "library": "twitter-bootstrap@3.3.7",
+ "library": "twitter-bootstrap@3.4.1",
"files": [
"css/bootstrap.css",
"css/bootstrap.css.map",
@@ -19,11 +19,11 @@
"destination": "wwwroot/lib/bootstrap/dist/"
},
{
- "library": "jquery-validation-unobtrusive@3.2.10",
+ "library": "jquery-validation-unobtrusive@4.0.0",
"destination": "wwwroot/lib/jquery-validation-unobtrusive/"
},
{
- "library": "jquery-validate@1.17.0",
+ "library": "jquery-validate@1.19.5",
"destination": "wwwroot/lib/jquery-validate/",
"files": [
"jquery.validate.min.js",
@@ -35,7 +35,7 @@
"destination": "wwwroot/lib/toastr/"
},
{
- "library": "aspnet-signalr@1.0.3",
+ "library": "aspnet-signalr@1.0.27",
"files": [
"signalr.js",
"signalr.min.js"
@@ -43,4 +43,4 @@
"destination": "wwwroot/lib/@aspnet/signalr/dist/browser/"
}
]
-}
\ No newline at end of file
+}
diff --git a/tests/FunctionalTests/FunctionalTests.csproj b/tests/FunctionalTests/FunctionalTests.csproj
index b80bd24d..e703b4d5 100644
--- a/tests/FunctionalTests/FunctionalTests.csproj
+++ b/tests/FunctionalTests/FunctionalTests.csproj
@@ -1,10 +1,9 @@
-
- net7.0
+
Microsoft.eShopWeb.FunctionalTests
false
- disable
+ enable
enable
@@ -15,14 +14,11 @@
-
-
-
-
- all
- runtime; build; native; contentfiles; analyzers
-
-
+
+
+
+
+
diff --git a/tests/FunctionalTests/Web/Controllers/OrderControllerIndex.cs b/tests/FunctionalTests/Web/Controllers/OrderControllerIndex.cs
index 72d1f23c..88b37bf1 100644
--- a/tests/FunctionalTests/Web/Controllers/OrderControllerIndex.cs
+++ b/tests/FunctionalTests/Web/Controllers/OrderControllerIndex.cs
@@ -23,7 +23,7 @@ public OrderIndexOnGet(TestApplication factory)
public async Task ReturnsRedirectGivenAnonymousUser()
{
var response = await Client.GetAsync("/order/my-orders");
- var redirectLocation = response.Headers.Location.OriginalString;
+ var redirectLocation = response!.Headers.Location!.OriginalString;
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Contains("/Account/Login", redirectLocation);
diff --git a/tests/FunctionalTests/Web/Pages/Basket/BasketPageCheckout.cs b/tests/FunctionalTests/Web/Pages/Basket/BasketPageCheckout.cs
index dd19a714..be2dbb0c 100644
--- a/tests/FunctionalTests/Web/Pages/Basket/BasketPageCheckout.cs
+++ b/tests/FunctionalTests/Web/Pages/Basket/BasketPageCheckout.cs
@@ -45,6 +45,6 @@ public async Task RedirectsToLoginIfNotAuthenticated()
formContent = new FormUrlEncodedContent(keyValues);
var postResponse2 = await Client.PostAsync("/Basket/Checkout", formContent);
- Assert.Contains("/Identity/Account/Login", postResponse2.RequestMessage.RequestUri.ToString());
+ Assert.Contains("/Identity/Account/Login", postResponse2!.RequestMessage!.RequestUri!.ToString()!);
}
}
diff --git a/tests/FunctionalTests/Web/Pages/Basket/CheckoutTest.cs b/tests/FunctionalTests/Web/Pages/Basket/CheckoutTest.cs
index 4657d77d..8d6c0be1 100644
--- a/tests/FunctionalTests/Web/Pages/Basket/CheckoutTest.cs
+++ b/tests/FunctionalTests/Web/Pages/Basket/CheckoutTest.cs
@@ -62,7 +62,7 @@ public async Task SucessfullyPay()
var checkOutResponse = await Client.PostAsync("/basket/checkout", checkOutContent);
var stringCheckOutResponse = await checkOutResponse.Content.ReadAsStringAsync();
- Assert.Contains("/Basket/Success", checkOutResponse.RequestMessage.RequestUri.ToString());
+ Assert.Contains("/Basket/Success", checkOutResponse.RequestMessage!.RequestUri!.ToString());
Assert.Contains("Thanks for your Order!", stringCheckOutResponse);
}
}
diff --git a/tests/FunctionalTests/Web/Pages/Basket/IndexTest.cs b/tests/FunctionalTests/Web/Pages/Basket/IndexTest.cs
index cd364589..1f68da7e 100644
--- a/tests/FunctionalTests/Web/Pages/Basket/IndexTest.cs
+++ b/tests/FunctionalTests/Web/Pages/Basket/IndexTest.cs
@@ -52,7 +52,7 @@ public async Task OnPostUpdateTo50Successfully()
var stringUpdateResponse = await updateResponse.Content.ReadAsStringAsync();
- Assert.Contains("/basket/update", updateResponse.RequestMessage.RequestUri.ToString());
+ Assert.Contains("/basket/update", updateResponse!.RequestMessage!.RequestUri!.ToString()!);
decimal expectedTotalAmount = 416.50M;
Assert.Contains(expectedTotalAmount.ToString("N2"), stringUpdateResponse);
}
@@ -92,7 +92,7 @@ public async Task OnPostUpdateTo0EmptyBasket()
var stringUpdateResponse = await updateResponse.Content.ReadAsStringAsync();
- Assert.Contains("/basket/update", updateResponse.RequestMessage.RequestUri.ToString());
+ Assert.Contains("/basket/update", updateResponse!.RequestMessage!.RequestUri!.ToString()!);
Assert.Contains("Basket is empty", stringUpdateResponse);
}
}
diff --git a/tests/FunctionalTests/Web/WebPageHelpers.cs b/tests/FunctionalTests/Web/WebPageHelpers.cs
index d858bfbb..1259c9ee 100644
--- a/tests/FunctionalTests/Web/WebPageHelpers.cs
+++ b/tests/FunctionalTests/Web/WebPageHelpers.cs
@@ -22,6 +22,6 @@ private static string RegexSearch(string regexpression, string input)
{
var regex = new Regex(regexpression);
var match = regex.Match(input);
- return match.Groups.Values.LastOrDefault().Value;
+ return match!.Groups!.Values!.LastOrDefault()!.Value;
}
}
diff --git a/tests/FunctionalTests/Web/WebTestFixture.cs b/tests/FunctionalTests/Web/WebTestFixture.cs
index 55b2e741..d9bb4914 100644
--- a/tests/FunctionalTests/Web/WebTestFixture.cs
+++ b/tests/FunctionalTests/Web/WebTestFixture.cs
@@ -23,6 +23,16 @@ protected override IHost CreateHost(IHostBuilder builder)
// Add mock/test services to the builder here
builder.ConfigureServices(services =>
{
+ var descriptors = services.Where(d =>
+ d.ServiceType == typeof(DbContextOptions) ||
+ d.ServiceType == typeof(DbContextOptions))
+ .ToList();
+
+ foreach (var descriptor in descriptors)
+ {
+ services.Remove(descriptor);
+ }
+
services.AddScoped(sp =>
{
// Replace SQLite with in-memory database for tests
diff --git a/tests/IntegrationTests/IntegrationTests.csproj b/tests/IntegrationTests/IntegrationTests.csproj
index c4acd73e..0923dc66 100644
--- a/tests/IntegrationTests/IntegrationTests.csproj
+++ b/tests/IntegrationTests/IntegrationTests.csproj
@@ -1,17 +1,20 @@
-
- net7.0
+
Microsoft.eShopWeb.IntegrationTests
false
-
-
-
-
-
+
+
+
+
+ all
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+
+
+
all
runtime; build; native; contentfiles; analyzers
diff --git a/tests/IntegrationTests/Repositories/OrderRepositoryTests/GetByIdWithItemsAsync.cs b/tests/IntegrationTests/Repositories/OrderRepositoryTests/GetByIdWithItemsAsync.cs
index a7d3e56d..77e91900 100644
--- a/tests/IntegrationTests/Repositories/OrderRepositoryTests/GetByIdWithItemsAsync.cs
+++ b/tests/IntegrationTests/Repositories/OrderRepositoryTests/GetByIdWithItemsAsync.cs
@@ -49,7 +49,7 @@ public async Task GetOrderAndItemsByOrderIdWhenMultipleOrdersPresent()
//Act
var spec = new OrderWithItemsByIdSpec(secondOrderId);
- var orderFromRepo = await _orderRepository.GetBySpecAsync(spec);
+ var orderFromRepo = await _orderRepository.FirstOrDefaultAsync(spec);
//Assert
Assert.Equal(secondOrderId, orderFromRepo.Id);
diff --git a/tests/PublicApiIntegrationTests/AuthEndpoints/AuthenticateEndpointTest.cs b/tests/PublicApiIntegrationTests/AuthEndpoints/AuthenticateEndpointTest.cs
index 03a969c7..62550e6f 100644
--- a/tests/PublicApiIntegrationTests/AuthEndpoints/AuthenticateEndpointTest.cs
+++ b/tests/PublicApiIntegrationTests/AuthEndpoints/AuthenticateEndpointTest.cs
@@ -7,29 +7,28 @@
using Microsoft.eShopWeb.PublicApi.AuthEndpoints;
using Microsoft.VisualStudio.TestTools.UnitTesting;
-namespace PublicApiIntegrationTests.AuthEndpoints
+namespace PublicApiIntegrationTests.AuthEndpoints;
+
+[TestClass]
+public class AuthenticateEndpoint
{
- [TestClass]
- public class AuthenticateEndpoint
+ [TestMethod]
+ [DataRow("demouser@microsoft.com", AuthorizationConstants.DEFAULT_PASSWORD, true)]
+ [DataRow("demouser@microsoft.com", "badpassword", false)]
+ [DataRow("baduser@microsoft.com", "badpassword", false)]
+ public async Task ReturnsExpectedResultGivenCredentials(string testUsername, string testPassword, bool expectedResult)
{
- [TestMethod]
- [DataRow("demouser@microsoft.com", AuthorizationConstants.DEFAULT_PASSWORD, true)]
- [DataRow("demouser@microsoft.com", "badpassword", false)]
- [DataRow("baduser@microsoft.com", "badpassword", false)]
- public async Task ReturnsExpectedResultGivenCredentials(string testUsername, string testPassword, bool expectedResult)
+ var request = new AuthenticateRequest()
{
- var request = new AuthenticateRequest()
- {
- Username = testUsername,
- Password = testPassword
- };
- var jsonContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
- var response = await ProgramTest.NewClient.PostAsync("api/authenticate", jsonContent);
- response.EnsureSuccessStatusCode();
- var stringResponse = await response.Content.ReadAsStringAsync();
- var model = stringResponse.FromJson();
+ Username = testUsername,
+ Password = testPassword
+ };
+ var jsonContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
+ var response = await ProgramTest.NewClient.PostAsync("api/authenticate", jsonContent);
+ response.EnsureSuccessStatusCode();
+ var stringResponse = await response.Content.ReadAsStringAsync();
+ var model = stringResponse.FromJson();
- Assert.AreEqual(expectedResult, model.Result);
- }
+ Assert.AreEqual(expectedResult, model!.Result);
}
}
diff --git a/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CatalogItemGetByIdEndpointTest.cs b/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CatalogItemGetByIdEndpointTest.cs
index 5882db04..9baefdec 100644
--- a/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CatalogItemGetByIdEndpointTest.cs
+++ b/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CatalogItemGetByIdEndpointTest.cs
@@ -4,29 +4,28 @@
using System.Net;
using System.Threading.Tasks;
-namespace PublicApiIntegrationTests.CatalogItemEndpoints
+namespace PublicApiIntegrationTests.CatalogItemEndpoints;
+
+[TestClass]
+public class CatalogItemGetByIdEndpointTest
{
- [TestClass]
- public class CatalogItemGetByIdEndpointTest
+ [TestMethod]
+ public async Task ReturnsItemGivenValidId()
{
- [TestMethod]
- public async Task ReturnsItemGivenValidId()
- {
- var response = await ProgramTest.NewClient.GetAsync("api/catalog-items/5");
- response.EnsureSuccessStatusCode();
- var stringResponse = await response.Content.ReadAsStringAsync();
- var model = stringResponse.FromJson();
+ var response = await ProgramTest.NewClient.GetAsync("api/catalog-items/5");
+ response.EnsureSuccessStatusCode();
+ var stringResponse = await response.Content.ReadAsStringAsync();
+ var model = stringResponse.FromJson();
- Assert.AreEqual(5, model.CatalogItem.Id);
- Assert.AreEqual("Roslyn Red Sheet", model.CatalogItem.Name);
- }
+ Assert.AreEqual(5, model!.CatalogItem.Id);
+ Assert.AreEqual("Roslyn Red Sheet", model.CatalogItem.Name);
+ }
- [TestMethod]
- public async Task ReturnsNotFoundGivenInvalidId()
- {
- var response = await ProgramTest.NewClient.GetAsync("api/catalog-items/0");
+ [TestMethod]
+ public async Task ReturnsNotFoundGivenInvalidId()
+ {
+ var response = await ProgramTest.NewClient.GetAsync("api/catalog-items/0");
- Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
- }
+ Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
}
}
diff --git a/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CatalogItemListPagedEndpoint.cs b/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CatalogItemListPagedEndpoint.cs
index 1040463c..54701115 100644
--- a/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CatalogItemListPagedEndpoint.cs
+++ b/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CatalogItemListPagedEndpoint.cs
@@ -2,48 +2,71 @@
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
using Microsoft.eShopWeb.Web.ViewModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System.Collections.Generic;
using System.Linq;
+using System.Net.Http;
+using System.Net;
using System.Threading.Tasks;
-namespace PublicApiIntegrationTests.CatalogItemEndpoints
+namespace PublicApiIntegrationTests.CatalogItemEndpoints;
+
+[TestClass]
+public class CatalogItemListPagedEndpoint
{
- [TestClass]
- public class CatalogItemListPagedEndpoint
+ [TestMethod]
+ public async Task ReturnsFirst10CatalogItems()
{
- [TestMethod]
- public async Task ReturnsFirst10CatalogItems()
- {
- var client = ProgramTest.NewClient;
- var response = await client.GetAsync("/api/catalog-items?pageSize=10");
- response.EnsureSuccessStatusCode();
- var stringResponse = await response.Content.ReadAsStringAsync();
- var model = stringResponse.FromJson();
+ var client = ProgramTest.NewClient;
+ var response = await client.GetAsync("/api/catalog-items?pageSize=10");
+ response.EnsureSuccessStatusCode();
+ var stringResponse = await response.Content.ReadAsStringAsync();
+ var model = stringResponse.FromJson();
- Assert.AreEqual(10, model.CatalogItems.Count());
- }
+ Assert.AreEqual(10, model!.CatalogItems.Count());
+ }
- [TestMethod]
- public async Task ReturnsCorrectCatalogItemsGivenPageIndex1()
- {
+ [TestMethod]
+ public async Task ReturnsCorrectCatalogItemsGivenPageIndex1()
+ {
- var pageSize = 10;
- var pageIndex = 1;
+ var pageSize = 10;
+ var pageIndex = 1;
- var client = ProgramTest.NewClient;
- var response = await client.GetAsync($"/api/catalog-items");
- response.EnsureSuccessStatusCode();
- var stringResponse = await response.Content.ReadAsStringAsync();
- var model = stringResponse.FromJson();
- var totalItem = model.CatalogItems.Count();
+ var client = ProgramTest.NewClient;
+ var response = await client.GetAsync($"/api/catalog-items");
+ response.EnsureSuccessStatusCode();
+ var stringResponse = await response.Content.ReadAsStringAsync();
+ var model = stringResponse.FromJson();
+ var totalItem = model!.CatalogItems.Count();
- var response2 = await client.GetAsync($"/api/catalog-items?pageSize={pageSize}&pageIndex={pageIndex}");
- response.EnsureSuccessStatusCode();
- var stringResponse2 = await response2.Content.ReadAsStringAsync();
- var model2 = stringResponse2.FromJson();
+ var response2 = await client.GetAsync($"/api/catalog-items?pageSize={pageSize}&pageIndex={pageIndex}");
+ response.EnsureSuccessStatusCode();
+ var stringResponse2 = await response2.Content.ReadAsStringAsync();
+ var model2 = stringResponse2.FromJson();
- var totalExpected = totalItem - (pageSize * pageIndex);
+ var totalExpected = totalItem - (pageSize * pageIndex);
- Assert.AreEqual(totalExpected, model2.CatalogItems.Count());
+ Assert.AreEqual(totalExpected, model2!.CatalogItems.Count());
+ }
+
+ [DataTestMethod]
+ [DataRow("catalog-items")]
+ [DataRow("catalog-brands")]
+ [DataRow("catalog-types")]
+ [DataRow("catalog-items/1")]
+ public async Task SuccessFullMutipleParallelCall(string endpointName)
+ {
+ var client = ProgramTest.NewClient;
+ var tasks = new List>();
+
+ for (int i = 0; i < 100; i++)
+ {
+ var task = client.GetAsync($"/api/{endpointName}");
+ tasks.Add(task);
}
+ await Task.WhenAll(tasks.ToList());
+ var totalKO = tasks.Count(t => t.Result.StatusCode != HttpStatusCode.OK);
+
+ Assert.AreEqual(0, totalKO);
}
}
diff --git a/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CreateCatalogItemEndpointTest.cs b/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CreateCatalogItemEndpointTest.cs
index a85923d0..6c5d79e4 100644
--- a/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CreateCatalogItemEndpointTest.cs
+++ b/tests/PublicApiIntegrationTests/CatalogItemEndpoints/CreateCatalogItemEndpointTest.cs
@@ -8,62 +8,61 @@
using System.Text.Json;
using System.Threading.Tasks;
-namespace PublicApiIntegrationTests.AuthEndpoints
+namespace PublicApiIntegrationTests.AuthEndpoints;
+
+[TestClass]
+public class CreateCatalogItemEndpointTest
{
- [TestClass]
- public class CreateCatalogItemEndpointTest
- {
- private int _testBrandId = 1;
- private int _testTypeId = 2;
- private string _testDescription = "test description";
- private string _testName = "test name";
- private decimal _testPrice = 1.23m;
+ private int _testBrandId = 1;
+ private int _testTypeId = 2;
+ private string _testDescription = "test description";
+ private string _testName = "test name";
+ private decimal _testPrice = 1.23m;
- [TestMethod]
- public async Task ReturnsNotAuthorizedGivenNormalUserToken()
- {
- var jsonContent = GetValidNewItemJson();
- var token = ApiTokenHelper.GetNormalUserToken();
- var client = ProgramTest.NewClient;
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
- var response = await client.PostAsync("api/catalog-items", jsonContent);
+ [TestMethod]
+ public async Task ReturnsNotAuthorizedGivenNormalUserToken()
+ {
+ var jsonContent = GetValidNewItemJson();
+ var token = ApiTokenHelper.GetNormalUserToken();
+ var client = ProgramTest.NewClient;
+ client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
+ var response = await client.PostAsync("api/catalog-items", jsonContent);
- Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode);
- }
+ Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode);
+ }
- [TestMethod]
- public async Task ReturnsSuccessGivenValidNewItemAndAdminUserToken()
- {
- var jsonContent = GetValidNewItemJson();
- var adminToken = ApiTokenHelper.GetAdminUserToken();
- var client = ProgramTest.NewClient;
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
- var response = await client.PostAsync("api/catalog-items", jsonContent);
- response.EnsureSuccessStatusCode();
- var stringResponse = await response.Content.ReadAsStringAsync();
- var model = stringResponse.FromJson();
+ [TestMethod]
+ public async Task ReturnsSuccessGivenValidNewItemAndAdminUserToken()
+ {
+ var jsonContent = GetValidNewItemJson();
+ var adminToken = ApiTokenHelper.GetAdminUserToken();
+ var client = ProgramTest.NewClient;
+ client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
+ var response = await client.PostAsync("api/catalog-items", jsonContent);
+ response.EnsureSuccessStatusCode();
+ var stringResponse = await response.Content.ReadAsStringAsync();
+ var model = stringResponse.FromJson();
- Assert.AreEqual(_testBrandId, model.CatalogItem.CatalogBrandId);
- Assert.AreEqual(_testTypeId, model.CatalogItem.CatalogTypeId);
- Assert.AreEqual(_testDescription, model.CatalogItem.Description);
- Assert.AreEqual(_testName, model.CatalogItem.Name);
- Assert.AreEqual(_testPrice, model.CatalogItem.Price);
- }
+ Assert.AreEqual(_testBrandId, model!.CatalogItem.CatalogBrandId);
+ Assert.AreEqual(_testTypeId, model.CatalogItem.CatalogTypeId);
+ Assert.AreEqual(_testDescription, model.CatalogItem.Description);
+ Assert.AreEqual(_testName, model.CatalogItem.Name);
+ Assert.AreEqual(_testPrice, model.CatalogItem.Price);
+ }
- private StringContent GetValidNewItemJson()
+ private StringContent GetValidNewItemJson()
+ {
+ var request = new CreateCatalogItemRequest()
{
- var request = new CreateCatalogItemRequest()
- {
- CatalogBrandId = _testBrandId,
- CatalogTypeId = _testTypeId,
- Description = _testDescription,
- Name = _testName,
- Price = _testPrice
- };
- var jsonContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
+ CatalogBrandId = _testBrandId,
+ CatalogTypeId = _testTypeId,
+ Description = _testDescription,
+ Name = _testName,
+ Price = _testPrice
+ };
+ var jsonContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
- return jsonContent;
- }
+ return jsonContent;
}
}
diff --git a/tests/PublicApiIntegrationTests/CatalogItemEndpoints/DeleteCatalogItemEndpointTest.cs b/tests/PublicApiIntegrationTests/CatalogItemEndpoints/DeleteCatalogItemEndpointTest.cs
index f41976ec..98c82123 100644
--- a/tests/PublicApiIntegrationTests/CatalogItemEndpoints/DeleteCatalogItemEndpointTest.cs
+++ b/tests/PublicApiIntegrationTests/CatalogItemEndpoints/DeleteCatalogItemEndpointTest.cs
@@ -5,34 +5,33 @@
using System.Net.Http.Headers;
using System.Threading.Tasks;
-namespace PublicApiIntegrationTests.CatalogItemEndpoints
+namespace PublicApiIntegrationTests.CatalogItemEndpoints;
+
+[TestClass]
+public class DeleteCatalogItemEndpointTest
{
- [TestClass]
- public class DeleteCatalogItemEndpointTest
+ [TestMethod]
+ public async Task ReturnsSuccessGivenValidIdAndAdminUserToken()
{
- [TestMethod]
- public async Task ReturnsSuccessGivenValidIdAndAdminUserToken()
- {
- var adminToken = ApiTokenHelper.GetAdminUserToken();
- var client = ProgramTest.NewClient;
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
- var response = await client.DeleteAsync("api/catalog-items/12");
- response.EnsureSuccessStatusCode();
- var stringResponse = await response.Content.ReadAsStringAsync();
- var model = stringResponse.FromJson();
+ var adminToken = ApiTokenHelper.GetAdminUserToken();
+ var client = ProgramTest.NewClient;
+ client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
+ var response = await client.DeleteAsync("api/catalog-items/12");
+ response.EnsureSuccessStatusCode();
+ var stringResponse = await response.Content.ReadAsStringAsync();
+ var model = stringResponse.FromJson();
- Assert.AreEqual("Deleted", model.Status);
- }
+ Assert.AreEqual("Deleted", model!.Status);
+ }
- [TestMethod]
- public async Task ReturnsNotFoundGivenInvalidIdAndAdminUserToken()
- {
- var adminToken = ApiTokenHelper.GetAdminUserToken();
- var client = ProgramTest.NewClient;
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
- var response = await client.DeleteAsync("api/catalog-items/0");
+ [TestMethod]
+ public async Task ReturnsNotFoundGivenInvalidIdAndAdminUserToken()
+ {
+ var adminToken = ApiTokenHelper.GetAdminUserToken();
+ var client = ProgramTest.NewClient;
+ client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
+ var response = await client.DeleteAsync("api/catalog-items/0");
- Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
- }
+ Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
}
}
diff --git a/tests/PublicApiIntegrationTests/ProgramTest.cs b/tests/PublicApiIntegrationTests/ProgramTest.cs
index ca922346..3f13136f 100644
--- a/tests/PublicApiIntegrationTests/ProgramTest.cs
+++ b/tests/PublicApiIntegrationTests/ProgramTest.cs
@@ -2,26 +2,25 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net.Http;
-namespace PublicApiIntegrationTests
+namespace PublicApiIntegrationTests;
+
+[TestClass]
+public class ProgramTest
{
- [TestClass]
- public class ProgramTest
- {
- private static WebApplicationFactory _application;
+ private static WebApplicationFactory _application = new();
- public static HttpClient NewClient
+ public static HttpClient NewClient
+ {
+ get
{
- get
- {
- return _application.CreateClient();
- }
+ return _application.CreateClient();
}
+ }
- [AssemblyInitialize]
- public static void AssemblyInitialize(TestContext _)
- {
- _application = new WebApplicationFactory();
+ [AssemblyInitialize]
+ public static void AssemblyInitialize(TestContext _)
+ {
+ _application = new WebApplicationFactory();
- }
}
}
diff --git a/tests/PublicApiIntegrationTests/PublicApiIntegrationTests.csproj b/tests/PublicApiIntegrationTests/PublicApiIntegrationTests.csproj
index 9d7ea094..467bc1f1 100644
--- a/tests/PublicApiIntegrationTests/PublicApiIntegrationTests.csproj
+++ b/tests/PublicApiIntegrationTests/PublicApiIntegrationTests.csproj
@@ -1,7 +1,6 @@
-
- net7.0
+
enable
false
@@ -20,11 +19,14 @@
-
-
-
-
-
+
+
+
+
+
+ all
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+
diff --git a/tests/UnitTests/ApplicationCore/Extensions/TestParent.cs b/tests/UnitTests/ApplicationCore/Extensions/TestParent.cs
index bd20e36b..ec791b41 100644
--- a/tests/UnitTests/ApplicationCore/Extensions/TestParent.cs
+++ b/tests/UnitTests/ApplicationCore/Extensions/TestParent.cs
@@ -1,7 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Diagnostics.CodeAnalysis;
-using System.Linq;
+using System.Diagnostics.CodeAnalysis;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions;
@@ -9,12 +6,22 @@ public class TestParent : IEquatable
{
public int Id { get; set; }
- public string Name { get; set; }
+ public string? Name { get; set; }
- public IEnumerable Children { get; set; }
+ public IEnumerable? Children { get; set; }
- public bool Equals([AllowNull] TestParent other) =>
- other?.Id == Id && other?.Name == Name &&
- (other?.Children is null && Children is null ||
- (other?.Children?.Zip(Children)?.All(t => t.First?.Equals(t.Second) ?? false) ?? false));
+ public bool Equals([AllowNull] TestParent other)
+ {
+ if (other?.Id == Id && other?.Name == Name)
+ {
+ if (Children is null)
+ {
+ return other?.Children is null;
+ }
+
+ return other?.Children?.Zip(Children).All(t => t.First?.Equals(t.Second) ?? false) ?? false;
+ }
+
+ return false;
+ }
}
diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/AddItemToBasket.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/AddItemToBasket.cs
index 7937d082..75e1744e 100644
--- a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/AddItemToBasket.cs
+++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/AddItemToBasket.cs
@@ -1,9 +1,10 @@
using System.Threading.Tasks;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
+using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
-using Moq;
+using NSubstitute;
using Xunit;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests;
@@ -11,33 +12,35 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
public class AddItemToBasket
{
private readonly string _buyerId = "Test buyerId";
- private readonly Mock> _mockBasketRepo = new();
+ private readonly IRepository _mockBasketRepo = Substitute.For>();
+ private readonly IAppLogger _mockLogger = Substitute.For>();
[Fact]
public async Task InvokesBasketRepositoryGetBySpecAsyncOnce()
{
var basket = new Basket(_buyerId);
- basket.AddItem(1, It.IsAny(), It.IsAny());
- _mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny(), default)).ReturnsAsync(basket);
+ basket.AddItem(1, 1.5m);
- var basketService = new BasketService(_mockBasketRepo.Object, null);
+ _mockBasketRepo.FirstOrDefaultAsync(Arg.Any(), default).Returns(basket);
+
+ var basketService = new BasketService(_mockBasketRepo, _mockLogger);
await basketService.AddItemToBasket(basket.BuyerId, 1, 1.50m);
- _mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny(), default), Times.Once);
+ await _mockBasketRepo.Received().FirstOrDefaultAsync(Arg.Any(), default);
}
[Fact]
public async Task InvokesBasketRepositoryUpdateAsyncOnce()
{
var basket = new Basket(_buyerId);
- basket.AddItem(1, It.IsAny(), It.IsAny());
- _mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny(), default)).ReturnsAsync(basket);
+ basket.AddItem(1, 1.1m, 1);
+ _mockBasketRepo.FirstOrDefaultAsync(Arg.Any(), default).Returns(basket);
- var basketService = new BasketService(_mockBasketRepo.Object, null);
+ var basketService = new BasketService(_mockBasketRepo, _mockLogger);
await basketService.AddItemToBasket(basket.BuyerId, 1, 1.50m);
- _mockBasketRepo.Verify(x => x.UpdateAsync(basket, default), Times.Once);
+ await _mockBasketRepo.Received().UpdateAsync(basket, default);
}
}
diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs
index 01eff048..0856d3d2 100644
--- a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs
+++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs
@@ -2,7 +2,8 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
-using Moq;
+//using Moq;
+using NSubstitute;
using Xunit;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests;
@@ -10,20 +11,21 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
public class DeleteBasket
{
private readonly string _buyerId = "Test buyerId";
- private readonly Mock> _mockBasketRepo = new();
+ private readonly IRepository _mockBasketRepo = Substitute.For>();
+ private readonly IAppLogger _mockLogger = Substitute.For>();
[Fact]
public async Task ShouldInvokeBasketRepositoryDeleteAsyncOnce()
{
var basket = new Basket(_buyerId);
- basket.AddItem(1, It.IsAny(), It.IsAny());
- basket.AddItem(2, It.IsAny(), It.IsAny());
- _mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny(), default))
- .ReturnsAsync(basket);
- var basketService = new BasketService(_mockBasketRepo.Object, null);
+ basket.AddItem(1, 1.1m, 1);
+ basket.AddItem(2, 1.1m, 1);
+ _mockBasketRepo.GetByIdAsync(Arg.Any(), default)
+ .Returns(basket);
+ var basketService = new BasketService(_mockBasketRepo, _mockLogger);
- await basketService.DeleteBasketAsync(It.IsAny());
+ await basketService.DeleteBasketAsync(1);
- _mockBasketRepo.Verify(x => x.DeleteAsync(It.IsAny(), default), Times.Once);
+ await _mockBasketRepo.Received().DeleteAsync(Arg.Any(), default);
}
}
diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs
index 93d46084..ad873fe2 100644
--- a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs
+++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs
@@ -4,7 +4,7 @@
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
-using Moq;
+using NSubstitute;
using Xunit;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests;
@@ -15,35 +15,36 @@ public class TransferBasket
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
- private readonly Mock> _mockBasketRepo = new();
+ private readonly IRepository _mockBasketRepo = Substitute.For>();
+ private readonly IAppLogger _mockLogger = Substitute.For>();
- [Fact]
- public async Task ThrowsGivenNullAnonymousId()
+ public class Results
{
- var basketService = new BasketService(null, null);
-
- await Assert.ThrowsAsync(async () => await basketService.TransferBasketAsync(null, "steve"));
+ private readonly Queue> values = new Queue>();
+ public Results(T result) { values.Enqueue(() => result); }
+ public Results Then(T value) { return Then(() => value); }
+ public Results Then(Func value)
+ {
+ values.Enqueue(value);
+ return this;
+ }
+ public T Next() { return values.Dequeue()(); }
}
- [Fact]
- public async Task ThrowsGivenNullUserId()
+ [Fact]
+ public async Task InvokesBasketRepositoryFirstOrDefaultAsyncOnceIfAnonymousBasketNotExists()
{
- var basketService = new BasketService(null, null);
+ var anonymousBasket = null as Basket;
+ var userBasket = new Basket(_existentUserBasketBuyerId);
+
+ var results = new Results(anonymousBasket)
+ .Then(userBasket);
- await Assert.ThrowsAsync