Skip to content

Commit f39096a

Browse files
authored
Merge pull request #2 from AJEETX/jwt
Jwt
2 parents a4c7739 + 4151ec8 commit f39096a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+568
-286
lines changed

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
2-
using Xero.Demo.Api.Tests.Setup;
3-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
42
using System;
53
using System.Collections.Generic;
64
using System.Linq;
5+
using System.Net.Http;
6+
using System.Net.Http.Headers;
77
using System.Net.Http.Json;
88
using System.Threading.Tasks;
9-
using Xunit;
109
using Xero.Demo.Api.Domain;
1110
using Xero.Demo.Api.Domain.Models;
11+
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
12+
using Xero.Demo.Api.Tests.Setup;
13+
using Xero.Demo.Api.Xero.Demo.Domain.Models;
14+
using Xunit;
15+
using static Xero.Demo.Api.Domain.Models.CONSTANTS;
1216

1317
namespace Xero.Demo.Api.Tests.EndpointTests.IntegrationTests
1418
{
@@ -24,11 +28,13 @@ public ProductControllerTest()
2428

2529
[Theory]
2630
[InlineData("en-US", "1")]
27-
//[InlineData("en-US", "2")]
2831
public async Task GetAsync_Returns_200(string culture, string version)
2932
{
3033
// Given
3134
var client = factory.CreateClient();
35+
var authResponse = await client.PostAsync(string.Format(SampleDataV1.readerLoginEndpoint, culture, version, Roles.Reader), null);
36+
var authDetails = JsonConvert.DeserializeObject<AuthenticateResponse>(await authResponse.Content.ReadAsStringAsync());
37+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authDetails.Token);
3238

3339
// When
3440
var response = await client.GetAsync(string.Format(SampleDataV1.productEndpoint, culture, version));
@@ -39,11 +45,13 @@ public async Task GetAsync_Returns_200(string culture, string version)
3945

4046
[Theory]
4147
[InlineData("en-US", "1")]
42-
[InlineData("en-US", "2")]
4348
public async Task GetByIdAsync_Returns_200(string culture, string version)
4449
{
4550
// Given
4651
var client = factory.CreateClient();
52+
var authResponse = await client.PostAsync(string.Format(SampleDataV1.readerLoginEndpoint, culture, version, Roles.Reader), null);
53+
var authDetails = JsonConvert.DeserializeObject<AuthenticateResponse>(await authResponse.Content.ReadAsStringAsync());
54+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authDetails.Token);
4755
var addProductResponse = await client.PostAsJsonAsync(string.Format(SampleDataV1.productEndpoint, culture, version), SampleDataV1.Product);
4856
var addedProduct = JsonConvert.DeserializeObject<Product>(await addProductResponse.Content.ReadAsStringAsync());
4957
var id = addedProduct.Id;
@@ -57,11 +65,10 @@ public async Task GetByIdAsync_Returns_200(string culture, string version)
5765

5866
[Theory]
5967
[InlineData("en-US", "1")]
60-
[InlineData("en-US", "2")]
6168
public async Task PostAsync_Returns_201(string culture, string version)
6269
{
6370
// Given
64-
var client = factory.CreateClient();
71+
var client = await SetupHttpClient(Roles.Admin, culture, version);
6572

6673
// When
6774
var response = await client.PostAsJsonAsync(string.Format(SampleDataV1.productEndpoint, culture, version), SampleDataV1.Product);
@@ -72,15 +79,15 @@ public async Task PostAsync_Returns_201(string culture, string version)
7279

7380
[Theory]
7481
[InlineData("en-US", "1")]
75-
[InlineData("en-US", "2")]
7682
public async Task PutAsync_Returns_204(string culture, string version)
7783
{
7884
// Given
79-
var client = factory.CreateClient();
80-
var addResponse = await client.PostAsJsonAsync(string.Format(SampleDataV1.productEndpoint, culture, version), SampleDataV1.Product);
85+
var client = await SetupHttpClient(Roles.Admin, culture, version);
86+
await client.PostAsJsonAsync(string.Format(SampleDataV1.productEndpoint, culture, version), SampleDataV1.Product);
8187

8288
var productResponse = await client.GetAsync(string.Format(SampleDataV1.productEndpoint, culture, version));
8389
var products = JsonConvert.DeserializeObject<List<ProductDTO>>(await productResponse.Content.ReadAsStringAsync());
90+
8491
var id = products.FirstOrDefault().Id;
8592
var putRequestPayload = new Product
8693
{
@@ -91,13 +98,24 @@ public async Task PutAsync_Returns_204(string culture, string version)
9198
Description = products.FirstOrDefault().Description
9299
};
93100

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

97106
// Then
98107
Assert.Equal(System.Net.HttpStatusCode.NoContent, response.StatusCode);
99108
}
100109

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+
101119
public void Dispose()
102120
{
103121
factory = null;

src/Api.Test/EndpointsTests/UnitTests/V1/Products/CreateTest.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
using Xero.Demo.Api.Endpoints.V1.Products;
2-
using Xero.Demo.Api.Domain.Models;
3-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Http;
42
using Microsoft.AspNetCore.Mvc;
53
using Microsoft.EntityFrameworkCore;
6-
using Microsoft.Extensions.Logging;
7-
using Moq;
84
using System;
95
using System.Threading.Tasks;
10-
using Xunit;
11-
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
126
using Xero.Demo.Api.Datastore;
7+
using Xero.Demo.Api.Domain.Models;
8+
using Xero.Demo.Api.Endpoints.V1.Products;
9+
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
10+
using Xunit;
1311

1412
namespace Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.Products
1513
{

src/Api.Test/EndpointsTests/UnitTests/V1/Products/DeleteTest.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
using Xero.Demo.Api.Endpoints.V1.Products;
2-
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
3-
using Xero.Demo.Api.Domain;
4-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Http;
52
using Microsoft.AspNetCore.Mvc;
63
using Microsoft.EntityFrameworkCore;
7-
using Microsoft.Extensions.Logging;
8-
using Moq;
94
using System;
105
using System.Threading.Tasks;
11-
using Xunit;
126
using Xero.Demo.Api.Datastore;
7+
using Xero.Demo.Api.Endpoints.V1.Products;
8+
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
9+
using Xunit;
1310

1411
namespace Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.Products
1512
{

src/Api.Test/EndpointsTests/UnitTests/V1/Products/ReadTest.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
using Xero.Demo.Api.Endpoints.V1.Products;
2-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Http;
32
using Microsoft.AspNetCore.Mvc;
43
using Microsoft.EntityFrameworkCore;
5-
using Microsoft.Extensions.Logging;
6-
using Moq;
74
using System;
85
using System.Collections.Generic;
96
using System.Threading.Tasks;
10-
using Xunit;
7+
using Xero.Demo.Api.Datastore;
118
using Xero.Demo.Api.Domain.Models;
9+
using Xero.Demo.Api.Endpoints.V1.Products;
1210
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
13-
using Xero.Demo.Api.Datastore;
11+
using Xunit;
1412

1513
namespace Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.Products
1614
{

src/Api.Test/EndpointsTests/UnitTests/V1/Products/UpdateTest.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
using Xero.Demo.Api.Endpoints.V1.Products;
2-
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
3-
using Xero.Demo.Api.Domain;
4-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Http;
52
using Microsoft.AspNetCore.Mvc;
63
using Microsoft.EntityFrameworkCore;
7-
using Microsoft.Extensions.Logging;
8-
using Moq;
94
using System;
105
using System.Threading.Tasks;
11-
using Xunit;
126
using Xero.Demo.Api.Datastore;
7+
using Xero.Demo.Api.Endpoints.V1.Products;
8+
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
9+
using Xunit;
1310

1411
namespace Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.Products
1512
{
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
using Xero.Demo.Api.Domain.Models;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
3+
using Xero.Demo.Api.Domain.Models;
44

55
namespace Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData
66
{
77
internal class SampleDataV1
88
{
99
public static string productEndpoint = "/api/{0}/v{1}/products", DatabaseString = "Filename=Product.db", TraceIdentifier = "TraceIdentifier", Database = "Database", NewDescription = "NewDescription";
1010
public static Guid ProductId = Guid.NewGuid();
11+
public static string readerLoginEndpoint = "/api/{0}/v{1}/login/{2}";
1112

1213
public static Product Product
1314
{
1415
get
1516
{
1617
return new Product
1718
{
18-
//Id = ProductId,
1919
Name = "product1",
2020
Description = "Description",
2121
Price = 11,
@@ -31,13 +31,5 @@ public static IReadOnlyList<Product> Products
3131
return new List<Product> { Product };
3232
}
3333
}
34-
35-
public static Dictionary<string, string> Traits
36-
{
37-
get
38-
{
39-
return new Dictionary<string, string> { { "Test1", "Unit" }, { "Test2", "Integration" } };
40-
}
41-
}
4234
}
4335
}

src/Api.Test/EndpointsTests/UnitTests/V2/Products/CreateTest.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
using Xero.Demo.Api.Endpoints.V2.Products;
2-
using Xero.Demo.Api.Domain.Models;
3-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Http;
42
using Microsoft.AspNetCore.Mvc;
53
using Microsoft.EntityFrameworkCore;
6-
using Microsoft.Extensions.Logging;
7-
using Moq;
84
using System;
95
using System.Threading.Tasks;
10-
using Xunit;
11-
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V2.TestData;
126
using Xero.Demo.Api.Datastore;
7+
using Xero.Demo.Api.Domain.Models;
8+
using Xero.Demo.Api.Endpoints.V2.Products;
9+
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V2.TestData;
10+
using Xunit;
1311

1412
namespace Xero.Demo.Api.Tests.EndpointTests.UnitTests.V2.Products
1513
{

src/Api.Test/EndpointsTests/UnitTests/V2/TestData/SampleData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Xero.Demo.Api.Domain.Models;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
2+
using Xero.Demo.Api.Domain.Models;
33

44
namespace Xero.Demo.Api.Tests.EndpointTests.UnitTests.V2.TestData
55
{

src/Api.Test/Setup/SetupStartup.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
1+
using Microsoft.AspNetCore.Authentication;
22
using Microsoft.AspNetCore.Hosting;
33
using Microsoft.AspNetCore.Mvc.Testing;
44
using Microsoft.EntityFrameworkCore;
@@ -8,11 +8,15 @@
88
using System.Linq;
99
using System.Threading.Tasks;
1010
using Xero.Demo.Api.Datastore;
11+
using Xero.Demo.Api.Tests.EndpointTests.IntegrationTests;
12+
using Xero.Demo.Api.Tests.EndpointTests.UnitTests.V1.TestData;
1113

1214
namespace Xero.Demo.Api.Tests.Setup
1315
{
1416
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
1517
{
18+
public Database db;
19+
1620
protected override void ConfigureWebHost(IWebHostBuilder builder)
1721
{
1822
builder.ConfigureServices(services =>
@@ -22,13 +26,11 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
2226
services.Remove(descriptor);
2327

2428
services.AddDbContext<Database>(options => options.UseInMemoryDatabase("TestDB"));
25-
//services.AddSingleton<IDatabase>(provider => provider.GetService<Database>());
26-
2729
var sp = services.BuildServiceProvider();
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
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Xero.Demo.Api.Domain;
3+
using Xero.Demo.Api.Domain.Extension;
4+
using Xero.Demo.Api.Xero.Demo.Domain.Services;
5+
using static Xero.Demo.Api.Domain.Models.CONSTANTS;
6+
7+
namespace Xero.Demo.Api.Endpoints.Common
8+
{
9+
[ApiVersion(ApiVersionNumbers.V1)]
10+
[ApiVersion(ApiVersionNumbers.V2)]
11+
public class LoginController : BaseApiController
12+
{
13+
private readonly IUserService _userService;
14+
15+
public LoginController(IUserService userService)
16+
{
17+
_userService = userService;
18+
}
19+
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>
25+
[HttpPost(Roles.Admin)]
26+
public IActionResult AuthenticateAdmin(string culture = "en-US")
27+
{
28+
var response = _userService.Authenticate(Roles.Admin);
29+
30+
if (response == null) return BadRequest(ModelState.GetErrorMessages());
31+
32+
return Ok(response);
33+
}
34+
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>
40+
[HttpPost(Roles.Editor)]
41+
public IActionResult AuthenticateEditor(string culture = "en-US")
42+
{
43+
var response = _userService.Authenticate(Roles.Editor);
44+
45+
if (response == null) return BadRequest(ModelState.GetErrorMessages());
46+
47+
return Ok(response);
48+
}
49+
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>
55+
[HttpPost(Roles.Reader)]
56+
public IActionResult AuthenticateReader(string culture = "en-US")
57+
{
58+
var response = _userService.Authenticate(Roles.Reader);
59+
60+
if (response == null) return BadRequest(ModelState.GetErrorMessages());
61+
62+
return Ok(response);
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)