180 lines
6.1 KiB
Python
180 lines
6.1 KiB
Python
import json
|
|
from datetime import date, datetime
|
|
from http import HTTPMethod, HTTPStatus
|
|
from pprint import pprint
|
|
|
|
from layercake.dynamodb import DynamoDBPersistenceLayer, PartitionKey
|
|
|
|
from routes.orders.checkout import _calc_due_date
|
|
|
|
from ...conftest import HttpApiProxy, LambdaContext
|
|
|
|
|
|
def test_checkout_coupon(
|
|
app,
|
|
seeds,
|
|
http_api_proxy: HttpApiProxy,
|
|
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
|
lambda_context: LambdaContext,
|
|
):
|
|
r = app.lambda_handler(
|
|
http_api_proxy(
|
|
raw_path='/orders',
|
|
method=HTTPMethod.POST,
|
|
body={
|
|
'org_id': 'f6000f79-6e5c-49a0-952f-3bda330ef278',
|
|
'cnpj': '00000000000191',
|
|
'name': 'Branco do Brasil',
|
|
'email': 'bb@users.noreply.saladeaula.digital',
|
|
'payment_method': 'CREDIT_CARD',
|
|
# 'installments': 2,
|
|
'credit_card': {
|
|
'holder_name': 'Sergio R Siqueira',
|
|
'number': '4111111111111111',
|
|
'exp_month': '01',
|
|
'exp_year': '2026',
|
|
'cvv': '123',
|
|
},
|
|
'created_by': {
|
|
'id': '15bacf02-1535-4bee-9022-19d106fd7518',
|
|
'name': 'Sérgio R Siqueira',
|
|
},
|
|
'address': {
|
|
'city': 'Curitiba',
|
|
'postcode': '81280350',
|
|
'neighborhood': 'Cidade Industrial',
|
|
'address1': 'Rua Monsenhor Ivo Zanlorenzi',
|
|
'address2': '5190, ap 1802',
|
|
'state': 'PR',
|
|
},
|
|
'items': [
|
|
{
|
|
'name': 'CIPA Grau de Risco 1',
|
|
'id': '3c27ea9c-9464-46a1-9717-8c1441793186',
|
|
'quantity': 1,
|
|
'unit_price': 99,
|
|
},
|
|
{
|
|
'name': 'CIPA Grau de Risco 2',
|
|
'id': '99bb3b60-4ded-4a8e-937c-ba2d78ec6454',
|
|
'quantity': 1,
|
|
'unit_price': 99,
|
|
},
|
|
],
|
|
'enrollments': [
|
|
{
|
|
'user': {
|
|
'name': 'Sérgio Rafael de Siqueira',
|
|
'cpf': '07879819908',
|
|
'id': '5OxmMjL-ujoR5IMGegQz',
|
|
'email': 'sergio@somosbeta.com.br',
|
|
},
|
|
'course': {
|
|
'name': 'CIPA Grau de Risco 1',
|
|
'id': '3c27ea9c-9464-46a1-9717-8c1441793186',
|
|
'access_period': 365,
|
|
},
|
|
'id': '2f026d38-1edc-44ea-abf3-60c10bc58909',
|
|
},
|
|
{
|
|
'course': {
|
|
'name': 'CIPA Grau de Risco 2',
|
|
'id': '99bb3b60-4ded-4a8e-937c-ba2d78ec6454',
|
|
'access_period': 365,
|
|
},
|
|
'scheduled_for': '2026-01-20',
|
|
'id': '1f0931ad-7dd4-4ca1-bce2-a2e89efa5b56',
|
|
'user': {
|
|
'name': 'Maitê L Siqueira',
|
|
'cpf': '02186829991',
|
|
'id': '87606a7f-de56-4198-a91d-b6967499d382',
|
|
'email': 'osergiosiqueira+maite@gmail.com',
|
|
},
|
|
},
|
|
],
|
|
'coupon': {
|
|
'code': '10OFF',
|
|
'type': 'PERCENT',
|
|
'amount': 10,
|
|
},
|
|
},
|
|
),
|
|
lambda_context,
|
|
)
|
|
body = json.loads(r['body'])
|
|
assert r['statusCode'] == HTTPStatus.CREATED
|
|
|
|
r = dynamodb_persistence_layer.collection.query(PartitionKey(body['id']))
|
|
pprint(r['items'])
|
|
|
|
|
|
def test_checkout_from_user(
|
|
app,
|
|
seeds,
|
|
http_api_proxy: HttpApiProxy,
|
|
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
|
lambda_context: LambdaContext,
|
|
):
|
|
r = app.lambda_handler(
|
|
http_api_proxy(
|
|
raw_path='/orders',
|
|
method=HTTPMethod.POST,
|
|
body={
|
|
'user_id': '15bacf02-1535-4bee-9022-19d106fd7518',
|
|
'cpf': '07879819908',
|
|
'name': 'Sérgio R Siqueira',
|
|
'email': 'sergio@somosbeta.com.br',
|
|
'payment_method': 'BANK_SLIP',
|
|
'address': {
|
|
'sk': 'METADATA#ADDRESS',
|
|
'address1': 'Rua Monsenhor Ivo Zanlorenzi',
|
|
'address2': '5190, ap 1802',
|
|
'postcode': '81280350',
|
|
'city': 'Curitiba',
|
|
'neighborhood': 'Cidade Industrial',
|
|
'state': 'PR',
|
|
},
|
|
'items': [
|
|
{
|
|
'id': 'e1c44881-2fe3-484e-ada2-12b6bf5b9398',
|
|
'name': 'NR-35 Segurança nos Trabalhos em Altura',
|
|
'quantity': 2,
|
|
'unit_price': 119,
|
|
'access_period': 20,
|
|
}
|
|
],
|
|
},
|
|
),
|
|
lambda_context,
|
|
)
|
|
print(r)
|
|
assert r['statusCode'] == HTTPStatus.CREATED
|
|
|
|
|
|
def test_calc_due_date_skips_weekends_and_holidays():
|
|
start_date = datetime(2026, 1, 9, 10, 30) # Friday
|
|
business_days = 3
|
|
|
|
holidays = {
|
|
date(2026, 1, 12), # Monday (holiday)
|
|
}
|
|
|
|
result = _calc_due_date(
|
|
start_date=start_date,
|
|
business_days=business_days,
|
|
holidays=holidays,
|
|
)
|
|
|
|
# Fri (9) + 3 calendar days -> Mon (12)
|
|
# Mon (12) is a holiday -> adjust to Tue (13)
|
|
expected = datetime(2026, 1, 13, 10, 30)
|
|
|
|
assert result == expected
|
|
|
|
|
|
def test_calc_due_date_only_weekends():
|
|
start_date = datetime(2026, 1, 8, 9, 0) # Thursday
|
|
result = _calc_due_date(start_date, 1)
|
|
|
|
assert result == datetime(2026, 1, 9, 9, 0)
|