from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import (
    DynamoDBPersistenceLayer,
    SortKey,
    TransactKey,
)

import events.set_cert_expired as app


def test_set_cert_expired(
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    lambda_context: LambdaContext,
):
    event = {
        'detail': {
            'old_image': {
                'id': '845fe390-e3c3-4514-97f8-c42de0566cf0',
                'sk': '0',
            }
        }
    }
    assert app.lambda_handler(event, lambda_context)  # type: ignore

    r = dynamodb_persistence_layer.collection.get_items(
        TransactKey('845fe390-e3c3-4514-97f8-c42de0566cf0')
        + SortKey('0')
        + SortKey('SCHEDULE#SET_CERT_EXPIRED#EXECUTED', rename_key='executed')
    )
    assert r['status'] == 'COMPLETED'
    assert 'executed' in r
    assert 'cert' in r


def test_existing_issued_cert(
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    lambda_context: LambdaContext,
):
    event = {
        'detail': {
            'old_image': {
                'id': '1ee108ae-67d4-4545-bf6d-4e641cdaa4e0',
                'sk': '0',
            }
        }
    }
    assert app.lambda_handler(event, lambda_context)  # type: ignore

    r = dynamodb_persistence_layer.collection.get_items(
        TransactKey('1ee108ae-67d4-4545-bf6d-4e641cdaa4e0') + SortKey('0')
    )
    assert 's3_uri' in r['cert']
