-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[InviteController] Clean up logic and add tests (#2663)
- Loading branch information
1 parent
c0b2bcf
commit 47537c4
Showing
4 changed files
with
218 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Backend.Tests.Mocks; | ||
using BackendFramework.Controllers; | ||
using BackendFramework.Interfaces; | ||
using BackendFramework.Models; | ||
using BackendFramework.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
using NUnit.Framework; | ||
|
||
namespace Backend.Tests.Controllers | ||
{ | ||
public class InviteControllerTests : IDisposable | ||
{ | ||
private IProjectRepository _projRepo = null!; | ||
private IUserRepository _userRepo = null!; | ||
private IInviteService _inviteService = null!; | ||
private IPermissionService _permissionService = null!; | ||
private InviteController _inviteController = null!; | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
_inviteController?.Dispose(); | ||
} | ||
} | ||
|
||
private string _projId = null!; | ||
private string _tokenActive = null!; | ||
private string _tokenExpired = null!; | ||
private const string EmailActive = "[email protected]"; | ||
private const string EmailExpired = "[email protected]"; | ||
private const string MissingId = "MISSING_ID"; | ||
|
||
[SetUp] | ||
public async Task Setup() | ||
{ | ||
_projRepo = new ProjectRepositoryMock(); | ||
_userRepo = new UserRepositoryMock(); | ||
_permissionService = new PermissionServiceMock(); | ||
_inviteService = new InviteService( | ||
_projRepo, _userRepo, _permissionService, new UserRoleRepositoryMock(), new EmailServiceMock()); | ||
_inviteController = new InviteController(_inviteService, _projRepo, _userRepo, _permissionService); | ||
|
||
var tokenPast = new EmailInvite(-1) { Email = EmailExpired }; | ||
_tokenExpired = tokenPast.Token; | ||
var tokenFuture = new EmailInvite(1) { Email = EmailActive }; | ||
_tokenActive = tokenFuture.Token; | ||
_projId = (await _projRepo.Create(new Project | ||
{ | ||
Name = "InviteControllerTests", | ||
InviteTokens = new List<EmailInvite> { tokenPast, tokenFuture } | ||
}))!.Id; | ||
} | ||
|
||
[Test] | ||
public void TestEmailInviteToProject() | ||
{ | ||
var data = new EmailInviteData { ProjectId = _projId }; | ||
var result = (ObjectResult)_inviteController.EmailInviteToProject(data).Result; | ||
Assert.That(result.Value, Is.Not.Empty); | ||
} | ||
|
||
[Test] | ||
public void TestEmailInviteToProjectUnauthorized() | ||
{ | ||
_inviteController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); | ||
var result = _inviteController.EmailInviteToProject(new EmailInviteData()).Result; | ||
Assert.That(result, Is.InstanceOf<ForbidResult>()); | ||
} | ||
|
||
[Test] | ||
public void TestEmailInviteToProjectNoProject() | ||
{ | ||
var data = new EmailInviteData { ProjectId = MissingId }; | ||
var result = _inviteController.EmailInviteToProject(data).Result; | ||
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public void TestValidateTokenNoProject() | ||
{ | ||
var result = _inviteController.ValidateToken(MissingId, _tokenActive).Result; | ||
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public void TestValidateTokenNoTokenNoUser() | ||
{ | ||
var result = _inviteController.ValidateToken(_projId, "not-a-token").Result; | ||
Assert.That(result, Is.InstanceOf<OkObjectResult>()); | ||
var value = ((OkObjectResult)result).Value; | ||
Assert.That(value, Is.InstanceOf<EmailInviteStatus>()); | ||
|
||
var status = (EmailInviteStatus)value!; | ||
Assert.That(status.IsTokenValid, Is.False); | ||
Assert.That(status.IsUserValid, Is.False); | ||
} | ||
|
||
[Test] | ||
public void TestValidateTokenExpiredTokenNoUser() | ||
{ | ||
var result = _inviteController.ValidateToken(_projId, _tokenExpired).Result; | ||
Assert.That(result, Is.InstanceOf<OkObjectResult>()); | ||
var value = ((OkObjectResult)result).Value; | ||
Assert.That(value, Is.InstanceOf<EmailInviteStatus>()); | ||
|
||
var status = (EmailInviteStatus)value!; | ||
Assert.That(status.IsTokenValid, Is.False); | ||
Assert.That(status.IsUserValid, Is.False); | ||
} | ||
|
||
[Test] | ||
public void TestValidateTokenValidTokenNoUser() | ||
{ | ||
var result = _inviteController.ValidateToken(_projId, _tokenActive).Result; | ||
Assert.That(result, Is.InstanceOf<OkObjectResult>()); | ||
var value = ((OkObjectResult)result).Value; | ||
Assert.That(value, Is.InstanceOf<EmailInviteStatus>()); | ||
|
||
var status = (EmailInviteStatus)value!; | ||
Assert.That(status.IsTokenValid, Is.True); | ||
Assert.That(status.IsUserValid, Is.False); | ||
} | ||
|
||
[Test] | ||
public void TestValidateTokenValidTokenUserAlreadyInProject() | ||
{ | ||
var roles = new Dictionary<string, string> { [_projId] = "role-id" }; | ||
_userRepo.Create(new User { Email = EmailActive, ProjectRoles = roles }); | ||
|
||
var result = _inviteController.ValidateToken(_projId, _tokenActive).Result; | ||
Assert.That(result, Is.InstanceOf<OkObjectResult>()); | ||
var value = ((OkObjectResult)result).Value; | ||
Assert.That(value, Is.InstanceOf<EmailInviteStatus>()); | ||
|
||
var status = (EmailInviteStatus)value!; | ||
Assert.That(status.IsTokenValid, Is.True); | ||
Assert.That(status.IsUserValid, Is.False); | ||
} | ||
|
||
[Test] | ||
public void TestValidateTokenExpiredTokenUserAvailable() | ||
{ | ||
_userRepo.Create(new User { Email = EmailExpired }); | ||
|
||
var result = _inviteController.ValidateToken(_projId, _tokenExpired).Result; | ||
Assert.That(result, Is.InstanceOf<OkObjectResult>()); | ||
var value = ((OkObjectResult)result).Value; | ||
Assert.That(value, Is.InstanceOf<EmailInviteStatus>()); | ||
|
||
var status = (EmailInviteStatus)value!; | ||
Assert.That(status.IsTokenValid, Is.False); | ||
Assert.That(status.IsUserValid, Is.True); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters