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

[#IOPAE-174]add possibility to create service with manage key #266

Merged
merged 1 commit into from
Feb 15, 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
38 changes: 37 additions & 1 deletion CreateService/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const anOrganizationFiscalCode = "01234567890" as OrganizationFiscalCode;
const anEmail = "test@example.com" as EmailString;

const aServiceId = "s123" as NonEmptyString;

const aManageSubscriptionId = "MANAGE-123" as NonEmptyString;
const aTokenName = "TOKEN_NAME" as NonEmptyString;

const someServicesMetadata: ServiceMetadata = {
Expand Down Expand Up @@ -111,6 +111,11 @@ const aUserAuthenticationDeveloper: IAzureApiAuthorization = {
userId: "u123" as NonEmptyString
};

const aUserAuthenticationDeveloperWithManageKey: IAzureApiAuthorization = {
...aUserAuthenticationDeveloper,
subscriptionId: aManageSubscriptionId
};

const aUserInfo: UserInfo = {
token_name: aTokenName
};
Expand Down Expand Up @@ -624,4 +629,35 @@ describe("CreateServiceHandler", () => {
expect(result.value).toHaveLength(2);
}
});

//MANAGE FLOW

it("should respond with a created service with subscriptionKeys using a Manage Key", async () => {
const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
sandboxFiscalCode
);
const result = await createServiceHandler(
mockContext,
aUserAuthenticationDeveloperWithManageKey,
undefined as any, // not used
someUserAttributes,
aServicePayload
);

expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).toHaveBeenCalledTimes(1);
expect(apiClientMock.getUser).toHaveBeenCalledTimes(1);
expect(mockAppinsights.trackEvent).toHaveBeenCalledTimes(1);
expect(result.kind).toBe("IResponseSuccessJson");
if (result.kind === "IResponseSuccessJson") {
expect(result.value).toEqual({
...aService,
...someSubscriptionKeys
});
}
});
});
13 changes: 11 additions & 2 deletions CreateService/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
IResponseErrorNotFound,
IResponseErrorTooManyRequests,
IResponseSuccessJson,
ResponseErrorForbiddenNotAuthorized,
ResponseSuccessJson
} from "@pagopa/ts-commons/lib/responses";

Expand Down Expand Up @@ -55,6 +56,11 @@ import { Service } from "@pagopa/io-functions-admin-sdk/Service";
import { Subscription } from "@pagopa/io-functions-admin-sdk/Subscription";
import { UserInfo } from "@pagopa/io-functions-admin-sdk/UserInfo";
import { StandardServiceCategoryEnum } from "@pagopa/io-functions-admin-sdk/StandardServiceCategory";
import { SequenceMiddleware } from "@pagopa/ts-commons/lib/sequence_middleware";
import {
AzureUserAttributesManageMiddleware,
IAzureUserAttributesManage
} from "@pagopa/io-functions-commons/dist/src/utils/middlewares/azure_user_attributes_manage";
import { APIClient } from "../clients/admin";
import { ServicePayload } from "../generated/definitions/ServicePayload";
import { ServiceWithSubscriptionKeys } from "../generated/definitions/ServiceWithSubscriptionKeys";
Expand Down Expand Up @@ -82,7 +88,7 @@ type ICreateServiceHandler = (
context: Context,
auth: IAzureApiAuthorization,
clientIp: ClientIp,
attrs: IAzureUserAttributes,
attrs: IAzureUserAttributes | IAzureUserAttributesManage,
servicePayload: ServicePayload
) => Promise<ResponseTypes>;

Expand Down Expand Up @@ -255,7 +261,10 @@ export function CreateService(
ContextMiddleware(),
AzureApiAuthMiddleware(new Set([UserGroup.ApiServiceWrite])),
ClientIpMiddleware,
AzureUserAttributesMiddleware(serviceModel),
SequenceMiddleware(ResponseErrorForbiddenNotAuthorized)(
AzureUserAttributesMiddleware(serviceModel),
AzureUserAttributesManageMiddleware()
),
RequiredBodyPayloadMiddleware(ServicePayload)
);
return wrapRequestHandler(
Expand Down