Skip to content

Commit

Permalink
[InviteService] Add tests (#2672)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Oct 10, 2023
1 parent 47537c4 commit e30eac5
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Backend.Tests/Services/InviteServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Linq;
using Backend.Tests.Mocks;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using BackendFramework.Services;
using NUnit.Framework;

namespace Backend.Tests.Services
{
public class InviteServiceTests
{
private IProjectRepository _projRepo = null!;
private IUserRepository _userRepo = null!;
private IUserRoleRepository _userRoleRepo = null!;
private IEmailService _emailService = null!;
private IPermissionService _permService = null!;
private InviteService _inviteService = null!;
private const string Email = "[email protected]";


private Project _proj = null!;
private User _user = null!;

[SetUp]
public void Setup()
{
_projRepo = new ProjectRepositoryMock();
_userRepo = new UserRepositoryMock();
_userRoleRepo = new UserRoleRepositoryMock();
_emailService = new EmailServiceMock();
_permService = new PermissionServiceMock(_userRepo);
_inviteService = new InviteService(_projRepo, _userRepo, _permService, _userRoleRepo, _emailService);

_proj = _projRepo.Create(new Project { Name = "InviteServiceTests" }).Result!;
_user = _userRepo.Create(new User()).Result!;
}

[Test]
public void TestCreateLinkWithToken()
{
var url = _inviteService.CreateLinkWithToken(_proj, Role.Harvester, Email).Result;
Assert.That(url, Does.Contain(Email));
Assert.That(url, Does.Contain(_proj.Id));
var token = _projRepo.GetProject(_proj.Id).Result!.InviteTokens.First().Token;
Assert.That(url, Does.Contain(token));
}

[Test]
public void TestRemoveTokenAndCreateUserRoleOwnerException()
{
var invite = new EmailInvite { Role = Role.Owner };
Assert.That(
() => _inviteService.RemoveTokenAndCreateUserRole(_proj, _user, invite),
Throws.TypeOf<InviteService.InviteException>());
}

[Test]
public void TestRemoveTokenAndCreateUserRoleAddsRole()
{
var result = _inviteService.RemoveTokenAndCreateUserRole(_proj, _user, new EmailInvite()).Result;
Assert.That(result, Is.True);
var userRoles = _userRoleRepo.GetAllUserRoles(_proj.Id).Result;
Assert.That(userRoles, Has.Count.EqualTo(1));
var userRole = userRoles.First();
Assert.That(_userRepo.GetUser(_user.Id).Result!.ProjectRoles[_proj.Id], Is.EqualTo(userRole.Id));
}

[Test]
public void TestRemoveTokenAndCreateUserRoleRemovesToken()
{
var invite = new EmailInvite(1);
_proj.InviteTokens.Add(invite);
_ = _projRepo.Update(_proj.Id, _proj).Result;
_proj = _projRepo.GetProject(_proj.Id).Result!;
Assert.That(_proj.InviteTokens, Has.Count.EqualTo(1));

var result = _inviteService.RemoveTokenAndCreateUserRole(_proj, _user, invite).Result;
Assert.That(result, Is.True);
_proj = _projRepo.GetProject(_proj.Id).Result!;
Assert.That(_proj.InviteTokens, Is.Empty);
}
}
}

0 comments on commit e30eac5

Please sign in to comment.