-
Notifications
You must be signed in to change notification settings - Fork 5
CCM-13787: create supplier mock for testing #535
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
Merged
Vlasis-Perdikidis
merged 37 commits into
main
from
feature/CCM-13787-createSupplierMockForTesting
Jun 12, 2026
Merged
Changes from 32 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
940bbdf
add supplier mock dummy lambda function
Vlasis-Perdikidis 72522b1
correct the name of the supplier_mock lambda
Vlasis-Perdikidis b709f6a
remove comment from tf file
Vlasis-Perdikidis 241808b
get dummy test to pass
Vlasis-Perdikidis 52da831
disable vale spelling from README
Vlasis-Perdikidis 577b851
correct lambda handler name
Vlasis-Perdikidis c3bde6b
restore changes
Vlasis-Perdikidis c22f094
call patch letter and add variables to parameter store
Vlasis-Perdikidis 5f4ea2a
merge main
Vlasis-Perdikidis c2c9675
resolve final conflicts
Vlasis-Perdikidis 98d5fb2
refresh package-lock file
Vlasis-Perdikidis e7a0433
correct lint and typecheck errors
Vlasis-Perdikidis b048bbf
resolve final conflicts
Vlasis-Perdikidis 92d4c60
re-add @pact-foundation/pact library in run-pact-tests.sh script
Vlasis-Perdikidis 510c828
install amd64 version of @pact-foundation if missing
Vlasis-Perdikidis 3199ce5
add ssm:GetParameter permission for supplierId parameter in supplier …
Vlasis-Perdikidis 3bd0da7
merge main
Vlasis-Perdikidis 3045c21
get package-lock in line with main
Vlasis-Perdikidis 6993b09
don't deploy the scheduler by default
Vlasis-Perdikidis 0bf202f
only deploy scheduler resources when flag is set to true
Vlasis-Perdikidis 994de51
deploy the schedule to test it
Vlasis-Perdikidis cfcdfef
add README with instructions on the supplier-mock lambda
Vlasis-Perdikidis 16651dc
add supplier-mock config in parameter store whith specificationId map…
Vlasis-Perdikidis da3ea16
fix typecheck
Vlasis-Perdikidis 343c992
remove comment
Vlasis-Perdikidis f7ac687
correct patch letter request body
Vlasis-Perdikidis dda6cdf
separate the ssm_parameter to its own file
Vlasis-Perdikidis ff83654
merge main
Vlasis-Perdikidis 93df971
npm instlal
Vlasis-Perdikidis 7611712
update README for supplier-mock lambda
Vlasis-Perdikidis 02f3f64
address Sid comments
Vlasis-Perdikidis 4fe3b42
move terraform resources into separate folders
Vlasis-Perdikidis 74d6a35
align README structure with the rest
Vlasis-Perdikidis 7c92bea
merge main
Vlasis-Perdikidis f0e3070
fix zod typecheck error
Vlasis-Perdikidis 1cab254
don't deploy the scheduler by default
Vlasis-Perdikidis 902aa5c
conditionally deploy the aws_iam_policy_document.supplier_mock_lambda…
Vlasis-Perdikidis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
5 changes: 5 additions & 0 deletions
5
infrastructure/terraform/components/api/iam_policy_supplier_mock_scheduler_invoke_policy.tf
This file contains hidden or 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,5 @@ | ||
| resource "aws_iam_policy" "supplier_mock_scheduler_invoke_policy" { | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
| name = "${local.csi}-supplier-mock-scheduler-invoke" | ||
| policy = data.aws_iam_policy_document.supplier_mock_scheduler_invoke_policy[0].json | ||
| } |
5 changes: 5 additions & 0 deletions
5
...raform/components/api/iam_role_policy_attachment_supplier_mock_scheduler_invoke_policy.tf
This file contains hidden or 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,5 @@ | ||
| resource "aws_iam_role_policy_attachment" "supplier_mock_scheduler_invoke_policy" { | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
| role = aws_iam_role.supplier_mock_scheduler[0].name | ||
| policy_arn = aws_iam_policy.supplier_mock_scheduler_invoke_policy[0].arn | ||
| } |
6 changes: 6 additions & 0 deletions
6
infrastructure/terraform/components/api/iam_role_supplier_mock_scheduler.tf
This file contains hidden or 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,6 @@ | ||
| resource "aws_iam_role" "supplier_mock_scheduler" { | ||
| name = "${local.csi}-supplier-mock-scheduler" | ||
| description = "Allows EventBridge Scheduler to invoke supplier mock lambda" | ||
| assume_role_policy = data.aws_iam_policy_document.supplier_mock_scheduler_trust_policy.json | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
| } |
87 changes: 87 additions & 0 deletions
87
infrastructure/terraform/components/api/module_lambda_supplier_mock.tf
This file contains hidden or 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,87 @@ | ||
| module "supplier_mock" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/4.0.5/terraform-lambda.zip" | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
|
|
||
| function_name = "supplier_mock" | ||
| description = "Mock the behaviour of a supplier" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| component = var.component | ||
| environment = var.environment | ||
| project = var.project | ||
| region = var.region | ||
| group = var.group | ||
|
|
||
| log_retention_in_days = var.log_retention_in_days | ||
| kms_key_arn = module.kms.key_arn | ||
|
|
||
| iam_policy_document = { | ||
| body = data.aws_iam_policy_document.supplier_mock_lambda.json | ||
| } | ||
|
|
||
| function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"] | ||
| function_code_base_path = local.aws_lambda_functions_dir_path | ||
| function_code_dir = "supplier-mock/dist" | ||
| function_include_common = true | ||
| handler_function_name = "supplierMockHandler" | ||
| runtime = "nodejs22.x" | ||
| memory = 512 | ||
| timeout = 29 | ||
| log_level = var.log_level | ||
|
|
||
| force_lambda_code_deploy = var.force_lambda_code_deploy | ||
| enable_lambda_insights = false | ||
|
|
||
| log_destination_arn = local.destination_arn | ||
| log_subscription_role_arn = local.acct.log_subscription_role_arn | ||
|
|
||
| lambda_env_vars = merge(local.common_lambda_env_vars, { | ||
| ENVIRONMENT = var.environment | ||
| GET_LETTERS_FUNCTION_NAME = module.get_letters.function_name | ||
| PATCH_LETTER_FUNCTION_NAME = module.patch_letter.function_name | ||
| SUPPLIER_MOCK_CONFIG_PARAM_NAME = aws_ssm_parameter.supplier_mock_config[0].name | ||
| }) | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "supplier_mock_lambda" { | ||
| statement { | ||
| sid = "KMSPermissions" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "kms:Decrypt", | ||
| "kms:GenerateDataKey", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.kms.key_arn, ## Requires shared kms module | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "AllowInvokeLambda" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "lambda:InvokeFunction", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.get_letters.function_arn, | ||
| module.patch_letter.function_arn | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "AllowReadSupplierMockConfigParameter" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "ssm:GetParameter", | ||
| ] | ||
|
|
||
| resources = [ | ||
| aws_ssm_parameter.supplier_mock_config[0].arn | ||
| ] | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
infrastructure/terraform/components/api/scheduler_schedule_supplier_mock.tf
This file contains hidden or 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,20 @@ | ||
| resource "aws_scheduler_schedule" "supplier_mock" { | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
| name = "${local.csi}-supplier-mock" | ||
| description = "Scheduled trigger for supplier mock lambda" | ||
| state = var.enable_supplier_mock_scheduler ? "ENABLED" : "DISABLED" | ||
|
|
||
| flexible_time_window { | ||
| mode = "OFF" | ||
| } | ||
|
|
||
| schedule_expression = var.supplier_mock_schedule_expression | ||
|
|
||
| target { | ||
| arn = module.supplier_mock[0].function_arn | ||
| role_arn = aws_iam_role.supplier_mock_scheduler[0].arn | ||
| input = jsonencode({ | ||
| source = "eventbridge-scheduler" | ||
| }) | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
infrastructure/terraform/components/api/scheduler_supplier_mock.tf
This file contains hidden or 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,36 @@ | ||
| data "aws_iam_policy_document" "supplier_mock_scheduler_trust_policy" { | ||
| statement { | ||
| sid = "AllowSchedulerAssumeRole" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "sts:AssumeRole", | ||
| ] | ||
|
|
||
| principals { | ||
| type = "Service" | ||
|
|
||
| identifiers = [ | ||
| "scheduler.amazonaws.com", | ||
| ] | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| data "aws_iam_policy_document" "supplier_mock_scheduler_invoke_policy" { | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
|
|
||
| statement { | ||
| sid = "AllowInvokeSupplierMockLambda" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "lambda:InvokeFunction", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.supplier_mock[0].function_arn, | ||
| ] | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
infrastructure/terraform/components/api/ssm_parameter_supplier_mock_config.tf
This file contains hidden or 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,17 @@ | ||
| resource "aws_ssm_parameter" "supplier_mock_config" { | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
| name = format("/nhs/supapi/%s/supplier-mock/config", var.environment) | ||
| description = "JSON object containing supplier mock config for supplierID, getLetters limit, specificationId mapping, etc." | ||
| type = "String" | ||
| value = jsonencode({ | ||
| limit = "100" | ||
| supplier_id = "TestSupplier1" | ||
| specification_id_mapping = { | ||
| "test-specification-id-1" = "ACCEPTED" | ||
| } | ||
| }) | ||
|
|
||
| lifecycle { | ||
| ignore_changes = [value] | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,5 @@ | ||
| coverage | ||
| node_modules | ||
| dist | ||
| .reports | ||
| .aws-sam |
This file contains hidden or 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,31 @@ | ||
| <!-- vale off --> | ||
|
|
||
| # Supplier Mock | ||
|
|
||
| ## What is does | ||
|
|
||
| The supplier-mock lambda simulates the supplier's system behaviour so that downstream services can simulate supplier interaction without relying on a live supplier system. | ||
| The mock simulates the journey of a letter with the Supplier. It utilises the api-handler and retrieves pending letters by calling the `getLetters` lambda directly and then provides status updates for each letter by calling the `patchLetter` lambda. | ||
| Each time the mock is called it will call `getLetters` once with for the **supplierId** `TestSupplier1` and with a **limit** of `100` letters. These default values can be configured in the AWS Parameter Store as explained in [How to modify parameter store variables](#how-to-modify-default-variable-values-in-parameter-store) . It then loops through each retrieved letter and updates its status by calling `patchLetter` and using the **specification_id_mapping** map to determine its status. If the map doesn't return a value it will default to `ACCEPTED` status. | ||
|
|
||
| ## How to deploy the supapi-supplier-mock schedule | ||
|
|
||
| The AWS EventBridge Schedule that calls the supplier-mock lambda is created by the terraform file **/infrastructure/terraform/components/api/scheduler_supplier_mock.tf**, however, by default it's not deployed as it's not needed by every dynamic environment. | ||
| If you need to deploy the schedule in your environment you need to set the variable `deploy_supplier_mock_scheduler = true` in the file **/infrastructure/terraform/components/api/variables.tf**. | ||
|
|
||
| ## How to run the lambda | ||
|
|
||
| The supplier-mock lambda can be activated by manually enabling the `{environment}-supapi-supplier-mock` AWS EventBridge schedule from the AWS console. The schedule is configured to call the lambda once per minute. You need to manually disable the schedule from the AWS console to stop calling the supplier-mock lambda. | ||
| **NOTE: The schedule is not deployed by default** | ||
|
|
||
| ## How to modify default variable values in parameter store | ||
|
|
||
| As mentioned above the `limit`, `supplierId` and `specification_id_mapping` default values are stored in the AWS Parameter Store and can be modified if needed by accessing the Parameter Store from the AWS Console. These store parameters are created in the **/infrastructure/terraform/components/api/ssm_parameter.tf** file, but only if the `deploy_supplier_mock_scheduler` variable is set to `true`. We deploy a JSON parameter in the location `/nhs/supapi/{environment}/supplier-mock/config` with the following key/value pairs: | ||
|
|
||
| | key | type | default value | | ||
| | ------------------------ | ------ | --------------------------------------- | | ||
| | limit | string | 100 | | ||
| | supplier_id | string | TestSupplier1 | | ||
| | specification_id_mapping | map | {"test-specification-id-1 = "ACCEPTED"} | | ||
|
|
||
| <!-- vale on --> |
This file contains hidden or 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,69 @@ | ||
| export const baseJestConfig = { | ||
| preset: "ts-jest", | ||
| extensionsToTreatAsEsm: [".ts"], | ||
| transform: { | ||
| "^.+\\.ts$": [ | ||
| "ts-jest", | ||
| { | ||
| useESM: true, | ||
| }, | ||
| ], | ||
| }, | ||
| transformIgnorePatterns: [ | ||
| "node_modules/(?!(@nhsdigital/nhs-notify-event-schemas-supplier-config)/)", | ||
| ], | ||
|
|
||
| // Automatically clear mock calls, instances, contexts and results before every test | ||
| clearMocks: true, | ||
|
|
||
| // Indicates whether the coverage information should be collected while executing the test | ||
| collectCoverage: true, | ||
|
|
||
| // The directory where Jest should output its coverage files | ||
| coverageDirectory: "./.reports/unit/coverage", | ||
|
|
||
| // Indicates which provider should be used to instrument code for coverage | ||
| coverageProvider: "babel", | ||
|
|
||
| coverageThreshold: { | ||
| global: { | ||
| branches: 100, | ||
| functions: 100, | ||
| lines: 100, | ||
| statements: -10, | ||
| }, | ||
| }, | ||
|
|
||
| coveragePathIgnorePatterns: ["/__tests__/"], | ||
| testPathIgnorePatterns: [".build"], | ||
| testMatch: ["**/?(*.)+(spec|test).[jt]s?(x)"], | ||
|
|
||
| // Use this configuration option to add custom reporters to Jest | ||
| reporters: [ | ||
| "default", | ||
| [ | ||
| "jest-html-reporter", | ||
| { | ||
| pageTitle: "Test Report", | ||
| outputPath: "./.reports/unit/test-report.html", | ||
| includeFailureMsg: true, | ||
| }, | ||
| ], | ||
| ], | ||
|
|
||
| // The test environment that will be used for testing | ||
| testEnvironment: "jsdom", | ||
| }; | ||
|
|
||
| const utilsJestConfig = { | ||
| ...baseJestConfig, | ||
|
|
||
| testEnvironment: "node", | ||
|
|
||
| coveragePathIgnorePatterns: [ | ||
| ...(baseJestConfig.coveragePathIgnorePatterns ?? []), | ||
| "zod-validators.ts", | ||
| ], | ||
| }; | ||
|
|
||
| export default utilsJestConfig; |
This file contains hidden or 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,22 @@ | ||
| { | ||
| "dependencies": { | ||
| "@aws-sdk/client-api-gateway": "^3.1030.0", | ||
| "@aws-sdk/client-lambda": "^3.1030.0", | ||
| "@aws-sdk/client-ssm": "^3.1054.0", | ||
| "@internal/helpers": "^0.1.0", | ||
| "aws-embedded-metrics": "^4.2.1", | ||
| "aws-lambda": "^1.0.7", | ||
| "pino": "^10.3.1", | ||
| "zod": "^4.3.6" | ||
| }, | ||
| "name": "nhs-notify-supplier-api-supplier-mock", | ||
| "private": true, | ||
| "scripts": { | ||
| "lambda-build": "rm -rf dist && npx esbuild --bundle --minify --sourcemap --target=es2020 --platform=node --loader:.node=file --entry-names=[name] --outdir=dist src/index.ts", | ||
| "lint": "eslint .", | ||
| "lint:fix": "eslint . --fix", | ||
| "test:unit": "jest", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "version": "0.0.1" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.