2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "resources/aws/aws_sns_topic"
|
2017-11-16 18:44:43 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require "resource_support/aws"
|
|
|
|
require "resources/aws/aws_sns_topic"
|
2019-05-21 00:19:38 +00:00
|
|
|
|
2017-11-16 18:44:43 +00:00
|
|
|
# MSNB = MockSnsBackend
|
|
|
|
# Abbreviation not used outside this file
|
|
|
|
|
|
|
|
#=============================================================================#
|
|
|
|
# Constructor Tests
|
|
|
|
#=============================================================================#
|
|
|
|
class AwsSnsTopicConstructorTest < Minitest::Test
|
|
|
|
def setup
|
2017-12-04 18:32:13 +00:00
|
|
|
AwsSnsTopic::BackendFactory.select(AwsMSNB::NoSubscriptions)
|
2017-11-16 18:44:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_constructor_some_args_required
|
|
|
|
assert_raises(ArgumentError) { AwsSnsTopic.new }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_constructor_accepts_scalar_arn
|
2019-06-11 22:24:35 +00:00
|
|
|
AwsSnsTopic.new("arn:aws:sns:us-east-1:123456789012:some-topic")
|
2017-11-16 18:44:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_constructor_accepts_arn_as_hash
|
2019-06-11 22:24:35 +00:00
|
|
|
AwsSnsTopic.new(arn: "arn:aws:sns:us-east-1:123456789012:some-topic")
|
2017-11-16 18:44:43 +00:00
|
|
|
end
|
2019-06-11 22:24:35 +00:00
|
|
|
|
2017-11-16 18:44:43 +00:00
|
|
|
def test_constructor_rejects_unrecognized_resource_params
|
2019-06-11 22:24:35 +00:00
|
|
|
assert_raises(ArgumentError) { AwsSnsTopic.new(beep: "boop") }
|
2017-11-16 18:44:43 +00:00
|
|
|
end
|
2019-06-11 22:24:35 +00:00
|
|
|
|
2017-11-16 18:44:43 +00:00
|
|
|
def test_constructor_rejects_non_arn_formats
|
|
|
|
[
|
2019-06-11 22:24:35 +00:00
|
|
|
"not-even-like-an-arn",
|
|
|
|
"arn:::::", # Empty
|
|
|
|
"arn::::::", # Too many colons
|
|
|
|
"arn:aws::us-east-1:123456789012:some-topic", # Omits SNS service
|
|
|
|
"arn::sns:us-east-1:123456789012:some-topic", # Omits partition
|
|
|
|
"arn:aws:sns:*:123456789012:some-topic", # All-region - not permitted for lookup
|
|
|
|
"arn:aws:sns:us-east-1::some-topic", # Default account - not permitted for lookup
|
2017-11-16 18:44:43 +00:00
|
|
|
].each do |example|
|
|
|
|
assert_raises(ArgumentError) { AwsSnsTopic.new(arn: example) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#=============================================================================#
|
|
|
|
# Search / Recall
|
|
|
|
#=============================================================================#
|
|
|
|
class AwsSnsTopicRecallTest < Minitest::Test
|
|
|
|
# No setup here - each test needs to explicitly declare
|
|
|
|
# what they want from the backend.
|
|
|
|
|
|
|
|
def test_recall_no_match_is_no_exception
|
2017-12-04 18:32:13 +00:00
|
|
|
AwsSnsTopic::BackendFactory.select(AwsMSNB::Miss)
|
2019-06-11 22:24:35 +00:00
|
|
|
topic = AwsSnsTopic.new("arn:aws:sns:us-east-1:123456789012:nope")
|
2017-11-16 18:44:43 +00:00
|
|
|
refute topic.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_recall_match_single_result_works
|
2019-06-11 22:24:35 +00:00
|
|
|
AwsSnsTopic::BackendFactory.select(AwsMSNB::NoSubscriptions)
|
|
|
|
topic = AwsSnsTopic.new("arn:aws:sns:us-east-1:123456789012:does-not-matter")
|
2017-11-16 18:44:43 +00:00
|
|
|
assert topic.exists?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#=============================================================================#
|
|
|
|
# Properties
|
|
|
|
#=============================================================================#
|
|
|
|
|
|
|
|
class AwsSnsTopicPropertiesTest < Minitest::Test
|
|
|
|
# No setup here - each test needs to explicitly declare
|
|
|
|
# what they want from the backend.
|
|
|
|
|
|
|
|
#---------------------------------------
|
|
|
|
# confirmed_subscription_count
|
|
|
|
#---------------------------------------
|
|
|
|
def test_prop_conf_sub_count_zero
|
2017-12-04 18:32:13 +00:00
|
|
|
AwsSnsTopic::BackendFactory.select(AwsMSNB::NoSubscriptions)
|
2019-06-11 22:24:35 +00:00
|
|
|
topic = AwsSnsTopic.new("arn:aws:sns:us-east-1:123456789012:does-not-matter")
|
2017-11-16 18:44:43 +00:00
|
|
|
assert_equal(0, topic.confirmed_subscription_count)
|
|
|
|
end
|
|
|
|
|
2018-02-01 02:54:47 +00:00
|
|
|
def test_prop_conf_sub_count_one
|
2017-12-04 18:32:13 +00:00
|
|
|
AwsSnsTopic::BackendFactory.select(AwsMSNB::OneSubscription)
|
2019-06-11 22:24:35 +00:00
|
|
|
topic = AwsSnsTopic.new("arn:aws:sns:us-east-1:123456789012:does-not-matter")
|
2017-11-16 18:44:43 +00:00
|
|
|
assert_equal(1, topic.confirmed_subscription_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#=============================================================================#
|
|
|
|
# Test Fixtures
|
|
|
|
#=============================================================================#
|
|
|
|
|
|
|
|
module AwsMSNB
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
class Miss < AwsBackendBase
|
2017-11-16 18:44:43 +00:00
|
|
|
def get_topic_attributes(criteria)
|
2019-06-11 22:24:35 +00:00
|
|
|
raise Aws::SNS::Errors::NotFound.new("No SNS topic for #{criteria[:topic_arn]}", "Nope")
|
2017-11-16 18:44:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
class NoSubscriptions < AwsBackendBase
|
2017-11-16 18:44:43 +00:00
|
|
|
def get_topic_attributes(_criteria)
|
|
|
|
OpenStruct.new({
|
|
|
|
attributes: { # Note that this is a plain hash, odd for AWS SDK
|
2019-06-11 22:24:35 +00:00
|
|
|
# Many other attributes available, see
|
2017-11-16 18:44:43 +00:00
|
|
|
# http://docs.aws.amazon.com/sdkforruby/api/Aws/SNS/Types/GetTopicAttributesResponse.html
|
2019-06-11 22:24:35 +00:00
|
|
|
"SubscriptionsConfirmed" => 0,
|
|
|
|
},
|
2017-11-16 18:44:43 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
class OneSubscription < AwsBackendBase
|
2017-11-16 18:44:43 +00:00
|
|
|
def get_topic_attributes(_criteria)
|
|
|
|
OpenStruct.new({
|
|
|
|
attributes: { # Note that this is a plain hash, odd for AWS SDK
|
2019-06-11 22:24:35 +00:00
|
|
|
# Many other attributes available, see
|
2017-11-16 18:44:43 +00:00
|
|
|
# http://docs.aws.amazon.com/sdkforruby/api/Aws/SNS/Types/GetTopicAttributesResponse.html
|
2019-06-11 22:24:35 +00:00
|
|
|
"SubscriptionsConfirmed" => 1,
|
|
|
|
},
|
2017-11-16 18:44:43 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2019-05-21 00:19:38 +00:00
|
|
|
end
|