Skip to content

Commit

Permalink
Adds tests for canWriteMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudify committed Jul 24, 2019
1 parent d7ab43b commit 899f48f
Show file tree
Hide file tree
Showing 5 changed files with 2,721 additions and 43 deletions.
132 changes: 132 additions & 0 deletions CreateMessage/__tests__/handler.test.ts
@@ -0,0 +1,132 @@
import * as fc from "fast-check";

import { FiscalCode } from "italia-ts-commons/lib/strings";

import { UserGroup } from "io-functions-commons/dist/src/utils/middlewares/azure_api_auth";

import { canWriteMessage } from "../handler";

//
// custom fastcheck arbitraries
//

const upperCaseAlphaArb = fc.subarray(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),
1,
1
);

const fiscalCodeAlphaNumArb = fc.subarray(
"0123456789LMNPQRSTUV".split(""),
1,
1
);

const fiscalCodeControlAlphaArb = fc.subarray("ABCDEHLMPRST".split(""), 1, 1);

// generate a fiscal code that matches FiscalCode's regexp
// note that this may not be an actual valid fiscal code as the last
// control digit needs to be calculated while here we just generate
// one randomly.
const fiscalCodeArb = fc
.tuple(
fc.array(upperCaseAlphaArb, 6, 6).map(_ => _.join("")),
fc.array(fiscalCodeAlphaNumArb, 2, 2).map(_ => _.join("")),
fiscalCodeControlAlphaArb,
fc.array(fiscalCodeAlphaNumArb, 2, 2).map(_ => _.join("")),
upperCaseAlphaArb,
fc.array(fiscalCodeAlphaNumArb, 3, 3).map(_ => _.join("")),
upperCaseAlphaArb
)
.map(sx => sx.join("") as FiscalCode);

const fiscalCodeArrayArb = fc.array(fiscalCodeArb);

const fiscalCodeSetArb = fiscalCodeArrayArb.map(_ => new Set(_));

//
// tests
//

describe("canWriteMessage", () => {
it("should respond with ResponseErrorForbiddenNotAuthorizedForProduction when service is in no group", () => {
fc.assert(
fc.property(
fiscalCodeArrayArb,
fiscalCodeArb,
fc.boolean(),
(authorizedRecipients, recipient, isAuthorized) => {
const response = canWriteMessage(
new Set(), // no groups
new Set(
authorizedRecipients.concat(isAuthorized ? [recipient] : [])
), // any authorized recipient, possibly also the current one
recipient // one random recipient
);
expect(response.isLeft()).toBeTruthy();
if (response.isLeft()) {
expect(response.value.kind).toEqual(
"IResponseErrorForbiddenNotAuthorizedForProduction"
);
}
}
)
);
});

it("should respond with ResponseErrorForbiddenNotAuthorizedForRecipient when service is trying to send message to an unauthorized recipient", () => {
fc.assert(
fc.property(
fiscalCodeArrayArb,
fiscalCodeArb,
(authorizedRecipients, recipient) => {
const response = canWriteMessage(
new Set([UserGroup.ApiLimitedMessageWrite]),
new Set(authorizedRecipients.filter(_ => _ !== recipient)), // current recipient is not authorized
recipient
);
expect(response.isLeft()).toBeTruthy();
if (response.isLeft()) {
expect(response.value.kind).toEqual(
"IResponseErrorForbiddenNotAuthorizedForRecipient"
);
}
}
)
);
});

it("should pass when service is trying to send message to an authorized recipient", () => {
fc.assert(
fc.property(
fiscalCodeArrayArb,
fiscalCodeArb,
(authorizedRecipients, recipient) => {
const response = canWriteMessage(
new Set([UserGroup.ApiLimitedMessageWrite]),
new Set([...authorizedRecipients, recipient]), // current recipient always authorized
recipient
);
expect(response.isRight()).toBeTruthy();
}
)
);
});

it("should pass when service can send messages to any recipient", () => {
fc.assert(
fc.property(
fiscalCodeSetArb,
fiscalCodeArb,
(authorizedRecipients, recipient) => {
const response = canWriteMessage(
new Set([UserGroup.ApiMessageWrite]),
authorizedRecipients,
recipient
);
expect(response.isRight()).toBeTruthy();
}
)
);
});
});
2 changes: 1 addition & 1 deletion CreateMessage/handler.ts
Expand Up @@ -137,7 +137,7 @@ type CreateMessageHandlerResponse = PromiseType<
/**
* Checks whether the client service can create a new message for the recipient
*/
const canWriteMessage = (
export const canWriteMessage = (
authGroups: IAzureApiAuthorization["groups"],
authorizedRecipients: IAzureUserAttributes["service"]["authorizedRecipients"],
fiscalCode: FiscalCode
Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
22 changes: 13 additions & 9 deletions package.json
Expand Up @@ -9,7 +9,7 @@
"prestart": "npm run build && func extensions install",
"start:host": "func start",
"start": "npm-run-all --parallel start:host watch",
"test": "echo \"No tests yet...\"",
"test": "jest",
"lint": "tslint -p ."
},
"description": "",
Expand All @@ -18,29 +18,33 @@
"@types/cors": "^2.8.4",
"@types/documentdb": "^1.10.5",
"@types/express": "^4.16.0",
"@types/nodemailer": "^4.6.2",
"@types/html-to-text": "^1.4.31",
"@types/jest": "^24.0.15",
"@types/nodemailer": "^4.6.2",
"azurite": "^3.1.2-preview",
"danger": "^4.0.2",
"danger-plugin-digitalcitizenship": "^0.3.1",
"fast-check": "^1.16.0",
"italia-tslint-rules": "^1.1.3",
"jest": "^24.8.0",
"nodemailer": "^4.6.7",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"ts-jest": "^24.0.2",
"tslint": "^5.17.0",
"typescript": "^3.3.3",
"nodemailer": "^4.6.7",
"danger": "^4.0.2",
"danger-plugin-digitalcitizenship": "^0.3.1"
"typescript": "^3.3.3"
},
"dependencies": {
"cors": "^2.8.4",
"documentdb": "^1.12.2",
"durable-functions": "^1.2.2",
"express": "^4.15.3",
"fp-ts": "1.12.0",
"html-to-text": "^4.0.0",
"io-functions-commons": "^0.3.7",
"io-functions-express": "^0.1.0",
"io-ts": "1.8.5",
"italia-ts-commons": "^5.1.5",
"winston": "^3.2.1",
"html-to-text": "^4.0.0"
"winston": "^3.2.1"
}
}
}

0 comments on commit 899f48f

Please sign in to comment.