Skip to content

Commit df65d3a

Browse files
committed
all but autowrapper
1 parent 6963cf7 commit df65d3a

File tree

8 files changed

+46
-42
lines changed

8 files changed

+46
-42
lines changed

src/Api.Test/EndpointsTests/IntegrationTests/ProductControllerTest.cs

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
using Microsoft.AspNetCore.Authentication;
2-
using Microsoft.AspNetCore.TestHost;
3-
using Microsoft.Extensions.DependencyInjection;
4-
using Microsoft.Extensions.Logging;
5-
using Microsoft.Extensions.Options;
6-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
72
using System;
83
using System.Collections.Generic;
94
using System.Linq;
5+
using System.Net.Http;
106
using System.Net.Http.Headers;
117
using System.Net.Http.Json;
12-
using System.Security.Claims;
13-
using System.Text.Encodings.Web;
148
using System.Threading.Tasks;
159
using Xero.Demo.Api.Domain;
1610
using Xero.Demo.Api.Domain.Models;
@@ -22,24 +16,6 @@
2216

2317
namespace Xero.Demo.Api.Tests.EndpointTests.IntegrationTests
2418
{
25-
public class TestAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
26-
{
27-
public TestAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
28-
: base(options, logger, encoder, clock) { }
29-
30-
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
31-
{
32-
var claims = new[] { new Claim(ClaimTypes.Role, CONSTANTS.Roles.Reader) };
33-
var identity = new ClaimsIdentity(claims, "Test");
34-
var principal = new ClaimsPrincipal(identity);
35-
var ticket = new AuthenticationTicket(principal, "Test");
36-
37-
var result = AuthenticateResult.Success(ticket);
38-
39-
return Task.FromResult(result);
40-
}
41-
}
42-
4319
[Trait("Category", "Integration")]
4420
public class ProductControllerTest : IDisposable
4521
{
@@ -52,7 +28,6 @@ public ProductControllerTest()
5228

5329
[Theory]
5430
[InlineData("en-US", "1")]
55-
//[InlineData("en-US", "2")] :: TO-DO
5631
public async Task GetAsync_Returns_200(string culture, string version)
5732
{
5833
// Given
@@ -93,10 +68,7 @@ public async Task GetByIdAsync_Returns_200(string culture, string version)
9368
public async Task PostAsync_Returns_201(string culture, string version)
9469
{
9570
// Given
96-
var client = factory.CreateClient();
97-
var authResponse = await client.PostAsync(string.Format(SampleDataV1.readerLoginEndpoint, culture, version, Roles.Admin), null);
98-
var authDetails = JsonConvert.DeserializeObject<AuthenticateResponse>(await authResponse.Content.ReadAsStringAsync());
99-
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authDetails.Token);
71+
var client = await SetupHttpClient(Roles.Admin, culture, version);
10072

10173
// When
10274
var response = await client.PostAsJsonAsync(string.Format(SampleDataV1.productEndpoint, culture, version), SampleDataV1.Product);
@@ -110,13 +82,12 @@ public async Task PostAsync_Returns_201(string culture, string version)
11082
public async Task PutAsync_Returns_204(string culture, string version)
11183
{
11284
// Given
113-
var client = factory.CreateClient();
114-
var authResponse = await client.PostAsync(string.Format(SampleDataV1.readerLoginEndpoint, culture, version, Roles.Editor), null);
115-
var authDetails = JsonConvert.DeserializeObject<AuthenticateResponse>(await authResponse.Content.ReadAsStringAsync());
116-
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authDetails.Token);
85+
var client = await SetupHttpClient(Roles.Admin, culture, version);
86+
await client.PostAsJsonAsync(string.Format(SampleDataV1.productEndpoint, culture, version), SampleDataV1.Product);
11787

11888
var productResponse = await client.GetAsync(string.Format(SampleDataV1.productEndpoint, culture, version));
11989
var products = JsonConvert.DeserializeObject<List<ProductDTO>>(await productResponse.Content.ReadAsStringAsync());
90+
12091
var id = products.FirstOrDefault().Id;
12192
var putRequestPayload = new Product
12293
{
@@ -127,13 +98,24 @@ public async Task PutAsync_Returns_204(string culture, string version)
12798
Description = products.FirstOrDefault().Description
12899
};
129100

101+
client = await SetupHttpClient(Roles.Reader, culture, version);
102+
130103
// When
131104
var response = await client.PutAsJsonAsync(string.Format(SampleDataV1.productEndpoint, culture, version) + $"/{id}", putRequestPayload);
132105

133106
// Then
134107
Assert.Equal(System.Net.HttpStatusCode.NoContent, response.StatusCode);
135108
}
136109

110+
private async Task<HttpClient> SetupHttpClient(string role, string culture, string version)
111+
{
112+
var client = factory.CreateClient();
113+
var authResponse = await client.PostAsync(string.Format(SampleDataV1.readerLoginEndpoint, culture, version, Roles.Reader), null);
114+
var authDetails = JsonConvert.DeserializeObject<AuthenticateResponse>(await authResponse.Content.ReadAsStringAsync());
115+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authDetails.Token);
116+
return client;
117+
}
118+
137119
public void Dispose()
138120
{
139121
factory = null;

src/Api.Test/Setup/SetupStartup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace Xero.Demo.Api.Tests.Setup
1515
{
1616
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
1717
{
18+
public Database db;
19+
1820
protected override void ConfigureWebHost(IWebHostBuilder builder)
1921
{
2022
builder.ConfigureServices(services =>
@@ -28,7 +30,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
2830

2931
using var scope = sp.CreateScope();
3032
var scopedServices = scope.ServiceProvider;
31-
var db = scopedServices.GetRequiredService<Database>();
33+
db = scopedServices.GetRequiredService<Database>();
3234
var logger = scopedServices.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();
3335

3436
try

src/Api/Endpoints/Common/LoginController.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public LoginController(IUserService userService)
1717
_userService = userService;
1818
}
1919

20+
/// <summary>
21+
/// Creates jwt token for all [GET POST PUT DELETE] request for Products
22+
/// </summary>
23+
/// <param name="culture">Enter the culture</param>
24+
/// <returns>Create jwt token for POST and DELETE request for Products</returns>
2025
[HttpPost(Roles.Admin)]
2126
public IActionResult AuthenticateAdmin(string culture = "en-US")
2227
{
@@ -27,6 +32,11 @@ public IActionResult AuthenticateAdmin(string culture = "en-US")
2732
return Ok(response);
2833
}
2934

35+
/// <summary>
36+
/// Creates jwt token for GET and PUT request for Products
37+
/// </summary>
38+
/// <param name="culture">Enter the culture</param>
39+
/// <returns>Create jwt token for PUT request for Products</returns>
3040
[HttpPost(Roles.Editor)]
3141
public IActionResult AuthenticateEditor(string culture = "en-US")
3242
{
@@ -37,6 +47,11 @@ public IActionResult AuthenticateEditor(string culture = "en-US")
3747
return Ok(response);
3848
}
3949

50+
/// <summary>
51+
/// Creates jwt token for only GET request for Products
52+
/// </summary>
53+
/// <param name="culture">Enter the culture</param>
54+
/// <returns>Create jwt token for only GET request for Products</returns>
4055
[HttpPost(Roles.Reader)]
4156
public IActionResult AuthenticateReader(string culture = "en-US")
4257
{

src/Api/Endpoints/V1/Products/Create.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ProductsController(Database db)
2121
}
2222

2323
/// <summary>
24-
/// Add product.
24+
/// Add product by sending valid JWT token provided through . 'api/{culture}/v1/Login/Admin'
2525
/// </summary>
2626
/// <param name="product">Enter the product</param>
2727
/// <param name="culture">Enter the culture</param>

src/Api/Endpoints/V1/Products/Delete.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public partial class ProductsController
1414
private readonly bool deleted = rowCountDeleted != 1;
1515

1616
/// <summary>
17-
/// Delete product.
17+
/// Delete product by sending valid JWT token provided through . 'api/{culture}/v1/Login/Admin'
1818
/// </summary>
1919
/// <param name="id">Enter the product id</param>
2020
/// <param name="culture"></param>

src/Api/Endpoints/V1/Products/Read.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Xero.Demo.Api.Endpoints.V1.Products
1515
public partial class ProductsController
1616
{
1717
/// <summary>
18-
/// List of products.
18+
/// Get products by sending valid JWT token provided through . 'api/{culture}/v1/Login/Reader'
1919
/// </summary>
2020
/// <returns>Returns list of products</returns>
2121
[Authorize(Policy = Policy.ShouldBeAReader)]
@@ -43,7 +43,7 @@ public async Task<IActionResult> GetAsync(string culture = "en-US")
4343
}
4444

4545
/// <summary>
46-
/// Get product by id.
46+
/// Get product by id by sending valid JWT token provided through . 'api/{culture}/v1/Login/Reader'
4747
/// </summary>
4848
/// <param name="id">Enter the id of product</param>
4949
/// <returns>Returns list of products</returns>

src/Api/Endpoints/V1/Products/Update.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Xero.Demo.Api.Endpoints.V1.Products
1212
public partial class ProductsController
1313
{
1414
/// <summary>
15-
/// Edit product.
15+
/// Get products by sending valid JWT token provided through . 'api/en-US/v1/Login/Editor'
1616
/// </summary>
1717
/// <param name="id">Enter the product id</param>
1818
/// <param name="product">Enter the product</param>

src/Api/Xero.Demo.Infrastructure/Extensions/AddMiddleWareExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
using Microsoft.FeatureManagement;
66
using Microsoft.OpenApi.Models;
77
using Swashbuckle.AspNetCore.SwaggerGen;
8+
using System;
9+
using System.IO;
10+
using System.Reflection;
811
using Xero.Demo.Api.Datastore;
912
using Xero.Demo.Api.Domain.Infrastructure;
1013
using Xero.Demo.Api.Domain.Infrastructure.Extensions;
@@ -48,6 +51,9 @@ public static void AddServices(this IServiceCollection services, IConfiguration
4851

4952
services.AddSwaggerGen(c =>
5053
{
54+
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML";
55+
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
56+
c.IncludeXmlComments(xmlPath);
5157
c.OperationFilter<SwaggerDefaultValues>();
5258
var securityDefinition = new OpenApiSecurityScheme()
5359
{
@@ -59,7 +65,6 @@ public static void AddServices(this IServiceCollection services, IConfiguration
5965
Type = SecuritySchemeType.Http,
6066
}; ;
6167
c.AddSecurityDefinition("jwt_auth", securityDefinition);
62-
6368
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
6469
{
6570
{

0 commit comments

Comments
 (0)