Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/github_actions/github/codeql-ac…
Browse files Browse the repository at this point in the history
…tion-2.22.1
  • Loading branch information
imnasnainaec authored Oct 16, 2023
2 parents a494078 + 59e73ea commit 2ae73ca
Show file tree
Hide file tree
Showing 121 changed files with 1,960 additions and 591 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"Doulos",
"Dups",
"endcap",
"Ethnologue",
"globaltool",
"graylist",
"Guids",
Expand All @@ -71,6 +72,7 @@
"piptools",
"Prenoun",
"Preverb",
"recaptcha",
"reportgenerator",
"sched",
"sillsdev",
Expand Down
165 changes: 165 additions & 0 deletions Backend.Tests/Controllers/InviteControllerTests.cs
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);
}
}
}
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);
}
}
}
54 changes: 32 additions & 22 deletions Backend.Tests/Services/MergeServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ public void UndoMergeMultiChildTest()
[Test]
public void AddMergeToBlacklistTest()
{
_ = _mergeBlacklistRepo.DeleteAllSets(ProjId).Result;
_ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result;
var wordIds = new List<string> { "1", "2" };

// Adding to blacklist should clear from graylist
Expand All @@ -219,7 +217,6 @@ public void AddMergeToBlacklistTest()
[Test]
public void AddMergeToBlacklistErrorTest()
{
_ = _mergeBlacklistRepo.DeleteAllSets(ProjId).Result;
var wordIds0 = new List<string>();
var wordIds1 = new List<string> { "1" };
Assert.That(
Expand All @@ -231,7 +228,6 @@ public void AddMergeToBlacklistErrorTest()
[Test]
public void IsInMergeBlacklistTest()
{
_ = _mergeBlacklistRepo.DeleteAllSets(ProjId).Result;
var wordIds = new List<string> { "1", "2", "3" };
var subWordIds = new List<string> { "3", "2" };

Expand All @@ -243,7 +239,6 @@ public void IsInMergeBlacklistTest()
[Test]
public void IsInMergeBlacklistErrorTest()
{
_ = _mergeBlacklistRepo.DeleteAllSets(ProjId).Result;
var wordIds0 = new List<string>();
var wordIds1 = new List<string> { "1" };
Assert.That(
Expand Down Expand Up @@ -298,7 +293,6 @@ public void UpdateMergeBlacklistTest()
[Test]
public void AddMergeToGraylistTest()
{
_ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result;
var wordIds = new List<string> { "1", "2" };
_ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds).Result;
var graylist = _mergeGraylistRepo.GetAllSets(ProjId).Result;
Expand All @@ -307,10 +301,24 @@ public void AddMergeToGraylistTest()
Assert.That(expectedEntry.ContentEquals(graylist.First()), Is.True);
}

[Test]
public void AddMergeToGraylistSupersetTest()
{
var wordIds12 = new List<string> { "1", "2" };
var wordIds13 = new List<string> { "1", "3" };
var wordIds123 = new List<string> { "1", "2", "3" };

_ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds12).Result;
_ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds13).Result;
Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(2));

_ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds123).Result;
Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(1));
}

[Test]
public void AddMergeToGraylistErrorTest()
{
_ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result;
var wordIds = new List<string>();
var wordIds1 = new List<string> { "1" };
Assert.That(
Expand All @@ -324,24 +332,28 @@ public void AddMergeToGraylistErrorTest()
[Test]
public void RemoveFromMergeGraylistTest()
{
_ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result;
var wordIds12 = new List<string> { "1", "2" };
var wordIds13 = new List<string> { "1", "3" };
_ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds12).Result;
_ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds13).Result;
var graylist = _mergeGraylistRepo.GetAllSets(ProjId).Result;
Assert.That(graylist, Has.Count.EqualTo(2));
var wordIds123 = new List<string> { "1", "2", "3" };
var removed = _mergeService.RemoveFromMergeGraylist(ProjId, UserId, wordIds123).Result;
Assert.That(removed, Has.Count.EqualTo(2));
graylist = _mergeGraylistRepo.GetAllSets(ProjId).Result;
Assert.That(graylist, Is.Empty);
var wordIds = new List<string> { "1", "2", "3" };
_ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds).Result;
Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(1));
Assert.That(_mergeService.RemoveFromMergeGraylist(ProjId, UserId, wordIds).Result, Is.True);
Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(0));
}

[Test]
public void RemoveFromMergeGraylistSupersetTest()
{
var wordIds = new List<string> { "1", "2" };
_ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds).Result;
Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(1));

wordIds.Add("3");
Assert.That(_mergeService.RemoveFromMergeGraylist(ProjId, UserId, wordIds).Result, Is.False);
Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(1));
}

[Test]
public void RemoveFromMergeGraylistErrorTest()
{
_ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result;
var wordIds = new List<string>();
var wordIds1 = new List<string> { "1" };
Assert.That(
Expand All @@ -355,7 +367,6 @@ public void RemoveFromMergeGraylistErrorTest()
[Test]
public void IsInMergeGraylistTest()
{
_ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result;
var wordIds = new List<string> { "1", "2", "3" };
var subWordIds = new List<string> { "3", "2" };

Expand All @@ -367,7 +378,6 @@ public void IsInMergeGraylistTest()
[Test]
public void IsInMergeGraylistErrorTest()
{
_ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result;
var wordIds0 = new List<string>();
var wordIds1 = new List<string> { "1" };
Assert.That(
Expand Down
Loading

0 comments on commit 2ae73ca

Please sign in to comment.