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