def test_redact_precompiled_letter_address_block_redacts_address_block(): address = extract_address_block(BytesIO(example_dwp_pdf)) address_regex = address.raw_address.replace("\n", "") assert address_regex == 'MR J DOE13 TEST LANETESTINGTONTE57 1NG' new_pdf = redact_precompiled_letter_address_block(BytesIO(example_dwp_pdf), address_regex) assert extract_address_block(new_pdf).raw_address == ""
def test_redact_precompiled_letter_address_block_address_repeated_on_2nd_page( ): address = extract_address_block( BytesIO(address_block_repeated_on_second_page)) address_regex = address.raw_address.replace("\n", "") expected = 'PEA NUTTPEANUT BUTTER JELLY COURTTOAST WHARFALL DAY TREAT STREETTASTY TOWNSNACKSHIRETT7 PBJ' assert address_regex == expected new_pdf = redact_precompiled_letter_address_block( BytesIO(address_block_repeated_on_second_page), address_regex) assert extract_address_block(new_pdf).raw_address == "" document = PdfReader(new_pdf) assert len(document.pages) == 2
def test_precompiled_sanitise_pdf_without_notify_tag(client, auth_header): assert not is_notify_tag_present(BytesIO(blank_with_address)) response = client.post( url_for('precompiled_blueprint.sanitise_precompiled_letter'), data=blank_with_address, headers={ 'Content-type': 'application/json', **auth_header }) assert response.status_code == 200 assert response.json == { "message": None, "file": ANY, "page_count": 1, "recipient_address": "Queen Elizabeth\nBuckingham Palace\nLondon\nSW1 1AA", "invalid_pages": None, 'redaction_failed_message': None } pdf = BytesIO(base64.b64decode(response.json["file"].encode())) assert is_notify_tag_present(pdf) assert extract_address_block(pdf).normalised == ('Queen Elizabeth\n' 'Buckingham Palace\n' 'London\n' 'SW1 1AA')
def test_extract_address_block(): assert extract_address_block( BytesIO(example_dwp_pdf)).raw_address == '\n'.join([ 'MR J DOE', '13 TEST LANE', 'TESTINGTON', 'TE57 1NG', ])
def test_add_address_to_precompiled_letter_puts_address_on_page(): address = '\n'.join([ 'Queen Elizabeth,', 'Buckingham Palace', 'London', 'SW1 1AA', ]) ret = add_address_to_precompiled_letter(BytesIO(blank_page), address) assert extract_address_block(ret).raw_address == address
def test_rewrite_address_block_end_to_end(pdf_data, address_snippet): new_pdf, address, message = rewrite_address_block( BytesIO(pdf_data), page_count=1, allow_international_letters=False, ) assert not message assert address == extract_address_block(new_pdf).raw_address assert address_snippet in address.lower()
def test_rewrite_address_block_doesnt_overwrite_if_it_cant_redact_address(): old_pdf = BytesIO(repeated_address_block) old_address = extract_address_block(old_pdf).raw_address new_pdf, address, message = rewrite_address_block( old_pdf, page_count=1, allow_international_letters=False, ) # assert that the pdf is unchanged. Specifically we haven't written the new address over the old one assert new_pdf.getvalue() == old_pdf.getvalue() assert message == 'More than one match for address block during redaction procedure' # template preview still needs to return the address even though it's unchanged. assert old_address == address