class Fog::AWS::SNS::Mock

Attributes

account_id[W]
region[R]

Public Class Methods

data() click to toggle source
# File lib/fog/aws/sns.rb, line 29
def self.data
  @data ||= Hash.new do |hash, region|
    hash[region] = Hash.new do |region_hash, key|
      region_hash[key] = {
        :topics        => {},
        :subscriptions => {},
        :permissions   => {},
      }
    end
  end
end
new(options={}) click to toggle source
# File lib/fog/aws/sns.rb, line 44
def initialize(options={})
  @region            = options[:region] || 'us-east-1'
  @aws_access_key_id = options[:aws_access_key_id]
  @account_id        = Fog::AWS::Mock.owner_id
  @module            = "sns"

  Fog::AWS.validate_region!(@region)
end

Public Instance Methods

add_permission(options = {}) click to toggle source
# File lib/fog/aws/requests/sns/add_permission.rb, line 16
def add_permission(options = {})
  topic_arn = options.delete('TopicArn')
  label     = options.delete('Label')
  actions   = Hash[options.select { |k,v| k.match(/^ActionName/) }].values
  members   = Hash[options.select { |k,v| k.match(/^AWSAccountId/) }].values

  self.data[:permissions][topic_arn][label] = {
    :members => members,
    :actions => actions,
  }

  response = Excon::Response.new
  response.body = {"RequestId" => Fog::AWS::Mock.request_id}
  response
end
create_topic(name) click to toggle source
# File lib/fog/aws/requests/sns/create_topic.rb, line 26
def create_topic(name)
  response = Excon::Response.new

  topic_arn = Fog::AWS::Mock.arn(@module, @account_id, name, @region)

  self.data[:topics][topic_arn] = {
    "Owner"                   => @account_id,
    "SubscriptionsPending"    => 0,
    "SubscriptionsConfirmed"  => 0,
    "SubscriptionsDeleted"    => 0,
    "DisplayName"             => name,
    "TopicArn"                => topic_arn,
    "EffectiveDeliveryPolicy" => %Q|{"http":{"defaultHealthyRetryPolicy":{"minDelayTarget":20,"maxDelayTarget":20,"numRetries":3,"numMaxDelayRetries":0,"numNoDelayRetries":0,"numMinDelayRetries":0,"backoffFunction":"linear"},"disableSubscriptionOverrides":false}}|,
    "Policy"                  => %Q|{"Version":"2008-10-17","Id":"__default_policy_ID","Statement":[{"Sid":"__default_statement_ID","Effect":"Allow","Principal":{"AWS":"*"},"Action":["SNS:Publish","SNS:RemovePermission","SNS:SetTopicAttributes","SNS:DeleteTopic","SNS:ListSubscriptionsByTopic","SNS:GetTopicAttributes","SNS:Receive","SNS:AddPermission","SNS:Subscribe"],"Resource":"arn:aws:sns:us-east-1:990279267269:Smithy","Condition":{"StringEquals":{"AWS:SourceOwner":"990279267269"}}}]}|
  }
  self.data[:permissions][topic_arn] = {}
  response.body = {"TopicArn" => topic_arn, "RequestId" => Fog::AWS::Mock.request_id}
  response
end
data() click to toggle source
# File lib/fog/aws/sns.rb, line 53
def data
  self.class.data[@region][@aws_access_key_id]
end
delete_topic(arn) click to toggle source
# File lib/fog/aws/requests/sns/delete_topic.rb, line 26
def delete_topic(arn)
  self.data[:topics].delete(arn)

  response = Excon::Response.new
  response.body = {"RequestId" => Fog::AWS::Mock.request_id}
  response
end
get_topic_attributes(arn) click to toggle source
# File lib/fog/aws/requests/sns/get_topic_attributes.rb, line 26
def get_topic_attributes(arn)
  response   = Excon::Response.new
  attributes = self.data[:topics][arn]

  response.body = {"Attributes" => attributes, "RequestId" => Fog::AWS::Mock.request_id}
  response
end
list_subscriptions(options={}) click to toggle source
# File lib/fog/aws/requests/sns/list_subscriptions.rb, line 26
def list_subscriptions(options={})
  response = Excon::Response.new

  response.body = {'Subscriptions' => self.data[:subscriptions].values, 'RequestId' => Fog::AWS::Mock.request_id}
  response
