Feature/migrate to minimal api (#662)
* migrate from classic controller to minimal api * fix all PublicApi integration test * update all nuget package add forget project * fix pay now * Adapt readme use in memory database * undo AuthenticateEndpoint to use EndpointBaseAsync * Update README.md Co-authored-by: Steve Smith <steve@kentsmiths.com> Co-authored-by: Steve Smith <steve@kentsmiths.com>
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>Microsoft.eShopWeb.FunctionalTests</RootNamespace>
|
||||
<IsPackable>false</IsPackable>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -13,14 +15,14 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.1" />
|
||||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
|
||||
public class TestApiApplication : WebApplicationFactory<Authenticate>
|
||||
public class TestApiApplication : WebApplicationFactory<AuthenticateEndpoint>
|
||||
{
|
||||
private readonly string _environment = "Testing";
|
||||
|
||||
|
||||
@@ -1,43 +1,43 @@
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.PublicApi.AuthEndpoints;
|
||||
using Xunit;
|
||||
//using System.Net.Http;
|
||||
//using System.Text;
|
||||
//using System.Text.Json;
|
||||
//using System.Threading.Tasks;
|
||||
//using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||
//using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
//using Microsoft.eShopWeb.PublicApi.AuthEndpoints;
|
||||
//using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
//namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
|
||||
[Collection("Sequential")]
|
||||
public class AuthenticateEndpoint : IClassFixture<TestApiApplication>
|
||||
{
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
//[Collection("Sequential")]
|
||||
//public class AuthenticateEndpoint : IClassFixture<TestApiApplication>
|
||||
//{
|
||||
// JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
|
||||
public AuthenticateEndpoint(TestApiApplication factory)
|
||||
{
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
// public AuthenticateEndpoint(TestApiApplication factory)
|
||||
// {
|
||||
// Client = factory.CreateClient();
|
||||
// }
|
||||
|
||||
public HttpClient Client { get; }
|
||||
// public HttpClient Client { get; }
|
||||
|
||||
[Theory]
|
||||
[InlineData("demouser@microsoft.com", AuthorizationConstants.DEFAULT_PASSWORD, true)]
|
||||
[InlineData("demouser@microsoft.com", "badpassword", false)]
|
||||
[InlineData("baduser@microsoft.com", "badpassword", false)]
|
||||
public async Task ReturnsExpectedResultGivenCredentials(string testUsername, string testPassword, bool expectedResult)
|
||||
{
|
||||
var request = new AuthenticateRequest()
|
||||
{
|
||||
Username = testUsername,
|
||||
Password = testPassword
|
||||
};
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
|
||||
var response = await Client.PostAsync("api/authenticate", jsonContent);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<AuthenticateResponse>();
|
||||
// [Theory]
|
||||
// [InlineData("demouser@microsoft.com", AuthorizationConstants.DEFAULT_PASSWORD, true)]
|
||||
// [InlineData("demouser@microsoft.com", "badpassword", false)]
|
||||
// [InlineData("baduser@microsoft.com", "badpassword", false)]
|
||||
// public async Task ReturnsExpectedResultGivenCredentials(string testUsername, string testPassword, bool expectedResult)
|
||||
// {
|
||||
// var request = new AuthenticateRequest()
|
||||
// {
|
||||
// Username = testUsername,
|
||||
// Password = testPassword
|
||||
// };
|
||||
// var jsonContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
|
||||
// var response = await Client.PostAsync("api/authenticate", jsonContent);
|
||||
// response.EnsureSuccessStatusCode();
|
||||
// var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
// var model = stringResponse.FromJson<AuthenticateResponse>();
|
||||
|
||||
Assert.Equal(expectedResult, model.Result);
|
||||
}
|
||||
}
|
||||
// Assert.Equal(expectedResult, model.Result);
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.Web.ViewModels;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
|
||||
[Collection("Sequential")]
|
||||
public class ApiCatalogControllerList : IClassFixture<TestApiApplication>
|
||||
{
|
||||
public ApiCatalogControllerList(TestApiApplication factory)
|
||||
{
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
|
||||
public HttpClient Client { get; }
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsFirst10CatalogItems()
|
||||
{
|
||||
var response = await Client.GetAsync("/api/catalog-items?pageSize=10");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<CatalogIndexViewModel>();
|
||||
|
||||
Assert.Equal(10, model.CatalogItems.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsLast2CatalogItemsGivenPageIndex1()
|
||||
{
|
||||
var response = await Client.GetAsync("/api/catalog-items?pageSize=10&pageIndex=1");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<CatalogIndexViewModel>();
|
||||
|
||||
Assert.Equal(2, model.CatalogItems.Count());
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.FunctionalTests.Web.Api;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
|
||||
[Collection("Sequential")]
|
||||
public class CreateEndpoint : IClassFixture<TestApiApplication>
|
||||
{
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
private int _testBrandId = 1;
|
||||
private int _testTypeId = 2;
|
||||
private string _testDescription = "test description";
|
||||
private string _testName = "test name";
|
||||
private decimal _testPrice = 1.23m;
|
||||
|
||||
public CreateEndpoint(TestApiApplication factory)
|
||||
{
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
|
||||
public HttpClient Client { get; }
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsNotAuthorizedGivenNormalUserToken()
|
||||
{
|
||||
var jsonContent = GetValidNewItemJson();
|
||||
var token = ApiTokenHelper.GetNormalUserToken();
|
||||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
var response = await Client.PostAsync("api/catalog-items", jsonContent);
|
||||
|
||||
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsSuccessGivenValidNewItemAndAdminUserToken()
|
||||
{
|
||||
var jsonContent = GetValidNewItemJson();
|
||||
var adminToken = ApiTokenHelper.GetAdminUserToken();
|
||||
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<CreateCatalogItemResponse>();
|
||||
|
||||
Assert.Equal(_testBrandId, model.CatalogItem.CatalogBrandId);
|
||||
Assert.Equal(_testTypeId, model.CatalogItem.CatalogTypeId);
|
||||
Assert.Equal(_testDescription, model.CatalogItem.Description);
|
||||
Assert.Equal(_testName, model.CatalogItem.Name);
|
||||
Assert.Equal(_testPrice, model.CatalogItem.Price);
|
||||
}
|
||||
|
||||
private StringContent GetValidNewItemJson()
|
||||
{
|
||||
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");
|
||||
|
||||
return jsonContent;
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.FunctionalTests.Web.Api;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
|
||||
[Collection("Sequential")]
|
||||
public class DeleteEndpoint : IClassFixture<TestApiApplication>
|
||||
{
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
|
||||
public DeleteEndpoint(TestApiApplication factory)
|
||||
{
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
|
||||
public HttpClient Client { get; }
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsSuccessGivenValidIdAndAdminUserToken()
|
||||
{
|
||||
var adminToken = ApiTokenHelper.GetAdminUserToken();
|
||||
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<DeleteCatalogItemResponse>();
|
||||
|
||||
Assert.Equal("Deleted", model.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsNotFoundGivenInvalidIdAndAdminUserToken()
|
||||
{
|
||||
var adminToken = ApiTokenHelper.GetAdminUserToken();
|
||||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
|
||||
var response = await Client.DeleteAsync("api/catalog-items/0");
|
||||
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
|
||||
[Collection("Sequential")]
|
||||
public class GetByIdEndpoint : IClassFixture<TestApiApplication>
|
||||
{
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
|
||||
public GetByIdEndpoint(TestApiApplication factory)
|
||||
{
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
|
||||
public HttpClient Client { get; }
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsItemGivenValidId()
|
||||
{
|
||||
var response = await Client.GetAsync("api/catalog-items/5");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<GetByIdCatalogItemResponse>();
|
||||
|
||||
Assert.Equal(5, model.CatalogItem.Id);
|
||||
Assert.Equal("Roslyn Red Sheet", model.CatalogItem.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsNotFoundGivenInvalidId()
|
||||
{
|
||||
var response = await Client.GetAsync("api/catalog-items/0");
|
||||
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
|
||||
<PackageReference Include="Moq" Version="4.16.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
|
||||
50
tests/PublicApiIntegrationTests/ApiTokenHelper.cs
Normal file
50
tests/PublicApiIntegrationTests/ApiTokenHelper.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
|
||||
namespace PublicApiIntegrationTests
|
||||
{
|
||||
public class ApiTokenHelper
|
||||
{
|
||||
public static string GetAdminUserToken()
|
||||
{
|
||||
string userName = "admin@microsoft.com";
|
||||
string[] roles = { "Administrators" };
|
||||
|
||||
return CreateToken(userName, roles);
|
||||
}
|
||||
|
||||
public static string GetNormalUserToken()
|
||||
{
|
||||
string userName = "demouser@microsoft.com";
|
||||
string[] roles = { };
|
||||
|
||||
return CreateToken(userName, roles);
|
||||
}
|
||||
|
||||
private static string CreateToken(string userName, string[] roles)
|
||||
{
|
||||
var claims = new List<Claim> { new Claim(ClaimTypes.Name, userName) };
|
||||
|
||||
foreach (var role in roles)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, role));
|
||||
}
|
||||
|
||||
var key = Encoding.ASCII.GetBytes(AuthorizationConstants.JWT_SECRET_KEY);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(claims.ToArray()),
|
||||
Expires = DateTime.UtcNow.AddHours(1),
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.eShopWeb;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||
using Microsoft.eShopWeb.PublicApi.AuthEndpoints;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PublicApiIntegrationTests.AuthEndpoints
|
||||
{
|
||||
[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)
|
||||
{
|
||||
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<AuthenticateResponse>();
|
||||
|
||||
Assert.AreEqual(expectedResult, model.Result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Microsoft.eShopWeb;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PublicApiIntegrationTests.CatalogItemEndpoints
|
||||
{
|
||||
[TestClass]
|
||||
public class CatalogItemGetByIdEndpointTest
|
||||
{
|
||||
[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<GetByIdCatalogItemResponse>();
|
||||
|
||||
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");
|
||||
|
||||
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using Microsoft.eShopWeb;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using Microsoft.eShopWeb.Web.ViewModels;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PublicApiIntegrationTests.CatalogItemEndpoints
|
||||
{
|
||||
[TestClass]
|
||||
public class CatalogItemListPagedEndpoint
|
||||
{
|
||||
[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<CatalogIndexViewModel>();
|
||||
|
||||
Assert.AreEqual(10, model.CatalogItems.Count());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ReturnsCorrectCatalogItemsGivenPageIndex1()
|
||||
{
|
||||
|
||||
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<ListPagedCatalogItemResponse>();
|
||||
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<ListPagedCatalogItemResponse>();
|
||||
|
||||
var totalExpected = totalItem - (pageSize * pageIndex);
|
||||
|
||||
Assert.AreEqual(totalExpected, model2.CatalogItems.Count());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using BlazorShared.Models;
|
||||
using Microsoft.eShopWeb;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PublicApiIntegrationTests.AuthEndpoints
|
||||
{
|
||||
[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;
|
||||
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
[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<CreateCatalogItemResponse>();
|
||||
|
||||
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()
|
||||
{
|
||||
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");
|
||||
|
||||
return jsonContent;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using BlazorShared.Models;
|
||||
using Microsoft.eShopWeb;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PublicApiIntegrationTests.CatalogItemEndpoints
|
||||
{
|
||||
[TestClass]
|
||||
public class DeleteCatalogItemEndpointTest
|
||||
{
|
||||
[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<DeleteCatalogItemResponse>();
|
||||
|
||||
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");
|
||||
|
||||
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
27
tests/PublicApiIntegrationTests/ProgramTest.cs
Normal file
27
tests/PublicApiIntegrationTests/ProgramTest.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace PublicApiIntegrationTests
|
||||
{
|
||||
[TestClass]
|
||||
public class ProgramTest
|
||||
{
|
||||
private static WebApplicationFactory<Program> _application;
|
||||
|
||||
public static HttpClient NewClient
|
||||
{
|
||||
get
|
||||
{
|
||||
return _application.CreateClient();
|
||||
}
|
||||
}
|
||||
|
||||
[AssemblyInitialize]
|
||||
public static void AssemblyInitialize(TestContext _)
|
||||
{
|
||||
_application = new WebApplicationFactory<Program>();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="appsettings.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\PublicApi\PublicApi.csproj" />
|
||||
<ProjectReference Include="..\..\src\Web\Web.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
3
tests/PublicApiIntegrationTests/appsettings.json
Normal file
3
tests/PublicApiIntegrationTests/appsettings.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"UseOnlyInMemoryDatabase": true
|
||||
}
|
||||
Reference in New Issue
Block a user