Skip to content

Commit

Permalink
Enhance mocking configuration to all existing local users to the mock…
Browse files Browse the repository at this point in the history
… userpool, to allow persisting changes across multiple development sessions. Before this change the mock configuration would only include the 3 test users, but now all existing users will be assigned a test password.
  • Loading branch information
Joshua-Douglas committed Jun 16, 2024
1 parent 8d5ce36 commit 6802a27
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
11 changes: 10 additions & 1 deletion api/openapi_server/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from openapi_server.app import create_app
from openapi_server.configs.mock_aws import AWSMockService
from openapi_server.configs.registry import HUUConfigRegistry
from openapi_server.repositories.user_repo import UserRepository
from openapi_server.models.database import DataAccessLayer

if __name__ == "__main__":
connexion_app = create_app()
Expand All @@ -15,7 +17,14 @@
match flask_app.environment:
case HUUConfigRegistry.DEVELOPMENT:
# Use mocked AWS Cognito service, and temporary user pool
with AWSMockService(flask_app):
with AWSMockService(flask_app) as service:
with DataAccessLayer.session() as session:
user_repo = UserRepository(session)
all_emails = [user.email for user in user_repo.get_all_users()]

for email in all_emails:
service.add_aws_userpool_user(email, "Test!123")

run_app()
case HUUConfigRegistry.STAGING:
# Use the real AWS Cognito service, and real user pool
Expand Down
33 changes: 30 additions & 3 deletions api/openapi_server/configs/mock_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ def destroy(self):
self.app.logger.info("Destroyed fake temporary userpool")

def __enter__(self):
self.create()
self.create()
return self

def __exit__(self, exc_type, exc_value, traceback):
self.destroy()
return self

class AWSMockService():
'''
Expand Down Expand Up @@ -138,6 +140,29 @@ def create_test_users(self):

self.test_users_created = True

def add_aws_userpool_user(self, email, password, attributes=None):
"""
Adds a new user to the temporary user pool with the given username, password, and attributes.
Attributes should be a list of dictionaries, each containing a 'Name' and 'Value' key.
"""
if attributes is None:
attributes = []

try:
response = self.app.boto_client.admin_create_user(
UserPoolId=self.app.config["COGNITO_USER_POOL_ID"],
Username=email,
TemporaryPassword=password,
UserAttributes=attributes,
MessageAction='SUPPRESS'
)
self._auto_signup_user(email)
self.app.logger.info(f"Added user {email} to the temporary user pool")
return response
except Exception as e:
self.app.logger.error(f"Failed to add user {email}: {str(e)}")
raise

def _auto_signup_user(self, email) -> bool:
'''
Auto-confirm a new user. Return True if successful and
Expand Down Expand Up @@ -193,7 +218,9 @@ def stop(self):
self.app.logger.info("Stopped mock AWS Cognito service")

def __enter__(self):
self.start()
self.start()
return self

def __exit__(self, exc_type, exc_value, traceback):
self.stop()
self.stop()
return self
3 changes: 3 additions & 0 deletions api/openapi_server/repositories/user_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def delete_user(self, user_id: int) -> bool:
def get_user(self, email: str) -> User:
return self.session.query(User).filter_by(email=email).first()

def get_all_users(self) -> List[User]:
return self.session.query(User).all()

def get_user_id(self, email: str) -> int:
return self.session.query(User).filter_by(email=email).first().id

Expand Down

0 comments on commit 6802a27

Please sign in to comment.