from http import HTTPMethod, HTTPStatus

from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair

from ...conftest import HttpApiProxy, LambdaContext


def test_post_admins(
    mock_app,
    dynamodb_seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = mock_app.lambda_handler(
        http_api_proxy(
            raw_path='/orgs/cJtK9SsnJhKPyxESe7g3DG/admins',
            method=HTTPMethod.POST,
            body={
                'id': '15ee05a3-4ceb-4b7e-9979-db75b28c9ade',
                'name': 'Sérgio R Siqueira',
                'email': 'sergio@somosbeta.com.br',
            },
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.CREATED

    data = dynamodb_persistence_layer.collection.query(
        KeyPair('cJtK9SsnJhKPyxESe7g3DG', 'admins'),
    )
    assert len(data['items']) == 2


def test_delete_admins(
    mock_app,
    dynamodb_seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = mock_app.lambda_handler(
        http_api_proxy(
            raw_path='/orgs/cJtK9SsnJhKPyxESe7g3DG/admins',
            method=HTTPMethod.DELETE,
            body={
                'user_id': 'e170d457-bd6b-475d-b6ae-4f3427a04873',
            },
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK

    data = dynamodb_persistence_layer.collection.query(
        KeyPair('cJtK9SsnJhKPyxESe7g3DG', 'admins'),
    )
    assert len(data['items']) == 0
