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 23, 2019
1 parent d7ab43b commit bb5bcef
Show file tree
Hide file tree
Showing 5 changed files with 2,629 additions and 42 deletions.
55 changes: 55 additions & 0 deletions CreateMessage/__tests__/handler.test.ts
@@ -0,0 +1,55 @@
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";

const aFiscalCode = "FRLFRC74E04B157I" as FiscalCode;

describe("canWriteMessage", () => {
it("should respond with ResponseErrorForbiddenNotAuthorizedForProduction when service is in no group", () => {
const response = canWriteMessage(
new Set(),
new Set([aFiscalCode]),
aFiscalCode
);
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", () => {
const response = canWriteMessage(
new Set([UserGroup.ApiLimitedMessageWrite]),
new Set([]),
aFiscalCode
);
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", () => {
const response = canWriteMessage(
new Set([UserGroup.ApiLimitedMessageWrite]),
new Set([aFiscalCode]),
aFiscalCode
);
expect(response.isRight()).toBeTruthy();
});

it("should pass when service can send messages to any recipient", () => {
const response = canWriteMessage(
new Set([UserGroup.ApiMessageWrite]),
new Set([]),
aFiscalCode
);
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',
};
19 changes: 11 additions & 8 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,32 @@
"@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",
"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 bb5bcef

Please sign in to comment.