end
list_subscriptions_by_topic(arn, options={}) click to toggle source
# File lib/fog/aws/requests/sns/list_subscriptions_by_topic.rb, line 28
def list_subscriptions_by_topic(arn, options={})
  response = Excon::Response.new

  subscriptions = self.data[:subscriptions].values.select { |s| s["TopicArn"] == arn }

  response.body = {'Subscriptions' => subscriptions, 'RequestId' => Fog::AWS::Mock.request_id}
  response
end
list_topics(options={}) click to toggle source
# File lib/fog/aws/requests/sns/list_topics.rb, line 26
def list_topics(options={})
  response = Excon::Response.new

  response.body = {'Topics' => self.data[:topics].keys, 'RequestId' => Fog::AWS::Mock.request_id}
  response
end
remove_permission(options = {}) click to toggle source
# File lib/fog/aws/requests/sns/remove_permission.rb, line 16
def remove_permission(options = {})
  topic_arn = options['TopicArn']
  label     = options['Label']

  self.data[:permissions][topic_arn].delete(label)

  response = Excon::Response.new
  response.body = {"RequestId" => Fog::AWS::Mock.request_id}
  response
end
reset_data() click to toggle source
# File lib/fog/aws/sns.rb, line 57
def reset_data
  self.class.data[@region].delete(@aws_access_key_id)
end
set_topic_attributes(arn, attribute_name, attribute_value) click to toggle source
# File lib/fog/aws/requests/sns/set_topic_attributes.rb, line 30
def set_topic_attributes(arn, attribute_name, attribute_value)
  attributes = self.data[:topics][arn]

  if %w(Policy DisplayName DeliveryPolicy).include?(attribute_name)
    attributes[attribute_name] = attribute_value
    self.data[:topics][arn] = attributes
  end

  response = Excon::Response.new
  response.body = {"RequestId" => Fog::AWS::Mock.request_id}
  response
end
subscribe(arn, endpoint, protocol) click to toggle source
# File lib/fog/aws/requests/sns/subscribe.rb, line 30
def subscribe(arn, endpoint, protocol)
  response = Excon::Response.new

  unless topic = self.data[:topics][arn]
    response.status = 400
    response.body = {
      'Code'    => 'InvalidParameterValue',
      'Message' => 'Invalid parameter: TopicArn',
      'Type'    => 'Sender',
    }

    return response
  end

  subscription_arn = Fog::AWS::Mock.arn(@module, @account_id, "#{topic["DisplayName"]}:#{Fog::AWS::Mock.request_id}", @region)

  self.data[:subscriptions][subscription_arn] = {
    "Protocol"        => protocol,
    "Owner"           => @account_id.to_s,
    "TopicArn"        => arn,
    "SubscriptionArn" => subscription_arn,
    "Endpoint"        => endpoint,
  }

  mock_data = Fog::AWS::SQS::Mock.data.values.find { |a| a.values.find { |d| d[:queues][endpoint] } }
  access_key = mock_data && mock_data.keys.first

  if protocol == "sqs" && access_key
    token     = SecureRandom.hex(128)
    message   = "You have chosen to subscribe to the topic #{arn}.\nTo confirm the subscription, visit the SubscribeURL included in this message."
    signature = Fog::HMAC.new("sha256", token).sign(message)

    Fog::AWS::SQS.new(
      :region                => self.region,
      :aws_access_key_id     => access_key,
      :aws_secret_access_key => SecureRandom.hex(3)
    ).send_message(endpoint, Fog::JSON.encode(
        "Type"             => "SubscriptionConfirmation",
        "MessageId"        => UUID.uuid,
        "Token"            => token,
        "TopicArn"         => arn,
        "Message"          => message,
        "SubscribeURL"     => "https://sns.#{self.region}.amazonaws.com/?Action=ConfirmSubscription&TopicArn=#{arn}&Token=#{token}",
        "Timestamp"        => Time.now.iso8601,
        "SignatureVersion" => "1",
        "Signature"        => signature,
        "SigningCertURL"   => "https://sns.#{self.region}.amazonaws.com/SimpleNotificationService-#{SecureRandom.hex(16)}.pem"
      ))
  end

  response.body = { 'SubscriptionArn' => 'pending confirmation', 'RequestId' => Fog::AWS::Mock.request_id }
  response
end