Techdebt: Replace sure with regular assertions in WAFv2 (#6657)

This commit is contained in:
kbalk 2023-08-14 05:54:27 -04:00 committed by GitHub
parent 795e9b8677
commit b9c848fd9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 74 deletions

View File

@ -1,9 +1,7 @@
import sure # noqa # pylint: disable=unused-import
import moto.server as server
from moto import mock_wafv2 from moto import mock_wafv2
from .test_helper_functions import CREATE_WEB_ACL_BODY, LIST_WEB_ACL_BODY
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
import moto.server as server
from .test_helper_functions import CREATE_WEB_ACL_BODY, LIST_WEB_ACL_BODY
CREATE_WEB_ACL_HEADERS = { CREATE_WEB_ACL_HEADERS = {
"X-Amz-Target": "AWSWAF_20190729.CreateWebACL", "X-Amz-Target": "AWSWAF_20190729.CreateWebACL",
@ -43,9 +41,9 @@ def test_create_web_acl():
) )
assert res.status_code == 400 assert res.status_code == 400
assert ( assert (
b"AWS WAF could not perform the operation because some resource in your request is a duplicate of an existing one." b"AWS WAF could not perform the operation because some resource "
in res.data b"in your request is a duplicate of an existing one."
) ) in res.data
assert b"WafV2DuplicateItem" in res.data assert b"WafV2DuplicateItem" in res.data
res = test_client.post( res = test_client.post(

View File

@ -1,11 +1,10 @@
import pytest
import sure # noqa # pylint: disable=unused-import
import boto3 import boto3
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
import pytest
from moto import mock_wafv2 from moto import mock_wafv2
from .test_helper_functions import CREATE_WEB_ACL_BODY, LIST_WEB_ACL_BODY
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
from .test_helper_functions import CREATE_WEB_ACL_BODY, LIST_WEB_ACL_BODY
@mock_wafv2 @mock_wafv2
@ -22,10 +21,11 @@ def test_create_web_acl():
with pytest.raises(ClientError) as ex: with pytest.raises(ClientError) as ex:
conn.create_web_acl(**CREATE_WEB_ACL_BODY("John", "REGIONAL")) conn.create_web_acl(**CREATE_WEB_ACL_BODY("John", "REGIONAL"))
err = ex.value.response["Error"] err = ex.value.response["Error"]
err["Message"].should.contain( assert (
"AWS WAF could not perform the operation because some resource in your request is a duplicate of an existing one." "AWS WAF could not perform the operation because some resource "
) "in your request is a duplicate of an existing one."
err["Code"].should.equal("WafV2DuplicateItem") ) in err["Message"]
assert err["Code"] == "WafV2DuplicateItem"
res = conn.create_web_acl(**CREATE_WEB_ACL_BODY("Carl", "CLOUDFRONT")) res = conn.create_web_acl(**CREATE_WEB_ACL_BODY("Carl", "CLOUDFRONT"))
web_acl = res["Summary"] web_acl = res["Summary"]
@ -84,16 +84,14 @@ def test_create_web_acl_with_all_arguments():
)["Summary"]["Id"] )["Summary"]["Id"]
wacl = client.get_web_acl(Name="test", Scope="CLOUDFRONT", Id=web_acl_id)["WebACL"] wacl = client.get_web_acl(Name="test", Scope="CLOUDFRONT", Id=web_acl_id)["WebACL"]
wacl.should.have.key("Description").equals("test desc") assert wacl["Description"] == "test desc"
wacl.should.have.key("DefaultAction").equals({"Allow": {}}) assert wacl["DefaultAction"] == {"Allow": {}}
wacl.should.have.key("VisibilityConfig").equals( assert wacl["VisibilityConfig"] == {
{ "SampledRequestsEnabled": False,
"SampledRequestsEnabled": False, "CloudWatchMetricsEnabled": False,
"CloudWatchMetricsEnabled": False, "MetricName": "idk",
"MetricName": "idk", }
} assert len(wacl["Rules"]) == 2
)
wacl.should.have.key("Rules").length_of(2)
@mock_wafv2 @mock_wafv2
@ -102,8 +100,8 @@ def test_get_web_acl():
body = CREATE_WEB_ACL_BODY("John", "REGIONAL") body = CREATE_WEB_ACL_BODY("John", "REGIONAL")
web_acl_id = conn.create_web_acl(**body)["Summary"]["Id"] web_acl_id = conn.create_web_acl(**body)["Summary"]["Id"]
wacl = conn.get_web_acl(Name="John", Scope="REGIONAL", Id=web_acl_id)["WebACL"] wacl = conn.get_web_acl(Name="John", Scope="REGIONAL", Id=web_acl_id)["WebACL"]
wacl.should.have.key("Name").equals("John") assert wacl["Name"] == "John"
wacl.should.have.key("Id").equals(web_acl_id) assert wacl["Id"] == web_acl_id
@mock_wafv2 @mock_wafv2
@ -134,13 +132,13 @@ def test_delete_web_acl():
conn.delete_web_acl(Name="Daphne", Id=wacl_id, Scope="REGIONAL", LockToken="n/a") conn.delete_web_acl(Name="Daphne", Id=wacl_id, Scope="REGIONAL", LockToken="n/a")
res = conn.list_web_acls(**LIST_WEB_ACL_BODY("REGIONAL")) res = conn.list_web_acls(**LIST_WEB_ACL_BODY("REGIONAL"))
res["WebACLs"].should.have.length_of(0) assert len(res["WebACLs"]) == 0
with pytest.raises(ClientError) as exc: with pytest.raises(ClientError) as exc:
conn.get_web_acl(Name="Daphne", Scope="REGIONAL", Id=wacl_id) conn.get_web_acl(Name="Daphne", Scope="REGIONAL", Id=wacl_id)
err = exc.value.response["Error"] err = exc.value.response["Error"]
err["Code"].should.equal("WAFNonexistentItemException") assert err["Code"] == "WAFNonexistentItemException"
err["Message"].should.equal( assert err["Message"] == (
"AWS WAF couldnt perform the operation because your resource doesnt exist." "AWS WAF couldnt perform the operation because your resource doesnt exist."
) )
@ -177,31 +175,25 @@ def test_update_web_acl():
"MetricName": "updated", "MetricName": "updated",
}, },
) )
resp.should.have.key("NextLockToken") assert "NextLockToken" in resp
acl = conn.get_web_acl(Name="Daphne", Scope="REGIONAL", Id=wacl_id)["WebACL"] acl = conn.get_web_acl(Name="Daphne", Scope="REGIONAL", Id=wacl_id)["WebACL"]
acl.should.have.key("Description").equals("updated_desc") assert acl["Description"] == "updated_desc"
acl.should.have.key("DefaultAction").equals( assert acl["DefaultAction"] == {"Block": {"CustomResponse": {"ResponseCode": 412}}}
{"Block": {"CustomResponse": {"ResponseCode": 412}}} assert acl["Rules"] == [
)
acl.should.have.key("Rules").equals(
[
{
"Name": "rule1",
"Priority": 456,
"Statement": {},
"VisibilityConfig": {
"SampledRequestsEnabled": True,
"CloudWatchMetricsEnabled": True,
"MetricName": "updated",
},
}
]
)
acl.should.have.key("VisibilityConfig").equals(
{ {
"SampledRequestsEnabled": True, "Name": "rule1",
"CloudWatchMetricsEnabled": True, "Priority": 456,
"MetricName": "updated", "Statement": {},
"VisibilityConfig": {
"SampledRequestsEnabled": True,
"CloudWatchMetricsEnabled": True,
"MetricName": "updated",
},
} }
) ]
assert acl["VisibilityConfig"] == {
"SampledRequestsEnabled": True,
"CloudWatchMetricsEnabled": True,
"MetricName": "updated",
}

