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 .test_helper_functions import CREATE_WEB_ACL_BODY, LIST_WEB_ACL_BODY
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 = {
"X-Amz-Target": "AWSWAF_20190729.CreateWebACL",
@ -43,9 +41,9 @@ def test_create_web_acl():
)
assert res.status_code == 400
assert (
b"AWS WAF could not perform the operation because some resource in your request is a duplicate of an existing one."
in res.data
)
b"AWS WAF could not perform the operation because some resource "
b"in your request is a duplicate of an existing one."
) in res.data
assert b"WafV2DuplicateItem" in res.data
res = test_client.post(

View File

@ -1,11 +1,10 @@
import pytest
import sure # noqa # pylint: disable=unused-import
import boto3
from botocore.exceptions import ClientError
import pytest
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 .test_helper_functions import CREATE_WEB_ACL_BODY, LIST_WEB_ACL_BODY
@mock_wafv2
@ -22,10 +21,11 @@ def test_create_web_acl():
with pytest.raises(ClientError) as ex:
conn.create_web_acl(**CREATE_WEB_ACL_BODY("John", "REGIONAL"))
err = ex.value.response["Error"]
err["Message"].should.contain(
"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")
assert (
"AWS WAF could not perform the operation because some resource "
"in your request is a duplicate of an existing one."
) in err["Message"]
assert err["Code"] == "WafV2DuplicateItem"
res = conn.create_web_acl(**CREATE_WEB_ACL_BODY("Carl", "CLOUDFRONT"))
web_acl = res["Summary"]
@ -84,16 +84,14 @@ def test_create_web_acl_with_all_arguments():
)["Summary"]["Id"]
wacl = client.get_web_acl(Name="test", Scope="CLOUDFRONT", Id=web_acl_id)["WebACL"]
wacl.should.have.key("Description").equals("test desc")
wacl.should.have.key("DefaultAction").equals({"Allow": {}})
wacl.should.have.key("VisibilityConfig").equals(
{
assert wacl["Description"] == "test desc"
assert wacl["DefaultAction"] == {"Allow": {}}
assert wacl["VisibilityConfig"] == {
"SampledRequestsEnabled": False,
"CloudWatchMetricsEnabled": False,
"MetricName": "idk",
}
)
wacl.should.have.key("Rules").length_of(2)
assert len(wacl["Rules"]) == 2
@mock_wafv2
@ -102,8 +100,8 @@ def test_get_web_acl():
body = CREATE_WEB_ACL_BODY("John", "REGIONAL")
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.should.have.key("Name").equals("John")
wacl.should.have.key("Id").equals(web_acl_id)
assert wacl["Name"] == "John"
assert wacl["Id"] == web_acl_id
@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")
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:
conn.get_web_acl(Name="Daphne", Scope="REGIONAL", Id=wacl_id)
err = exc.value.response["Error"]
err["Code"].should.equal("WAFNonexistentItemException")
err["Message"].should.equal(
assert err["Code"] == "WAFNonexistentItemException"
assert err["Message"] == (
"AWS WAF couldnt perform the operation because your resource doesnt exist."
)
@ -177,15 +175,12 @@ def test_update_web_acl():
"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.should.have.key("Description").equals("updated_desc")
acl.should.have.key("DefaultAction").equals(
{"Block": {"CustomResponse": {"ResponseCode": 412}}}
)
acl.should.have.key("Rules").equals(
[
assert acl["Description"] == "updated_desc"
assert acl["DefaultAction"] == {"Block": {"CustomResponse": {"ResponseCode": 412}}}
assert acl["Rules"] == [
{
"Name": "rule1",
"Priority": 456,
@ -197,11 +192,8 @@ def test_update_web_acl():
},
}
]
)
acl.should.have.key("VisibilityConfig").equals(
{
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
from botocore.exceptions import ClientError
import pytest
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 .test_helper_functions import CREATE_WEB_ACL_BODY
@mock_wafv2
@ -28,8 +27,8 @@ def test_associate_with_unknown_resource():
WebACLArn=f"{wacl_arn}2", ResourceArn="unknownarnwithminlength20"
)
err = exc.value.response["Error"]
err["Code"].should.equal("WAFNonexistentItemException")
err["Message"].should.equal(
assert err["Code"] == "WAFNonexistentItemException"
assert err["Message"] == (
"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)
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)
stage = apigw.get_stage(restApiId=api_id, stageName="test")
stage.shouldnt.have.key("webAclArn")
assert "webAclArn" not in stage
@mock_apigateway
@ -68,14 +67,14 @@ def test_get_web_acl_for_resource():
_, stage_arn = create_apigateway_stage(client=apigw)
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)
resp = conn.get_web_acl_for_resource(ResourceArn=stage_arn)
resp.should.have.key("WebACL")
resp["WebACL"].should.have.key("Name").equals("John")
resp["WebACL"].should.have.key("ARN").equals(wacl_arn)
assert "WebACL" in resp
assert resp["WebACL"]["Name"] == "John"
assert resp["WebACL"]["ARN"] == wacl_arn
@mock_wafv2

View File

@ -6,4 +6,4 @@ from moto import mock_wafv2
def test_list_rule_groups():
client = boto3.client("wafv2", region_name="us-east-2")
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 sure # noqa # pylint: disable=unused-import
from moto import mock_wafv2
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.should.have.key("TagList").equals([])
assert tag_info["TagList"] == []
@mock_wafv2
@ -31,7 +31,7 @@ def test_list_tags_for_resource():
)["Summary"]["ARN"]
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
@ -57,9 +57,10 @@ def test_tag_resource():
)
tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"]
tag_info.should.have.key("TagList").equals(
[{"Key": "k1", "Value": "v1"}, {"Key": "k2", "Value": "v2"}]
)
assert tag_info["TagList"] == [
{"Key": "k1", "Value": "v1"},
{"Key": "k2", "Value": "v2"},
]
@mock_wafv2
@ -80,4 +81,4 @@ def test_untag_resource():
conn.untag_resource(ResourceARN=arn, TagKeys=["k1"])
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"}]