update profile
This commit is contained in:
@@ -64,8 +64,6 @@ class AuditLogMiddleware(BaseMiddlewareHandler):
|
||||
ip_addr = req_context.http.source_ip
|
||||
response = next_middleware(app)
|
||||
|
||||
print(app.context['_route'])
|
||||
|
||||
# Successful response
|
||||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status#successful_responses
|
||||
if 200 <= response.status_code < 300 and user:
|
||||
|
||||
@@ -10,9 +10,11 @@ from elasticsearch import Elasticsearch
|
||||
from layercake.dynamodb import (
|
||||
DynamoDBCollection,
|
||||
DynamoDBPersistenceLayer,
|
||||
KeyPair,
|
||||
MissingError,
|
||||
SortKey,
|
||||
TransactKey,
|
||||
)
|
||||
from layercake.extra_types import CpfStr, NameStr
|
||||
from pydantic import UUID4, BaseModel, StringConstraints
|
||||
|
||||
import cognito
|
||||
@@ -21,6 +23,7 @@ from api_gateway import JSONResponse
|
||||
from boto3clients import dynamodb_client, idp_client
|
||||
from middlewares import AuditLogMiddleware
|
||||
from models import User
|
||||
from rules.user import update_user
|
||||
from settings import ELASTIC_CONN, USER_POOOL_ID, USER_TABLE
|
||||
|
||||
from .emails import router as emails
|
||||
@@ -65,6 +68,46 @@ def post_user(payload: User):
|
||||
return JSONResponse(status_code=HTTPStatus.CREATED)
|
||||
|
||||
|
||||
class UserData(BaseModel):
|
||||
name: NameStr
|
||||
cpf: CpfStr
|
||||
|
||||
|
||||
@router.put(
|
||||
'/<id>',
|
||||
compress=True,
|
||||
tags=['User'],
|
||||
summary='Update user',
|
||||
middlewares=[
|
||||
AuditLogMiddleware('USER_UPDATE', user_collect, ('id', 'name', 'new_cpf'))
|
||||
],
|
||||
)
|
||||
def put_user(id: str, payload: UserData):
|
||||
update_user(
|
||||
{
|
||||
'id': id,
|
||||
'name': payload.name,
|
||||
'cpf': payload.cpf,
|
||||
},
|
||||
persistence_layer=user_layer,
|
||||
)
|
||||
return JSONResponse(
|
||||
body={
|
||||
'id': id,
|
||||
'name': payload.name,
|
||||
'new_cpf': payload.cpf,
|
||||
},
|
||||
status_code=HTTPStatus.OK,
|
||||
)
|
||||
|
||||
|
||||
@router.get('/<id>', compress=True, tags=['User'], summary='Get user')
|
||||
def get_user(id: str):
|
||||
return user_collect.get_items(
|
||||
TransactKey(id) + SortKey('0') + SortKey('last_profile_edit')
|
||||
)
|
||||
|
||||
|
||||
class Password(BaseModel):
|
||||
cognito_sub: UUID4
|
||||
new_password: Annotated[str, StringConstraints(min_length=6)]
|
||||
@@ -95,11 +138,6 @@ def password(id: str, payload: Password):
|
||||
)
|
||||
|
||||
|
||||
@router.get('/<id>', compress=True, tags=['User'], summary='Get user')
|
||||
def get_user(id: str):
|
||||
return user_collect.get_item(KeyPair(id, '0'))
|
||||
|
||||
|
||||
@router.get('/<sub>/idp', compress=True, include_in_schema=False)
|
||||
def get_idp(sub: str):
|
||||
return cognito.admin_get_user(
|
||||
|
||||
@@ -1,11 +1,88 @@
|
||||
import json
|
||||
from http import HTTPMethod, HTTPStatus
|
||||
|
||||
from layercake.dynamodb import DynamoDBCollection, DynamoDBPersistenceLayer, KeyPair
|
||||
from layercake.dynamodb import (
|
||||
DynamoDBCollection,
|
||||
DynamoDBPersistenceLayer,
|
||||
KeyPair,
|
||||
SortKey,
|
||||
TransactKey,
|
||||
)
|
||||
|
||||
from ..conftest import HttpApiProxy, LambdaContext
|
||||
|
||||
|
||||
def test_update_user_cpf(
|
||||
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='/users/5OxmMjL-ujoR5IMGegQz',
|
||||
method=HTTPMethod.PUT,
|
||||
body={
|
||||
'name': 'Sérgio Siqueira',
|
||||
'cpf': '26886156020',
|
||||
},
|
||||
),
|
||||
lambda_context,
|
||||
)
|
||||
assert r['statusCode'] == HTTPStatus.OK
|
||||
|
||||
collect = DynamoDBCollection(dynamodb_persistence_layer)
|
||||
user = collect.get_items(
|
||||
TransactKey('5OxmMjL-ujoR5IMGegQz')
|
||||
+ SortKey('0')
|
||||
+ SortKey('last_profile_edit')
|
||||
)
|
||||
assert 'last_profile_edit' in user
|
||||
|
||||
|
||||
def test_update_user_name(
|
||||
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='/users/5OxmMjL-ujoR5IMGegQz',
|
||||
method=HTTPMethod.PUT,
|
||||
body={
|
||||
'name': 'Sérgio R Siqueira',
|
||||
'cpf': '07879819908',
|
||||
},
|
||||
),
|
||||
lambda_context,
|
||||
)
|
||||
assert r['statusCode'] == HTTPStatus.OK
|
||||
|
||||
|
||||
def test_update_user_conflict_cpf(
|
||||
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='/users/5OxmMjL-ujoR5IMGegQz',
|
||||
method=HTTPMethod.PUT,
|
||||
body={
|
||||
'name': 'Sérgio Siqueira',
|
||||
'cpf': '08679004901',
|
||||
},
|
||||
),
|
||||
lambda_context,
|
||||
)
|
||||
assert r['statusCode'] == HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
def test_get_emails(
|
||||
mock_app,
|
||||
dynamodb_seeds,
|
||||
|
||||
@@ -20,3 +20,4 @@
|
||||
{"id": {"S": "QV4sXY3DvSTUMGJ4QqsrwJ"}, "sk": {"S": "generated_items#43ea4475-c369-4f90-b576-135b7df5106b"}}
|
||||
{"id": {"S": "email"}, "sk": {"S": "sergio@somosbeta.com.br"}}
|
||||
{"id": {"S": "cpf"}, "sk": {"S": "07879819908"}}
|
||||
{"id": {"S": "cpf"}, "sk": {"S": "08679004901"}}
|
||||
|
||||
Reference in New Issue
Block a user