Created
February 3, 2022 06:42
-
-
Save MOAMIndustries/d4c21214e36ee0c7f9b2337505dfd13b to your computer and use it in GitHub Desktop.
Testing integration of cognito postConfirmationFunction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
import boto3 | |
from faker import Faker | |
import random | |
from dotenv import load_dotenv, find_dotenv | |
import os | |
import pytest | |
# 1. Account created | |
# 2. User is deactivated | |
# 3. User exists in the database | |
test_numbers = [ | |
'+1555555555', | |
'+1555555555' | |
] | |
organisations = [ | |
'bobs_burgers', | |
'cogswell_cogs' | |
] | |
load_dotenv(find_dotenv()) | |
class User: | |
def __init__(self) -> None: | |
self.fake = Faker() | |
self.name = self.fake.name() | |
self.email = self.fake.ascii_safe_email() | |
self.password = self.fake.password(length=8,special_chars=True) | |
self.organisation = organisations[random.randint(0,len(organisations)-1)] | |
self.number = test_numbers[random.randint(0,len(test_numbers)-1)] | |
def UserAttributes(self): | |
return [ | |
{ | |
'Name':'name', | |
'Value':self.name | |
}, | |
{ | |
'Name':'custom:organisation', #custom parameters must have prefix when called from Boto3 | |
'Value':self.organisation | |
}, | |
{ | |
'Name':'email', | |
'Value':self.email | |
} | |
] | |
def cleanup(self,client): | |
response = client.admin_delete_user( | |
UserPoolId=os.environ['CognitoUserPool'], | |
Username=self.number | |
) | |
print(response) | |
@pytest.fixture(scope='module') | |
def cognito_client(): | |
session = boto3.Session(profile_name=os.environ['profile']) | |
cognito_client = session.client('cognito-idp',region_name=os.environ['region']) | |
return cognito_client | |
@pytest.fixture(scope='session') | |
def user(cognito_client): | |
user = User() | |
yield user | |
user.cleanup(cognito_client) | |
@pytest.mark.dependency() | |
def test_user_signup(user,cognito_client): | |
# Check that cognito is configured | |
response = cognito_client.sign_up( | |
ClientId=os.environ['UserClient'], | |
Username = user.number, | |
Password = user.password, | |
UserAttributes = user.UserAttributes() | |
) | |
response = cognito_client.admin_confirm_sign_up( | |
UserPoolId=os.environ['CognitoUserPool'], | |
Username = user.number, | |
) | |
@pytest.mark.dependency(depends=['test_user_signup']) | |
def test_user_in_database(user,session): | |
# Check if user name exists in the database | |
pass | |
@pytest.mark.dependency(depends=['test_user_signup']) | |
def test_user_is_disables(user,cognito_client) | |
# Check user has been disabled | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment