Skip to content

Commit

Permalink
feat: temp commit with test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
joel@joellee.org committed Jan 10, 2024
1 parent a3f5668 commit 5a68683
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 2 deletions.
13 changes: 12 additions & 1 deletion wrappers/.ci/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,19 @@ services:
interval: 10s
timeout: 5s
retries: 20
cognito_mock_server:
image: jagregory/cognito-local
ports:
- "9229:922"
healthcheck:
test: curl --fail http://0.0.0.0:9229/ || exit 1
interval: 11s
timeout: 6s
retries: 3
cognito:
image: humanmade/local-cognito
container_name: auth0-local
build:
context: ../dockerfiles/cognito
ports:
- "3333:3333"
healthcheck:
Expand Down
13 changes: 13 additions & 0 deletions wrappers/dockerfiles/cognito/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.10-slim

WORKDIR /usr/src/app

RUN apt-get update \
&& apt-get install -y --no-install-recommends curl

# Install AWS CLI and Boto3
RUN pip install --no-cache-dir awscli boto3

COPY ./server.py .

CMD [ "python", "./server.py" ]
111 changes: 111 additions & 0 deletions wrappers/dockerfiles/cognito/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env python3
import http.server
import socketserver
import json
import boto3
import random
import string

# Constants for Cognito
endpoint_url = "http://localhost:9229"
user_pool_name = "MyUserPool"
number_of_users = 90

# Create a Cognito Identity Provider client
client = boto3.client('cognito-idp', endpoint_url=endpoint_url)

def random_string(length=8):
"""Generate a random string of fixed length."""
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))

def create_user_pool(name):
try:
response = client.create_user_pool(
PoolName=name,
Policies={
'PasswordPolicy': {
'MinimumLength': 8,
'RequireUppercase': True,
'RequireLowercase': True,
'RequireNumbers': True,
'RequireSymbols': True
}
},
UsernameAttributes=['email'],
AutoVerifiedAttributes=['email']
)
return response['UserPool']['Id']
except client.exceptions.ResourceExistsException:
print(f"User pool {name} already exists.")
response = client.list_user_pools(MaxResults=1, Filter=f'name = "{name}"')
if response['UserPools']:
return response['UserPools'][0]['Id']
else:
raise Exception(f"User pool {name} not found after creation attempt.")

def create_user(user_pool_id, username):
try:
email = f"{username}@example.com"
response = client.admin_create_user(
UserPoolId=user_pool_id,
Username=username,
UserAttributes=[
{
'Name': 'email',
'Value': email
},
{
'Name': 'email_verified',
'Value': 'True'
}
],
)
print(f"User created: {username} with email {email}")
return response
except Exception as e:
print(f"Error creating user {username}: {e}")

class MockServerHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
response_data = self.list_users()

self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(response_data).encode())

def list_users(self):
try:
response = client.list_users(UserPoolId=user_pool_id, Limit=10)
users = response.get('Users', [])
for user in users:
# Convert datetime objects to string
if 'UserCreateDate' in user:
user['UserCreateDate'] = user['UserCreateDate'].isoformat()
if 'UserLastModifiedDate' in user:
user['UserLastModifiedDate'] = user['UserLastModifiedDate'].isoformat()
print(response)
return response
except Exception as e:
return {'error': str(e)}

if __name__ == "__main__":
# Create user pool and add users
user_pool_id = create_user_pool(user_pool_name)
for _ in range(number_of_users):
username = random_string()
create_user(user_pool_id, username)

# Define server address and port
port = 3792
server_address = ('', port)
# Create an HTTP server instance
httpd = socketserver.TCPServer(server_address, MockServerHandler)
print(f"Cognito mock Server running on port {port}...")
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print("Server stopped")
2 changes: 1 addition & 1 deletion wrappers/src/fdw/cognito_fdw/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod tests {
r#"
CREATE FOREIGN TABLE cognito_view (
email text,
username text,
username text
)
SERVER cognito_server
options (
Expand Down

0 comments on commit 5a68683

Please sign in to comment.