Cognito IdP: Raise UsernameExistsException from sign_up when user exists (#3765)

* Raise UsernameExistsException from sign_up when user exists

* Run formatter

* Use pytest.raises

* Fix test
This commit is contained in:
Sawyer Hollenshead 2021-03-16 18:22:53 -04:00 committed by GitHub
parent 5fe3a707ed
commit b9f83c200f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -883,6 +883,8 @@ class CognitoIdpBackend(BaseBackend):
user_pool = p
if user_pool is None:
raise ResourceNotFoundError(client_id)
elif username in user_pool.users:
raise UsernameExistsException(username)
user = CognitoIdpUser(
user_pool_id=user_pool.id,

View File

@ -1603,6 +1603,26 @@ def test_sign_up():
result["UserSub"].should_not.be.none
@mock_cognitoidp
def test_sign_up_existing_user():
conn = boto3.client("cognito-idp", "us-west-2")
user_pool_id = conn.create_user_pool(PoolName=str(uuid.uuid4()))["UserPool"]["Id"]
client_id = conn.create_user_pool_client(
UserPoolId=user_pool_id, ClientName=str(uuid.uuid4()),
)["UserPoolClient"]["ClientId"]
username = str(uuid.uuid4())
password = str(uuid.uuid4())
# Add initial user
conn.sign_up(ClientId=client_id, Username=username, Password=password)
with pytest.raises(ClientError) as err:
# Attempt to add user again
conn.sign_up(ClientId=client_id, Username=username, Password=password)
err.value.response["Error"]["Code"].should.equal("UsernameExistsException")
@mock_cognitoidp
def test_confirm_sign_up():
conn = boto3.client("cognito-idp", "us-west-2")