-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1a6d52
commit 0041d3e
Showing
19 changed files
with
353 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# This workflow will build a .NET project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net | ||
|
||
name: test | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
test: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v3 | ||
with: | ||
dotnet-version: 7.0.x | ||
- name: Setup SQL Server | ||
run: docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=strong_password" -e "MSSQL_USER=nice_user" -p 1433:1433 --name=sqlserver -d mcr.microsoft.com/mssql/server:2022-latest | ||
- name: Restore dependencies | ||
run: dotnet restore | ||
- name: Setup "testing.json" | ||
run: | | ||
cd SuperHeroAPI | ||
echo '{ "Database": { "URL": "Server=sqlserver,1433;User ID=nice_user;password=strong_password;Database=superhero_testing;Trusted_Connection=True;TrustServerCertificate=True;" } }' > testing.json | ||
ls -lha | ||
- name: Build | ||
run: dotnet build --no-restore | ||
- name: Test | ||
run: dotnet test --no-build --verbosity normal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SuperHeroAPI.Models.Request; | ||
using SuperHeroAPI.Models.Response; | ||
using SuperHeroAPI.Services; | ||
using System.Dynamic; | ||
using System.Net.Http.Json; | ||
|
||
namespace IntegrationTests | ||
{ | ||
public class AuthenticationTests | ||
{ | ||
[Fact] | ||
public async void Signup() | ||
{ | ||
// Arrange | ||
var application = new SuperHeroWebApplicationFactory(); | ||
using (var scope = application.Services.CreateScope()) | ||
{ | ||
AuthenticationRequest request = new AuthenticationRequest(); | ||
request.Username = "user"; | ||
request.Password = "password"; | ||
|
||
var client = application.CreateClient(); | ||
|
||
// Act | ||
var response = await client.PostAsJsonAsync("/auth/signup", request); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
|
||
var responseBody = await response.Content.ReadAsStringAsync(); | ||
responseBody.Should().NotBeNullOrEmpty(); | ||
var tokenService = scope.ServiceProvider.GetRequiredService<TokenService>(); | ||
tokenService.IsValidToken(responseBody).Should().BeTrue(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async void Signin() | ||
{ | ||
// Arrange | ||
var application = new SuperHeroWebApplicationFactory(); | ||
using (var scope = application.Services.CreateScope()) | ||
{ | ||
AuthenticationRequest request = new AuthenticationRequest(); | ||
request.Username = "eduardo"; | ||
request.Password = "senha"; | ||
|
||
var client = application.CreateClient(); | ||
|
||
// Act | ||
var response = await client.PostAsJsonAsync("/auth/signin", request); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
|
||
var responseBody = await response.Content.ReadAsStringAsync(); | ||
responseBody.Should().NotBeNullOrEmpty(); | ||
var tokenService = scope.ServiceProvider.GetRequiredService<TokenService>(); | ||
tokenService.IsValidToken(responseBody).Should().BeTrue(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async void Signin_WrongPassword() | ||
{ | ||
// Arrange | ||
var application = new SuperHeroWebApplicationFactory(); | ||
using (var scope = application.Services.CreateScope()) | ||
{ | ||
AuthenticationRequest request = new AuthenticationRequest(); | ||
request.Username = "eduardo"; | ||
request.Password = "password"; | ||
|
||
var client = application.CreateClient(); | ||
|
||
// Act | ||
var response = await client.PostAsJsonAsync("/auth/signin", request); | ||
|
||
// Assert | ||
response.StatusCode.Should().Be(System.Net.HttpStatusCode.BadRequest); | ||
|
||
var responseBody = await response.Content.ReadFromJsonAsync<ErrorResponse>(); | ||
responseBody.Should().NotBeNull(); | ||
responseBody!.Message.Should().Be("Invalid credentials."); | ||
} | ||
} | ||
|
||
|
||
[Fact] | ||
public async void Signup_UsernameAlreadyTaken() | ||
{ | ||
// Arrange | ||
var application = new SuperHeroWebApplicationFactory(); | ||
using (var scope = application.Services.CreateScope()) | ||
{ | ||
AuthenticationRequest request = new AuthenticationRequest(); | ||
request.Username = "eduardo"; | ||
request.Password = "password"; | ||
|
||
var client = application.CreateClient(); | ||
|
||
// Act | ||
var response = await client.PostAsJsonAsync("/auth/signup", request); | ||
|
||
// Assert | ||
response.StatusCode.Should().Be(System.Net.HttpStatusCode.BadRequest); | ||
|
||
var responseBody = await response.Content.ReadFromJsonAsync<ErrorResponse>(); | ||
responseBody.Should().NotBeNull(); | ||
responseBody!.Message.Should().Be("Username already taken."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using SuperHeroAPI.Data; | ||
using SuperHeroAPI.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IntegrationTests | ||
{ | ||
internal class DatabaseUtil | ||
{ | ||
private DatabaseUtil() { } | ||
|
||
public static void Seed(DataContext context) | ||
{ | ||
User user = new User | ||
{ | ||
Username = "eduardo", | ||
Password = BCrypt.Net.BCrypt.HashPassword("senha") | ||
}; | ||
context.User.Add(user); | ||
|
||
context.SaveChanges(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.11" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.11" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SuperHeroAPI\SuperHeroAPI.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using SuperHeroAPI.Data; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IntegrationTests | ||
{ | ||
internal class SuperHeroWebApplicationFactory : WebApplicationFactory<Program> | ||
{ | ||
protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
{ | ||
builder.ConfigureTestServices(services => | ||
{ | ||
services.RemoveAll(typeof(DbContextOptions<DataContext>)); | ||
services.AddDbContext<DataContext>(options => { | ||
options.UseSqlServer(getDatabaseUrl()); | ||
}); | ||
var dbContext = CreateDbContext(services); | ||
dbContext.Database.EnsureDeleted(); | ||
dbContext.Database.EnsureCreated(); | ||
DatabaseUtil.Seed(dbContext); | ||
}); | ||
} | ||
|
||
private static string getDatabaseUrl() | ||
{ | ||
var config = new ConfigurationBuilder() | ||
.AddEnvironmentVariables() | ||
.AddJsonFile("testing.json", optional: false) | ||
.Build(); | ||
|
||
var url = config["Database:URL"]!; | ||
|
||
return url; | ||
} | ||
|
||
private static DataContext CreateDbContext(IServiceCollection services) | ||
{ | ||
var serviceProvider = services.BuildServiceProvider(); | ||
var scope = serviceProvider.CreateScope(); | ||
var dbContext = scope.ServiceProvider.GetRequiredService<DataContext>(); | ||
|
||
return dbContext; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# SuperHeroAPI | ||
|
||
> API inspired by [this video](https://youtu.be/8pH5Lv4d5-g) | ||
> Swagger: http://localhost:5015/swagger | ||
## Routes | ||
|
||
| Description | Method | URL | Body | | ||
|:---------------------------|:------:|:-------------------|:-------------------------------------------------------------------------------------------------------| | ||
| Sign in | POST | /auth/signin | { "username": "username", "password": "password" } | | ||
| Sign up | POST | /auth/signup | { "username": "username", "password": "password" } | | ||
| Index user super heroes | GET | /superhero | | | ||
| Show user super hero | GET | /superhero/:id | | | ||
| Create user super hero | POST | /superhero | { "name": "string", "firstName": "string", "lastName": "string", "place": "string" } | | ||
| Update user super hero | PUT | /superhero/:id | { "name": "string", "firstName": "string", "lastName": "string", "place": "string" } | | ||
| Delete user super hero | DELETE | /superhero/:id | | | ||
|
||
## Useful commands | ||
|
||
1. If `dotnet-ef` is not installed: `dotnet tool install --global dotnet-ef` | ||
0. Run migrations: `dotnet ef database update` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace SuperHeroAPI.Models.Request | ||
{ | ||
public class AuthenticationRequest | ||
{ | ||
public string Username { get; set; } = string.Empty; | ||
public string Password { get; set; } = string.Empty; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace SuperHeroAPI.Models.Response | ||
{ | ||
public class ErrorResponse | ||
{ | ||
public string Message { get; set; } | ||
|
||
public ErrorResponse(string message) | ||
{ | ||
this.Message = message; | ||
} | ||
} | ||
} |
Oops, something went wrong.