From 0d6511787eebe99a74f1ac579451df21ce3c7633 Mon Sep 17 00:00:00 2001 From: tonyfages Date: Sun, 7 Apr 2024 14:34:28 +0200 Subject: [PATCH] =?UTF-8?q?Add=20some=20Tests=20=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AuthentificationServiceTests.cs | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 VeraxShield/TestVeraxShield/AuthentificationServiceTests.cs diff --git a/VeraxShield/TestVeraxShield/AuthentificationServiceTests.cs b/VeraxShield/TestVeraxShield/AuthentificationServiceTests.cs new file mode 100644 index 0000000..609cab8 --- /dev/null +++ b/VeraxShield/TestVeraxShield/AuthentificationServiceTests.cs @@ -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 _mockDataService = new Mock(); + + 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 { 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()); + + var requete = new RequeteConnexion + { + Pseudo = "nonExistentUser", + MotDePasse = "wrongPassword" + }; + + // Act & Assert + await Assert.ThrowsAsync(() => _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())) + .Returns(Task.CompletedTask) + .Callback(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 + } + +} \ No newline at end of file