Techdebt: Replace sure with regular assertions in ElastiCache (#6543)

This commit is contained in:
Bert Blommers 2023-07-20 09:36:20 +00:00 committed by GitHub
parent baf4f9c834
commit a0af77f32c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 70 deletions

View File

@ -1,6 +1,5 @@
import boto3
import pytest
import sure # noqa # pylint: disable=unused-import
from botocore.exceptions import ClientError
from moto import mock_elasticache
@ -22,18 +21,17 @@ def test_create_user_no_password_required():
NoPasswordRequired=True,
)
resp.should.have.key("UserId").equals(user_id)
resp.should.have.key("UserName").equals("User1")
resp.should.have.key("Status").equals("active")
resp.should.have.key("Engine").equals("Redis")
resp.should.have.key("MinimumEngineVersion").equals("6.0")
resp.should.have.key("AccessString").equals("on ~* +@all")
resp.should.have.key("UserGroupIds").equals([])
resp.should.have.key("Authentication")
resp["Authentication"].should.have.key("Type").equals("no-password")
resp["Authentication"].shouldnt.have.key("PasswordCount")
resp.should.have.key("ARN").equals(
f"arn:aws:elasticache:ap-southeast-1:{ACCOUNT_ID}:user:{user_id}"
assert resp["UserId"] == user_id
assert resp["UserName"] == "User1"
assert resp["Status"] == "active"
assert resp["Engine"] == "Redis"
assert resp["MinimumEngineVersion"] == "6.0"
assert resp["AccessString"] == "on ~* +@all"
assert resp["UserGroupIds"] == []
assert resp["Authentication"]["Type"] == "no-password"
assert "PasswordCount" not in resp["Authentication"]
assert (
resp["ARN"] == f"arn:aws:elasticache:ap-southeast-1:{ACCOUNT_ID}:user:{user_id}"
)
@ -50,8 +48,8 @@ def test_create_user_with_password_too_short():
Passwords=["mysecretpass"],
)
err = exc.value.response["Error"]
err["Code"].should.equal("InvalidParameterValue")
err["Message"].should.equal("Passwords length must be between 16-128 characters.")
assert err["Code"] == "InvalidParameterValue"
assert err["Message"] == "Passwords length must be between 16-128 characters."
@mock_elasticache
@ -66,18 +64,17 @@ def test_create_user_with_password():
Passwords=["mysecretpassthatsverylong"],
)
resp.should.have.key("UserId").equals(user_id)
resp.should.have.key("UserName").equals("User1")
resp.should.have.key("Status").equals("active")
resp.should.have.key("Engine").equals("Redis")
resp.should.have.key("MinimumEngineVersion").equals("6.0")
resp.should.have.key("AccessString").equals("on ~* +@all")
resp.should.have.key("UserGroupIds").equals([])
resp.should.have.key("Authentication")
resp["Authentication"].should.have.key("Type").equals("password")
resp["Authentication"].should.have.key("PasswordCount").equals(1)
resp.should.have.key("ARN").equals(
f"arn:aws:elasticache:ap-southeast-1:{ACCOUNT_ID}:user:{user_id}"
assert resp["UserId"] == user_id
assert resp["UserName"] == "User1"
assert resp["Status"] == "active"
assert resp["Engine"] == "Redis"
assert resp["MinimumEngineVersion"] == "6.0"
assert resp["AccessString"] == "on ~* +@all"
assert resp["UserGroupIds"] == []
assert resp["Authentication"]["Type"] == "password"
assert resp["Authentication"]["PasswordCount"] == 1
assert (
resp["ARN"] == f"arn:aws:elasticache:ap-southeast-1:{ACCOUNT_ID}:user:{user_id}"
)
@ -89,9 +86,10 @@ def test_create_user_without_password():
UserId="user1", UserName="User1", Engine="Redis", AccessString="?"
)
err = exc.value.response["Error"]
err["Code"].should.equal("InvalidParameterValue")
err["Message"].should.equal(
"No password was provided. If you want to create/update the user without password, please use the NoPasswordRequired flag."
assert err["Code"] == "InvalidParameterValue"
assert (
err["Message"]
== "No password was provided. If you want to create/update the user without password, please use the NoPasswordRequired flag."
)
@ -116,8 +114,8 @@ def test_create_user_twice():
Passwords=["mysecretpassthatsverylong"],
)
err = exc.value.response["Error"]
err["Code"].should.equal("UserAlreadyExists")
err["Message"].should.equal("User user1 already exists.")
assert err["Code"] == "UserAlreadyExists"
assert err["Message"] == "User user1 already exists."
@mock_elasticache
@ -126,8 +124,8 @@ def test_delete_user_unknown():
with pytest.raises(ClientError) as exc:
client.delete_user(UserId="unknown")
err = exc.value.response["Error"]
err["Code"].should.equal("UserNotFound")
err["Message"].should.equal("User unknown not found.")
assert err["Code"] == "UserNotFound"
assert err["Message"] == "User unknown not found."
@mock_elasticache
@ -146,12 +144,12 @@ def test_delete_user():
# Initial status is 'deleting'
resp = client.describe_users(UserId="user1")
resp["Users"][0]["Status"].should.equal("deleting")
assert resp["Users"][0]["Status"] == "deleting"
# User is only deleted after some time
with pytest.raises(ClientError) as exc:
client.describe_users(UserId="unknown")
exc.value.response["Error"]["Code"].should.equal("UserNotFound")
assert exc.value.response["Error"]["Code"] == "UserNotFound"
@mock_elasticache
@ -159,20 +157,18 @@ def test_describe_users_initial():
client = boto3.client("elasticache", region_name="us-east-2")
resp = client.describe_users()
resp.should.have.key("Users").length_of(1)
resp["Users"][0].should.equal(
{
"UserId": "default",
"UserName": "default",
"Status": "active",
"Engine": "redis",
"MinimumEngineVersion": "6.0",
"AccessString": "on ~* +@all",
"UserGroupIds": [],
"Authentication": {"Type": "no-password"},
"ARN": f"arn:aws:elasticache:us-east-2:{ACCOUNT_ID}:user:default",
}
)
assert len(resp["Users"]) == 1
assert resp["Users"][0] == {
"UserId": "default",
"UserName": "default",
"Status": "active",
"Engine": "redis",
"MinimumEngineVersion": "6.0",
"AccessString": "on ~* +@all",
"UserGroupIds": [],
"Authentication": {"Type": "no-password"},
"ARN": f"arn:aws:elasticache:us-east-2:{ACCOUNT_ID}:user:default",
}
@mock_elasticache
@ -189,20 +185,18 @@ def test_describe_users():
resp = client.describe_users()
resp.should.have.key("Users").length_of(2)
resp["Users"].should.contain(
{
"UserId": "user1",
"UserName": "User1",
"Status": "active",
"Engine": "Redis",
"MinimumEngineVersion": "6.0",
"AccessString": "on ~* +@all",
"UserGroupIds": [],
"Authentication": {"Type": "password", "PasswordCount": 1},
"ARN": f"arn:aws:elasticache:ap-southeast-1:{ACCOUNT_ID}:user:user1",
}
)
assert len(resp["Users"]) == 2
assert {
"UserId": "user1",
"UserName": "User1",
"Status": "active",
"Engine": "Redis",
"MinimumEngineVersion": "6.0",
"AccessString": "on ~* +@all",
"UserGroupIds": [],
"Authentication": {"Type": "password", "PasswordCount": 1},
"ARN": f"arn:aws:elasticache:ap-southeast-1:{ACCOUNT_ID}:user:user1",
} in resp["Users"]
@mock_elasticache
@ -212,5 +206,5 @@ def test_describe_users_unknown_userid():
with pytest.raises(ClientError) as exc:
client.describe_users(UserId="unknown")
err = exc.value.response["Error"]
err["Code"].should.equal("UserNotFound")
err["Message"].should.equal("User unknown not found.")
assert err["Code"] == "UserNotFound"
assert err["Message"] == "User unknown not found."

View File

@ -1,5 +1,3 @@
import sure # noqa # pylint: disable=unused-import
import moto.server as server
@ -10,5 +8,5 @@ def test_elasticache_describe_users():
data = "Action=DescribeUsers"
headers = {"Host": "elasticache.us-east-1.amazonaws.com"}
resp = test_client.post("/", data=data, headers=headers)
resp.status_code.should.equal(200)
str(resp.data).should.contain("<UserId>default</UserId>")
assert resp.status_code == 200
assert "<UserId>default</UserId>" in str(resp.data)