Skip to content

Commit

Permalink
[DMS-352] Return failure body when path not parseable (#312)
Browse files Browse the repository at this point in the history
* Return failure body with not found path

* E2E test

* E2E tests
  • Loading branch information
simpat-adam authored Oct 23, 2024
1 parent 24ad9b4 commit 2edd9d9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ public async Task Execute(PipelineContext context, Func<Task> next)
"ParsePathMiddleware: Not a valid path - {TraceId}",
context.FrontendRequest.TraceId
);
context.FrontendResponse = new FrontendResponse(StatusCode: 404, Body: "", Headers: []);
context.FrontendResponse = new FrontendResponse(
StatusCode: 404,
Body: FailureResponse.ForNotFound(
"The specified data could not be found.",
context.FrontendRequest.TraceId
),
Headers: [],
ContentType: "application/problem+json"
);
return;
}

Expand All @@ -80,7 +88,7 @@ public async Task Execute(PipelineContext context, Func<Task> next)
traceId: context.FrontendRequest.TraceId,
validationErrors: new Dictionary<string, string[]>
{
{ "$.id", new[] { $"The value '{pathInfo.DocumentUuid}' is not valid." } }
{ "$.id", new[] { $"The value '{pathInfo.DocumentUuid}' is not valid." } },
},
errors: []
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ Feature: Validation of the structure of the URLs
}
"""

## The resolution of this ticket will solve the execution error: https://edfi.atlassian.net/browse/DMS-352
@API-067 @ignore
@API-067
Scenario: 01 Ensure clients cannot retrieve information when the data model name is missing
When a GET request is made to "/schools"
Then it should respond with 404
Expand All @@ -44,8 +43,7 @@ Feature: Validation of the structure of the URLs
}
"""

## The resolution of this ticket will solve the execution error: https://edfi.atlassian.net/browse/DMS-352
@API-068 @ignore
@API-068
Scenario: 02 Ensure clients cannot create a resource when the data model name is missing
When a POST request is made to "/schools" with
"""
Expand All @@ -72,16 +70,19 @@ Feature: Validation of the structure of the URLs
"type": "urn:ed-fi:api:not-found",
"title": "Not Found",
"status": 404,
"correlationId": null
"correlationId": null,
"validationErrors": {},
"errors": []
}
"""
And the response headers include
"""
Content-Type: application/problem+json
{
"Content-Type": "application/problem+json"
}
"""

## The resolution of this ticket will solve the execution error: https://edfi.atlassian.net/browse/DMS-352
@API-069 @ignore
@API-069
Scenario: 03 Ensure clients cannot update a resource when the data model name is missing
When a PUT request is made to "/schools/{id}" with
"""
Expand Down Expand Up @@ -109,7 +110,9 @@ Feature: Validation of the structure of the URLs
"type": "urn:ed-fi:api:not-found",
"title": "Not Found",
"status": 404,
"correlationId": null
"correlationId": null,
"validationErrors": {},
"errors": []
}
"""
And the response headers include
Expand All @@ -119,8 +122,7 @@ Feature: Validation of the structure of the URLs
}
"""

## The resolution of this ticket will solve the execution error: https://edfi.atlassian.net/browse/DMS-352
@API-070 @ignore
@API-070
Scenario: 04 Ensure clients cannot delete a resource when the data model name is missing
When a DELETE request is made to "/schools/{id}"
Then it should respond with 404
Expand All @@ -131,7 +133,9 @@ Feature: Validation of the structure of the URLs
"type": "urn:ed-fi:api:not-found",
"title": "Not Found",
"status": 404,
"correlationId": null
"correlationId": null,
"validationErrors": {},
"errors": []
}
"""
And the response headers include
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public async Task GivenAPOSTRequestIsMadeToWith(string url, string body)
{
await ExecutePostRequest(url, body);

_apiResponse.Status.Should().BeOneOf(OkCreated, $"Given post to {url} failed:\n{_apiResponse.TextAsync().Result}");
_apiResponse
.Status.Should()
.BeOneOf(OkCreated, $"Given post to {url} failed:\n{_apiResponse.TextAsync().Result}");

WaitForOpenSearch(_scenarioContext.ScenarioInfo.Tags);
}
Expand Down Expand Up @@ -112,7 +114,6 @@ private async Task<List<IAPIResponse>> ProcessDataTable(string entityType, DataT
OkCreated,
$"POST request for {entityType} descriptor {descriptor["descriptorName"]} failed:\n{response.TextAsync().Result}"
);

}

foreach (var row in dataTable.Rows)
Expand Down Expand Up @@ -206,7 +207,7 @@ public async Task WhenSendingAPOSTRequestToWithBody(string url, string body)

