Skip to content

Commit

Permalink
Add SaveRequestHandler permission test
Browse files Browse the repository at this point in the history
  • Loading branch information
volkanceylan committed Jan 27, 2025
1 parent c01cecf commit c9fd3eb
Showing 1 changed file with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace Serenity.Services;

public partial class SaveRequestHandler_Permission_Tests
{
const string insertPermission = "Test:Insert";
const string updatePermission = "Test:Update";

[Fact]
public void Create_Throws_If_User_DoesntHave_TheInsertPermission()
{
var context = new NullRequestContext().WithPermissions(x => x is not insertPermission);
var handler = new SaveRequestHandler<TestRow>(context);
Assert.Throws<ValidationError>(() => handler.Create(new MockUnitOfWork(), new()
{
Entity = new() { Name = "Test" }
}));
}

[Fact]
public void Create_Allows_If_User_Has_TheInsertPermission()
{
var context = new NullRequestContext().WithPermissions(x => x is insertPermission);
var handler = new SaveRequestHandler<TestRow>(context);
using var connection = new MockDbConnection();
connection.InterceptManipulateRow(args =>
{
var insertRow = Assert.IsType<TestRow>(args.Row);
Assert.False(args.Id.HasValue);
Assert.True(args.GetNewId);
Assert.True(insertRow.IsAssigned(TestRow.Fields.Name));
Assert.Equal("Test", insertRow.Name);
Assert.False(insertRow.IsAssigned(TestRow.Fields.Id));
return 123;
});
var row = new TestRow() { Name = "Test" };
handler.Create(new MockUnitOfWork(connection), new()
{
Entity = row
});
Assert.False(row.IsAssigned(TestRow.Fields.Id));

Assert.Equal(1, connection.AllCallCount);
Assert.Single(connection.ManipulateRowCalls);
}

[InsertPermission(insertPermission)]
[UpdatePermission(updatePermission)]
private class TestRow : Row<TestRow.RowFields>, IIdRow
{
[IdProperty, Identity]
public int? Id { get => fields.Id[this]; set => fields.Id[this] = value; }

[NameProperty]
public string Name { get => fields.Name[this]; set => fields.Name[this] = value; }
public class RowFields : RowFieldsBase
{
public Int32Field Id;

Check warning on line 57 in tests/Serenity.Net.Tests/services/requesthandlers/save/SaveRequestHandler_Create_Permission_Tests.cs

View workflow job for this annotation

GitHub Actions / tests

Field 'SaveRequestHandler_Permission_Tests.TestRow.RowFields.Id' is never assigned to, and will always have its default value null

Check warning on line 57 in tests/Serenity.Net.Tests/services/requesthandlers/save/SaveRequestHandler_Create_Permission_Tests.cs

View workflow job for this annotation

GitHub Actions / tests

Field 'SaveRequestHandler_Permission_Tests.TestRow.RowFields.Id' is never assigned to, and will always have its default value null
public StringField Name;

Check warning on line 58 in tests/Serenity.Net.Tests/services/requesthandlers/save/SaveRequestHandler_Create_Permission_Tests.cs

View workflow job for this annotation

GitHub Actions / tests

Field 'SaveRequestHandler_Permission_Tests.TestRow.RowFields.Name' is never assigned to, and will always have its default value null

Check warning on line 58 in tests/Serenity.Net.Tests/services/requesthandlers/save/SaveRequestHandler_Create_Permission_Tests.cs

View workflow job for this annotation

GitHub Actions / tests

Field 'SaveRequestHandler_Permission_Tests.TestRow.RowFields.Name' is never assigned to, and will always have its default value null
}
}
}

0 comments on commit c9fd3eb

Please sign in to comment.