Skip to content

Commit 304b89f

Browse files
Fixed some integration tests.
1 parent 2b76e5d commit 304b89f

File tree

3 files changed

+105
-19
lines changed

3 files changed

+105
-19
lines changed

src/eligibility_signposting_api/processors/hashing_service.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import hashlib
2+
import hmac
23
from typing import Annotated, NewType
34

45
from wireup import service, Inject
@@ -9,9 +10,13 @@
910

1011

1112
def _hash(nhs_number: str, secret_value: str) -> str:
12-
"""Internal helper to hash NHS number with a given secret value."""
13-
combined = f"{nhs_number}{secret_value}"
14-
return hashlib.sha256(combined.encode("utf-8")).hexdigest()
13+
nhs_str = str(nhs_number)
14+
15+
return hmac.new(
16+
secret_value.encode("utf-8"),
17+
nhs_str.encode("utf-8"),
18+
hashlib.sha512,
19+
).hexdigest()
1520

1621

1722
@service

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import hashlib
2+
import hmac
13
import string
24
from random import choice, randint
35

tests/integration/conftest.py

Lines changed: 95 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import datetime
2+
import hashlib
3+
import hmac
24
import json
35
import logging
46
import os
@@ -379,11 +381,25 @@ def person_table(dynamodb_resource: ServiceResource) -> Generator[Any]:
379381

380382
@pytest.fixture
381383
def persisted_person(person_table: Any, faker: Faker) -> Generator[eligibility_status.NHSNumber]:
382-
nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
384+
385+
nhs_num = faker.nhs_number()
386+
nhs_number = eligibility_status.NHSNumber(nhs_num)
387+
388+
nhs_num_hash = hmac.new(
389+
"test_value".encode("utf-8"),
390+
nhs_num.encode("utf-8"),
391+
hashlib.sha512,
392+
).hexdigest()
393+
394+
#nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
395+
#nhs_number = eligibility_status.NHSNumber(nhs_num)
396+
#nhs_number = nhs_num_hash
397+
383398
date_of_birth = eligibility_status.DateOfBirth(faker.date_of_birth(minimum_age=18, maximum_age=65))
384399

385400
for row in (
386-
rows := person_rows_builder(nhs_number, date_of_birth=date_of_birth, postcode="hp1", cohorts=["cohort1"]).data
401+
#rows := person_rows_builder(nhs_number, date_of_birth=date_of_birth, postcode="hp1", cohorts=["cohort1"]).data
402+
rows := person_rows_builder(nhs_num_hash, date_of_birth=date_of_birth, postcode="hp1", cohorts=["cohort1"]).data
387403
):
388404
person_table.put_item(Item=row)
389405

@@ -395,12 +411,23 @@ def persisted_person(person_table: Any, faker: Faker) -> Generator[eligibility_s
395411

396412
@pytest.fixture
397413
def persisted_77yo_person(person_table: Any, faker: Faker) -> Generator[eligibility_status.NHSNumber]:
398-
nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
414+
#nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
415+
416+
nhs_num = faker.nhs_number()
417+
nhs_number = eligibility_status.NHSNumber(nhs_num)
418+
419+
nhs_num_hash = hmac.new(
420+
"test_value".encode("utf-8"),
421+
nhs_num.encode("utf-8"),
422+
hashlib.sha512,
423+
).hexdigest()
424+
399425
date_of_birth = eligibility_status.DateOfBirth(faker.date_of_birth(minimum_age=77, maximum_age=77))
400426

401427
for row in (
402428
rows := person_rows_builder(
403-
nhs_number,
429+
nhs_num_hash,
430+
#nhs_number,
404431
date_of_birth=date_of_birth,
405432
postcode="hp1",
406433
cohorts=["cohort1", "cohort2"],
@@ -416,12 +443,23 @@ def persisted_77yo_person(person_table: Any, faker: Faker) -> Generator[eligibil
416443

417444
@pytest.fixture
418445
def persisted_person_all_cohorts(person_table: Any, faker: Faker) -> Generator[eligibility_status.NHSNumber]:
419-
nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
446+
#nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
447+
448+
nhs_num = faker.nhs_number()
449+
nhs_number = eligibility_status.NHSNumber(nhs_num)
450+
451+
nhs_num_hash = hmac.new(
452+
"test_value".encode("utf-8"),
453+
nhs_num.encode("utf-8"),
454+
hashlib.sha512,
455+
).hexdigest()
456+
420457
date_of_birth = eligibility_status.DateOfBirth(faker.date_of_birth(minimum_age=74, maximum_age=74))
421458

422459
for row in (
423460
rows := person_rows_builder(
424-
nhs_number,
461+
nhs_num_hash,
462+
# nhs_number,
425463
date_of_birth=date_of_birth,
426464
postcode="SW19",
427465
cohorts=["cohort_label1", "cohort_label2", "cohort_label3", "cohort_label4", "cohort_label5"],
@@ -438,12 +476,22 @@ def persisted_person_all_cohorts(person_table: Any, faker: Faker) -> Generator[e
438476

439477
@pytest.fixture
440478
def person_with_all_data(person_table: Any, faker: Faker) -> Generator[eligibility_status.NHSNumber]:
441-
nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
479+
#nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
480+
481+
nhs_num = faker.nhs_number()
482+
nhs_number = eligibility_status.NHSNumber(nhs_num)
483+
484+
nhs_num_hash = hmac.new(
485+
"test_value".encode("utf-8"),
486+
nhs_num.encode("utf-8"),
487+
hashlib.sha512,
488+
).hexdigest()
489+
442490
date_of_birth = eligibility_status.DateOfBirth(datetime.date(1990, 2, 28))
443491

444492
for row in (
445493
rows := person_rows_builder(
446-
nhs_number=nhs_number,
494+
nhs_number=nhs_num_hash,
447495
date_of_birth=date_of_birth,
448496
gender="0",
449497
postcode="SW18",
@@ -470,9 +518,19 @@ def person_with_all_data(person_table: Any, faker: Faker) -> Generator[eligibili
470518

471519
@pytest.fixture
472520
def persisted_person_no_cohorts(person_table: Any, faker: Faker) -> Generator[eligibility_status.NHSNumber]:
473-
nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
521+
#nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
522+
523+
nhs_num = faker.nhs_number()
524+
nhs_number = eligibility_status.NHSNumber(nhs_num)
474525

475-
for row in (rows := person_rows_builder(nhs_number).data):
526+
nhs_num_hash = hmac.new(
527+
"test_value".encode("utf-8"),
528+
nhs_num.encode("utf-8"),
529+
hashlib.sha512,
530+
).hexdigest()
531+
532+
#for row in (rows := person_rows_builder(nhs_number).data):
533+
for row in (rows := person_rows_builder(nhs_num_hash).data):
476534
person_table.put_item(Item=row)
477535

478536
yield nhs_number
@@ -483,10 +541,21 @@ def persisted_person_no_cohorts(person_table: Any, faker: Faker) -> Generator[el
483541

484542
@pytest.fixture
485543
def persisted_person_pc_sw19(person_table: Any, faker: Faker) -> Generator[eligibility_status.NHSNumber]:
486-
nhs_number = eligibility_status.NHSNumber(
487-
faker.nhs_number(),
488-
)
489-
for row in (rows := person_rows_builder(nhs_number, postcode="SW19", cohorts=["cohort1"]).data):
544+
# nhs_number = eligibility_status.NHSNumber(
545+
# faker.nhs_number(),
546+
# )
547+
548+
nhs_num = faker.nhs_number()
549+
nhs_number = eligibility_status.NHSNumber(nhs_num)
550+
551+
nhs_num_hash = hmac.new(
552+
"test_value".encode("utf-8"),
553+
nhs_num.encode("utf-8"),
554+
hashlib.sha512,
555+
).hexdigest()
556+
557+
#for row in (rows := person_rows_builder(nhs_number, postcode="SW19", cohorts=["cohort1"]).data):
558+
for row in (rows := person_rows_builder(nhs_num_hash, postcode="SW19", cohorts=["cohort1"]).data):
490559
person_table.put_item(Item=row)
491560

492561
yield nhs_number
@@ -499,11 +568,21 @@ def persisted_person_pc_sw19(person_table: Any, faker: Faker) -> Generator[eligi
499568
def persisted_person_with_no_person_attribute_type(
500569
person_table: Any, faker: Faker
501570
) -> Generator[eligibility_status.NHSNumber]:
502-
nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
571+
#nhs_number = eligibility_status.NHSNumber(faker.nhs_number())
503572
date_of_birth = eligibility_status.DateOfBirth(faker.date_of_birth(minimum_age=18, maximum_age=65))
504573

574+
nhs_num = faker.nhs_number()
575+
nhs_number = eligibility_status.NHSNumber(nhs_num)
576+
577+
nhs_num_hash = hmac.new(
578+
"test_value".encode("utf-8"),
579+
nhs_num.encode("utf-8"),
580+
hashlib.sha512,
581+
).hexdigest()
582+
505583
for row in (
506-
rows := person_rows_builder(nhs_number, date_of_birth=date_of_birth, postcode="hp1", cohorts=["cohort1"]).data
584+
#rows := person_rows_builder(nhs_number, date_of_birth=date_of_birth, postcode="hp1", cohorts=["cohort1"]).data
585+
rows := person_rows_builder(nhs_num_hash, date_of_birth=date_of_birth, postcode="hp1", cohorts=["cohort1"]).data
507586
):
508587
if row["ATTRIBUTE_TYPE"] != "PERSON":
509588
person_table.put_item(Item=row)

0 commit comments

Comments
 (0)