Add tests for purchasing credits (#2720)

* Use workaround for tests

* Add tests for purchasing credits

* Use the correct current_user

* Wrap parentheses around second condition
This commit is contained in:
Andy Zhao 2019-05-06 15:27:30 -04:00 committed by Ben Halpern
parent 35fe810b51
commit d1e4831c4f
2 changed files with 97 additions and 3 deletions

View file

@ -66,9 +66,10 @@ class CreditsController < ApplicationController
def create_charge
@amount = generate_cost
source = Rails.env.test? ? @card.id : (@card || @customer.default_source)
Stripe::Charge.create(
customer: @customer.id,
source: @card || @customer.default_source,
source: source,
amount: @amount,
description: "Purchase of #{@number_to_purchase} credits.",
currency: "usd",

View file

@ -40,14 +40,107 @@ RSpec.describe "Credits", type: :request do
StripeMock.stop
end
xit "creates unspent credits" do
it "creates unspent credits" do
post "/credits", params: {
credit: {
number_to_purchase: 20
},
stripe_token: stripe_helper.generate_card_token
}
expect(user.credits.size).to eq(20)
expect(user.credits.where(spent: false).size).to eq(20)
end
it "makes a valid Stripe charge" do
post "/credits", params: {
credit: {
number_to_purchase: 20
},
stripe_token: stripe_helper.generate_card_token
}
customer = Stripe::Customer.retrieve(user.stripe_id_code)
expect(customer.charges.first.amount).to eq 8000
end
context "when a user already has a card" do
before do
customer = Stripe::Customer.create(email: user.email)
user.update_column(:stripe_id_code, customer.id)
customer.sources.create(source: stripe_helper.generate_card_token)
end
it "makes a valid Stripe charge" do
customer = Stripe::Customer.retrieve(user.stripe_id_code)
post "/credits", params: {
credit: {
number_to_purchase: 20
},
selected_card: customer.sources.first.id
}
expect(customer.charges.first.amount).to eq 8000
end
it "creates unspent credits" do
customer = Stripe::Customer.retrieve(user.stripe_id_code)
post "/credits", params: {
credit: {
number_to_purchase: 20
},
selected_card: customer.sources.first.id
}
expect(user.credits.where(spent: false).size).to eq(20)
end
it "charges a new card if given one" do
post "/credits", params: {
credit: {
number_to_purchase: 20
},
stripe_token: stripe_helper.generate_card_token
}
customer = Stripe::Customer.retrieve(user.stripe_id_code)
card_id = customer.sources.data.last.id
expect(customer.charges.first.source.id).to eq card_id
end
end
context "when purchasing as an organization" do
let(:org_admin) { create(:user, :org_admin) }
before { sign_in org_admin }
it "creates unspent credits for the organization" do
post "/credits", params: {
user_type: "organization",
credit: {
number_to_purchase: 20
},
stripe_token: stripe_helper.generate_card_token
}
expect(Credit.where(organization_id: org_admin.organization_id, spent: false).size).to eq 20
end
it "makes a valid Stripe charge" do
post "/credits", params: {
user_type: "organization",
credit: {
number_to_purchase: 20
},
stripe_token: stripe_helper.generate_card_token
}
customer = Stripe::Customer.retrieve(org_admin.stripe_id_code)
expect(customer.charges.first.amount).to eq 8000
end
it "does not create unspent credits for the current_user" do
post "/credits", params: {
user_type: "organization",
credit: {
number_to_purchase: 20
},
stripe_token: stripe_helper.generate_card_token
}
expect(org_admin.credits.where(spent: false).size).to eq 0
end
end
end
end