Add some Tests ✅
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
034deb078c
commit
0d6511787e
@ -0,0 +1,82 @@
|
|||||||
|
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);
|
||||||
|
|
||||||
|
// Assert is handled by the Callback in the Setup
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue