2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe DeleteCampaignGiftOption do
|
|
|
|
describe '.delete' do
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:profile) { force_create(:profile, user: force_create(:user)) }
|
|
|
|
let(:campaign) { force_create(:campaign, profile: profile) }
|
|
|
|
let(:campaign_gift_option) { force_create(:campaign_gift_option, campaign: campaign) }
|
|
|
|
let(:campaign_gift) { force_create(:campaign_gift, campaign_gift_option: campaign_gift_option) }
|
2018-03-25 17:30:42 +00:00
|
|
|
describe 'param validation' do
|
|
|
|
it 'does basic validation' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect do
|
|
|
|
DeleteCampaignGiftOption.delete(nil,
|
|
|
|
nil)
|
|
|
|
end
|
|
|
|
.to(raise_error do |error|
|
|
|
|
expect(error).to be_a ParamValidation::ValidationError
|
|
|
|
expect_validation_errors(error.data, [
|
|
|
|
{
|
|
|
|
key: :campaign,
|
|
|
|
name: :required
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: :campaign,
|
|
|
|
name: :is_a
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: :campaign_gift_option_id,
|
|
|
|
name: :required
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: :campaign_gift_option_id,
|
|
|
|
name: :is_integer
|
|
|
|
}
|
|
|
|
])
|
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does cgo verification' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { DeleteCampaignGiftOption.delete(campaign, 5555) }.to(raise_error do |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a ParamValidation::ValidationError
|
2019-07-30 21:29:24 +00:00
|
|
|
expect_validation_errors(error.data, [{ key: :campaign_gift_option_id }])
|
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cgo deletion is rejected because a gift has already been sold' do
|
|
|
|
campaign_gift_option
|
|
|
|
campaign_gift
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { DeleteCampaignGiftOption.delete(campaign, campaign_gift_option.id) }.to(raise_error do |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a ParamValidation::ValidationError
|
2019-07-30 21:29:24 +00:00
|
|
|
expect_validation_errors(error.data, [{ key: :campaign_gift_option_id }])
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error.message).to eq("#{campaign_gift_option.id} already has campaign gifts. It can't be deleted for safety reasons.")
|
|
|
|
|
|
|
|
expect(CampaignGiftOption.any?).to eq true
|
2019-07-30 21:29:24 +00:00
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'cgo deletion succeeds' do
|
2019-07-30 21:29:24 +00:00
|
|
|
Timecop.freeze(2020, 10, 12) do
|
2018-03-25 17:30:42 +00:00
|
|
|
campaign_gift_option
|
|
|
|
result = DeleteCampaignGiftOption.delete(campaign, campaign_gift_option.id)
|
|
|
|
expect(result).to be_a CampaignGiftOption
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(result.attributes).to eq campaign_gift_option.attributes
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(CampaignGiftOption.any?).to eq false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|