Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
2,916 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,6 @@ venv.bak/ | |
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
*$py.class | ||
|
||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
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 | ||
} from "../handler"; | ||
|
||
import { | ||
fiscalCodeArb, | ||
fiscalCodeArrayArb, | ||
fiscalCodeSetArb, | ||
maxAmountArb, | ||
newMessageWithDefaultEmailArb, | ||
newMessageWithPaymentDataArb, | ||
upperCaseAlphaArb | ||
} 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 alphanumStringArb = fc | ||
.array(upperCaseAlphaArb, 16, 16) | ||
.map(_ => _.join("") as NonEmptyString); | ||
const messageIdArb = alphanumStringArb; | ||
const senderUserIdArb = alphanumStringArb; | ||
const serviceIdArb = alphanumStringArb; | ||
const ttlArb = fc | ||
.integer(3600, 604800) | ||
// tslint:disable-next-line:no-useless-cast | ||
.map(_ => _ as number & WithinRangeInteger<3600, 604800>); | ||
|
||
it("should create a Message document", async () => { | ||
await fc.assert( | ||
fc.asyncProperty( | ||
messageIdArb, | ||
senderUserIdArb, | ||
fiscalCodeArb, | ||
ttlArb, | ||
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 | ||
}); | ||
} | ||
) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.