Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Auto Approve 4K role #4982

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/Ombi.Core.Tests/Rule/Request/AutoApproveRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public async Task Should_ReturnSuccess_WhenAdminAndRequestMovie()
Assert.True(request.Approved);
}

[Test]
public async Task Should_ReturnSuccess_WhenAdminAndRequest4KMovie()
{
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.Admin)).ReturnsAsync(true);
var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie, Is4kRequest = true };
var result = await Rule.Execute(request);

Assert.True(result.Success);
Assert.True(request.Approved);
}

[Test]
public async Task Should_ReturnSuccess_WhenAdminAndRequestTV()
{
Expand All @@ -76,6 +87,17 @@ public async Task Should_ReturnSuccess_WhenAutoApproveMovieAndRequestMovie()
Assert.True(request.Approved);
}

[Test]
public async Task Should_ReturnSuccess_WhenAutoApprove4KMovieAndRequest4KMovie()
{
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.AutoApprove4KMovie)).ReturnsAsync(true);
var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie, Is4kRequest = true };
var result = await Rule.Execute(request);

Assert.True(result.Success);
Assert.True(request.Approved);
}

[Test]
public async Task Should_ReturnFail_WhenAutoApproveMovie_And_RequestTV()
{
Expand Down Expand Up @@ -115,7 +137,7 @@ public async Task Should_ReturnSuccess_WhenSystemUserAndRequestTV()
public async Task Should_ReturnFail_WhenAutoApproveTV_And_RequestMovie()
{
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.AutoApproveTv)).ReturnsAsync(true);
var request = new BaseRequest() { RequestType = Store.Entities.RequestType.Movie };
var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie };
var result = await Rule.Execute(request);

Assert.True(result.Success);
Expand All @@ -126,7 +148,7 @@ public async Task Should_ReturnFail_WhenAutoApproveTV_And_RequestMovie()
public async Task Should_ReturnFail_WhenNoClaimsAndRequestMovie()
{
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), It.IsAny<string>())).ReturnsAsync(false);
var request = new BaseRequest() { RequestType = Store.Entities.RequestType.Movie };
var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie };
var result = await Rule.Execute(request);

Assert.True(result.Success);
Expand Down
35 changes: 29 additions & 6 deletions src/Ombi.Core.Tests/Rule/Request/CanRequestRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ public async Task Should_ReturnSuccess_WhenRequestingMovieWithMovieRole()
}

[Test]
public async Task Should_ReturnSuccess_WhenRequestingMovie4KWithMovieRole()
public async Task Should_ReturnSuccess_WhenRequestingMovieWithAutoApproveRole()
{
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.AutoApproveMovie)).ReturnsAsync(true);
var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie };
var result = await Rule.Execute(request);

Assert.True(result.Success);
}

[Test]
public async Task Should_ReturnSuccess_WhenRequestingMovie4KWithMovie4KRole()
{
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.RequestMovie)).ReturnsAsync(true);
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.Request4KMovie)).ReturnsAsync(true);
var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie, Has4KRequest = true };
var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie, Is4kRequest = true };
var result = await Rule.Execute(request);

Assert.True(result.Success);
Expand All @@ -74,15 +83,29 @@ public async Task Should_ReturnFailure_WhenRequestingMovie4KWithMovieRole()
}

[Test]
public async Task Should_ReturnSuccess_WhenRequestingMovie4KWithAutoApprove()
public async Task Should_ReturnSuccess_WhenRequestingMovie4KWithAutoApprove4K()
{
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.RequestMovie)).ReturnsAsync(false);
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.AutoApproveMovie)).ReturnsAsync(false);
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.Request4KMovie)).ReturnsAsync(false);
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.AutoApprove4KMovie)).ReturnsAsync(true);
var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie, Is4kRequest = true };
var result = await Rule.Execute(request);

Assert.True(result.Success);
}

[Test]
public async Task Should_ReturnFailure_WhenRequestingMovie4KWithout4KRoles()
{
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.RequestMovie)).ReturnsAsync(true);
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.AutoApproveMovie)).ReturnsAsync(true);
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.Request4KMovie)).ReturnsAsync(false);
var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie, Has4KRequest = true };
UserManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), OmbiRoles.AutoApprove4KMovie)).ReturnsAsync(false);
var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie, Is4kRequest = true };
var result = await Rule.Execute(request);

Assert.True(result.Success);
Assert.False(result.Success);
}

[Test]
Expand Down
12 changes: 8 additions & 4 deletions src/Ombi.Core/Rule/Rules/Request/AutoApproveRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task<RuleResult> Execute(BaseRequest obj)
{
if (obj is MovieRequests movie)
{
await Check4K(movie);
await ApproveMovie(movie);
}
else
{
Expand All @@ -45,10 +45,14 @@ public async Task<RuleResult> Execute(BaseRequest obj)
return Success();
}

if (obj.RequestType == RequestType.Movie && await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveMovie))
if (obj.RequestType == RequestType.Movie)
{
var movie = (MovieRequests)obj;
await Check4K(movie);
var autoApproveRole = movie.Is4kRequest ? OmbiRoles.AutoApprove4KMovie : OmbiRoles.AutoApproveMovie;
if (await _manager.IsInRoleAsync(user, autoApproveRole))
{
await ApproveMovie(movie);
}
}
if (obj.RequestType == RequestType.TvShow && await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveTv))
obj.Approved = true;
Expand All @@ -57,7 +61,7 @@ public async Task<RuleResult> Execute(BaseRequest obj)
return Success(); // We don't really care, we just don't set the obj to approve
}

private async Task Check4K(MovieRequests movie)
private async Task ApproveMovie(MovieRequests movie)
{
var featureEnabled = await _featureService.FeatureEnabled(FeatureNames.Movie4KRequests);
if (movie.Is4kRequest && featureEnabled)
Expand Down
20 changes: 6 additions & 14 deletions src/Ombi.Core/Rule/Rules/Request/CanRequestRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,13 @@ public async Task<RuleResult> Execute(BaseRequest obj)
if (obj.RequestType == RequestType.Movie)
{
var movie = (MovieRequests)obj;
var hasAutoApprove = await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveMovie);
if (await _manager.IsInRoleAsync(user, OmbiRoles.RequestMovie) || hasAutoApprove)

var requestRole = movie.Is4kRequest ? OmbiRoles.Request4KMovie : OmbiRoles.RequestMovie;
var autoApproveRole = movie.Is4kRequest ? OmbiRoles.AutoApprove4KMovie : OmbiRoles.AutoApproveMovie;

if (await _manager.IsInRoleAsync(user, requestRole) || await _manager.IsInRoleAsync(user, autoApproveRole))
{
if (movie.Is4kRequest && !hasAutoApprove)
{
var has4kPermission = await _manager.IsInRoleAsync(user, OmbiRoles.Request4KMovie);
if (has4kPermission)
{
return Success();
}
}
else
{
return Success();
}
return Success();
}
return Fail(ErrorCode.NoPermissionsRequestMovie, "You do not have permissions to Request a Movie");
}
Expand Down
3 changes: 2 additions & 1 deletion src/Ombi.Helpers/OmbiRoles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public static class OmbiRoles
{
// DONT FORGET TO ADD TO IDENTITYCONTROLLER.CREATEROLES AND THE UI!
// DONT FORGET TO ADD TO IDENTITYCONTROLLER.CREATEROLES!

public const string Admin = nameof(Admin);
public const string AutoApproveMovie = nameof(AutoApproveMovie);
Expand All @@ -17,5 +17,6 @@ public static class OmbiRoles
public const string ManageOwnRequests = nameof(ManageOwnRequests);
public const string EditCustomPage = nameof(EditCustomPage);
public const string Request4KMovie = nameof(Request4KMovie);
public const string AutoApprove4KMovie = nameof(AutoApprove4KMovie);
}
}
Loading