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

release/2024-09-11 #310

Merged
merged 12 commits into from
Sep 13, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ jobs:
uses: ./.github/actions/catch-failed-step
- name: Send job result to slack
id: slack
uses: slackapi/slack-github-action@v1.26.0
uses: slackapi/slack-github-action@v1.27.0
with:
payload: |
{
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
uses: ./.github/actions/catch-failed-step
- name: Send merge result to slack
id: slack
uses: slackapi/slack-github-action@v1.26.0
uses: slackapi/slack-github-action@v1.27.0
with:
payload: |
{
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2024-09-11
- [PI-493] Generating Party Keys
- [PI-494] Generating ASIDs
- [PI-499] CI s3 tests are running twice
- [PI-513] Unit test post request failing

## 2024-09-05
- [PI-219] Upgrade to Python 3.12
- [PI-491] Case-insensitive search
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.09.05
2024.09.11
4 changes: 4 additions & 0 deletions changelog/2024-09-11.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- [PI-493] Generating Party Keys
- [PI-494] Generating ASIDs
- [PI-499] CI s3 tests are running twice
- [PI-513] Unit test post request failing
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "connecting-party-manager"
version = "2024.09.05"
version = "2024.09.11"
description = "Repository for the Connecting Party Manager API and related services"
authors = ["NHS England"]
license = "LICENSE.md"
Expand Down Expand Up @@ -40,7 +40,7 @@ hypothesis = "^6.87.3"
aws-lambda-powertools = { extras = ["aws-sdk"], version = "^2.26.0" }
parse = "^1.19.1"
pytest-mock = "^3.12.0"
datamodel-code-generator = "^0.25.1"
datamodel-code-generator = "^0.26.0"
pyyaml = "^6.0.1"
proxygen-cli = "^2.1.14"
moto = "^5.0.1"
Expand Down
4 changes: 2 additions & 2 deletions scripts/test/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ test--slow: ## Run slow (pytest) tests
$(MAKE) _pytest _INTERNAL_FLAGS="-m 'slow'" _CACHE_CLEAR=$(_CACHE_CLEAR)

test--s3: aws--login ## Run (pytest) tests that require s3 downloads
$(MAKE) _pytest _INTERNAL_FLAGS="-m 's3'" _CACHE_CLEAR=$(_CACHE_CLEAR) AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) AWS_SESSION_TOKEN=$(AWS_SESSION_TOKEN)
$(MAKE) _pytest _INTERNAL_FLAGS="-m 's3' $(_INTERNAL_FLAGS)" _CACHE_CLEAR=$(_CACHE_CLEAR) AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) AWS_SESSION_TOKEN=$(AWS_SESSION_TOKEN)

test--smoke: aws--login ## Run end-to-end smoke tests (pytest)
AWS_DEFAULT_REGION=$(AWS_DEFAULT_REGION) AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) AWS_SESSION_TOKEN=$(AWS_SESSION_TOKEN) WORKSPACE=$(WORKSPACE) ACCOUNT=$(ACCOUNT) poetry run python -m pytest $(PYTEST_FLAGS) -m 'smoke' --ignore=src/layers --ignore=src/etl $(_CACHE_CLEAR)

test--%--rerun: ## Rerun failed integration or unit (pytest) tests
$(MAKE) test--$* _INTERNAL_FLAGS="--last-failed --last-failed-no-failures none" _CACHE_CLEAR=
$(MAKE) test--$* _INTERNAL_FLAGS="--last-failed --last-failed-no-failures none" _CACHE_CLEAR=$(_CACHE_CLEAR)

test--feature--integration: aws--login ## Run integration feature (gherkin) tests
$(MAKE) _behave _INTERNAL_FLAGS="--define='test_mode=integration' $(_INTERNAL_FLAGS)" AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) AWS_SESSION_TOKEN=$(AWS_SESSION_TOKEN)
Expand Down
2 changes: 1 addition & 1 deletion src/etl/notify/tests/test_notify_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from test_helpers.terraform import read_terraform_output

EXAMPLE_DOT_COM = "https://example.com"
EXAMPLE_DOT_COM = "https://httpbin.org"

NOTIFY_ENVIRONMENT = {
"SLACK_WEBHOOK_URL": EXAMPLE_DOT_COM,
Expand Down
1 change: 1 addition & 0 deletions src/layers/domain/core/cpm_system_id/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .v1 import * # noqa: F403, F401
265 changes: 265 additions & 0 deletions src/layers/domain/core/cpm_system_id/tests/test_cpm_system_id_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
import pytest
from domain.core.cpm_system_id import AsidId, PartyKeyId
from domain.repository.cpm_system_id_repository import CpmSystemIdRepository
from event.aws.client import dynamodb_client

from test_helpers.terraform import read_terraform_output


def test_party_key_generator_format_key():
generator = PartyKeyId.create(current_id={"latest": 123456}, ods_code="ABC")
expected_key = "ABC-123457" # Expecting the number to be formatted with 6 digits
assert generator.latest_id == expected_key


def test_party_key_generator_validate_key_valid():
valid_key = "ABC-123457"
is_valid = PartyKeyId.validate_key(valid_key)
assert is_valid


@pytest.mark.parametrize(
"invalid_key",
[
"ABC000124", # Missing hyphen
"123-000124", # Numeric ODS code
"ABC-1234", # Number part too short
"ABC-1234567", # Number part too long
"AB1-000124", # ODS code contains a digit
"ABC-0001A4", # Number part contains a non-digit character
"", # Empty string
],
)
def test_party_key_generator_validate_key_invalid_format(invalid_key):
is_valid = PartyKeyId.validate_key(invalid_key)
assert not is_valid


def test_party_key_generator_increment_number():
# Test that the number is incremented correctly
generator = PartyKeyId.create(current_id={"latest": 123456}, ods_code="XYZ")
expected_key = "XYZ-123457" # Expecting increment from 123456 to 123457
assert generator.latest == 123457
assert generator.latest_id == expected_key


@pytest.mark.integration
def test_party_key_generation_seeded():
TABLE_NAME = read_terraform_output("dynamodb_table_name.value")
client = dynamodb_client()
repository = CpmSystemIdRepository(table_name=TABLE_NAME, dynamodb_client=client)
current_id = repository.read(key_name="PARTYKEYNUMBER")
generator = PartyKeyId.create(current_id=current_id, ods_code="ABC")
assert generator.latest == 850000
assert generator.latest_id == "ABC-850000"
repository.create_or_update(key_name="PARTYKEYNUMBER", new_number=generator.latest)
new_id = repository.read(key_name="PARTYKEYNUMBER")
assert new_id["latest"] == 850000


@pytest.mark.parametrize(
"start, expected",
[("850010", 850011), ("875789", 875790), ("961237", 961238)],
)
@pytest.mark.integration
def test_party_key_generation(start, expected):
TABLE_NAME = read_terraform_output("dynamodb_table_name.value")
client = dynamodb_client()
# Insert a dummy entry with an initial 'latest' value
client.put_item(
TableName=TABLE_NAME,
Item={
"pk": {"S": "CSI#PARTYKEYNUMBER"},
"sk": {"S": "CSI#PARTYKEYNUMBER"},
"latest": {"N": f"{start}"}, # Set the initial value for the test
},
)

repository = CpmSystemIdRepository(table_name=TABLE_NAME, dynamodb_client=client)
current_id = repository.read(key_name="PARTYKEYNUMBER")
generator = PartyKeyId.create(current_id=current_id, ods_code="ABC")
assert generator.latest == expected
assert generator.latest_id == f"ABC-{str(expected)}"
repository.create_or_update(key_name="PARTYKEYNUMBER", new_number=generator.latest)
new_id = repository.read(key_name="PARTYKEYNUMBER")
assert new_id["latest"] == expected


@pytest.mark.integration
def test_party_key_generation_increment():
TABLE_NAME = read_terraform_output("dynamodb_table_name.value")
client = dynamodb_client()
start_value = 100000

# Insert a dummy entry with an initial 'latest' value
client.put_item(
TableName=TABLE_NAME,
Item={
"pk": {"S": "CSI#PARTYKEYNUMBER"},
"sk": {"S": "CSI#PARTYKEYNUMBER"},
"latest": {"N": f"{start_value}"}, # Set the initial value for the test
},
)

repository = CpmSystemIdRepository(table_name=TABLE_NAME, dynamodb_client=client)

# Number of times to call the generator
num_calls = 5
current_id = repository.read(key_name="PARTYKEYNUMBER")

previous_latest = int(current_id["latest"])

for _ in range(num_calls):
generator = PartyKeyId.create(current_id=current_id, ods_code="ABC")

expected_latest = previous_latest + 1

assert generator.latest == expected_latest
assert generator.latest_id == f"ABC-{expected_latest}"

# Update repository with new number
repository.create_or_update(
key_name="PARTYKEYNUMBER", new_number=generator.latest
)

# Fetch the updated ID and check if it was correctly incremented
new_id = repository.read(key_name="PARTYKEYNUMBER")
assert int(new_id["latest"]) == expected_latest

# Update for next iteration
previous_latest = expected_latest
current_id = new_id

# Final assertion to check if latest is num_calls greater than start
final_latest = int(repository.read(key_name="PARTYKEYNUMBER")["latest"])
assert final_latest == start_value + num_calls


def test_asid_generator_validate_key_valid():
valid_key = "223456789014"
is_valid = AsidId.validate_key(valid_key)
assert is_valid


@pytest.mark.parametrize(
"invalid_key",
[
"1234567890123",
"12345678901",
"1234567890",
"123456789",
"12345678",
"1234567",
"123456",
"12345",
"1234",
"123",
"12",
"1",
"", # Empty string
],
)
def test_asid_generator_validate_key_invalid_format(invalid_key):
is_valid = AsidId.validate_key(invalid_key)
assert not is_valid


def test_asid_generator_increment_number():
# Test that the number is incremented correctly
generator = AsidId.create(current_id={"latest": 223456789012})
assert generator.latest == 223456789013
assert generator.latest_id == "223456789013"


@pytest.mark.integration
def test_asid_key_generation_seeded():
TABLE_NAME = read_terraform_output("dynamodb_table_name.value")
client = dynamodb_client()
repository = CpmSystemIdRepository(table_name=TABLE_NAME, dynamodb_client=client)
current_id = repository.read(key_name="ASIDNUMBER")
generator = AsidId.create(current_id=current_id)
assert generator.latest == 200000100000
assert generator.latest_id == "200000100000"
repository.create_or_update(key_name="ASIDNUMBER", new_number=generator.latest)
new_id = repository.read(key_name="ASIDNUMBER")
assert new_id["latest"] == 200000100000


@pytest.mark.parametrize(
"start, expected",
[
("200000000000", 200000000001),
("200001000000", 200001000001),
("200001000009", 200001000010),
],
)
@pytest.mark.integration
def test_asid_key_generation(start, expected):
TABLE_NAME = read_terraform_output("dynamodb_table_name.value")
client = dynamodb_client()
# Insert a dummy entry with an initial 'latest' value
client.put_item(
TableName=TABLE_NAME,
Item={
"pk": {"S": "CSI#ASIDNUMBER"},
"sk": {"S": "CSI#ASIDNUMBER"},
"latest": {"N": f"{start}"}, # Set the initial value for the test
},
)

repository = CpmSystemIdRepository(table_name=TABLE_NAME, dynamodb_client=client)
current_id = repository.read(key_name="ASIDNUMBER")
generator = AsidId.create(current_id=current_id)
assert generator.latest == expected
assert generator.latest_id == str(expected)
repository.create_or_update(key_name="ASIDNUMBER", new_number=generator.latest)
new_id = repository.read(key_name="ASIDNUMBER")
assert new_id["latest"] == expected


@pytest.mark.integration
def test_asid_generation_increment():
TABLE_NAME = read_terraform_output("dynamodb_table_name.value")
client = dynamodb_client()
start_value = 200000000000

# Insert a dummy entry with an initial 'latest' value
client.put_item(
TableName=TABLE_NAME,
Item={
"pk": {"S": "CSI#ASIDNUMBER"},
"sk": {"S": "CSI#ASIDNUMBER"},
"latest": {"N": f"{start_value}"}, # Set the initial value for the test
},
)

repository = CpmSystemIdRepository(table_name=TABLE_NAME, dynamodb_client=client)

# Number of times to call the generator
num_calls = 5
current_id = repository.read(key_name="ASIDNUMBER")

previous_latest = int(current_id["latest"])

for _ in range(num_calls):
generator = AsidId.create(current_id=current_id)

expected_latest = previous_latest + 1

assert generator.latest == expected_latest
assert generator.latest_id == str(expected_latest)

# Update repository with new number
repository.create_or_update(key_name="ASIDNUMBER", new_number=generator.latest)

# Fetch the updated ID and check if it was correctly incremented
new_id = repository.read(key_name="ASIDNUMBER")
assert int(new_id["latest"]) == expected_latest

# Update for next iteration
previous_latest = expected_latest
current_id = new_id

# Final assertion to check if latest is num_calls greater than start_value
final_latest = int(repository.read(key_name="ASIDNUMBER")["latest"])
assert final_latest == start_value + num_calls
Loading
Loading