View File

@ -1,11 +1,10 @@
import pytest
import sure # noqa # pylint: disable=unused-import
import boto3 import boto3
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
import pytest
from moto import mock_apigateway, mock_wafv2 from moto import mock_apigateway, mock_wafv2
from .test_helper_functions import CREATE_WEB_ACL_BODY
from tests.test_apigateway.test_apigateway_stage import create_method_integration from tests.test_apigateway.test_apigateway_stage import create_method_integration
from .test_helper_functions import CREATE_WEB_ACL_BODY
@mock_wafv2 @mock_wafv2
@ -28,8 +27,8 @@ def test_associate_with_unknown_resource():
WebACLArn=f"{wacl_arn}2", ResourceArn="unknownarnwithminlength20" WebACLArn=f"{wacl_arn}2", ResourceArn="unknownarnwithminlength20"
) )
err = exc.value.response["Error"] err = exc.value.response["Error"]
err["Code"].should.equal("WAFNonexistentItemException") assert err["Code"] == "WAFNonexistentItemException"
err["Message"].should.equal( assert err["Message"] == (
"AWS WAF couldnt perform the operation because your resource doesnt exist." "AWS WAF couldnt perform the operation because your resource doesnt exist."
) )
@ -48,12 +47,12 @@ def test_associate_with_apigateway_stage():
conn.associate_web_acl(WebACLArn=wacl_arn, ResourceArn=stage_arn) conn.associate_web_acl(WebACLArn=wacl_arn, ResourceArn=stage_arn)
stage = apigw.get_stage(restApiId=api_id, stageName="test") stage = apigw.get_stage(restApiId=api_id, stageName="test")
stage.should.have.key("webAclArn").equals(wacl_arn) assert stage["webAclArn"] == wacl_arn
conn.disassociate_web_acl(ResourceArn=stage_arn) conn.disassociate_web_acl(ResourceArn=stage_arn)
stage = apigw.get_stage(restApiId=api_id, stageName="test") stage = apigw.get_stage(restApiId=api_id, stageName="test")
stage.shouldnt.have.key("webAclArn") assert "webAclArn" not in stage
@mock_apigateway @mock_apigateway
@ -68,14 +67,14 @@ def test_get_web_acl_for_resource():
_, stage_arn = create_apigateway_stage(client=apigw) _, stage_arn = create_apigateway_stage(client=apigw)
resp = conn.get_web_acl_for_resource(ResourceArn=stage_arn) resp = conn.get_web_acl_for_resource(ResourceArn=stage_arn)
resp.shouldnt.have.key("WebACL") assert "WebACL" not in resp
conn.associate_web_acl(WebACLArn=wacl_arn, ResourceArn=stage_arn) conn.associate_web_acl(WebACLArn=wacl_arn, ResourceArn=stage_arn)
resp = conn.get_web_acl_for_resource(ResourceArn=stage_arn) resp = conn.get_web_acl_for_resource(ResourceArn=stage_arn)
resp.should.have.key("WebACL") assert "WebACL" in resp
resp["WebACL"].should.have.key("Name").equals("John") assert resp["WebACL"]["Name"] == "John"
resp["WebACL"].should.have.key("ARN").equals(wacl_arn) assert resp["WebACL"]["ARN"] == wacl_arn
@mock_wafv2 @mock_wafv2

View File

@ -6,4 +6,4 @@ from moto import mock_wafv2
def test_list_rule_groups(): def test_list_rule_groups():
client = boto3.client("wafv2", region_name="us-east-2") client = boto3.client("wafv2", region_name="us-east-2")
resp = client.list_rule_groups(Scope="CLOUDFRONT") resp = client.list_rule_groups(Scope="CLOUDFRONT")
resp.should.have.key("RuleGroups").equals([]) assert resp["RuleGroups"] == []

View File

@ -1,5 +1,5 @@
import boto3 import boto3
import sure # noqa # pylint: disable=unused-import
from moto import mock_wafv2 from moto import mock_wafv2
from .test_helper_functions import CREATE_WEB_ACL_BODY from .test_helper_functions import CREATE_WEB_ACL_BODY
@ -12,7 +12,7 @@ def test_list_tags_for_resource__none_supplied():
] ]
tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"] tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"]
tag_info.should.have.key("TagList").equals([]) assert tag_info["TagList"] == []
@mock_wafv2 @mock_wafv2
@ -31,7 +31,7 @@ def test_list_tags_for_resource():
)["Summary"]["ARN"] )["Summary"]["ARN"]
tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"] tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"]
tag_info.should.have.key("TagList").equals([{"Key": "k1", "Value": "v1"}]) assert tag_info["TagList"] == [{"Key": "k1", "Value": "v1"}]
@mock_wafv2 @mock_wafv2
@ -57,9 +57,10 @@ def test_tag_resource():
) )
tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"] tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"]
tag_info.should.have.key("TagList").equals( assert tag_info["TagList"] == [
[{"Key": "k1", "Value": "v1"}, {"Key": "k2", "Value": "v2"}] {"Key": "k1", "Value": "v1"},
) {"Key": "k2", "Value": "v2"},
]
@mock_wafv2 @mock_wafv2
@ -80,4 +81,4 @@ def test_untag_resource():
conn.untag_resource(ResourceARN=arn, TagKeys=["k1"]) conn.untag_resource(ResourceARN=arn, TagKeys=["k1"])
tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"] tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"]
tag_info.should.have.key("TagList").equals([{"Key": "k2", "Value": "v2"}]) assert tag_info["TagList"] == [{"Key": "k2", "Value": "v2"}]