Skip to content

Return Forbidden when operation is inaccessible, to match resource endpoint status code #1562

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

Merged
merged 1 commit into from
Jun 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected virtual void ValidateEnabledOperations(IList<OperationContainer> opera
{
string operationCode = GetOperationCodeText(operationKind);

errors.Add(new ErrorObject(HttpStatusCode.UnprocessableEntity)
errors.Add(new ErrorObject(HttpStatusCode.Forbidden)
{
Title = "The requested operation is not accessible.",
Detail = $"The '{operationCode}' relationship operation is not accessible for relationship '{operationRequest.Relationship}' " +
Expand All @@ -155,7 +155,7 @@ protected virtual void ValidateEnabledOperations(IList<OperationContainer> opera
{
string operationCode = GetOperationCodeText(operationKind);

errors.Add(new ErrorObject(HttpStatusCode.UnprocessableEntity)
errors.Add(new ErrorObject(HttpStatusCode.Forbidden)
{
Title = "The requested operation is not accessible.",
Detail = $"The '{operationCode}' resource operation is not accessible for resource type '{operationRequest.PrimaryResourceType}'.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task Can_create_resources_for_matching_resource_type()
}

[Fact]
public async Task Cannot_create_resource_for_mismatching_resource_type()
public async Task Cannot_create_resource_for_inaccessible_operation()
{
// Arrange
var requestBody = new
Expand All @@ -96,20 +96,20 @@ public async Task Cannot_create_resource_for_mismatching_resource_type()
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePostAtomicAsync<Document>(route, requestBody);

// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.UnprocessableEntity);
httpResponse.ShouldHaveStatusCode(HttpStatusCode.Forbidden);

responseDocument.Errors.ShouldHaveCount(1);

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.StatusCode.Should().Be(HttpStatusCode.Forbidden);
error.Title.Should().Be("The requested operation is not accessible.");
error.Detail.Should().Be("The 'add' resource operation is not accessible for resource type 'performers'.");
error.Source.ShouldNotBeNull();
error.Source.Pointer.Should().Be("/atomic:operations[0]");
}

[Fact]
public async Task Cannot_update_resource_for_matching_resource_type()
public async Task Cannot_update_resource_for_inaccessible_operation()
{
// Arrange
MusicTrack existingTrack = _fakers.MusicTrack.Generate();
Expand Down Expand Up @@ -145,20 +145,20 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePostAtomicAsync<Document>(route, requestBody);

// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.UnprocessableEntity);
httpResponse.ShouldHaveStatusCode(HttpStatusCode.Forbidden);

responseDocument.Errors.ShouldHaveCount(1);

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.StatusCode.Should().Be(HttpStatusCode.Forbidden);
error.Title.Should().Be("The requested operation is not accessible.");
error.Detail.Should().Be("The 'update' resource operation is not accessible for resource type 'musicTracks'.");
error.Source.ShouldNotBeNull();
error.Source.Pointer.Should().Be("/atomic:operations[0]");
}

[Fact]
public async Task Cannot_add_to_ToMany_relationship_for_matching_resource_type()
public async Task Cannot_add_to_ToMany_relationship_for_inaccessible_operation()
{
// Arrange
MusicTrack existingTrack = _fakers.MusicTrack.Generate();
Expand Down Expand Up @@ -201,12 +201,12 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePostAtomicAsync<Document>(route, requestBody);

// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.UnprocessableEntity);
httpResponse.ShouldHaveStatusCode(HttpStatusCode.Forbidden);

responseDocument.Errors.ShouldHaveCount(1);

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.StatusCode.Should().Be(HttpStatusCode.Forbidden);
error.Title.Should().Be("The requested operation is not accessible.");
error.Detail.Should().Be("The 'add' relationship operation is not accessible for relationship 'performers' on resource type 'musicTracks'.");
error.Source.ShouldNotBeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public AtomicDefaultConstrainedOperationsControllerTests(IntegrationTestContext<
}

[Fact]
public async Task Cannot_delete_resource_for_disabled_resource_endpoint()
public async Task Cannot_delete_resource_for_inaccessible_operation()
{
// Arrange
TextLanguage existingLanguage = _fakers.TextLanguage.Generate();
Expand Down Expand Up @@ -53,20 +53,20 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePostAtomicAsync<Document>(route, requestBody);

// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.UnprocessableEntity);
httpResponse.ShouldHaveStatusCode(HttpStatusCode.Forbidden);

responseDocument.Errors.ShouldHaveCount(1);

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.StatusCode.Should().Be(HttpStatusCode.Forbidden);
error.Title.Should().Be("The requested operation is not accessible.");
error.Detail.Should().Be("The 'remove' resource operation is not accessible for resource type 'textLanguages'.");
error.Source.ShouldNotBeNull();
error.Source.Pointer.Should().Be("/atomic:operations[0]");
}

[Fact]
public async Task Cannot_change_ToMany_relationship_for_disabled_resource_endpoints()
public async Task Cannot_change_ToMany_relationship_for_inaccessible_operations()
{
// Arrange
TextLanguage existingLanguage = _fakers.TextLanguage.Generate();
Expand Down Expand Up @@ -145,26 +145,26 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePostAtomicAsync<Document>(route, requestBody);

// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.UnprocessableEntity);
httpResponse.ShouldHaveStatusCode(HttpStatusCode.Forbidden);

responseDocument.Errors.ShouldHaveCount(3);

ErrorObject error1 = responseDocument.Errors[0];
error1.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error1.StatusCode.Should().Be(HttpStatusCode.Forbidden);
error1.Title.Should().Be("The requested operation is not accessible.");
error1.Detail.Should().Be("The 'update' relationship operation is not accessible for relationship 'lyrics' on resource type 'textLanguages'.");
error1.Source.ShouldNotBeNull();
error1.Source.Pointer.Should().Be("/atomic:operations[0]");

ErrorObject error2 = responseDocument.Errors[1];
error2.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error2.StatusCode.Should().Be(HttpStatusCode.Forbidden);
error2.Title.Should().Be("The requested operation is not accessible.");
error2.Detail.Should().Be("The 'add' relationship operation is not accessible for relationship 'lyrics' on resource type 'textLanguages'.");
error2.Source.ShouldNotBeNull();
error2.Source.Pointer.Should().Be("/atomic:operations[1]");

ErrorObject error3 = responseDocument.Errors[2];
error3.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error3.StatusCode.Should().Be(HttpStatusCode.Forbidden);
error3.Title.Should().Be("The requested operation is not accessible.");
error3.Detail.Should().Be("The 'remove' relationship operation is not accessible for relationship 'lyrics' on resource type 'textLanguages'.");
error3.Source.ShouldNotBeNull();
Expand Down
Loading