Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 59 additions & 21 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using solidInCsharp;
using solidInCsharp.Repository;
using solidInCsharp.Service;

namespace solidInCsharp
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();

var key = Encoding.ASCII.GetBytes(Settings.Secret);
builder.Services.AddAuthentication(x =>
{
public class Program
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});

builder.Services.AddDbContext<UsuarioRepository>(options => options.UseInMemoryDatabase(databaseName: "Test"));
builder.Services.AddDbContext<ProdutoRepository>(options => options.UseInMemoryDatabase(databaseName: "Test") );

builder.Services.AddScoped<IUsuarioRepository, UsuarioRepository>();
builder.Services.AddScoped<IProdutoRepository, ProdutoRepository>();

builder.Services.AddScoped<ICriptografiaService, CriptografiaService>();
builder.Services.AddScoped<IJWTService, JWTService>();

builder.Services.AddScoped<IUsuarioService, UsuarioService>();
builder.Services.AddScoped<IProdutoReportService, ProdutoReportService>();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();
7 changes: 3 additions & 4 deletions Service/CriptografiaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public bool ValidarSenha(string senhaCripto, string senhaDigitada) {
byte[] hashBytes = Convert.FromBase64String(senhaCripto);
byte[] salt = new byte[16];
Array.Copy(hashBytes, 0, salt, 0, 16);
var pbkdf2 = new Rfc2898DeriveBytes(senhaDigitada, salt, 100000);
var pbkdf2 = new Rfc2898DeriveBytes(senhaDigitada, salt, 100000, HashAlgorithmName.SHA256);
byte[] hash = pbkdf2.GetBytes(20);
for (int i=0; i < 20; i++) {
if (hashBytes[i+16] != hash[i]) {
Expand All @@ -32,9 +32,8 @@ public bool ValidarSenha(string senhaCripto, string senhaDigitada) {
}

public string CriptografarSenha(string senha) {
byte[] salt;
new RNGCryptoServiceProvider().GetBytes(salt = new byte[16]);
var pbkdf2 = new Rfc2898DeriveBytes(senha, salt, 100000);
byte[] salt = RandomNumberGenerator.GetBytes(16);
var pbkdf2 = new Rfc2898DeriveBytes(senha, salt, 100000, HashAlgorithmName.SHA256);
byte[] hash = pbkdf2.GetBytes(20);
byte[] hashBytes = new byte[36];
Array.Copy(salt, 0, hashBytes, 0, 16);
Expand Down
90 changes: 0 additions & 90 deletions Startup.cs

This file was deleted.

33 changes: 33 additions & 0 deletions app.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "solidInCsharp", "solidInCsharp.csproj", "{25E7D50D-20BA-4BBC-8618-3FB9548E6EC4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{BC70228D-2A11-4108-8ABA-A1B271468DE8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "solidInCsharp.Tests", "tests\solidInCsharp.Tests\solidInCsharp.Tests.csproj", "{5E528372-8332-4961-BF16-04AD3A5D2765}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{25E7D50D-20BA-4BBC-8618-3FB9548E6EC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25E7D50D-20BA-4BBC-8618-3FB9548E6EC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25E7D50D-20BA-4BBC-8618-3FB9548E6EC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25E7D50D-20BA-4BBC-8618-3FB9548E6EC4}.Release|Any CPU.Build.0 = Release|Any CPU
{5E528372-8332-4961-BF16-04AD3A5D2765}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E528372-8332-4961-BF16-04AD3A5D2765}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E528372-8332-4961-BF16-04AD3A5D2765}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E528372-8332-4961-BF16-04AD3A5D2765}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5E528372-8332-4961-BF16-04AD3A5D2765} = {BC70228D-2A11-4108-8ABA-A1B271468DE8}
EndGlobalSection
EndGlobal
13 changes: 8 additions & 5 deletions solidInCsharp.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
</ItemGroup>


<ItemGroup>
<Compile Remove="tests\**" />
</ItemGroup>

</Project>
58 changes: 58 additions & 0 deletions tests/solidInCsharp.Tests/CriptografiaServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using solidInCsharp.Service;
using Xunit;

namespace solidInCsharp.Tests
{
public class CriptografiaServiceTests
{
private readonly ICriptografiaService _criptografiaService;

public CriptografiaServiceTests()
{
_criptografiaService = new CriptografiaService();
}

[Fact]
public void CriptografarSenha_DeveRetornarUmHashValido()
{
// Arrange
var senha = "minhaSenhaSuperSecreta";

// Act
var hash = _criptografiaService.CriptografarSenha(senha);

// Assert
Assert.NotNull(hash);
Assert.NotEmpty(hash);
}

[Fact]
public void ValidarSenha_DeveRetornarVerdadeiroParaSenhaCorreta()
{
// Arrange
var senha = "minhaSenhaSuperSecreta";
var hash = _criptografiaService.CriptografarSenha(senha);

// Act
var resultado = _criptografiaService.ValidarSenha(hash, senha);

// Assert
Assert.True(resultado);
}

[Fact]
public void ValidarSenha_DeveRetornarFalsoParaSenhaIncorreta()
{
// Arrange
var senhaCorreta = "minhaSenhaSuperSecreta";
var senhaIncorreta = "senhaIncorreta";
var hash = _criptografiaService.CriptografarSenha(senhaCorreta);

// Act
var resultado = _criptografiaService.ValidarSenha(hash, senhaIncorreta);

// Assert
Assert.False(resultado);
}
}
}
40 changes: 40 additions & 0 deletions tests/solidInCsharp.Tests/UsuarioServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Moq;
using solidInCsharp.Model;
using solidInCsharp.Repository;
using solidInCsharp.Service;
using Xunit;

namespace solidInCsharp.Tests
{
public class UsuarioServiceTests
{
private readonly Mock<IUsuarioRepository> _usuarioRepositoryMock;
private readonly Mock<ICriptografiaService> _criptografiaServiceMock;
private readonly Mock<IJWTService> _jwtServiceMock;
private readonly IUsuarioService _usuarioService;

public UsuarioServiceTests()
{
_usuarioRepositoryMock = new Mock<IUsuarioRepository>();
_criptografiaServiceMock = new Mock<ICriptografiaService>();
_jwtServiceMock = new Mock<IJWTService>();
_usuarioService = new UsuarioService(_usuarioRepositoryMock.Object, _criptografiaServiceMock.Object, _jwtServiceMock.Object);
}

[Fact]
public void CriarUsuario_DeveChamarRepositorioParaSalvarUsuario()
{
// Arrange
var email = "[email protected]";
var nome = "Teste";
var senha = "123";
_criptografiaServiceMock.Setup(x => x.CriptografarSenha(It.IsAny<string>())).Returns("senhaCriptografada");

// Act
_usuarioService.CriarUsuario(email, nome, senha);

// Assert
_usuarioRepositoryMock.Verify(x => x.Add(It.IsAny<Usuario>()), Times.Once);
}
}
}
28 changes: 28 additions & 0 deletions tests/solidInCsharp.Tests/solidInCsharp.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\solidInCsharp.csproj" />
</ItemGroup>

</Project>