Skip to content

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudify committed Jul 25, 2019
1 parent d7ab43b commit 76ad181
Show file tree
Hide file tree
Showing 8 changed files with 3,074 additions and 73 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Expand Up @@ -40,7 +40,7 @@ jobs:
- checkout
- *restore_node_cache
- *install_node_modules
- run: yarn test
- run: yarn test:coverage
- run: 'bash <(curl -s https://codecov.io/bash)'

lint:
Expand Down Expand Up @@ -69,4 +69,4 @@ workflows:
- danger:
filters:
branches:
ignore: master
ignore: master
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -40,4 +40,6 @@ venv.bak/
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
*$py.class

coverage
241 changes: 241 additions & 0 deletions CreateMessage/__tests__/handler.test.ts
@@ -0,0 +1,241 @@
import * as fc from "fast-check";

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

import { WithinRangeInteger } from "italia-ts-commons/lib/numbers";
import { NonEmptyString } from "italia-ts-commons/lib/strings";

import { right } from "fp-ts/lib/Either";

import {
canDefaultAddresses,
canPaymentAmount,
canWriteMessage,
createMessageDocument,
forkOrchestrator
} from "../handler";

import {
fiscalCodeArb,
fiscalCodeArrayArb,
fiscalCodeSetArb,
maxAmountArb,
newMessageWithDefaultEmailArb,
newMessageWithPaymentDataArb,
upperCaseAlphaArb,
alphaStringArb,
messageTimeToLiveArb,
newMessageArb,
newMessageWithoutContentArb,
serviceArb,
versionedServiceArb
} from "../../utils/arbitraries";

//
// 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();
}
)
);
});
});

describe("canDefaultAddresses", () => {
it("should always respond with ResponseErrorForbiddenNotAuthorizedForDefaultAddresses when default addresses are provided", () => {
fc.assert(
fc.property(newMessageWithDefaultEmailArb, m => {
const response = canDefaultAddresses(m);
expect(response.isLeft()).toBeTruthy();
})
);
});
});

describe("canPaymentAmount", () => {
it("should authorize payment if under the allowed amount", () => {
fc.assert(
fc.property(
newMessageWithPaymentDataArb,
maxAmountArb,
(message, maxAmount) => {
const response = canPaymentAmount(message.content, maxAmount);
if (message.content.payment_data.amount <= maxAmount) {
expect(response.isRight()).toBeTruthy();
} else {
expect(response.isLeft()).toBeTruthy();
}
}
)
);
});
});

describe("createMessageDocument", () => {
const messageIdArb = alphaStringArb(16);
const senderUserIdArb = alphaStringArb(16);
const serviceIdArb = alphaStringArb(16);

it("should create a Message document", async () => {
await fc.assert(
fc.asyncProperty(
messageIdArb,
senderUserIdArb,
fiscalCodeArb,
messageTimeToLiveArb,
serviceIdArb,
async (messageId, senderUserId, fiscalCode, ttl, senderServiceId) => {
const mockMessageModel = ({
create: jest.fn(() => Promise.resolve(right({})))
} as unknown) as MessageModel;
const responseTask = createMessageDocument(
messageId,
mockMessageModel,
senderUserId,
fiscalCode,
ttl,
senderServiceId
);

const response = await responseTask.run();

expect(mockMessageModel.create).toHaveBeenCalledTimes(1);
expect(response.isRight()).toBeTruthy();
expect(response.getOrElse(undefined)).toMatchObject({
fiscalCode,
id: messageId,
indexedId: messageId,
isPending: true,
kind: "INewMessageWithoutContent",
senderServiceId,
senderUserId,
timeToLiveSeconds: ttl
});
}
)
);
});
});

describe("forkOrchestrator", () => {
it("should fork a durable orchestrator", async () => {
await fc.assert(
fc.asyncProperty(
newMessageArb,
newMessageWithoutContentArb,
versionedServiceArb,
async (newMessage, newMessageWithoutContent, service) => {
const mockDfClient = {
startNew: jest.fn(() => Promise.resolve("orchestratorId"))
};
const getDfClient = jest.fn(() => mockDfClient);
const response = await forkOrchestrator(
// tslint:disable-next-line: no-any
getDfClient as any,
newMessage.content,
service,
newMessageWithoutContent
).run();
expect(response.isRight()).toBeTruthy();
expect(getDfClient).toHaveBeenCalledTimes(1);
expect(mockDfClient.startNew).toHaveBeenCalledTimes(1);
expect(mockDfClient.startNew).toHaveBeenCalledWith(
"CreatedMessageOrchestrator",
undefined,
expect.objectContaining({
content: newMessage.content,
defaultAddresses: {}, // deprecated feature
message: newMessageWithoutContent,
senderMetadata: {
departmentName: service.departmentName,
organizationFiscalCode: service.organizationFiscalCode,
organizationName: service.organizationName,
serviceName: service.serviceName
},
serviceVersion: service.version
})
);
expect(response.getOrElse(undefined)).toEqual("orchestratorId");
}
)
);
});
});

0 comments on commit 76ad181

Please sign in to comment.