add integration

This commit is contained in:
2025-08-06 18:46:21 -03:00
parent e08f16bbaa
commit ff25ade76e
16 changed files with 314 additions and 132 deletions

View File

@@ -3,6 +3,7 @@ import json
import os
from dataclasses import dataclass
from http import HTTPMethod
from urllib.parse import urlencode
import jsonlines
import pytest
@@ -37,16 +38,22 @@ class HttpApiProxy:
body: dict | str | None = None,
*,
headers: dict = {},
cookies: dict = {},
cookies: list[str] = [],
query_string_parameters: dict = {},
is_base64_encoded: bool = True,
**kwargs,
) -> dict:
if isinstance(body, dict):
body = json.dumps(body)
if is_base64_encoded and body:
body = _base64_encode(body)
return {
'version': '2.0',
'routeKey': '$default',
'rawPath': raw_path,
'rawQueryString': 'parameter1=value1&parameter1=value2&parameter2=value',
'rawQueryString': urlencode(query_string_parameters),
'cookies': cookies,
'headers': headers,
'queryStringParameters': query_string_parameters,
@@ -69,17 +76,17 @@ class HttpApiProxy:
'time': '12/Mar/2020:19:03:58 +0000',
'timeEpoch': 1583348638390,
},
'body': _base64_dict(body) if isinstance(body, dict) else body,
'body': body,
'pathParameters': {'parameter1': 'value1'},
'isBase64Encoded': is_base64_encoded,
'stageVariables': {'stageVariable1': 'value1', 'stageVariable2': 'value2'},
}
def _base64_dict(obj: dict = {}) -> str | None:
if not obj:
def _base64_encode(s: str) -> str | None:
if not s:
return None
return base64.b64encode(json.dumps(obj).encode()).decode()
return base64.b64encode(s.encode()).decode()
@pytest.fixture
@@ -128,7 +135,7 @@ def dynamodb_persistence_layer(dynamodb_client):
@pytest.fixture()
def dynamodb_seeds(dynamodb_client):
def seeds(dynamodb_client):
from layercake.dynamodb import serialize
with open('tests/seeds.jsonl', 'rb') as fp:
@@ -142,7 +149,7 @@ def dynamodb_seeds(dynamodb_client):
@pytest.fixture
def mock_app():
def app():
import app
return app

View File

@@ -2,19 +2,26 @@ from http import HTTPMethod
from layercake.dynamodb import DynamoDBPersistenceLayer
from jose_ import generate_jwt
from ..conftest import HttpApiProxy, LambdaContext
def test_authorize(
mock_app,
dynamodb_seeds,
app,
seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
http_api_proxy: HttpApiProxy,
lambda_context: LambdaContext,
):
client_id = 'd72d4005-1fa7-4430-9754-80d5e2487bb6'
r = mock_app.lambda_handler(
id_token = generate_jwt(
user_id='357db1c5-7442-4075-98a3-fbe5c938a419',
email='sergio@somosbeta.com.br',
)
r = app.lambda_handler(
http_api_proxy(
raw_path='/authorize',
method=HTTPMethod.GET,
@@ -25,6 +32,9 @@ def test_authorize(
'scope': 'openid',
'nonce': '123',
},
cookies=[
f'id_token={id_token}; HttpOnly; Secure',
],
),
lambda_context,
)

View File

@@ -3,16 +3,17 @@ from http import HTTPMethod
from ..conftest import HttpApiProxy, LambdaContext
def test_html_page(
mock_app,
dynamodb_seeds,
def test_html(
app,
seeds,
http_api_proxy: HttpApiProxy,
lambda_context: LambdaContext,
):
r = mock_app.lambda_handler(
r = app.lambda_handler(
http_api_proxy(
raw_path='/login',
method=HTTPMethod.GET,
query_string_parameters={'continue': 'http://localhost'},
),
lambda_context,
)
@@ -21,20 +22,19 @@ def test_html_page(
def test_login(
mock_app,
dynamodb_seeds,
app,
seeds,
http_api_proxy: HttpApiProxy,
lambda_context: LambdaContext,
):
r = mock_app.lambda_handler(
r = app.lambda_handler(
http_api_proxy(
raw_path='/login',
method=HTTPMethod.POST,
headers={
'Content-Type': 'application/x-www-form-urlencoded',
},
body='username=sergio@somosbeta.com.br&password=pytest@123',
is_base64_encoded=False,
body='username=sergio@somosbeta.com.br&password=pytest@123&continue=https://localhost',
),
lambda_context,
)

View File

@@ -1,7 +1,11 @@
// OAuth2
{"id": "OAUTH2_CLIENT", "sk": "CLIENT_ID#d72d4005-1fa7-4430-9754-80d5e2487bb6", "secret": "1nFD8alDbGHgc3g1RLY960xyRJVee0SlMoIB0MUlSuiJy28W", "name": "pytest", "scope": "openid profile", "redirect_uris": ["https://localhost/callback"], "response_types": ["code"], "grant_types": ["authorization_code"]}
{"id": "OAUTH2_CODE#CLIENT_ID#d72d4005-1fa7-4430-9754-80d5e2487bb6", "sk": "CODE#kyqp3oSuRFTfuBaCmq3XOgGWg67l42Kt3D6xPEj7Yd3MLdi9", "redirect_uri": "https://localhost/callback", "user_id": "0cb0ce87-9df6-40c1-9fa7-7dfdafd7910e", "nonce": "123", "scope": "openid profile email"}
// Post-migration: uncomment the following line
// {"id": "EMAIL", "sk": "sergio@somosbeta.com.br", "user_id": "357db1c5-7442-4075-98a3-fbe5c938a419"}
{"id": "email", "sk": "sergio@somosbeta.com.br", "user_id": "357db1c5-7442-4075-98a3-fbe5c938a419"}
// User data
{"id": "357db1c5-7442-4075-98a3-fbe5c938a419", "sk": "0", "name": "Sérgio R Siqueira", "email": "sergio@somosbeta.com.br"}
{"id": "357db1c5-7442-4075-98a3-fbe5c938a419", "sk": "PASSWORD", "hash": "$pbkdf2-sha256$29000$IuTcm7M2BiAEgPB.b.3dGw$d8xVCbx8zxg7MeQBrOvCOgniiilsIHEMHzoH/OXftLQ"}