-
-
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.
Merge branch 'master' into dependabot/github_actions/github/codeql-ac…
…tion-2.22.1
- Loading branch information
Showing
121 changed files
with
1,960 additions
and
591 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
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
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); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.