Skip to content

Commit

Permalink
Merge pull request #176 from soat-fiap/gherkin_bdd
Browse files Browse the repository at this point in the history
test: add gherkin unit tests #173
  • Loading branch information
italopessoa authored Nov 16, 2024
2 parents 9a82b58 + d02faec commit 09aa738
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 107 deletions.

This file was deleted.

8 changes: 0 additions & 8 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Auth/BmbRoles.cs

This file was deleted.

36 changes: 0 additions & 36 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Auth/JwtExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,12 @@
using System.Security.Claims;
using System.Text;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Dto;
using Microsoft.IdentityModel.Tokens;

namespace FIAP.TechChallenge.ByteMeBurger.Api.Auth;

/// <summary>
/// Jwt token extensions methods
/// </summary>
public static class JwtExtensions
{
/// <summary>
/// Configure Jtw token validation
/// </summary>
/// <param name="services">Service collection</param>
/// <param name="configuration">Configuration</param>
public static void ConfigureJwt(this IServiceCollection services, IConfiguration configuration)
{
var jwtOptions = configuration
.GetSection("JwtOptions")
.Get<JwtOptions>();

services.AddAuthentication()
.AddJwtBearer(options =>
{
if (jwtOptions.UseAccessToken)
{
options.Events = AccessTokenAuthEventsHandler.Instance;
}

options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = jwtOptions.Issuer,
ValidAudience = jwtOptions.Audience,
ValidateAudience = true,
ValidateIssuer = true,
ValidateLifetime = true,
LogValidationExceptions = true,
IssuerSigningKey =
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SigningKey))
};
});
}

// https://stackoverflow.com/a/55740879/2921329
/// <summary>
/// Get customer details from Jwt Claims
Expand Down
20 changes: 0 additions & 20 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Auth/JwtOptions.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.ComponentModel.DataAnnotations;
using FIAP.TechChallenge.ByteMeBurger.Api.Auth;
using Bmb.Auth;
using FIAP.TechChallenge.ByteMeBurger.Api.Model.Customers;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Contracts;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Dto;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.ObjectModel;
using FIAP.TechChallenge.ByteMeBurger.Api.Auth;
using Bmb.Auth;
using FIAP.TechChallenge.ByteMeBurger.Api.Model.Orders;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Contracts;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Dto;
using Bmb.Domain.Core.ValueObjects;
using FIAP.TechChallenge.ByteMeBurger.Api.Auth;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Hybrid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FIAP.TechChallenge.ByteMeBurger.Api.Auth;
using Bmb.Auth;
using FIAP.TechChallenge.ByteMeBurger.Api.Model.Products;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Contracts;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Dto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.MySql" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageReference Include="Bmb.Auth" Version="0.0.1" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.12" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3"/>
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.0.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
Expand Down
2 changes: 1 addition & 1 deletion src/FIAP.TechChallenge.ByteMeBurger.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json.Serialization;
using FIAP.TechChallenge.ByteMeBurger.Api.Auth;
using Bmb.Auth;
using FIAP.TechChallenge.ByteMeBurger.DI;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@
<ProjectReference Include="..\FIAP.TechChallenge.ByteMeBurger.Test.Common\FIAP.TechChallenge.ByteMeBurger.Test.Common.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="UseCases\Orders\Gherkin\CreateOrder.feature">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Feature: Create new order

Scenario: Create a new order successfully
Given Selected product exists
And Tracking code is created
When UseCase is called
Then it should create the order
And it should publish integration event
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Bmb.Domain.Core.Events;
using Bmb.Domain.Core.Interfaces;
using FIAP.TechChallenge.ByteMeBurger.Application.UseCases.Orders;
using Xunit.Gherkin.Quick;
using OrderCreated = Bmb.Domain.Core.Events.Integration.OrderCreated;

namespace FIAP.TechChallenge.ByteMeBurger.Application.Test.UseCases.Orders.Gherkin;

