diff --git a/Govor.API.Tests/UnitTests/Services/AdminStuff/InvitationGeneratorTests.cs b/Govor.API.Tests/UnitTests/Services/AdminStuff/InvitationGeneratorTests.cs new file mode 100644 index 0000000..76ecad5 --- /dev/null +++ b/Govor.API.Tests/UnitTests/Services/AdminStuff/InvitationGeneratorTests.cs @@ -0,0 +1,89 @@ +using AutoFixture; +using Govor.API.Services.AdminsStuff; +using Govor.API.Services.AdminsStuff.Interfaces; +using Govor.Core.Models; +using Govor.Core.Repositories.Invaites; +using Moq; + +namespace Govor.API.Tests.UnitTests.Services.AdminStuff; + +[TestFixture] +public class InvitationGeneratorTests +{ + private Fixture _fixture; + private Mock _invitesRepositoryMock; + private IInvitationGenerator _invitationGenerator; + + [SetUp] + public void SetUp() + { + _fixture = new Fixture(); + _fixture.Behaviors.OfType().ToList().ForEach(b => _fixture.Behaviors.Remove(b)); + _fixture.Behaviors.Add(new OmitOnRecursionBehavior()); + + _invitesRepositoryMock = new Mock(); + _invitationGenerator = new InvitationGenerator(_invitesRepositoryMock.Object); + } + + [Test] + public async Task GenerateInvitationCode_ShouldReturnNonEmptyString_WhenCalled() + { + // Arrange + var endDate = _fixture.Create(); + var maxUsers = _fixture.Create(); + var isAdmin = _fixture.Create(); + var description = _fixture.Create(); + + // Act + var code = await _invitationGenerator.GenerateInvitationCode(endDate, maxUsers, isAdmin, description); + + // Assert + Assert.That(code, Is.Not.Null.And.Not.Empty); + } + + + [Test] + public async Task GenerateInvitationCode_ShouldCallAddAsyncOnRepository_WithCorrectInvitation() + { + // Arrange + var endDate = DateTime.UtcNow.AddDays(7); // Specific date for easier assertion if needed + var maxUsers = 10; + var isAdmin = false; + var description = "Test Invitation"; + Invitation capturedInvitation = null; + + _invitesRepositoryMock.Setup(repo => repo.AddAsync(It.IsAny())) + .Callback(inv => capturedInvitation = inv) + .Returns(Task.CompletedTask); + + // Act + var code = await _invitationGenerator.GenerateInvitationCode(endDate, maxUsers, isAdmin, description); + + // Assert + _invitesRepositoryMock.Verify(repo => repo.AddAsync(It.IsAny()), Times.Once); + Assert.That(capturedInvitation, Is.Not.Null); + Assert.That(capturedInvitation.EndDate.ToUniversalTime(), Is.EqualTo(endDate.ToUniversalTime())); + Assert.That(capturedInvitation.MaxParticipants, Is.EqualTo(maxUsers)); + Assert.That(capturedInvitation.IsAdmin, Is.EqualTo(isAdmin)); + Assert.That(capturedInvitation.Description, Is.EqualTo(description)); + Assert.That(capturedInvitation.Code, Is.EqualTo(code)); + Assert.That(capturedInvitation.IsActive, Is.True); // Assuming new invitations are active by default + } + + [Test] + public async Task GenerateInvitationCode_ShouldGenerateUniqueCode_WhenCalled() + { + // Arrange + var endDate = _fixture.Create(); + var maxUsers = _fixture.Create(); + var isAdmin = _fixture.Create(); + var description = _fixture.Create(); + + // Act + var code1 = await _invitationGenerator.GenerateInvitationCode(endDate, maxUsers, isAdmin, description); + var code2 = await _invitationGenerator.GenerateInvitationCode(endDate, maxUsers, isAdmin, description); + + // Assert + Assert.That(code1, Is.Not.EqualTo(code2)); + } +} \ No newline at end of file diff --git a/Govor.API.Tests/UnitTests/Services/Authentication/JwtServiceTests.cs b/Govor.API.Tests/UnitTests/Services/Authentication/JwtServiceTests.cs new file mode 100644 index 0000000..4f3d36e --- /dev/null +++ b/Govor.API.Tests/UnitTests/Services/Authentication/JwtServiceTests.cs @@ -0,0 +1,59 @@ +using System.IdentityModel.Tokens.Jwt; +using AutoFixture; +using Govor.API.Services.Authentication; +using Govor.API.Services.Authentication.Interfaces; +using Govor.Core.Models; +using Microsoft.Extensions.Options; +using Moq; + +namespace Govor.API.Tests.UnitTests.Services.Authentication; + +[TestFixture] +public class JwtServiceTests +{ + private Fixture _fixture; + private Mock> _jwtOptionsMock; + private Mock _invitesServiceMock; + private IJwtService _jwtService; + + private JwtOption _testJwtOptions; + + [SetUp] + public void SetUp() + { + _fixture = new Fixture(); + _fixture.Behaviors.OfType().ToList().ForEach(b => _fixture.Behaviors.Remove(b)); + _fixture.Behaviors.Add(new OmitOnRecursionBehavior()); + + _testJwtOptions = new JwtOption + { + SecretKeу = "THIS IS A TEST SECRET KEY THAT IS LONG ENOUGH", // Ensure key size is sufficient for HMACSHA256 + Hours = 1 + }; + + _jwtOptionsMock = new Mock>(); + _jwtOptionsMock.Setup(o => o.Value).Returns(_testJwtOptions); + + _invitesServiceMock = new Mock(); + + _jwtService = new JwtService(_jwtOptionsMock.Object, _invitesServiceMock.Object); + } + + [Test] + public void GenerateJwtToken_ShouldReturnValidJwtString() + { + // Arrange + var user = _fixture.Create(); + var expectedRole = "User"; + _invitesServiceMock.Setup(s => s.GetRole(user)).Returns(Task.FromResult(expectedRole)); + // Act + var tokenString = _jwtService.GenerateJwtToken(user); + + // Assert + Assert.That(tokenString, Is.Not.Null.And.Not.Empty); + + // Attempt to parse the token to ensure it's a JWT + var handler = new JwtSecurityTokenHandler(); + Assert.DoesNotThrow(() => handler.ReadJwtToken(tokenString)); + } +} \ No newline at end of file