WebMock stub data from Aws CognitoIdentityProvider

I have the following module in my Rails controller:

module AwsAuth
  extend ActiveSupport::Concern

  require 'aws-sdk'

  def get_cognito_user(token)
    cognitoidentityprovider = Aws::CognitoIdentityProvider::Client.new(region: ENV['AWS_REGION'])

    begin
      cognito_user = cognitoidentityprovider.get_user({ access_token: token })

      puts cognito_user

      return {"email" => cognito_user.username}

    rescue StandardError => msg
      puts "ERROR!"
      puts msg
      return {"error" => msg}
    end
  end
end

      

At the moment it returns cognito_user:

#<Aws::CognitoIdentityProvider::Types::GetUserResponse:0x7fe51b0013a8
    mfa_options = nil,
    user_attributes = nil,
    username = nil
>

      

How can I get the response in Rspec so that the username and user_attributes are not null?

+3


source to share


1 answer


after playing with yzalavin suggestion ... the following works for me ...



allow_any_instance_of( AwsAuth )
      .to receive(:get_cognito_user)
      .and_return( JSON[ {email: "testy@example.com"}.to_json ] )

      

0


source







All Articles