Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests for CreateMessage function #5

Merged
merged 1 commit into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ venv.bak/
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
*$py.class

coverage
236 changes: 236 additions & 0 deletions CreateMessage/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
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 { right } from "fp-ts/lib/Either";

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

import {
alphaStringArb,
fiscalCodeArb,
fiscalCodeArrayArb,
fiscalCodeSetArb,
maxAmountArb,
messageTimeToLiveArb,
newMessageArb,
newMessageWithDefaultEmailArb,
newMessageWithoutContentArb,
newMessageWithPaymentDataArb,
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");
}
)
);
});
});
Loading