private async Task ExecutePostRequest(string url, string body)
{
url = addDataPrefixIfNecessary(url);
url = AddDataPrefixIfNecessary(url);

_logger.log.Information($"POST url: {url}");
_logger.log.Information($"POST body: {body}");
Expand Down Expand Up @@ -246,7 +247,7 @@ public async Task WhenSendingAPOSTRequestToWithBodyAndCustomHeader(
string body
)
{
url = addDataPrefixIfNecessary(url);
url = AddDataPrefixIfNecessary(url);
_logger.log.Information($"POST url: {url}");
_logger.log.Information($"POST body: {body}");

Expand All @@ -265,7 +266,7 @@ string body
[When("a POST request is made for dependent resource {string} with")]
public async Task WhenSendingAPOSTRequestForDependentResourceWithBody(string url, string body)
{
url = addDataPrefixIfNecessary(url);
url = AddDataPrefixIfNecessary(url);
_apiResponse = await _playwrightContext.ApiRequestContext?.PostAsync(url, new() { Data = body })!;

_dependentId = extractDataFromResponseAndReturnIdIfAvailable(_apiResponse);
Expand All @@ -274,7 +275,7 @@ public async Task WhenSendingAPOSTRequestForDependentResourceWithBody(string url
[When("a PUT request is made to {string} with")]
public async Task WhenAPUTRequestIsMadeToWith(string url, string body)
{
url = addDataPrefixIfNecessary(url).Replace("{id}", _id).Replace("{dependentId}", _dependentId);
url = AddDataPrefixIfNecessary(url).Replace("{id}", _id).Replace("{dependentId}", _dependentId);

body = body.Replace("{id}", _id).Replace("{dependentId}", _dependentId);
_logger.log.Information($"PUT url: {url}");
Expand All @@ -287,7 +288,7 @@ public async Task WhenAPUTRequestIsMadeToWith(string url, string body)
[When("a PUT request is made to referenced resource {string} with")]
public async Task WhenAPUTRequestIsMadeToReferencedResourceWith(string url, string body)
{
url = addDataPrefixIfNecessary(url).Replace("{id}", _referencedResourceId);
url = AddDataPrefixIfNecessary(url).Replace("{id}", _referencedResourceId);

_logger.log.Information(url);
body = body.Replace("{id}", _referencedResourceId);
Expand Down Expand Up @@ -317,7 +318,7 @@ public async Task WhenAPUTRequestIsMadeToReferencedResourceWith(string url, stri
[When("a DELETE request is made to {string}")]
public async Task WhenADELETERequestIsMadeTo(string url)
{
url = addDataPrefixIfNecessary(url).Replace("{id}", _id);
url = AddDataPrefixIfNecessary(url).Replace("{id}", _id);
_apiResponse = await _playwrightContext.ApiRequestContext?.DeleteAsync(url)!;

WaitForOpenSearch(_scenarioContext.ScenarioInfo.Tags);
Expand All @@ -326,15 +327,15 @@ public async Task WhenADELETERequestIsMadeTo(string url)
[When("a DELETE request is made to referenced resource {string}")]
public async Task WhenADELETERequestIsMadeToReferencedResource(string url)
{
url = addDataPrefixIfNecessary(url).Replace("{id}", _referencedResourceId);
url = AddDataPrefixIfNecessary(url).Replace("{id}", _referencedResourceId);

_apiResponse = await _playwrightContext.ApiRequestContext?.DeleteAsync(url)!;
}

[When("a GET request is made to {string}")]
public async Task WhenAGETRequestIsMadeTo(string url)
{
url = addDataPrefixIfNecessary(url).Replace("{id}", _id);
url = AddDataPrefixIfNecessary(url).Replace("{id}", _id);
_logger.log.Information(url);
_apiResponse = await _playwrightContext.ApiRequestContext?.GetAsync(url)!;
}
Expand Down Expand Up @@ -659,15 +660,20 @@ public async Task ThenGettingLessSchoolsThanTheTotalCount()

#endregion

private static string addDataPrefixIfNecessary(string input)
private static string AddDataPrefixIfNecessary(string input)
{
// Discovery endpoint
if (input == "/")
{
return input;
}

// Prefer that the "url" fragment have a starting slash, but write
// the code so it will work either way.
input = input.StartsWith('/') ? input[1..] : input;

// If it doesn't start with ed-fi, then assume that this is looking
// for metadata and should not have "data" added to the URL.
input = input.StartsWith("ed-fi") ? $"data/{input}" : input;
// metadata should not have "data" added to the URL.
input = input.StartsWith("metadata") ? input : $"data/{input}";

return input;
}
Expand Down

0 comments on commit 2edd9d9

Please sign in to comment.