设置Stripe API密钥以测试RSpec的env

时间:2015-02-16 23:37:16

标签: ruby-on-rails security curl rspec stripe-payments

如何为RSpec的测试环境设置Stripe API密钥?

我正在尝试测试用户是否可以在以下Custom Checkout操作中绕过条带费用,有效地发布到资源并写入数据库(例如使用cURL或其他工具)。

  def create

    customer = Stripe::Customer.create(
        :email        => params[:stripeEmail],
        :card         => params[:stripeToken],
    )
    charge = Stripe::Charge.create(
        :customer     => customer.id,
        :amount       => 1000,
    )

    if charge["paid"] == true
      @customer.create(email: params[:stripeEmail],
                       first_name: params[:first_name],
                       last_name:  params[:last_name],
                       agreed_to_terms: params[:agreed_to_terms],
                       )
      UserMailer.welcome_email(@customer).deliver_now
      redirect_to root_url
    else
      flash[:error] = "Something went wrong! Please try again."
      redirect_to error_url
    end

  rescue Stripe::CardError => e
    flash[:error] = e.message
    redirect_to :back
  end

但规范要求我提供API密钥。

  describe 'POST #create' do

    context "with valid credentials" do
      it "but without Stripe, redirects to root_url" do
        @product = create(:valid_credentials)
        post :create, {first_name: "John", last_name: "Doe", stripeEmail: "john@doe.com", agreed_to_terms: true}
        expect(response).to redirect_to root_url
      end
    end

如下所示

No API key provided. Set your API key using "Stripe.api_key = <API-KEY>". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email support@stripe.com if you have any questions.

3 个答案:

答案 0 :(得分:1)

不,除非你构建了这个功能。

答案 1 :(得分:1)

StripeMock库正在模拟before块中的成功。使用

StripeMock.prepare_card_error(:missing)
前面的块中的

答案 2 :(得分:0)

在你的config / secrets.yml中添加条纹标记

test:
    secret_key_base: <your_secret_key>
    stripe_publishable_key: <your_stripe_publishable_key>
    stripe_secret_key: <your_stripe_secret_key>
    stripe_client_id: <your_stripe_client_id>

在config / initializers / stripe.rb中设置你的stripe_key

Stripe.api_key = Rails.application.secrets.stripe_secret_key
Stripe.api_version = '2015-04-07'

您可以在https://dashboard.stripe.com/account/apikeys中获取Stripe API密钥 请记住保持密钥安全,不要从版本控制存储库(github,bitbucket等)共享和排除您的secrets.yml文件