Skip to content

Commit

Permalink
fix: error using upper to get environment variables (#91)
Browse files Browse the repository at this point in the history
* fix: error using upper to get environment variables

* feat: add tests to credentials
  • Loading branch information
gabriel-f-santos authored Jan 30, 2024
1 parent d22c90e commit 4119bdc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion faster_sam/middlewares/lambda_authorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __post_init__(self) -> None:
return None

for attr in attrs:
setattr(self, attr.name, os.getenv(f"AWS_{attr.name.upper}"))
setattr(self, attr.name, os.getenv(f"AWS_{attr.name.upper()}"))


class LambdaClient:
Expand Down
35 changes: 35 additions & 0 deletions tests/test_middleware_lambda_authorizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import io
import json
import os
import unittest
from datetime import datetime, timedelta, timezone
from http import HTTPStatus
Expand Down Expand Up @@ -304,3 +305,37 @@ def test_expired_refresh(self):

self.assertIsNotNone(client.client)
self.assertEqual(client.expired, False)


@mock.patch.dict(
"os.environ",
{
"AWS_ROLE_ARN": "arn:aws:iam::22555448866:role/role-from-environment",
"AWS_WEB_IDENTITY_PROVIDER": "gcp-environment",
"AWS_ROLE_SESSION_NAME": "my-role-session-name-environment",
"AWS_REGION": "us-east-1-environment",
},
)
class TestCredentials(unittest.TestCase):
def test_load_credentials_from_environment(self):
credentials = lambda_authorizer.Credentials()

self.assertEqual(credentials.role_arn, os.environ["AWS_ROLE_ARN"])
self.assertEqual(credentials.web_identity_provider, os.environ["AWS_WEB_IDENTITY_PROVIDER"])
self.assertEqual(credentials.role_session_name, os.environ["AWS_ROLE_SESSION_NAME"])
self.assertEqual(credentials.region, os.environ["AWS_REGION"])

def test_credentials(self):
credentials = lambda_authorizer.Credentials(
role_arn="arn:aws:iam::22555448866:role/role-to-assume",
web_identity_provider="gcp",
role_session_name="my-role-session-name",
region="us-east-1",
)

self.assertNotEqual(credentials.role_arn, os.environ["AWS_ROLE_ARN"])
self.assertNotEqual(
credentials.web_identity_provider, os.environ["AWS_WEB_IDENTITY_PROVIDER"]
)
self.assertNotEqual(credentials.role_session_name, os.environ["AWS_ROLE_SESSION_NAME"])
self.assertNotEqual(credentials.region, os.environ["AWS_REGION"])

0 comments on commit 4119bdc

Please sign in to comment.