Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check existence of tenant before updating #11

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading