discourse-subscriptions/spec/requests/admin/coupons_controller_spec.rb

88 lines
2.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "rails_helper"
2024-01-16 11:51:44 -05:00
RSpec.describe DiscourseSubscriptions::Admin::CouponsController do
2024-03-06 08:51:25 -05:00
before { SiteSetting.discourse_subscriptions_enabled = true }
2024-01-16 11:51:44 -05:00
it "is a subclass of AdminController" do
expect(DiscourseSubscriptions::Admin::CouponsController < ::Admin::AdminController).to eq(true)
end
2024-01-16 11:51:44 -05:00
context "when unauthenticated" do
it "does nothing" do
::Stripe::PromotionCode.expects(:list).never
get "/s/admin/coupons.json"
expect(response.status).to eq(404)
end
2024-01-16 11:51:44 -05:00
end
2024-01-16 11:51:44 -05:00
context "when authenticated" do
let(:admin) { Fabricate(:admin) }
2024-01-16 11:51:44 -05:00
before { sign_in(admin) }
2024-01-16 11:51:44 -05:00
describe "#index" do
it "returns a list of promo codes" do
::Stripe::PromotionCode
.expects(:list)
.with({ limit: 100 })
.returns({ data: [{ id: "promo_123", coupon: { valid: true } }] })
2024-01-16 11:51:44 -05:00
get "/s/admin/coupons.json"
expect(response.status).to eq(200)
expect(response.parsed_body[0]["id"]).to eq("promo_123")
end
2024-01-16 11:51:44 -05:00
it "only returns valid promo codes" do
::Stripe::PromotionCode
.expects(:list)
.with({ limit: 100 })
.returns({ data: [{ id: "promo_123", coupon: { valid: false } }] })
2024-01-16 11:51:44 -05:00
get "/s/admin/coupons.json"
expect(response.status).to eq(200)
expect(response.parsed_body).to be_blank
end
2024-01-16 11:51:44 -05:00
end
2024-01-16 11:51:44 -05:00
describe "#create" do
it "creates a coupon with an amount off" do
::Stripe::Coupon.expects(:create).returns(id: "coup_123")
::Stripe::PromotionCode.expects(:create).returns(
{ code: "p123", coupon: { amount_off: 2000 } },
)
2024-01-16 11:51:44 -05:00
post "/s/admin/coupons.json",
params: {
promo: "p123",
discount_type: "amount",
discount: "2000",
active: true,
}
expect(response.status).to eq(200)
expect(response.parsed_body["code"]).to eq("p123")
expect(response.parsed_body["coupon"]["amount_off"]).to eq(2000)
end
2024-01-16 11:51:44 -05:00
it "creates a coupon with a percent off" do
::Stripe::Coupon.expects(:create).returns(id: "coup_123")
::Stripe::PromotionCode.expects(:create).returns(
{ code: "p123", coupon: { percent_off: 20 } },
)
2024-01-16 11:51:44 -05:00
post "/s/admin/coupons.json",
params: {
promo: "p123",
discount_type: "percent",
discount: "20",
active: true,
}
expect(response.status).to eq(200)
expect(response.parsed_body["code"]).to eq("p123")
expect(response.parsed_body["coupon"]["percent_off"]).to eq(20)
end
end
end
end