2021-01-13 12:47:22 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 07:35:06 -05:00
|
|
|
require "rails_helper"
|
2021-01-13 12:47:22 -05:00
|
|
|
|
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
|
2021-01-13 12:47:22 -05:00
|
|
|
|
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)
|
2021-01-13 12:47:22 -05:00
|
|
|
end
|
2024-01-16 11:51:44 -05:00
|
|
|
end
|
2021-01-13 12:47:22 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
context "when authenticated" do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
2021-01-13 12:47:22 -05:00
|
|
|
|
2024-01-16 11:51:44 -05:00
|
|
|
before { sign_in(admin) }
|
2021-01-13 12:47:22 -05:00
|
|
|
|
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 } }] })
|
2021-01-13 12:47:22 -05:00
|
|
|
|
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
|
2021-01-13 12:47:22 -05:00
|
|
|
|
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 } }] })
|
2021-01-13 12:47:22 -05:00
|
|
|
|
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
|
2021-01-13 12:47:22 -05:00
|
|
|
end
|
2024-01-16 11:51:44 -05:00
|
|
|
end
|
2021-01-13 12:47:22 -05:00
|
|
|
|
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 } },
|
|
|
|
)
|
2021-01-13 12:47:22 -05:00
|
|
|
|
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
|
2021-01-13 12:47:22 -05:00
|
|
|
|
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 } },
|
|
|
|
)
|
2021-01-13 12:47:22 -05:00
|
|
|
|
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)
|
2021-01-13 12:47:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|