From 43c5e3e5aa89290deb9be3765b757acf60cb87d5 Mon Sep 17 00:00:00 2001 From: Simone Infante Date: Tue, 6 Oct 2020 18:41:39 +0200 Subject: [PATCH] Refactoring accorting to lint --- .../__tests__/handler.test.ts | 18 ++--- .../__tests__/handler.test.ts | 20 ++--- SendValidationEmailActivity/index.ts | 6 +- StoreSpidLogs/__test__/index.test.ts | 34 ++++----- utils/__tests__/config.test.ts | 73 ++++++++----------- utils/config.ts | 30 ++++---- utils/email.ts | 4 +- 7 files changed, 89 insertions(+), 96 deletions(-) diff --git a/HandleNHNotificationCallActivity/__tests__/handler.test.ts b/HandleNHNotificationCallActivity/__tests__/handler.test.ts index dd91c9fc..30849735 100644 --- a/HandleNHNotificationCallActivity/__tests__/handler.test.ts +++ b/HandleNHNotificationCallActivity/__tests__/handler.test.ts @@ -2,25 +2,25 @@ // tslint:disable-next-line: no-object-mutation process.env = { ...process.env, - MESSAGE_CONTAINER_NAME: "msg", - QueueStorageConnection: "foobar", AZURE_NH_ENDPOINT: "Endpoint=sb://anendpoint.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=XXX", AZURE_NH_HUB_NAME: "AZURE_NH_HUB_NAME", - SUBSCRIPTIONS_FEED_TABLE: "feed", - CUSTOMCONNSTR_COSMOSDB_KEY: "key", - CUSTOMCONNSTR_COSMOSDB_URI: "uri", + COSMOSDB_KEY: "key", COSMOSDB_NAME: "cosmoname", COSMOSDB_URI: "uri", - COSMOSDB_KEY: "key", + CUSTOMCONNSTR_COSMOSDB_KEY: "key", + CUSTOMCONNSTR_COSMOSDB_URI: "uri", FUNCTIONS_PUBLIC_URL: "url", - PUBLIC_API_URL: "url", - PUBLIC_API_KEY: "key", MAILHOG_HOSTNAME: "mailhog", MAIL_FROM: "mail@example.it", + MESSAGE_CONTAINER_NAME: "msg", NODE_ENV: "dev", + PUBLIC_API_KEY: "key", + PUBLIC_API_URL: "url", + QueueStorageConnection: "foobar", REQ_SERVICE_ID: "req_id_dev", - SPID_LOGS_PUBLIC_KEY: "key" + SPID_LOGS_PUBLIC_KEY: "key", + SUBSCRIPTIONS_FEED_TABLE: "feed" }; import { NonEmptyString } from "italia-ts-commons/lib/strings"; diff --git a/HandleNHNotificationCallOrchestrator/__tests__/handler.test.ts b/HandleNHNotificationCallOrchestrator/__tests__/handler.test.ts index 8f2efe70..c64682ca 100644 --- a/HandleNHNotificationCallOrchestrator/__tests__/handler.test.ts +++ b/HandleNHNotificationCallOrchestrator/__tests__/handler.test.ts @@ -2,25 +2,25 @@ // tslint:disable-next-line: no-object-mutation process.env = { ...process.env, - QueueStorageConnection: "foobar", - MESSAGE_CONTAINER_NAME: "msg", - SUBSCRIPTIONS_FEED_TABLE: "feed", - CUSTOMCONNSTR_COSMOSDB_KEY: "key", - CUSTOMCONNSTR_COSMOSDB_URI: "uri", - COSMOSDB_NAME: "cosmoname", - COSMOSDB_URI: "uri", AZURE_NH_ENDPOINT: "Endpoint=sb://anendpoint.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=C4xIzNZv4VrUnu5jkmPH635MApRUj8wABky8VfduYqg=", AZURE_NH_HUB_NAME: "AZURE_NH_HUB_NAME", COSMOSDB_KEY: "key", + COSMOSDB_NAME: "cosmoname", + COSMOSDB_URI: "uri", + CUSTOMCONNSTR_COSMOSDB_KEY: "key", + CUSTOMCONNSTR_COSMOSDB_URI: "uri", FUNCTIONS_PUBLIC_URL: "url", - PUBLIC_API_URL: "url", - PUBLIC_API_KEY: "key", MAILHOG_HOSTNAME: "mailhog", MAIL_FROM: "mail@example.it", + MESSAGE_CONTAINER_NAME: "msg", NODE_ENV: "dev", + PUBLIC_API_KEY: "key", + PUBLIC_API_URL: "url", + QueueStorageConnection: "foobar", REQ_SERVICE_ID: "req_id_dev", - SPID_LOGS_PUBLIC_KEY: "key" + SPID_LOGS_PUBLIC_KEY: "key", + SUBSCRIPTIONS_FEED_TABLE: "feed" }; import { NonEmptyString } from "italia-ts-commons/lib/strings"; import { context as contextMock } from "../../__mocks__/durable-functions"; diff --git a/SendValidationEmailActivity/index.ts b/SendValidationEmailActivity/index.ts index 9cf2608d..86a8e8c4 100644 --- a/SendValidationEmailActivity/index.ts +++ b/SendValidationEmailActivity/index.ts @@ -21,9 +21,9 @@ const config = getConfigOrThrow(); const isProduction = config.NODE_ENV === "production"; // Optional SendGrid key -const sendgridApiKey = NonEmptyString.decode( - config.SENDGRID_API_KEY -).getOrElse(undefined); +const sendgridApiKey = NonEmptyString.decode(config.SENDGRID_API_KEY).getOrElse( + undefined +); // Mailup const mailupUsername = config.MAILUP_USERNAME; diff --git a/StoreSpidLogs/__test__/index.test.ts b/StoreSpidLogs/__test__/index.test.ts index 4eab1b9e..4b86db05 100644 --- a/StoreSpidLogs/__test__/index.test.ts +++ b/StoreSpidLogs/__test__/index.test.ts @@ -2,10 +2,23 @@ /* tslint:disable:no-object-mutation */ process.env = { - UBSCRIPTIONS_FEED_TABLE: "feed", APPINSIGHTS_INSTRUMENTATIONKEY: "foo", + AZURE_NH_ENDPOINT: "azendpoint", + AZURE_NH_HUB_NAME: "azhub", + COSMOSDB_KEY: "key", + COSMOSDB_NAME: "cosmoname", + COSMOSDB_URI: "uri", + CUSTOMCONNSTR_COSMOSDB_KEY: "key", + CUSTOMCONNSTR_COSMOSDB_URI: "uri", + FUNCTIONS_PUBLIC_URL: "url", + MAILHOG_HOSTNAME: "mailhog", + MAIL_FROM: "mail@example.it", + MESSAGE_CONTAINER_NAME: "msg", + NODE_ENV: "dev", + PUBLIC_API_KEY: "key", + PUBLIC_API_URL: "url", QueueStorageConnection: "foobar", - SUBSCRIPTIONS_FEED_TABLE: "feed", + REQ_SERVICE_ID: "req_id_dev", SPID_BLOB_CONTAINER_NAME: "spidblob", SPID_BLOB_STORAGE_CONNECTION_STRING: "foobar", SPID_LOGS_PUBLIC_KEY: `-----BEGIN PUBLIC KEY----- @@ -14,21 +27,8 @@ l09Fs0z2U4aA37BrXlSo1DwQ2O9i2XFxXGJmE83siSWEfRlMWlabMu7Yj6dkZvmj dGIO4gotO33TgiAQcwRo+4pwjoCN7Td47yssCcj9C727zBt+Br+XK7B1bRcqjc0J YdF4yiVtD7G4RDXmRQIDAQAB -----END PUBLIC KEY-----`, - MESSAGE_CONTAINER_NAME: "msg", - CUSTOMCONNSTR_COSMOSDB_URI: "uri", - CUSTOMCONNSTR_COSMOSDB_KEY: "key", - COSMOSDB_NAME: "cosmoname", - COSMOSDB_URI: "uri", - COSMOSDB_KEY: "key", - FUNCTIONS_PUBLIC_URL: "url", - PUBLIC_API_URL: "url", - PUBLIC_API_KEY: "key", - MAILHOG_HOSTNAME: "mailhog", - MAIL_FROM: "mail@example.it", - NODE_ENV: "dev", - REQ_SERVICE_ID: "req_id_dev", - AZURE_NH_HUB_NAME: "azhub", - AZURE_NH_ENDPOINT: "azendpoint", + SUBSCRIPTIONS_FEED_TABLE: "feed", + UBSCRIPTIONS_FEED_TABLE: "feed" }; import { format } from "date-fns"; diff --git a/utils/__tests__/config.test.ts b/utils/__tests__/config.test.ts index 3eb664a0..9b42c1b4 100644 --- a/utils/__tests__/config.test.ts +++ b/utils/__tests__/config.test.ts @@ -1,9 +1,13 @@ import { Either } from "fp-ts/lib/Either"; -import { IConfig, MailerConfig } from "../config"; +import { MailerConfig } from "../config"; const aMailFrom = "example@test.com"; +const mailSecret = "a-mu-secret"; +const mailUsername = "a-mu-username"; +const devEnv = "dev"; +const prodEnv = "production"; -const noop = () => {}; +const noop = () => null; const expectRight = (e: Either, t: (r: R) => void = noop) => e.fold( _ => @@ -22,7 +26,7 @@ describe("MailerConfig", () => { it("should decode configuration for sendgrid", () => { const rawConf = { MAIL_FROM: aMailFrom, - NODE_ENV: "production", + NODE_ENV: prodEnv, SENDGRID_API_KEY: "a-sg-key" }; const result = MailerConfig.decode(rawConf); @@ -35,11 +39,11 @@ describe("MailerConfig", () => { it("should decode configuration for sendgrid even if mailup conf is passed", () => { const rawConf = { + MAILUP_SECRET: mailSecret, + MAILUP_USERNAME: mailUsername, MAIL_FROM: aMailFrom, - NODE_ENV: "production", - SENDGRID_API_KEY: "a-sg-key", - MAILUP_USERNAME: "a-mu-username", - MAILUP_SECRET: "a-mu-secret" + NODE_ENV: prodEnv, + SENDGRID_API_KEY: "a-sg-key" }; const result = MailerConfig.decode(rawConf); @@ -50,10 +54,10 @@ describe("MailerConfig", () => { it("should decode configuration for mailup", () => { const rawConf = { + MAILUP_SECRET: mailSecret, + MAILUP_USERNAME: mailUsername, MAIL_FROM: aMailFrom, - NODE_ENV: "production", - MAILUP_USERNAME: "a-mu-username", - MAILUP_SECRET: "a-mu-secret" + NODE_ENV: prodEnv }; const result = MailerConfig.decode(rawConf); @@ -77,8 +81,8 @@ describe("MailerConfig", () => { const rawConf = { MAIL_FROM: aMailFrom, - NODE_ENV: "production", - MAIL_TRANSPORTS: [aRawTrasport, aRawTrasport].join(";") + MAIL_TRANSPORTS: [aRawTrasport, aRawTrasport].join(";"), + NODE_ENV: prodEnv }; const result = MailerConfig.decode(rawConf); @@ -89,9 +93,9 @@ describe("MailerConfig", () => { it("should decode configuration for mailhog", () => { const rawConf = { + MAILHOG_HOSTNAME: "a-mh-host", MAIL_FROM: aMailFrom, - NODE_ENV: "dev", - MAILHOG_HOSTNAME: "a-mh-host" + NODE_ENV: devEnv }; const result = MailerConfig.decode(rawConf); @@ -103,7 +107,7 @@ describe("MailerConfig", () => { it("should require mailhog if not in prod", () => { const rawConf = { MAIL_FROM: aMailFrom, - NODE_ENV: "dev" + NODE_ENV: devEnv }; const result = MailerConfig.decode(rawConf); @@ -113,7 +117,7 @@ describe("MailerConfig", () => { it("should require at least on transporter if in prod", () => { const rawConf = { MAIL_FROM: aMailFrom, - NODE_ENV: "production" + NODE_ENV: prodEnv }; const result = MailerConfig.decode(rawConf); @@ -122,9 +126,9 @@ describe("MailerConfig", () => { it("should not allow mailhog if in prod", () => { const rawConf = { + MAILHOG_HOSTNAME: "a-mh-host", MAIL_FROM: aMailFrom, - NODE_ENV: "production", - MAILHOG_HOSTNAME: "a-mh-host" + NODE_ENV: prodEnv }; const result = MailerConfig.decode(rawConf); @@ -134,8 +138,8 @@ describe("MailerConfig", () => { it("should not decode configuration with empty transport", () => { const rawConf = { MAIL_FROM: aMailFrom, - NODE_ENV: "production", - MAIL_TRANSPORTS: "" + MAIL_TRANSPORTS: "", + NODE_ENV: prodEnv }; const result = MailerConfig.decode(rawConf); @@ -152,30 +156,17 @@ describe("MailerConfig", () => { }); it("should not decode ambiguos configuration", () => { - const withMailUp = { - MAILUP_USERNAME: "a-mu-username", - MAILUP_SECRET: "a-mu-secret" - }; - const withSendGrid = { - SENDGRID_API_KEY: "a-sg-key" - }; - const withMultiTransport = { - MAIL_TRANSPORTS: "a-trasnport-name" - }; - const base = { + const rawConf = { + MAILUP_SECRET: mailSecret, + MAILUP_USERNAME: mailUsername, MAIL_FROM: aMailFrom, - NODE_ENV: "production" + MAIL_TRANSPORTS: "a-trasnport-name", + NODE_ENV: prodEnv, + SENDGRID_API_KEY: "a-sg-key" }; - const examples = [ - // the following configuration is not ambiguos as sendgrid would override mailup anyway - // see here for the rationale: https://github.com/pagopa/io-functions-admin/pull/89#commitcomment-42917672 - // { ...base, ...withMailUp, ...withSendGrid }, - { ...base, ...withMultiTransport, ...withSendGrid }, - { ...base, ...withMailUp, ...withMultiTransport }, - { ...base, ...withMailUp, ...withSendGrid, ...withMultiTransport } - ]; + const result = MailerConfig.decode(rawConf); - examples.map(MailerConfig.decode).forEach(_ => expectLeft(_)); + expectLeft(result); }); }); diff --git a/utils/config.ts b/utils/config.ts index 87e20bc7..83111b7e 100644 --- a/utils/config.ts +++ b/utils/config.ts @@ -1,4 +1,3 @@ -import { nonEmptyArray } from "fp-ts/lib/NonEmptyArray2v"; /** * Config module * @@ -91,12 +90,12 @@ export const MailerConfig = t.intersection([ export type ReqServiceIdConfig = t.TypeOf; export const ReqServiceIdConfig = t.union([ t.interface({ - REQ_SERVICE_ID: t.undefined, - NODE_ENV: t.literal("production") + NODE_ENV: t.literal("production"), + REQ_SERVICE_ID: t.undefined }), t.interface({ - REQ_SERVICE_ID: NonEmptyString, NODE_ENV: AnyBut("production", t.string), + REQ_SERVICE_ID: NonEmptyString }) ]); @@ -104,24 +103,27 @@ export const ReqServiceIdConfig = t.union([ export type IConfig = t.TypeOf; export const IConfig = t.intersection([ t.interface({ - QueueStorageConnection: NonEmptyString, - MESSAGE_CONTAINER_NAME: NonEmptyString, - SUBSCRIPTIONS_FEED_TABLE: NonEmptyString, + AZURE_NH_ENDPOINT: NonEmptyString, + AZURE_NH_HUB_NAME: NonEmptyString, - CUSTOMCONNSTR_COSMOSDB_KEY: NonEmptyString, - CUSTOMCONNSTR_COSMOSDB_URI: NonEmptyString, + COSMOSDB_KEY: NonEmptyString, COSMOSDB_NAME: NonEmptyString, COSMOSDB_URI: NonEmptyString, - COSMOSDB_KEY: NonEmptyString, + + CUSTOMCONNSTR_COSMOSDB_KEY: NonEmptyString, + CUSTOMCONNSTR_COSMOSDB_URI: NonEmptyString, FUNCTIONS_PUBLIC_URL: NonEmptyString, - PUBLIC_API_URL: NonEmptyString, + + MESSAGE_CONTAINER_NAME: NonEmptyString, + PUBLIC_API_KEY: NonEmptyString, + PUBLIC_API_URL: NonEmptyString, - SPID_LOGS_PUBLIC_KEY: NonEmptyString, + QueueStorageConnection: NonEmptyString, - AZURE_NH_HUB_NAME: NonEmptyString, - AZURE_NH_ENDPOINT: NonEmptyString, + SPID_LOGS_PUBLIC_KEY: NonEmptyString, + SUBSCRIPTIONS_FEED_TABLE: NonEmptyString, isProduction: t.boolean }), diff --git a/utils/email.ts b/utils/email.ts index 8eff8421..803b9285 100644 --- a/utils/email.ts +++ b/utils/email.ts @@ -30,7 +30,7 @@ const fetchWithTimeout = setFetchTimeout( abortableFetch ); -const config = getConfigOrThrow(); +const conf = getConfigOrThrow(); interface IMailUpOptions { mailupSecret: NonEmptyString; @@ -64,7 +64,7 @@ export function getMailerTransporter(opts: MailTransportOptions): Mail { : // For development we use mailhog to intercept emails // Use the `docker-compose.yml` file to run the mailhog server NodeMailer.createTransport({ - host: config.MAILHOG_HOSTNAME || "localhost", + host: conf.MAILHOG_HOSTNAME || "localhost", port: 1025, secure: false });