mirror of https://github.com/Ombi-app/Ombi
Added the Rules Engine Pattern and the Auto approve and request rules #865
parent
7f2cfdafd1
commit
d10b0f7081
@ -0,0 +1,87 @@
|
||||
using Ombi.Core.Rule.Rules;
|
||||
using Xunit;
|
||||
using System.Security.Principal;
|
||||
using Moq;
|
||||
using Ombi.Core.Models.Requests;
|
||||
using Ombi.Core.Claims;
|
||||
|
||||
namespace Ombi.Core.Tests
|
||||
{
|
||||
public class AutoApproveRuleTests
|
||||
{
|
||||
public AutoApproveRuleTests()
|
||||
{
|
||||
PrincipalMock = new Mock<IPrincipal>();
|
||||
Rule = new AutoApproveRule(PrincipalMock.Object);
|
||||
}
|
||||
|
||||
private AutoApproveRule Rule { get; }
|
||||
private Mock<IPrincipal> PrincipalMock { get; }
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnSuccess_WhenAdminAndRequestMovie()
|
||||
{
|
||||
PrincipalMock.Setup(x => x.IsInRole(OmbiClaims.Admin)).Returns(true);
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.Movie };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, true);
|
||||
Assert.Equal(request.Approved, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnSuccess_WhenAdminAndRequestTV()
|
||||
{
|
||||
PrincipalMock.Setup(x => x.IsInRole(OmbiClaims.Admin)).Returns(true);
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.TvShow };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, true);
|
||||
Assert.Equal(request.Approved, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnSuccess_WhenAutoApproveMovieAndRequestMovie()
|
||||
{
|
||||
PrincipalMock.Setup(x => x.IsInRole(OmbiClaims.AutoApproveMovie)).Returns(true);
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.Movie };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, true);
|
||||
Assert.Equal(request.Approved, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnSuccess_WhenAutoApproveTVAndRequestTV()
|
||||
{
|
||||
PrincipalMock.Setup(x => x.IsInRole(OmbiClaims.AutoApproveTv)).Returns(true);
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.TvShow };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, true);
|
||||
Assert.Equal(request.Approved, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnFail_WhenNoClaimsAndRequestMovie()
|
||||
{
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.Movie };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, true);
|
||||
Assert.Equal(request.Approved, false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnFail_WhenNoClaimsAndRequestTV()
|
||||
{
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.TvShow };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, true);
|
||||
Assert.Equal(request.Approved, false);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
using Ombi.Core.Rule.Rules;
|
||||
using Xunit;
|
||||
using System.Security.Principal;
|
||||
using Moq;
|
||||
using Ombi.Core.Models.Requests;
|
||||
using Ombi.Core.Claims;
|
||||
|
||||
namespace Ombi.Core.Tests
|
||||
{
|
||||
public class CanRequestRuleTests
|
||||
{
|
||||
public CanRequestRuleTests()
|
||||
{
|
||||
PrincipalMock = new Mock<IPrincipal>();
|
||||
Rule = new CanRequestRule(PrincipalMock.Object);
|
||||
}
|
||||
|
||||
private CanRequestRule Rule { get; }
|
||||
private Mock<IPrincipal> PrincipalMock { get; }
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnSuccess_WhenRequestingMovieWithMovieRole()
|
||||
{
|
||||
PrincipalMock.Setup(x => x.IsInRole(OmbiClaims.RequestMovie)).Returns(true);
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.Movie };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnFail_WhenRequestingMovieWithoutMovieRole()
|
||||
{
|
||||
PrincipalMock.Setup(x => x.IsInRole(OmbiClaims.RequestMovie)).Returns(false);
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.Movie };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, false);
|
||||
Assert.Equal(string.IsNullOrEmpty(result.Message), false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnSuccess_WhenRequestingMovieWithAdminRole()
|
||||
{
|
||||
PrincipalMock.Setup(x => x.IsInRole(OmbiClaims.Admin)).Returns(true);
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.Movie };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnSuccess_WhenRequestingTVWithAdminRole()
|
||||
{
|
||||
PrincipalMock.Setup(x => x.IsInRole(OmbiClaims.Admin)).Returns(true);
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.TvShow };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnSuccess_WhenRequestingTVWithTVRole()
|
||||
{
|
||||
PrincipalMock.Setup(x => x.IsInRole(OmbiClaims.RequestTv)).Returns(true);
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.TvShow };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_ReturnFail_WhenRequestingTVWithoutTVRole()
|
||||
{
|
||||
PrincipalMock.Setup(x => x.IsInRole(OmbiClaims.RequestTv)).Returns(false);
|
||||
var request = new BaseRequestModel() { Type = Store.Entities.RequestType.TvShow };
|
||||
var result = Rule.Execute(request);
|
||||
|
||||
Assert.Equal(result.Success, false);
|
||||
Assert.Equal(string.IsNullOrEmpty(result.Message), false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using Moq;
|
||||
using Ombi.Core.IdentityResolver;
|
||||
using Ombi.Core.Models;
|
||||
using Ombi.Store.Entities;
|
||||
using Ombi.Store.Repository;
|
||||
using Xunit;
|
||||
|
||||
namespace Ombi.Tests.Core
|
||||
{
|
||||
public class UserIdentityManagerTests
|
||||
{
|
||||
public UserIdentityManagerTests()
|
||||
{
|
||||
UserRepo = new Mock<IUserRepository>();
|
||||
var mapper = new Mock<IMapper>();
|
||||
Manager = new UserIdentityManager(UserRepo.Object, mapper.Object);
|
||||
}
|
||||
private Mock<IUserRepository> UserRepo { get; }
|
||||
private IUserIdentityManager Manager { get; }
|
||||
|
||||
[Fact]
|
||||
public async Task CredentialsValid()
|
||||
{
|
||||
UserRepo.Setup(x => x.GetUser("ABC")).ReturnsAsync(() => new User{Password = "4tDKIbNCZ0pMxNzSSjVbT6mG88o52x9jOixPEwQS9rg=", Salt = new byte[]{30,80,214,127,185,134,75,86,80,177,0,242,202,161,219,246}});
|
||||
|
||||
Assert.True(await Manager.CredentialsValid("ABC", "ABC"));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue