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

[#IOCOM-917] Add new RC model #357

Merged
merged 9 commits into from
Jan 24, 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
135 changes: 135 additions & 0 deletions src/models/__tests__/rc_configuration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import * as E from "fp-ts/lib/Either";
import { NonEmptyString, Ulid } from "@pagopa/ts-commons/lib/strings";
import {
RCConfigurationBase,
RCConfiguration,
RetrievedRCConfiguration
} from "../rc_configuration";
import { Has_preconditionEnum } from "../../../generated/definitions/ThirdPartyData";
import { NonNegativeInteger } from "@pagopa/ts-commons/lib/numbers";

const aRemoteContentEnvironmentConfiguration = {
baseUrl: "https://anydomain.anytld/api/v1/anyapi" as NonEmptyString,
detailsAuthentication: {
headerKeyName: "X-Functions-Key" as NonEmptyString,
key: "anykey" as NonEmptyString,
type: "API_KEY" as NonEmptyString
}
};

const aRemoteContentConfigurationWithNoEnv: RCConfigurationBase = {
userId: "aUserId" as NonEmptyString,
configurationId: "01HMRBX079WA5SGYBQP1A7FSKH" as Ulid,
name: "aName" as NonEmptyString,
description: "a simple description" as NonEmptyString,
hasPrecondition: Has_preconditionEnum.ALWAYS,
disableLollipopFor: [],
isLollipopEnabled: false
};

const aRemoteContentConfigurationWithProdEnv: RCConfiguration = {
...aRemoteContentConfigurationWithNoEnv,
prodEnvironment: aRemoteContentEnvironmentConfiguration
};

const aRetrievedRemoteContentConfigurationWithProdEnv: RetrievedRCConfiguration = {
...aRemoteContentConfigurationWithProdEnv,
id: `${aRemoteContentConfigurationWithProdEnv.configurationId}-${"0".repeat(
16
)}` as NonEmptyString,
version: 0 as NonNegativeInteger,
_etag: "_etag",
_rid: "_rid",
_self: "_self",
_ts: 1
};

const aRemoteContentConfigurationWithTestEnv: RCConfiguration = {
...aRemoteContentConfigurationWithNoEnv,
testEnvironment: {
...aRemoteContentEnvironmentConfiguration,
testUsers: []
}
};

const aRetrievedRemoteContentConfigurationWithTestEnv: RetrievedRCConfiguration = {
...aRemoteContentConfigurationWithTestEnv,
id: `${aRemoteContentConfigurationWithTestEnv.configurationId}-${"0".repeat(
16
)}` as NonEmptyString,
version: 0 as NonNegativeInteger,
_etag: "_etag",
_rid: "_rid",
_self: "_self",
_ts: 1
};

const aRemoteContentConfigurationWithBothEnv: RCConfiguration = {
...aRemoteContentConfigurationWithNoEnv,
prodEnvironment: aRemoteContentEnvironmentConfiguration,
testEnvironment: {
...aRemoteContentEnvironmentConfiguration,
testUsers: []
}
};

const aRetrievedRemoteContentConfigurationWithBothEnv: RetrievedRCConfiguration = {
...aRemoteContentConfigurationWithBothEnv,
id: `${aRemoteContentConfigurationWithProdEnv.configurationId}-${"0".repeat(
16
)}` as NonEmptyString,
version: 0 as NonNegativeInteger,
_etag: "_etag",
_rid: "_rid",
_self: "_self",
_ts: 1
};

describe("RC", () => {
it("GIVEN a valid RC object with test environment WHEN the object is decoded THEN the decode succeed", async () => {
const result = RCConfiguration.decode(
aRemoteContentConfigurationWithTestEnv
);
expect(E.isRight(result)).toBeTruthy();
});

it("GIVEN a retrieved RC object with test environment WHEN the object is decoded THEN the decode succeed", async () => {
const result = RCConfiguration.decode(
aRetrievedRemoteContentConfigurationWithTestEnv
);
expect(E.isRight(result)).toBeTruthy();
});

it("GIVEN a valid RC object with prod environment WHEN the object is decoded THEN the decode succeed", async () => {
const result = RCConfiguration.decode(
aRemoteContentConfigurationWithProdEnv
);
expect(E.isRight(result)).toBeTruthy();
});

it("GIVEN a retrieved RC object with prod environment WHEN the object is decoded THEN the decode succeed", async () => {
const result = RCConfiguration.decode(
aRetrievedRemoteContentConfigurationWithProdEnv
);
expect(E.isRight(result)).toBeTruthy();
});

it("GIVEN a valid RC object with both environments WHEN the object is decoded THEN the decode succeed", async () => {
const result = RCConfiguration.decode(
aRemoteContentConfigurationWithBothEnv
);
expect(E.isRight(result)).toBeTruthy();
});

it("GIVEN a retrieved RC object with both environment WHEN the object is decoded THEN the decode succeed", async () => {
const result = RCConfiguration.decode(
aRetrievedRemoteContentConfigurationWithBothEnv
);
expect(E.isRight(result)).toBeTruthy();
});

it("GIVEN a not valid RC object with no environments WHEN the object is decoded THEN the decode fail", async () => {
const result = RCConfiguration.decode(aRemoteContentConfigurationWithNoEnv);
expect(E.isLeft(result)).toBeTruthy();
});
});
123 changes: 0 additions & 123 deletions src/models/__tests__/remote_content_configuration.test.ts

This file was deleted.

110 changes: 110 additions & 0 deletions src/models/rc_configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import * as t from "io-ts";

import {
FiscalCode,
NonEmptyString,
Ulid
} from "@pagopa/ts-commons/lib/strings";
import { enumType } from "@pagopa/ts-commons/lib/types";
import { Container } from "@azure/cosmos";
import {
CosmosdbModelVersioned,
RetrievedVersionedModel
} from "../utils/cosmosdb_model_versioned";
import { Has_preconditionEnum } from "../../generated/definitions/ThirdPartyData";

export const RC_CONFIGURATION_COLLECTION_NAME = "remote-content-configuration";

const RC_CONFIGURATION_MODEL_PK_FIELD = "configurationId";

export const RCClientCert = t.interface({
clientCert: NonEmptyString,
clientKey: NonEmptyString,
serverCa: NonEmptyString
});

const RCAuthenticationDetails = t.interface({
headerKeyName: NonEmptyString,
key: NonEmptyString,
type: NonEmptyString
});

export type RCAuthenticationConfig = t.TypeOf<typeof RCAuthenticationConfig>;
export const RCAuthenticationConfig = t.intersection([
RCAuthenticationDetails,
t.partial({ cert: RCClientCert })
]);

export type RCEnvironmentConfig = t.TypeOf<typeof RCEnvironmentConfig>;
export const RCEnvironmentConfig = t.interface({
baseUrl: NonEmptyString,
detailsAuthentication: RCAuthenticationConfig
});

export type RCTestEnvironmentConfig = t.TypeOf<typeof RCTestEnvironmentConfig>;
export const RCTestEnvironmentConfig = t.intersection([
t.interface({
testUsers: t.readonlyArray(FiscalCode)
}),
RCEnvironmentConfig
]);

const RCConfigurationR = t.interface({
configurationId: Ulid,
description: NonEmptyString,
disableLollipopFor: t.readonlyArray(FiscalCode),
hasPrecondition: enumType<Has_preconditionEnum>(
Has_preconditionEnum,
"hasPrecondition"
),
isLollipopEnabled: t.boolean,
name: NonEmptyString,
userId: NonEmptyString
});

const RCConfigurationO = t.partial({});

export const RCConfigurationBase = t.intersection([
RCConfigurationR,
RCConfigurationO
]);
export type RCConfigurationBase = t.TypeOf<typeof RCConfigurationBase>;

export type RCConfiguration = t.TypeOf<typeof RCConfiguration>;
export const RCConfiguration = t.intersection([
RCConfigurationBase,
t.union([
t.intersection([
t.interface({ prodEnvironment: RCEnvironmentConfig }),
t.partial({ testEnvironment: RCTestEnvironmentConfig })
]),
t.intersection([
t.partial({ prodEnvironment: RCEnvironmentConfig }),
t.interface({ testEnvironment: RCTestEnvironmentConfig })
])
])
]);

export const RetrievedRCConfiguration = t.intersection([
RCConfiguration,
RetrievedVersionedModel
]);
export type RetrievedRCConfiguration = t.TypeOf<
typeof RetrievedRCConfiguration
>;

export class RCConfigurationModel extends CosmosdbModelVersioned<
RCConfiguration,
RCConfiguration,
RetrievedRCConfiguration,
typeof RC_CONFIGURATION_MODEL_PK_FIELD
> {
constructor(container: Container) {
super(
container,
RCConfiguration,
RetrievedRCConfiguration,
RC_CONFIGURATION_MODEL_PK_FIELD
);
}
}
3 changes: 3 additions & 0 deletions src/models/remote_content_configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export type RetrievedRemoteContentConfiguration = t.TypeOf<
typeof RetrievedRemoteContentConfiguration
>;

/**
* @deprecated This model is deprecated, use the one inside ./remote_content.ts instead
*/
export class RemoteContentConfigurationModel extends CosmosdbModel<
RemoteContentConfiguration,
RemoteContentConfiguration,
Expand Down