[FeatureFile("./UseCases/Orders/Gherkin/CreateOrder.feature")]
public class CreateOrderSteps : Feature
{
// Given Selected product exists
// And Tracking code is created
// When UseCase is called
// Then it should create the order
private readonly CreateOrderUseCase _useCase;
private readonly Mock<IProductRepository> _mockProductRepository;
private readonly Mock<IOrderTrackingCodeService> _mockOrderTrackingCodeService;
private readonly Mock<IDispatcher> _mockDispatcher;
private Order _order;
private Product _product;

public CreateOrderSteps()
{
_mockDispatcher = new Mock<IDispatcher>();
_mockProductRepository = new Mock<IProductRepository>();
_mockOrderTrackingCodeService = new Mock<IOrderTrackingCodeService>();

_useCase = new CreateOrderUseCase(_mockProductRepository.Object, _mockOrderTrackingCodeService.Object,
_mockDispatcher.Object);
}

[Given("Selected product exists")]
public void SetupExistingProduct()
{
_product = new Product(Guid.NewGuid(), "product", "description", ProductCategory.Drink, 10, []);
_mockProductRepository.Setup(p => p.FindByIdAsync(_product.Id))
.ReturnsAsync(_product)
.Verifiable();
}

[And("Tracking code is created")]
public void SetupTrackingCode()
{
_mockOrderTrackingCodeService.Setup(s => s.GetNextAsync())
.ReturnsAsync(new OrderTrackingCode("code"))
.Verifiable();
}

[When("UseCase is called")]
public async Task WhenUseCaseIsCalled()
{
_order = await _useCase.Execute(null, new List<SelectedProduct>
{
new(_product.Id, 1)
});
}

[Then("it should create the order")]
public void ThenItShouldCreateTheOrder()
{
_order.Should().NotBeNull();
_order.TrackingCode.Should().Be(new OrderTrackingCode("code"));
_order.OrderItems[0].ProductId.Should().Be(_product.Id);
_mockProductRepository.VerifyAll();
}

[And("it should publish integration event")]
public void ThenItShouldPublishTheEvent()
{
_mockDispatcher.Verify(d => d.PublishIntegrationAsync(It.IsAny<OrderCreated>(), default), Times.Once);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="Xunit.Gherkin.Quick" Version="4.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>

Expand Down
17 changes: 9 additions & 8 deletions tf/variables.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
variable "rds_cluster_identifier" {
type = string
default = "techchallenge-mysql-default"
default = "gh-techchallenge-mysql"
}

variable "profile" {
Expand All @@ -17,7 +17,7 @@ variable "region" {

variable "eks_cluster_name" {
type = string
default = "quixada"
default = "eks_dev_quixada"
}

variable "apgw_name" {
Expand All @@ -28,6 +28,7 @@ variable "apgw_name" {
variable "jwt_signing_key" {
type = string
sensitive = true
default = ""
}

variable "jwt_issuer" {
Expand All @@ -44,7 +45,7 @@ variable "jwt_aud" {

variable "api_docker_image" {
type = string
default = "ghcr.io/soat-fiap/fiap.techchallenge.bytemeburger/api:sha-b83177c"
default = "ghcr.io/soat-fiap/fiap.techchallenge.bytemeburger/api:latest"
}

variable "internal_elb_name" {
Expand All @@ -55,28 +56,28 @@ variable "internal_elb_name" {
variable "db_user" {
type = string
sensitive = true
default = "db_user"
default = "test"
}

variable "db_pwd" {
type = string
sensitive = true
default = "db_password"
default = "test"
}

variable "api_access_key_id" {
type = string
nullable = false
sensitive = true
default = ""
}

variable "api_secret_access_key" {
type = string
nullable = false
sensitive = true
default = ""
}

variable "user_pool_name" {
type = string
default = "bmb-users-pool-local"
default = "bmb-users-pool-dev"
}

0 comments on commit 09aa738

Please sign in to comment.