Compare commits
14 Commits
Author | SHA1 | Date |
---|---|---|
|
ba5f3e8b9a | 1 year ago |
|
1c0828e644 | 1 year ago |
|
611b39bba7 | 1 year ago |
|
46247dbe57 | 1 year ago |
|
dfd4b23cf5 | 1 year ago |
|
9d2c1a7963 | 1 year ago |
|
849dd23da8 | 1 year ago |
|
05cdfc4c6c | 1 year ago |
|
54fdb89520 | 1 year ago |
|
0d6511787e | 1 year ago |
|
034deb078c | 1 year ago |
|
f40fc50e75 | 1 year ago |
|
c55fd18aa2 | 1 year ago |
|
ee38b16662 | 1 year ago |
@ -1,7 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="UserContentModel">
|
<component name="UserContentModel">
|
||||||
<attachedFolders />
|
<attachedFolders>
|
||||||
|
<Path>../../Blazor_SAE</Path>
|
||||||
|
</attachedFolders>
|
||||||
<explicitIncludes />
|
<explicitIncludes />
|
||||||
<explicitExcludes />
|
<explicitExcludes />
|
||||||
</component>
|
</component>
|
||||||
|
@ -0,0 +1,105 @@
|
|||||||
|
using System.Security.Claims;
|
||||||
|
using Moq;
|
||||||
|
using VeraxShield.composants.formulaires.modeles;
|
||||||
|
using VeraxShield.modele.utilisateurs;
|
||||||
|
using VeraxShield.services.UtilisateursDataService;
|
||||||
|
|
||||||
|
namespace TestVeraxShield;
|
||||||
|
|
||||||
|
public class AuthentificationServiceTests
|
||||||
|
{
|
||||||
|
private readonly AuthentificationService _authService;
|
||||||
|
private readonly Mock<IUtilisateursDataService> _mockDataService = new Mock<IUtilisateursDataService>();
|
||||||
|
|
||||||
|
public AuthentificationServiceTests()
|
||||||
|
{
|
||||||
|
_authService = new AuthentificationService(_mockDataService.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Connexion_WithValidCredentials_ShouldSucceed()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testUser = new Utilisateur("testUser", "Test", "User", "test@user.com", BCrypt.Net.BCrypt.HashPassword("password"), "User", false);
|
||||||
|
|
||||||
|
_mockDataService.Setup(s => s.getAllUtilisateurs())
|
||||||
|
.ReturnsAsync(new List<Utilisateur> { testUser });
|
||||||
|
|
||||||
|
var requete = new RequeteConnexion
|
||||||
|
{
|
||||||
|
Pseudo = "testUser",
|
||||||
|
MotDePasse = "password"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
await _authService.Connexion(requete);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Connexion_WithInvalidCredentials_ShouldThrowException()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_mockDataService.Setup(s => s.getAllUtilisateurs())
|
||||||
|
.ReturnsAsync(new List<Utilisateur>());
|
||||||
|
|
||||||
|
var requete = new RequeteConnexion
|
||||||
|
{
|
||||||
|
Pseudo = "nonExistentUser",
|
||||||
|
MotDePasse = "wrongPassword"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
await Assert.ThrowsAsync<Exception>(() => _authService.Connexion(requete));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Inscription_ShouldCreateNewUser()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var requete = new RequeteInscription
|
||||||
|
{
|
||||||
|
Pseudo = "newUser",
|
||||||
|
Nom = "New",
|
||||||
|
Prenom = "User",
|
||||||
|
Mail = "new@user.com",
|
||||||
|
MotDePasse = "newPassword"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Setup the mock to verify that AjouterUtilisateur is called with a Utilisateur that matches the inscription details
|
||||||
|
_mockDataService.Setup(s => s.AjouterUtilisateur(It.IsAny<Utilisateur>()))
|
||||||
|
.Returns(Task.CompletedTask)
|
||||||
|
.Callback<Utilisateur>(u =>
|
||||||
|
{
|
||||||
|
Assert.Equal(requete.Pseudo, u.Pseudo);
|
||||||
|
Assert.True(BCrypt.Net.BCrypt.Verify(requete.MotDePasse, u.Mdp));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await _authService.Inscription(requete);
|
||||||
|
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public async Task GetUtilisateur_ReturnsCorrectUser_WithClaims()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expectedPseudo = "testUser";
|
||||||
|
var expectedRole = "User";
|
||||||
|
var testUser = new Utilisateur("testUser", "Test", "User", "test@user.com", BCrypt.Net.BCrypt.HashPassword("password"), "User", false);
|
||||||
|
var mockDataService = new Mock<IUtilisateursDataService>();
|
||||||
|
mockDataService.Setup(s => s.getAllUtilisateurs())
|
||||||
|
.ReturnsAsync(new List<Utilisateur> { testUser });
|
||||||
|
|
||||||
|
var authService = new AuthentificationService(mockDataService.Object);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var utilisateurCourant = await authService.GetUtilisateur(expectedPseudo);
|
||||||
|
utilisateurCourant.Claims.Add(ClaimTypes.Email, "test@user.com");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(utilisateurCourant);
|
||||||
|
Assert.True(utilisateurCourant.EstAuthentifie);
|
||||||
|
Assert.Equal(expectedPseudo, utilisateurCourant.Pseudo);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
using Moq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using VeraxShield.composants.formulaires.modeles;
|
||||||
|
using VeraxShield.modele.utilisateurs;
|
||||||
|
using VeraxShield.services.UtilisateursDataService;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace VeraxShield.UnitTests
|
||||||
|
{
|
||||||
|
public class DonneurEtatTests
|
||||||
|
{
|
||||||
|
private readonly DonneurEtat _donneurEtat;
|
||||||
|
private readonly Mock<IAuthentificationService> _mockAuthService;
|
||||||
|
private readonly Mock<IUtilisateursDataService> _mockUserDataService;
|
||||||
|
|
||||||
|
public DonneurEtatTests()
|
||||||
|
{
|
||||||
|
_mockAuthService = new Mock<IAuthentificationService>();
|
||||||
|
_mockUserDataService = new Mock<IUtilisateursDataService>();
|
||||||
|
_donneurEtat = new DonneurEtat(_mockAuthService.Object, _mockUserDataService.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Connexion_ValidCredentials_SetsCurrentUser()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var requeteConnexion = new RequeteConnexion { Pseudo = "testUser", MotDePasse = "testPass" };
|
||||||
|
var utilisateurCourant = new UtilisateurCourant
|
||||||
|
{
|
||||||
|
Pseudo = "testUser",
|
||||||
|
EstAuthentifie = true,
|
||||||
|
Claims = new Dictionary<string, string> { { ClaimTypes.Role, "User" } }
|
||||||
|
};
|
||||||
|
|
||||||
|
_mockAuthService.Setup(x => x.GetUtilisateur(requeteConnexion.Pseudo)).ReturnsAsync(utilisateurCourant);
|
||||||
|
_mockAuthService.Setup(x => x.Connexion(requeteConnexion)).Returns(Task.CompletedTask);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await _donneurEtat.Connexion(requeteConnexion);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(_donneurEtat._utilisateurCourant);
|
||||||
|
Assert.True(_donneurEtat._utilisateurCourant.EstAuthentifie);
|
||||||
|
_mockAuthService.Verify(x => x.Connexion(requeteConnexion), Times.Once);
|
||||||
|
_mockAuthService.Verify(x => x.GetUtilisateur(requeteConnexion.Pseudo), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Deconnexion_ClearsCurrentUser()
|
||||||
|
{
|
||||||
|
// Arrange - assume user is logged in
|
||||||
|
_donneurEtat._utilisateurCourant = new UtilisateurCourant { EstAuthentifie = true };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await _donneurEtat.Deconnexion();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Null(_donneurEtat._utilisateurCourant);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Inscription_ValidData_RegistersUser()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var requeteInscription = new RequeteInscription
|
||||||
|
{
|
||||||
|
Pseudo = "newUser",
|
||||||
|
MotDePasse = "newPass",
|
||||||
|
Mail = "newUser@test.com",
|
||||||
|
Nom = "New",
|
||||||
|
Prenom = "User"
|
||||||
|
};
|
||||||
|
|
||||||
|
_mockAuthService.Setup(x => x.Inscription(requeteInscription)).Returns(Task.CompletedTask);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await _donneurEtat.Inscription(requeteInscription);
|
||||||
|
|
||||||
|
// Assert - Since Inscription does not automatically log in the user, we check if the method was called.
|
||||||
|
_mockAuthService.Verify(x => x.Inscription(requeteInscription), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
using Moq;
|
||||||
|
using VeraxShield.modele.utilisateurs;
|
||||||
|
using VeraxShield.services.UtilisateursDataService;
|
||||||
|
|
||||||
|
namespace TestVeraxShield;
|
||||||
|
|
||||||
|
public class IAuthentificationServiceTests
|
||||||
|
{
|
||||||
|
private readonly Mock<IUtilisateursDataService> _mockDataService;
|
||||||
|
private readonly AuthentificationService _authService;
|
||||||
|
|
||||||
|
public IAuthentificationServiceTests()
|
||||||
|
{
|
||||||
|
_mockDataService = new Mock<IUtilisateursDataService>();
|
||||||
|
_authService = new AuthentificationService(_mockDataService.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetUtilisateur_ValidUser_ReturnsUser()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expectedUser = new UtilisateurCourant { Pseudo = "user1", EstAuthentifie = true };
|
||||||
|
_mockDataService.Setup(x => x.getAllUtilisateurs()).ReturnsAsync(new List<Utilisateur>
|
||||||
|
{
|
||||||
|
new Utilisateur("user1", "Name", "Surname", "user1@example.com", "password", "User", false)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await _authService.GetUtilisateur("user1");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expectedUser.Pseudo, result.Pseudo);
|
||||||
|
Assert.True(result.EstAuthentifie);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetUtilisateur_NonExistentUser_ThrowsException()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_mockDataService.Setup(x => x.getAllUtilisateurs()).ReturnsAsync(new List<Utilisateur>());
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
await Assert.ThrowsAsync<Exception>(() => _authService.GetUtilisateur("user2"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
using Moq;
|
||||||
|
using VeraxShield.modele.utilisateurs;
|
||||||
|
using VeraxShield.services.UtilisateursDataService;
|
||||||
|
|
||||||
|
namespace TestVeraxShield;
|
||||||
|
|
||||||
|
public class IUtilisateursDataServiceTests
|
||||||
|
{
|
||||||
|
private readonly Mock<IUtilisateursDataService> _service = new Mock<IUtilisateursDataService>();
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AjouterUtilisateur_AddsUserSuccessfully()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var user = new Utilisateur("testUser", "User", "Test", "dez", "password", "User", false);
|
||||||
|
_service.Setup(s => s.AjouterUtilisateur(It.IsAny<Utilisateur>()))
|
||||||
|
.Returns(Task.CompletedTask)
|
||||||
|
.Callback<Utilisateur>(u => Assert.Equal("testUser", u.Pseudo));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await _service.Object.AjouterUtilisateur(user);
|
||||||
|
|
||||||
|
// Assert is handled by the Callback
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
using Moq;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using RichardSzalay.MockHttp;
|
||||||
|
using VeraxShield.modele.utilisateurs;
|
||||||
|
using VeraxShield.services.UtilisateursDataService;
|
||||||
|
|
||||||
|
namespace TestVeraxShield;
|
||||||
|
|
||||||
|
public class UtilisateursDataServiceApiTests
|
||||||
|
{
|
||||||
|
private readonly MockHttpMessageHandler _mockHttp;
|
||||||
|
private readonly HttpClient _clientHttp;
|
||||||
|
private readonly Mock<NavigationManager> _mockNavigationManager;
|
||||||
|
private readonly UtilisateursDataServiceApi _service;
|
||||||
|
|
||||||
|
public UtilisateursDataServiceApiTests()
|
||||||
|
{
|
||||||
|
_mockHttp = new MockHttpMessageHandler();
|
||||||
|
|
||||||
|
// Mock the response for the API call
|
||||||
|
_mockHttp.When("https://Verax.com/api/utilisateurs/recuperer")
|
||||||
|
.Respond("application/json", JsonConvert.SerializeObject(new List<Utilisateur>
|
||||||
|
{
|
||||||
|
new Utilisateur("testUser", "User", "Test", "dez", "password", "User", false)
|
||||||
|
}));
|
||||||
|
|
||||||
|
_clientHttp = _mockHttp.ToHttpClient();
|
||||||
|
_clientHttp.BaseAddress = new System.Uri("https://Verax.com");
|
||||||
|
|
||||||
|
_mockNavigationManager = new Mock<NavigationManager>();
|
||||||
|
_service = new UtilisateursDataServiceApi(_clientHttp, _mockNavigationManager.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAllUtilisateurs_ReturnsUsers()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var users = await _service.getAllUtilisateurs();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Single(users);
|
||||||
|
Assert.Equal("testUser", users[0].Pseudo);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetUtilisateurFromPseudo_ReturnsUser()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var user = await _service.getUtilisateurFromPseudo("testUser");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("testUser", user.Pseudo);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue