Skip to content

Commit

Permalink
[#IOPID-1268] Addressable type ResponseErrorPreconditionFailed (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnedMarshal committed Dec 19, 2023
1 parent d70409d commit 35d267e
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 28 deletions.
128 changes: 102 additions & 26 deletions src/__tests__/responses.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { ResponseSuccessJson } from "../responses";
import {
HttpStatusCodeEnum,
ResponseErrorPreconditionFailed,
ResponseSuccessJson,
} from "../responses";

import MockResponse from "../__mocks__/response";

import { getResponseErrorForbiddenNoAuthorizationGroups, ResponseErrorForbiddenNoAuthorizationGroups, ResponseErrorForbiddenNotAuthorized, getResponseErrorForbiddenNotAuthorized} from "../responses";
import {
getResponseErrorForbiddenNoAuthorizationGroups,
ResponseErrorForbiddenNoAuthorizationGroups,
ResponseErrorForbiddenNotAuthorized,
getResponseErrorForbiddenNotAuthorized,
} from "../responses";

describe("ResponseSuccessJson", () => {

it("should remove the kind property", () => {
const kindlessData = {
a: 1,
b: "2"
b: "2",
};

const kindedData = {
...kindlessData,
kind: "I_AM_UNIQUE"
kind: "I_AM_UNIQUE",
};

const mockResponse = MockResponse();
Expand All @@ -28,35 +36,103 @@ describe("ResponseSuccessJson", () => {
});

describe("ResponseErrorForbiddenNotAuthorized", () => {

it("should return the standard response", () => {
expect(getResponseErrorForbiddenNotAuthorized())
.toMatchObject(expect.objectContaining({kind: "IResponseErrorForbiddenNotAuthorized", detail: expect.stringContaining("You do not have enough permission to complete the operation you requested")}));
expect(ResponseErrorForbiddenNotAuthorized)
.toMatchObject(expect.objectContaining({kind: "IResponseErrorForbiddenNotAuthorized", detail: expect.stringContaining("You do not have enough permission to complete the operation you requested")}));
})
expect(getResponseErrorForbiddenNotAuthorized()).toMatchObject(
expect.objectContaining({
kind: "IResponseErrorForbiddenNotAuthorized",
detail: expect.stringContaining(
"You do not have enough permission to complete the operation you requested"
),
})
);
expect(ResponseErrorForbiddenNotAuthorized).toMatchObject(
expect.objectContaining({
kind: "IResponseErrorForbiddenNotAuthorized",
detail: expect.stringContaining(
"You do not have enough permission to complete the operation you requested"
),
})
);
});

it("should return a response with a custom detail", () => {
const customDetail = "aCustomDetail";
expect(getResponseErrorForbiddenNotAuthorized(customDetail))
.toMatchObject(expect.objectContaining({kind: "IResponseErrorForbiddenNotAuthorized", detail: expect.stringContaining(customDetail)}));
})

})
expect(getResponseErrorForbiddenNotAuthorized(customDetail)).toMatchObject(
expect.objectContaining({
kind: "IResponseErrorForbiddenNotAuthorized",
detail: expect.stringContaining(customDetail),
})
);
});
});

describe("ResponseErrorForbiddenNoAuthorizationGroups ", () => {

it("should return the standard response", () => {
expect(getResponseErrorForbiddenNoAuthorizationGroups())
.toMatchObject(expect.objectContaining({kind: "IResponseErrorForbiddenNoAuthorizationGroups", detail: expect.stringContaining("You are not part of any valid scope, you should ask the administrator to give you the required permissions.")}));
expect(ResponseErrorForbiddenNoAuthorizationGroups)
.toMatchObject(expect.objectContaining({kind: "IResponseErrorForbiddenNoAuthorizationGroups", detail: expect.stringContaining("You are not part of any valid scope, you should ask the administrator to give you the required permissions.")}));
})
expect(getResponseErrorForbiddenNoAuthorizationGroups()).toMatchObject(
expect.objectContaining({
kind: "IResponseErrorForbiddenNoAuthorizationGroups",
detail: expect.stringContaining(
"You are not part of any valid scope, you should ask the administrator to give you the required permissions."
),
})
);
expect(ResponseErrorForbiddenNoAuthorizationGroups).toMatchObject(
expect.objectContaining({
kind: "IResponseErrorForbiddenNoAuthorizationGroups",
detail: expect.stringContaining(
"You are not part of any valid scope, you should ask the administrator to give you the required permissions."
),
})
);
});

it("should return a response with a custom detail", () => {
const customDetail = "aCustomDetail";
expect(getResponseErrorForbiddenNoAuthorizationGroups(customDetail))
.toMatchObject(expect.objectContaining({kind: "IResponseErrorForbiddenNoAuthorizationGroups", detail: expect.stringContaining(customDetail)}));
})
expect(
getResponseErrorForbiddenNoAuthorizationGroups(customDetail)
).toMatchObject(
expect.objectContaining({
kind: "IResponseErrorForbiddenNoAuthorizationGroups",
detail: expect.stringContaining(customDetail),
})
);
});
});

describe("ResponseErrorPreconditionFailed", () => {
const expectedDetail = "The precondition are failed";
it("should return the standard response with a detail", () => {
expect(ResponseErrorPreconditionFailed(expectedDetail)).toMatchObject(
expect.objectContaining({
kind: "IResponseErrorPreconditionFailed",
detail: expect.stringContaining(expectedDetail),
})
);
});

})
it("should return a response with detail and custom problemType", () => {
const customProblemType = "https://customproblemtype.com";
const mockResponse = MockResponse();
const response = ResponseErrorPreconditionFailed(
expectedDetail,
customProblemType
);
expect(response).toMatchObject(
expect.objectContaining({
kind: "IResponseErrorPreconditionFailed",
detail: expect.stringContaining(expectedDetail),
})
);

response.apply(mockResponse);

expect(mockResponse.json).toHaveBeenCalledWith(
expect.objectContaining({
status: HttpStatusCodeEnum.HTTP_STATUS_412,
type: customProblemType,
detail: expect.stringContaining(expectedDetail),
title: "Precondition Failed",
})
);
});
});
6 changes: 4 additions & 2 deletions src/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,15 @@ export type IResponseErrorPreconditionFailed =
*/
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
export function ResponseErrorPreconditionFailed(
detail: string
detail: string,
problemType?: string
): IResponseErrorPreconditionFailed {
return {
...ResponseErrorGeneric(
HttpStatusCodeEnum.HTTP_STATUS_412,
"Precondition Failed",
detail
detail,
problemType
),
kind: "IResponseErrorPreconditionFailed",
};
Expand Down

0 comments on commit 35d267e

Please sign in to comment.