|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | + |
| 5 | +RSpec.describe Api::V2::SubscriptionsController do |
| 6 | + let(:organization) { create(:organization) } |
| 7 | + let(:customer) { create(:customer, organization:) } |
| 8 | + let(:plan) { create(:plan, organization:, pricing_type: "product_catalog") } |
| 9 | + let(:subscription) { create(:subscription, customer:, plan:, organization:) } |
| 10 | + |
| 11 | + describe "GET /api/v2/subscriptions" do |
| 12 | + subject { get_with_token(organization, "/api/v2/subscriptions") } |
| 13 | + |
| 14 | + before { create(:subscription_product_item, organization:, subscription:) } |
| 15 | + |
| 16 | + include_examples "requires API permission", "subscription", "read" |
| 17 | + |
| 18 | + it "returns subscriptions in the v2 shape" do |
| 19 | + subject |
| 20 | + |
| 21 | + expect(response).to have_http_status(:success) |
| 22 | + |
| 23 | + result = json[:subscriptions].sole |
| 24 | + expect(result[:lago_id]).to eq(subscription.id) |
| 25 | + expect(result[:plan_code]).to eq(plan.code) |
| 26 | + expect(result[:subscription_product_items_count]).to eq(1) |
| 27 | + expect(result).not_to have_key(:current_billing_period_started_at) |
| 28 | + expect(result).not_to have_key(:plan_amount_cents) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + describe "GET /api/v2/subscriptions/:external_id" do |
| 33 | + subject { get_with_token(organization, "/api/v2/subscriptions/#{subscription.external_id}") } |
| 34 | + |
| 35 | + let!(:subscription_product_item) { create(:subscription_product_item, organization:, subscription:) } |
| 36 | + |
| 37 | + include_examples "requires API permission", "subscription", "read" |
| 38 | + |
| 39 | + it "returns the subscription with its rate card entries" do |
| 40 | + subject |
| 41 | + |
| 42 | + expect(response).to have_http_status(:success) |
| 43 | + expect(json[:subscription][:lago_id]).to eq(subscription.id) |
| 44 | + expect(json[:subscription][:subscription_product_items].sole[:lago_id]).to eq(subscription_product_item.id) |
| 45 | + end |
| 46 | + |
| 47 | + context "when it does not exist" do |
| 48 | + subject { get_with_token(organization, "/api/v2/subscriptions/unknown") } |
| 49 | + |
| 50 | + it "returns a not found error" do |
| 51 | + subject |
| 52 | + |
| 53 | + expect(response).to be_not_found_error("subscription") |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | +end |
0 commit comments