Skip to content

Commit

Permalink
fix: check existence of tenant before updating (#11)
Browse files Browse the repository at this point in the history
* fix: check existence of tenant before updating

* update tests to include check for deleting non-existent tenant
  • Loading branch information
suhussai committed Mar 27, 2024
1 parent 4d325b3 commit 2dd5fbd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions resources/functions/tenant-management/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import uuid

import boto3
from boto3.dynamodb.conditions import Attr
import botocore
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import (APIGatewayRestResolver,
Expand Down Expand Up @@ -100,6 +101,9 @@ def update_tenant(tenantId):
try:
response = __update_tenant(tenantId, input_details)
updated_tenant = response['Attributes']
except dynamodb.meta.client.exceptions.ConditionalCheckFailedException:
logger.info(f'received request to update non-existing tenant {tenantId}')
raise NotFoundError(f"Tenant {tenantId} not found.")
except botocore.exceptions.ClientError as error:
logger.error(error)
raise InternalServerError("Unknown error during processing!")
Expand All @@ -116,6 +120,9 @@ def delete_tenant(tenantId):
response = __update_tenant(tenantId, {'tenantStatus': 'Deleting'})
__create_control_plane_event(
json.dumps(response['Attributes']), offboarding_detail_type)
except dynamodb.meta.client.exceptions.ConditionalCheckFailedException:
logger.info(f'received request to update non-existing tenant {tenantId}')
raise NotFoundError(f"Tenant {tenantId} not found.")
except botocore.exceptions.ClientError as error:
logger.error(error)
raise InternalServerError("Unknown error during processing!")
Expand All @@ -142,6 +149,7 @@ def __update_tenant(tenantId, tenant):
Key={
'tenantId': tenantId,
},
ConditionExpression=Attr('tenantId').eq(tenantId),
UpdateExpression=''.join(update_expression),
ExpressionAttributeValues=expression_attribute_values,
ReturnValues="ALL_NEW"
Expand Down
12 changes: 12 additions & 0 deletions scripts/test-sbt-aws.sh
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ else
log_test "fail" "Failed to delete tenant"
fi

# Test deleting a non-existent tenant
echo "Testing delete-tenant for non-existent tenant..."
fake_tenant_id=$(openssl rand -hex 10)
delete_output=$(./sbt-aws.sh delete-tenant "$fake_tenant_id" 2>&1)
delete_response=$(echo "$delete_output" | jq -r '.')

if [ "$(echo "$delete_response" | jq -r '.statusCode')" = "404" ] && [ "$(echo "$delete_response" | jq -r '.message')" = "Tenant '$fake_tenant_id' not found." ]; then
log_test "pass" "Received expected error when deleting non-existent tenant"
else
log_test "fail" "Unexpected output when deleting non-existent tenant"
fi

# Set the exit code based on the overall test status
if [ "$TEST_PASSED" = true ]; then
exit 0
Expand Down

0 comments on commit 2dd5fbd

Please sign in to comment.