Existing user now raises a UsernameExistsException
If a user is attempted to be added to a pool that already contains a user with that username, the UsernameExistsException is thrown, to match AWS behaviour.
This commit is contained in:
parent
40271d2c4e
commit
ec99a6b827
@ -24,6 +24,16 @@ class UserNotFoundError(BadRequest):
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
class UsernameExistsException(BadRequest):
|
||||||
|
|
||||||
|
def __init__(self, message):
|
||||||
|
super(UsernameExistsException, self).__init__()
|
||||||
|
self.description = json.dumps({
|
||||||
|
"message": message,
|
||||||
|
'__type': 'UsernameExistsException',
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
class GroupExistsException(BadRequest):
|
class GroupExistsException(BadRequest):
|
||||||
|
|
||||||
def __init__(self, message):
|
def __init__(self, message):
|
||||||
|
@ -14,7 +14,8 @@ from jose import jws
|
|||||||
|
|
||||||
from moto.compat import OrderedDict
|
from moto.compat import OrderedDict
|
||||||
from moto.core import BaseBackend, BaseModel
|
from moto.core import BaseBackend, BaseModel
|
||||||
from .exceptions import GroupExistsException, NotAuthorizedError, ResourceNotFoundError, UserNotFoundError
|
from .exceptions import GroupExistsException, NotAuthorizedError, ResourceNotFoundError, UserNotFoundError, \
|
||||||
|
UsernameExistsException
|
||||||
|
|
||||||
UserStatus = {
|
UserStatus = {
|
||||||
"FORCE_CHANGE_PASSWORD": "FORCE_CHANGE_PASSWORD",
|
"FORCE_CHANGE_PASSWORD": "FORCE_CHANGE_PASSWORD",
|
||||||
@ -561,6 +562,9 @@ class CognitoIdpBackend(BaseBackend):
|
|||||||
if not user_pool:
|
if not user_pool:
|
||||||
raise ResourceNotFoundError(user_pool_id)
|
raise ResourceNotFoundError(user_pool_id)
|
||||||
|
|
||||||
|
if username in user_pool.users:
|
||||||
|
raise UsernameExistsException(username)
|
||||||
|
|
||||||
user = CognitoIdpUser(user_pool_id, username, temporary_password, UserStatus["FORCE_CHANGE_PASSWORD"], attributes)
|
user = CognitoIdpUser(user_pool_id, username, temporary_password, UserStatus["FORCE_CHANGE_PASSWORD"], attributes)
|
||||||
user_pool.users[user.username] = user
|
user_pool.users[user.username] = user
|
||||||
return user
|
return user
|
||||||
|
@ -886,6 +886,36 @@ def test_admin_create_user():
|
|||||||
result["User"]["Enabled"].should.equal(True)
|
result["User"]["Enabled"].should.equal(True)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_cognitoidp
|
||||||
|
def test_admin_create_existing_user():
|
||||||
|
conn = boto3.client("cognito-idp", "us-west-2")
|
||||||
|
|
||||||
|
username = str(uuid.uuid4())
|
||||||
|
value = str(uuid.uuid4())
|
||||||
|
user_pool_id = conn.create_user_pool(PoolName=str(uuid.uuid4()))["UserPool"]["Id"]
|
||||||
|
conn.admin_create_user(
|
||||||
|
UserPoolId=user_pool_id,
|
||||||
|
Username=username,
|
||||||
|
UserAttributes=[
|
||||||
|
{"Name": "thing", "Value": value}
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
caught = False
|
||||||
|
try:
|
||||||
|
conn.admin_create_user(
|
||||||
|
UserPoolId=user_pool_id,
|
||||||
|
Username=username,
|
||||||
|
UserAttributes=[
|
||||||
|
{"Name": "thing", "Value": value}
|
||||||
|
],
|
||||||
|
)
|
||||||
|
except conn.exceptions.UsernameExistsException:
|
||||||
|
caught = True
|
||||||
|
|
||||||
|
caught.should.be.true
|
||||||
|
|
||||||
|
|
||||||
@mock_cognitoidp
|
@mock_cognitoidp
|
||||||
def test_admin_get_user():
|
def test_admin_get_user():
|
||||||
conn = boto3.client("cognito-idp", "us-west-2")
|
conn = boto3.client("cognito-idp", "us-west-2")
|
||||||
|
Loading…
Reference in New Issue
Block a user