Compare commits
137 Commits
@ -0,0 +1,114 @@
|
|||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: default
|
||||||
|
|
||||||
|
trigger:
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: build
|
||||||
|
image: mcr.microsoft.com/dotnet/sdk:7.0
|
||||||
|
commands:
|
||||||
|
- cd Project/adminBlazor/
|
||||||
|
- dotnet restore adminBlazor.sln
|
||||||
|
- dotnet build adminBlazor.sln -c Release --no-restore
|
||||||
|
|
||||||
|
- name: tests
|
||||||
|
image: mcr.microsoft.com/dotnet/sdk:7.0
|
||||||
|
commands:
|
||||||
|
- cd Project/adminBlazor
|
||||||
|
- dotnet restore adminBlazor.sln
|
||||||
|
- dotnet test adminBlazor.sln --no-restore
|
||||||
|
depends_on: [build]
|
||||||
|
|
||||||
|
- name: code-analysis
|
||||||
|
image: hub.codefirst.iut.uca.fr/marc.chevaldonne/codefirst-dronesonarplugin-dotnet7
|
||||||
|
environment:
|
||||||
|
project_key: SAE_2A_Anglais_Blazor
|
||||||
|
sonar_host: https://codefirst.iut.uca.fr/sonar/
|
||||||
|
sonar_token:
|
||||||
|
from_secret: SECRET_SONAR_LOGIN
|
||||||
|
commands:
|
||||||
|
- cd Project/adminBlazor
|
||||||
|
- dotnet restore adminBlazor.sln
|
||||||
|
- dotnet sonarscanner begin /k:$${project_key} /d:sonar.host.url=$${sonar_host} /d:sonar.login=$${sonar_token}
|
||||||
|
- dotnet build adminBlazor.sln -c Release --no-restore
|
||||||
|
- dotnet sonarscanner end /d:sonar.login=$${sonar_token}
|
||||||
|
depends_on: [tests]
|
||||||
|
|
||||||
|
- name: generate-and-deploy-docs
|
||||||
|
image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-docdeployer
|
||||||
|
failure: ignore
|
||||||
|
volumes:
|
||||||
|
- name: docs
|
||||||
|
path: /docs
|
||||||
|
commands:
|
||||||
|
- /entrypoint.sh
|
||||||
|
when:
|
||||||
|
branch:
|
||||||
|
- blazor
|
||||||
|
- blazor-ci
|
||||||
|
depends_on: [ build ]
|
||||||
|
|
||||||
|
- name: docker-build-api
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
dockerfile: Project/adminBlazor/adminBlazor.Api/Dockerfile
|
||||||
|
context: Project/adminBlazor/
|
||||||
|
registry: hub.codefirst.iut.uca.fr
|
||||||
|
repo: hub.codefirst.iut.uca.fr/antoine.jourdain/sae_2a_anglais
|
||||||
|
username:
|
||||||
|
from_secret: SECRET_REGISTRY_USERNAME
|
||||||
|
password:
|
||||||
|
from_secret: SECRET_REGISTRY_PASSWORD
|
||||||
|
when:
|
||||||
|
branch:
|
||||||
|
- blazor
|
||||||
|
- blazor-ci
|
||||||
|
|
||||||
|
- name: deploy-container-api
|
||||||
|
image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
|
||||||
|
environment:
|
||||||
|
IMAGENAME: hub.codefirst.iut.uca.fr/antoine.jourdain/sae_2a_anglais:latest
|
||||||
|
CONTAINERNAME: api-in-english-please
|
||||||
|
COMMAND: create
|
||||||
|
OVERWRITE: true
|
||||||
|
depends_on: [ docker-build-api ]
|
||||||
|
when:
|
||||||
|
branch:
|
||||||
|
- blazor
|
||||||
|
- blazor-ci
|
||||||
|
|
||||||
|
- name: docker-build-app
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
dockerfile: Project/adminBlazor/adminBlazor/Dockerfile
|
||||||
|
context: Project/adminBlazor/
|
||||||
|
registry: hub.codefirst.iut.uca.fr
|
||||||
|
repo: hub.codefirst.iut.uca.fr/antoine.jourdain/sae_2a_anglais
|
||||||
|
username:
|
||||||
|
from_secret: SECRET_REGISTRY_USERNAME
|
||||||
|
password:
|
||||||
|
from_secret: SECRET_REGISTRY_PASSWORD
|
||||||
|
when:
|
||||||
|
branch:
|
||||||
|
- blazor
|
||||||
|
- blazor-ci
|
||||||
|
|
||||||
|
- name: deploy-container-app
|
||||||
|
image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
|
||||||
|
environment:
|
||||||
|
IMAGENAME: hub.codefirst.iut.uca.fr/antoine.jourdain/sae_2a_anglais:latest
|
||||||
|
CONTAINERNAME: in-english-please
|
||||||
|
COMMAND: create
|
||||||
|
OVERWRITE: true
|
||||||
|
depends_on: [ docker-build-app ]
|
||||||
|
when:
|
||||||
|
branch:
|
||||||
|
- blazor
|
||||||
|
- blazor-ci
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
- name: docs
|
||||||
|
temp: {}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,30 @@
|
|||||||
|
**/.classpath
|
||||||
|
**/.dockerignore
|
||||||
|
**/.env
|
||||||
|
**/.git
|
||||||
|
**/.gitignore
|
||||||
|
**/.project
|
||||||
|
**/.settings
|
||||||
|
**/.toolstarget
|
||||||
|
**/.vs
|
||||||
|
**/.vscode
|
||||||
|
**/*.*proj.user
|
||||||
|
**/*.dbmdl
|
||||||
|
**/*.jfm
|
||||||
|
**/azds.yaml
|
||||||
|
**/bin
|
||||||
|
**/charts
|
||||||
|
**/docker-compose*
|
||||||
|
**/Dockerfile*
|
||||||
|
**/node_modules
|
||||||
|
**/npm-debug.log
|
||||||
|
**/obj
|
||||||
|
**/secrets.dev.yaml
|
||||||
|
**/values.dev.yaml
|
||||||
|
LICENSE
|
||||||
|
README.md
|
||||||
|
!**/.gitignore
|
||||||
|
!.git/HEAD
|
||||||
|
!.git/config
|
||||||
|
!.git/packed-refs
|
||||||
|
!.git/refs/heads/**
|
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup>
|
||||||
|
<ActiveDebugProfile>https</ActiveDebugProfile>
|
||||||
|
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
|
||||||
|
<Controller_SelectedScaffolderCategoryPath>root/Common/MVC/Controller</Controller_SelectedScaffolderCategoryPath>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
||||||
|
</PropertyGroup>
|
||||||
|
</Project>
|
@ -0,0 +1,6 @@
|
|||||||
|
@API_HostAddress = http://localhost:5124
|
||||||
|
|
||||||
|
GET {{API_HostAddress}}/weatherforecast/
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,30 @@
|
|||||||
|
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||||
|
USER app
|
||||||
|
WORKDIR /app
|
||||||
|
EXPOSE 8080
|
||||||
|
EXPOSE 8081
|
||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
WORKDIR /src
|
||||||
|
COPY ["API/API.csproj", "API/"]
|
||||||
|
COPY ["DTOToEntity/DTOToEntity.csproj", "DTOToEntity/"]
|
||||||
|
COPY ["DbContext/DbContextLib.csproj", "DbContext/"]
|
||||||
|
COPY ["Entities/Entities.csproj", "Entities/"]
|
||||||
|
COPY ["DTO/DTO.csproj", "DTO/"]
|
||||||
|
COPY ["StubbedContext/StubbedContextLib.csproj", "StubbedContext/"]
|
||||||
|
RUN dotnet restore "./API/./API.csproj"
|
||||||
|
COPY . .
|
||||||
|
WORKDIR "/src/API"
|
||||||
|
RUN dotnet build "./API.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||||
|
|
||||||
|
FROM build AS publish
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
RUN dotnet publish "./API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||||
|
|
||||||
|
FROM base AS final
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=publish /app/publish .
|
||||||
|
ENTRYPOINT ["dotnet", "API.dll"]
|
@ -0,0 +1,117 @@
|
|||||||
|
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using StubbedContextLib;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.SqlServer;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Entities;
|
||||||
|
using DTOToEntity;
|
||||||
|
using DTO;
|
||||||
|
using System;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using DbContextLib;
|
||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.OpenApi.Models;
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// Add services to the container.
|
||||||
|
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
|
||||||
|
builder.Services.AddDbContext<StubbedContext>(options =>
|
||||||
|
{
|
||||||
|
options.UseSqlServer(builder.Configuration.GetConnectionString("StubbedContext"));
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.Services.AddSwaggerGen(option =>
|
||||||
|
{
|
||||||
|
option.SwaggerDoc("v1", new OpenApiInfo { Title = "Test API", Version = "v1" });
|
||||||
|
option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
In = ParameterLocation.Header,
|
||||||
|
Description = "Please enter a valid token",
|
||||||
|
Name = "Authorization",
|
||||||
|
Type = SecuritySchemeType.Http,
|
||||||
|
BearerFormat = "JWT",
|
||||||
|
Scheme = "Bearer"
|
||||||
|
});
|
||||||
|
option.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||||
|
{
|
||||||
|
{
|
||||||
|
new OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Reference = new OpenApiReference
|
||||||
|
{
|
||||||
|
Type=ReferenceType.SecurityScheme,
|
||||||
|
Id="Bearer"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new string[]{}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
var validIssuer = builder.Configuration.GetValue<string>("JwtTokenSettings:ValidIssuer");
|
||||||
|
var validAudience = builder.Configuration.GetValue<string>("JwtTokenSettings:ValidAudience");
|
||||||
|
var symmetricSecurityKey = builder.Configuration.GetValue<string>("JwtTokenSettings:SymmetricSecurityKey");
|
||||||
|
|
||||||
|
builder.Services.AddAuthentication(options => {
|
||||||
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||||
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||||
|
//options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||||
|
})
|
||||||
|
.AddJwtBearer(options =>
|
||||||
|
{
|
||||||
|
options.IncludeErrorDetails = true;
|
||||||
|
options.TokenValidationParameters = new TokenValidationParameters()
|
||||||
|
{
|
||||||
|
ValidateIssuer = true,
|
||||||
|
ValidateAudience = true,
|
||||||
|
ValidateLifetime = true,
|
||||||
|
ValidateIssuerSigningKey = true,
|
||||||
|
ValidIssuer = validIssuer,
|
||||||
|
ValidAudience = validAudience,
|
||||||
|
IssuerSigningKey = new SymmetricSecurityKey(
|
||||||
|
Encoding.UTF8.GetBytes(symmetricSecurityKey)
|
||||||
|
),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
builder.Services.AddApiVersioning(o =>
|
||||||
|
{
|
||||||
|
o.DefaultApiVersion = new ApiVersion(1, 1);
|
||||||
|
o.AssumeDefaultVersionWhenUnspecified = true;
|
||||||
|
o.ReportApiVersions = true;
|
||||||
|
});
|
||||||
|
builder.Services.AddScoped<IGroupService, GroupService>();
|
||||||
|
builder.Services.AddScoped<IService<LangueDTO>,LangueService>();
|
||||||
|
builder.Services.AddScoped<IService<RoleDTO>,RoleService>();
|
||||||
|
builder.Services.AddScoped<ITranslateService,TranslateService>();
|
||||||
|
builder.Services.AddScoped<IUserService, UserService>();
|
||||||
|
builder.Services.AddScoped<IVocabularyService, VocabularyService>();
|
||||||
|
builder.Services.AddScoped<IVocabularyListService, VocabularyListService>();
|
||||||
|
var app = builder.Build();
|
||||||
|
// Configure the HTTP request pipeline.
|
||||||
|
if (app.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseAuthentication();
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.Run();
|
@ -0,0 +1,52 @@
|
|||||||
|
{
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
},
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"applicationUrl": "http://localhost:5124"
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
},
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"applicationUrl": "https://localhost:7013;http://localhost:5124"
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Docker": {
|
||||||
|
"commandName": "Docker",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_HTTPS_PORTS": "8081",
|
||||||
|
"ASPNETCORE_HTTP_PORTS": "8080"
|
||||||
|
},
|
||||||
|
"publishAllPorts": true,
|
||||||
|
"useSSL": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:32547",
|
||||||
|
"sslPort": 44345
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
namespace API
|
||||||
|
{
|
||||||
|
public class WeatherForecast
|
||||||
|
{
|
||||||
|
public DateOnly Date { get; set; }
|
||||||
|
|
||||||
|
public int TemperatureC { get; set; }
|
||||||
|
|
||||||
|
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||||
|
|
||||||
|
public string? Summary { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"JwtTokenSettings": {
|
||||||
|
"ValidIssuer": "ExampleIssuer",
|
||||||
|
"ValidAudience": "ValidAudience",
|
||||||
|
"SymmetricSecurityKey": "fvh8456477hth44j6wfds98bq9hp8bqh9ubq9gjig3qr0[94vj5",
|
||||||
|
"JwtRegisteredClaimNamesSub": "345h098bb8reberbwr4vvb8945"
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
@ -0,0 +1,296 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using StubbedContextLib;
|
||||||
|
using API.Controllers;
|
||||||
|
using DTOToEntity;
|
||||||
|
using DTO;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace TU
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class GroupTU
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestGetGroups()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
GroupEntity g1 = new GroupEntity { Id = 3, Num = 1, sector = "sect3", year = 2020 };
|
||||||
|
await context.Groups.AddAsync(g1);
|
||||||
|
await context.Groups.AddAsync(new GroupEntity { Id = 4, Num = 2, sector = "sect4" });
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Créer un mock pour le logger
|
||||||
|
var mockLogger = new Mock<ILogger<GroupController>>();
|
||||||
|
|
||||||
|
var controller = new GroupController(new GroupService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var result = await controller.GetGroups(0, 5);
|
||||||
|
|
||||||
|
Assert.IsNotNull(result.Value.Items.ToArray());
|
||||||
|
Assert.AreEqual(3, result.Value.TotalCount);
|
||||||
|
Assert.AreEqual(g1.Num, result.Value.Items.First().Num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestGetGroup()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
GroupEntity g1 = new GroupEntity { Id = 3, Num = 1, sector = "sect3", year = 2020 };
|
||||||
|
await context.Groups.AddAsync(g1);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Créer un mock pour le logger
|
||||||
|
var mockLogger = new Mock<ILogger<GroupController>>();
|
||||||
|
|
||||||
|
var controller = new GroupController(new GroupService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var result = await controller.GetGroup(3);
|
||||||
|
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(g1.Id, result.Value.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestAddGroup()
|
||||||
|
{
|
||||||
|
// Créer une connexion SQLite en mémoire
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
// Créer un mock pour le logger
|
||||||
|
var mockLogger = new Mock<ILogger<GroupController>>();
|
||||||
|
|
||||||
|
var controller = new GroupController(new GroupService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var newGroupDTO = new GroupDTO { Id = 0, Num = 3, sector = "sect5", Year = 2022 };
|
||||||
|
|
||||||
|
var result = await controller.AddGroup(newGroupDTO);
|
||||||
|
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(newGroupDTO.Num, result.Value.Num);
|
||||||
|
Assert.AreEqual(newGroupDTO.sector, result.Value.sector);
|
||||||
|
Assert.AreEqual(newGroupDTO.Year, result.Value.Year);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestDeleteGroup()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
GroupEntity g1 = new GroupEntity { Id = 3, Num = 1, sector = "sect3", year = 2020 };
|
||||||
|
await context.Groups.AddAsync(g1);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var mockLogger = new Mock<ILogger<GroupController>>();
|
||||||
|
|
||||||
|
var controller = new GroupController(new GroupService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var result = await controller.DeleteGroup(3);
|
||||||
|
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(g1.Num, result.Value.Num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestUpdateGroup()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
GroupEntity g1 = new GroupEntity { Id = 4, Num = 4, sector = "sect3", year = 2020 };
|
||||||
|
GroupDTO updatedGroupDTO = new GroupDTO { Id = 4, Num = 2, sector = "sect4", Year = 2021 };
|
||||||
|
await context.Groups.AddAsync(g1);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var mockLogger = new Mock<ILogger<GroupController>>();
|
||||||
|
var controller = new GroupController(new GroupService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var result = await controller.UpdateGroup(updatedGroupDTO);
|
||||||
|
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(updatedGroupDTO.Num, result.Value.Num);
|
||||||
|
Assert.AreEqual(updatedGroupDTO.sector, result.Value.sector);
|
||||||
|
Assert.AreEqual(updatedGroupDTO.Year, result.Value.Year);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestGetGroupsByNum()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
await context.Groups.AddRangeAsync(
|
||||||
|
new GroupEntity { Id = 3, Num = 10, sector = "sect1", year = 2021 },
|
||||||
|
new GroupEntity { Id = 4, Num = 2, sector = "sect2", year = 2021 },
|
||||||
|
new GroupEntity { Id = 5, Num = 10, sector = "sect1", year = 2022 }
|
||||||
|
);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var mockLogger = new Mock<ILogger<GroupController>>();
|
||||||
|
|
||||||
|
var controller = new GroupController(new GroupService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var result = await controller.GetGroupsByNum(0, 5, 10);
|
||||||
|
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(2, result.Value.Items.Count());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestGetGroupsBySector()
|
||||||
|
{
|
||||||
|
// Créer une connexion SQLite en mémoire
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
await context.Groups.AddRangeAsync(
|
||||||
|
new GroupEntity { Id = 3, Num = 1, sector = "sect1", year = 2021 },
|
||||||
|
new GroupEntity { Id = 4, Num = 2, sector = "sect2", year = 2021 },
|
||||||
|
new GroupEntity { Id = 5, Num = 1, sector = "sect1", year = 2022 }
|
||||||
|
);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var mockLogger = new Mock<ILogger<GroupController>>();
|
||||||
|
|
||||||
|
var controller = new GroupController(new GroupService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var result = await controller.GetGroupsBySector(0, 5, "sect1");
|
||||||
|
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(2, result.Value.Items.Count());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestGetGroupsByYear()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
await context.Groups.AddRangeAsync(
|
||||||
|
new GroupEntity { Id = 3, Num = 1, sector = "sect1", year = 2021 },
|
||||||
|
new GroupEntity { Id = 4, Num = 2, sector = "sect2", year = 2021 },
|
||||||
|
new GroupEntity { Id = 5, Num = 1, sector = "sect1", year = 2022 }
|
||||||
|
);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var mockLogger = new Mock<ILogger<GroupController>>();
|
||||||
|
|
||||||
|
var controller = new GroupController(new GroupService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var result = await controller.GetGroupsByYear(0, 5, 2021);
|
||||||
|
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(2, result.Value.Items.Count());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestAddUserToGroup()
|
||||||
|
{
|
||||||
|
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
var mockLogger = new Mock<ILogger<GroupController>>();
|
||||||
|
|
||||||
|
var controller = new GroupController(new GroupService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var result = await controller.AddUserToGroup(1, 1);
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(1, result.Value.Id);
|
||||||
|
|
||||||
|
|
||||||
|
var test = await context.Groups.FirstOrDefaultAsync(g => g.Id == 1);
|
||||||
|
var testUser = await context.Users.FirstOrDefaultAsync(g => g.Id == 1);
|
||||||
|
Assert.IsNotNull(test);
|
||||||
|
Assert.IsNotNull(testUser);
|
||||||
|
Assert.AreEqual(test.Users.First(), testUser);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,128 @@
|
|||||||
|
using API.Controllers;
|
||||||
|
using DbContextLib;
|
||||||
|
using DTOToEntity;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using StubbedContextLib;
|
||||||
|
using DTO;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace TU
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class LangueTU
|
||||||
|
{
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestAddLangue()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
// Créer un mock pour le logger
|
||||||
|
var mockLogger = new Mock<ILogger<LangueController>>();
|
||||||
|
|
||||||
|
var controller = new LangueController(new LangueService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var newLangue = new LangueDTO { name = "test" };
|
||||||
|
|
||||||
|
var result = await controller.AddLangue(newLangue);
|
||||||
|
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(newLangue.name, result.Value.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestDeleteLangue()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
var newLangue = new LangueDTO { name = "test" };
|
||||||
|
await context.Langues.AddAsync(newLangue.ToEntity());
|
||||||
|
// Créer un mock pour le logger
|
||||||
|
var mockLogger = new Mock<ILogger<LangueController>>();
|
||||||
|
|
||||||
|
var controller = new LangueController(new LangueService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
|
||||||
|
var result = await controller.DeleteLangue("test");
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(newLangue.name, result.Value.name);
|
||||||
|
var res = await context.Langues.FirstOrDefaultAsync(l => l.name == "test");
|
||||||
|
Assert.IsNull(res);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestGetById()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
var newLangue = new LangueDTO { name = "test" };
|
||||||
|
await context.Langues.AddAsync(newLangue.ToEntity());
|
||||||
|
// Créer un mock pour le logger
|
||||||
|
var mockLogger = new Mock<ILogger<LangueController>>();
|
||||||
|
|
||||||
|
var controller = new LangueController(new LangueService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
|
||||||
|
var result = await controller.GetLangue("test");
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(newLangue.name, result.Value.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestGetGroups()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
var mockLogger = new Mock<ILogger<LangueController>>();
|
||||||
|
|
||||||
|
var controller = new LangueController(new LangueService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
|
||||||
|
var result = await controller.GetLangues(0, 5);
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(2, result.Value.TotalCount);
|
||||||
|
Assert.AreEqual("English", result.Value.Items.ToList()[0].name);
|
||||||
|
Assert.AreEqual("French", result.Value.Items.ToList()[1].name);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,122 @@
|
|||||||
|
using API.Controllers;
|
||||||
|
using DbContextLib;
|
||||||
|
using DTO;
|
||||||
|
using DTOToEntity;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using StubbedContextLib;
|
||||||
|
|
||||||
|
namespace TU
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class RoleTU
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestAddRole()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
var mockLogger = new Mock<ILogger<RoleController>>();
|
||||||
|
|
||||||
|
var controller = new RoleController(new RoleService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var newRole = new RoleDTO { Id = 1, Name = "test" };
|
||||||
|
|
||||||
|
var result = await controller.AddRole(newRole);
|
||||||
|
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
//ici on met 4 pour verifier que le Id n'est pas celui que l'on donne mais bien : CountList + 1
|
||||||
|
Assert.AreEqual(4, result.Value.Id);
|
||||||
|
Assert.AreEqual("test", result.Value.Name);
|
||||||
|
var test = context.Roles.Find((long)4);
|
||||||
|
Assert.IsNotNull(test);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestDeleteRole()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
var mockLogger = new Mock<ILogger<RoleController>>();
|
||||||
|
|
||||||
|
var controller = new RoleController(new RoleService(context), mockLogger.Object);
|
||||||
|
var result = await controller.DeleteRole(1);
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(1, result.Value.Id);
|
||||||
|
Assert.AreEqual("Admin", result.Value.Name);
|
||||||
|
var test = context.Roles.Find((long)1);
|
||||||
|
Assert.IsNull(test);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestGetRole()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
var mockLogger = new Mock<ILogger<RoleController>>();
|
||||||
|
|
||||||
|
var controller = new RoleController(new RoleService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
|
||||||
|
var result = await controller.GetRole((long)1);
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(1, result.Value.Id);
|
||||||
|
Assert.AreEqual("Admin", result.Value.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestGetRoles()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<SAEContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
var mockLogger = new Mock<ILogger<RoleController>>();
|
||||||
|
|
||||||
|
var controller = new RoleController(new RoleService(context), mockLogger.Object);
|
||||||
|
|
||||||
|
var result = await controller.GetRoles(0, 5);
|
||||||
|
Assert.IsNotNull(result.Value);
|
||||||
|
Assert.AreEqual(3, result.Value.TotalCount);
|
||||||
|
Assert.AreEqual(3, result.Value.Items.ToList()[2].Id);
|
||||||
|
Assert.AreEqual("Student", result.Value.Items.ToList()[2].Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue