* Add ProMembership model * Implement ProMembershipsController#create * Implement basic ProMembershipsController#show * Add ProMembership to ledger * Populate user history after pro subscription is created * Add fields for expiration notifications * Add ProMemberships::ExpirationNotifier to notify users of expiring memberships * Add tasks for recurring jobs to notify users of expiration * Add auto_recharge column to ProMembership * Add ProMemberships::Biller (incomplete) * Fix specs * Add ProMembership to Administrate * Fix spec * Add has_enough_credits? to User and Organization * Add Payments::Customer class * Finish ProMembership::Biller functionality * Fix ProMemberships::Creator check for credits * Disable destroy actions for ProMembershipsController * Correctly authenticate ProMembershipsController actions * Make sure only pro user's history can be indexed * Add ProMembershipsController#update action for auto recharge * Use regular AR to save new credits and add touch to the purchaser * Clarify Pro membership create policy * Display information about an existing pro membership * Add UI to show page * Add system test for Pro membership creation * Implement edit membership * Make sure users with pro memberships can access history and dashboard pro * Fix padding issue * Show a different text for a user that has credits but not enough for Pro * Move Pro Membership functionality inside settings * Update Pro Membership link in email notifications * Bust all relevant caches * Add the Pro checkmark around the website * Use Users::ResaveArticlesJob instead of delay * Add/remove user from pro-members chat channel * Use the appropriate Pro checkmark * Remove unfinished pro elements * Remove checkmark JS
97 lines
3.4 KiB
Ruby
97 lines
3.4 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe Payments::Customer do
|
|
before do
|
|
StripeMock.start
|
|
end
|
|
|
|
after do
|
|
StripeMock.stop
|
|
end
|
|
|
|
describe ".get" do
|
|
it "retrieves an existing customer" do
|
|
customer = Stripe::Customer.create
|
|
expect(described_class.get(customer.id)).to be_present
|
|
end
|
|
|
|
it "raises Payments::CustomerNotFoundError if the customer does not exist" do
|
|
expect { described_class.get("foobar") }.to raise_error(Payments::InvalidRequestError)
|
|
end
|
|
|
|
it "raises Payments::PaymentsError for any other known error" do
|
|
allow(Stripe::Customer).to receive(:retrieve).with("foobar").and_raise(Stripe::StripeError)
|
|
expect { described_class.get("foobar") }.to raise_error(Payments::PaymentsError)
|
|
end
|
|
end
|
|
|
|
describe ".create" do
|
|
it "creates a new customer" do
|
|
expect(described_class.create).to be_present
|
|
end
|
|
|
|
it "raises an error if anything in the params is invalid" do
|
|
error = Stripe::InvalidRequestError.new("message", :email)
|
|
allow(Stripe::Customer).to receive(:create).and_raise(error)
|
|
expect { described_class.create(email: "foobar") }.to raise_error(Payments::InvalidRequestError)
|
|
end
|
|
|
|
it "raises Payments::PaymentsError for any other known error" do
|
|
allow(Stripe::Customer).to receive(:create).with(email: "foobar").
|
|
and_raise(Stripe::StripeError)
|
|
expect { described_class.create(email: "foobar") }.to raise_error(Payments::PaymentsError)
|
|
end
|
|
end
|
|
|
|
describe ".create_source" do
|
|
it "creates a new source" do
|
|
customer = Stripe::Customer.create
|
|
expect(described_class.create_source(customer.id, "token")).to be_present
|
|
end
|
|
|
|
it "raises an error if anything in the params is invalid" do
|
|
error = Stripe::InvalidRequestError.new("message", :token)
|
|
allow(Stripe::Customer).to receive(:create_source).and_raise(error)
|
|
expect { described_class.create_source("customer_id", "token") }.to raise_error(Payments::InvalidRequestError)
|
|
end
|
|
|
|
it "raises Payments::PaymentsError for any other known error" do
|
|
allow(Stripe::Customer).to receive(:create_source).and_raise(Stripe::StripeError)
|
|
expect { described_class.create_source("customer_id", "token") }.to raise_error(Payments::PaymentsError)
|
|
end
|
|
end
|
|
|
|
describe ".charge" do
|
|
let(:stripe_helper) { StripeMock.create_test_helper }
|
|
|
|
it "charges a customer" do
|
|
customer = Stripe::Customer.create
|
|
charge = described_class.charge(customer: customer, amount: 1, description: "Test charge")
|
|
expect(charge).to be_present
|
|
end
|
|
|
|
it "charges a customer with an explicit card id" do
|
|
customer = Stripe::Customer.create
|
|
token = stripe_helper.generate_card_token
|
|
card = Stripe::Customer.create_source(customer.id, source: token)
|
|
charge = described_class.charge(
|
|
customer: customer, amount: 1, description: "Test charge", card_id: card.id,
|
|
)
|
|
expect(charge).to be_present
|
|
end
|
|
|
|
it "raises a card error if the card has any troubles" do
|
|
StripeMock.prepare_card_error(:expired_card)
|
|
|
|
customer = Stripe::Customer.create
|
|
token = stripe_helper.generate_card_token
|
|
card = Stripe::Customer.create_source(customer.id, source: token)
|
|
|
|
expect do
|
|
described_class.charge(
|
|
customer: customer, amount: 1, description: "Test charge", card_id: card.id,
|
|
)
|
|
end.to raise_error(Payments::CardError)
|
|
end
|
|
end
|
|
end
|