Skip to content

Commit

Permalink
Refactoring accorting to lint
Browse files Browse the repository at this point in the history
  • Loading branch information
infantesimone committed Oct 6, 2020
1 parent 3d56063 commit 43c5e3e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 96 deletions.
18 changes: 9 additions & 9 deletions HandleNHNotificationCallActivity/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
20 changes: 10 additions & 10 deletions HandleNHNotificationCallOrchestrator/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
6 changes: 3 additions & 3 deletions SendValidationEmailActivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 17 additions & 17 deletions StoreSpidLogs/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-----
Expand All @@ -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";
Expand Down
73 changes: 32 additions & 41 deletions utils/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -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 = <L, R>(e: Either<L, R>, t: (r: R) => void = noop) =>
e.fold(
_ =>
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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 = [

This comment has been minimized.

Copy link
@gunzip

gunzip Oct 6, 2020

Contributor

why did you removed this tests? (they look useful)

This comment has been minimized.

Copy link
@infantesimone

infantesimone Oct 7, 2020

Author Contributor

To avoid the tslint error with the examples array:

  • Only ReadonlyArray allowed.

So, in rawconf I put only one ambiguous configuration. To test multiple ambiguous configurations, can the array become ReadonlyArray?
In this case, do you need an interface to represent an ambiguous configuration, for example:

  • const examplas: ReadOnlyArray<AmbiguosInterface>.

This comment has been minimized.

Copy link
@gunzip

gunzip Oct 7, 2020

Contributor

you can safely disable (selectively) tslint in tests. I don't remember the exact syntax here but should be something alike:

// tslint:disable-next-line readonly-arrays

This comment has been minimized.

Copy link
@infantesimone

infantesimone Oct 7, 2020

Author Contributor

ok

// 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);
});
});
30 changes: 16 additions & 14 deletions utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { nonEmptyArray } from "fp-ts/lib/NonEmptyArray2v";
/**
* Config module
*
Expand Down Expand Up @@ -91,37 +90,40 @@ export const MailerConfig = t.intersection([
export type ReqServiceIdConfig = t.TypeOf<typeof ReqServiceIdConfig>;
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
})
]);

// global app configuration
export type IConfig = t.TypeOf<typeof IConfig>;
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
}),
Expand Down
4 changes: 2 additions & 2 deletions utils/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const fetchWithTimeout = setFetchTimeout(
abortableFetch
);

const config = getConfigOrThrow();
const conf = getConfigOrThrow();

interface IMailUpOptions {
mailupSecret: NonEmptyString;
Expand Down Expand Up @@ -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
});
Expand Down

0 comments on commit 43c5e3e

Please sign in to comment.