discourse-subscriptions/spec/requests/user/payments_controller_spec.rb

65 lines
2.0 KiB
Ruby
Raw Permalink Normal View History

2019-12-17 16:31:58 +11:00
# frozen_string_literal: true
require "rails_helper"
2019-12-17 16:31:58 +11:00
2024-01-16 17:51:44 +01:00
RSpec.describe DiscourseSubscriptions::User::PaymentsController do
2024-03-06 14:51:25 +01:00
before { SiteSetting.discourse_subscriptions_enabled = true }
2024-01-16 17:51:44 +01:00
it "is a subclass of ApplicationController" do
expect(DiscourseSubscriptions::User::PaymentsController < ::ApplicationController).to eq(true)
end
2019-12-17 16:31:58 +11:00
2024-01-16 17:51:44 +01:00
context "when not authenticated" do
it "does not get the payment intents" do
::Stripe::PaymentIntent.expects(:list).never
get "/subscriptions/user/payments.json"
2024-01-16 17:51:44 +01:00
expect(response.status).to eq(403)
2019-12-17 16:31:58 +11:00
end
2024-01-16 17:51:44 +01:00
end
2019-12-17 16:31:58 +11:00
2024-01-16 17:51:44 +01:00
context "when authenticated" do
let(:user) { Fabricate(:user, email: "zasch@example.com") }
2024-01-16 17:51:44 +01:00
before do
sign_in(user)
Fabricate(:customer, customer_id: "c_345678", user_id: user.id)
Fabricate(:product, external_id: "prod_8675309")
Fabricate(:product, external_id: "prod_8675310")
end
2024-01-16 17:51:44 +01:00
it "gets payment intents" do
created_time = Time.now
::Stripe::Invoice
.expects(:list)
.with(customer: "c_345678")
.returns(
data: [
{ id: "inv_900007", lines: { data: [plan: { product: "prod_8675309" }] } },
{ id: "inv_900008", lines: { data: [plan: { product: "prod_8675310" }] } },
{ id: "inv_900008", lines: { data: [plan: { product: "prod_8675310" }] } },
],
)
::Stripe::PaymentIntent
.expects(:list)
.with(customer: "c_345678")
.returns(
data: [
{ id: "pi_900008", invoice: "inv_900008", created: created_time },
{ id: "pi_900008", invoice: "inv_900008", created: created_time },
{ id: "pi_900007", invoice: "inv_900007", created: Time.now },
{ id: "pi_007", invoice: "inv_007", created: Time.now },
],
)
get "/subscriptions/user/payments.json"
2024-01-16 17:51:44 +01:00
parsed_body = response.parsed_body
invoice = parsed_body[0]["invoice"]
expect(invoice).to eq("inv_900007")
expect(parsed_body.count).to eq(2)
2019-12-17 16:31:58 +11:00
end
end
2024-04-24 10:09:53 -06:00
end