Exemplo n.º 1
0
def test_find_letter_pdf_in_s3_raises_if_not_found(sample_notification):
    bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
    s3 = boto3.client('s3', region_name='eu-west-1')
    s3.create_bucket(
        Bucket=bucket_name,
        CreateBucketConfiguration={'LocationConstraint': 'eu-west-1'})

    with pytest.raises(LetterPDFNotFound):
        find_letter_pdf_in_s3(sample_notification)
Exemplo n.º 2
0
def test_find_letter_pdf_in_s3_returns_object(sample_notification):
    bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
    s3 = boto3.client('s3', region_name='eu-west-1')
    s3.create_bucket(
        Bucket=bucket_name,
        CreateBucketConfiguration={'LocationConstraint': 'eu-west-1'})

    _, prefix = get_bucket_name_and_prefix_for_notification(
        sample_notification)
    s3.put_object(Bucket=bucket_name, Key=f'{prefix}-and-then-some', Body=b'f')

    assert find_letter_pdf_in_s3(
        sample_notification).key == f'{prefix}-and-then-some'
Exemplo n.º 3
0
def get_key_and_size_of_letters_to_be_sent_to_print(print_run_deadline,
                                                    postage):
    letters_awaiting_sending = dao_get_letters_to_be_printed(
        print_run_deadline, postage)
    for letter in letters_awaiting_sending:
        try:
            letter_pdf = find_letter_pdf_in_s3(letter)
            yield {
                "Key": letter_pdf.key,
                "Size": letter_pdf.size,
                "ServiceId": str(letter.service_id)
            }
        except (BotoClientError, LetterPDFNotFound) as e:
            current_app.logger.exception(
                f"Error getting letter from bucket for notification: {letter.id} with reference: {letter.reference}",
                e)
def _delete_letters_from_s3(notification_type, service_id, date_to_delete_from,
                            query_limit):
    letters_to_delete_from_s3 = db.session.query(Notification).filter(
        Notification.notification_type == notification_type,
        Notification.created_at < date_to_delete_from,
        Notification.service_id == service_id,
        # although letters in non completed statuses do have PDFs in s3, they do not exist in the
        # production-letters-pdf bucket as they never made it that far so we do not try and delete
        # them from it
        Notification.status.in_(NOTIFICATION_STATUS_TYPES_COMPLETED)).limit(
            query_limit).all()
    for letter in letters_to_delete_from_s3:
        try:
            letter_pdf = find_letter_pdf_in_s3(letter)
            letter_pdf.delete()
        except (ClientError, LetterPDFNotFound):
            current_app.logger.exception(
                "Could not delete S3 object for letter: {}".format(letter.id))