2019-05-12 22:16:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 07:29:26 -05:00
|
|
|
require "rails_helper"
|
2019-04-18 17:52:59 -04:00
|
|
|
|
|
|
|
describe AdPlugin::HouseAdSetting do
|
|
|
|
let(:defaults) { AdPlugin::HouseAdSetting::DEFAULTS }
|
|
|
|
|
2022-12-29 07:29:26 -05:00
|
|
|
describe "#all" do
|
2019-04-18 17:52:59 -04:00
|
|
|
subject { AdPlugin::HouseAdSetting.all }
|
|
|
|
|
|
|
|
it "returns defaults when nothing has been set" do
|
|
|
|
expect(subject).to eq(defaults)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns defaults and overrides" do
|
2022-12-29 07:29:26 -05:00
|
|
|
AdPlugin.pstore_set("ad-setting:topic_list_top", "Banner")
|
|
|
|
expect(subject[:topic_list_top]).to eq("Banner")
|
|
|
|
expect(subject.except(:topic_list_top)).to eq(defaults.except(:topic_list_top))
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:29:26 -05:00
|
|
|
describe "#update" do
|
2019-04-18 17:52:59 -04:00
|
|
|
before do
|
|
|
|
AdPlugin::HouseAd.create(name: "Banner", html: "<p>Banner</p>")
|
|
|
|
AdPlugin::HouseAd.create(name: "Donate", html: "<p>Donate</p>")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can set override for the first time" do
|
2022-12-29 07:29:26 -05:00
|
|
|
expect { AdPlugin::HouseAdSetting.update(:topic_list_top, "Banner|Donate") }.to change {
|
|
|
|
PluginStoreRow.count
|
|
|
|
}.by(1)
|
|
|
|
expect(AdPlugin::HouseAdSetting.all[:topic_list_top]).to eq("Banner|Donate")
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can update an existing override" do
|
2022-12-29 07:29:26 -05:00
|
|
|
AdPlugin.pstore_set("ad-setting:topic_list_top", "Banner")
|
|
|
|
expect { AdPlugin::HouseAdSetting.update(:topic_list_top, "Banner|Donate") }.to_not change {
|
|
|
|
PluginStoreRow.count
|
|
|
|
}
|
|
|
|
expect(AdPlugin::HouseAdSetting.all[:topic_list_top]).to eq("Banner|Donate")
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes ad names that don't exist" do
|
2022-12-29 07:29:26 -05:00
|
|
|
AdPlugin::HouseAdSetting.update(:topic_list_top, "Coupon|Banner|Donate")
|
|
|
|
expect(AdPlugin::HouseAdSetting.all[:topic_list_top]).to eq("Banner|Donate")
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can reset to default" do
|
2022-12-29 07:29:26 -05:00
|
|
|
AdPlugin.pstore_set("ad-setting:topic_list_top", "Banner")
|
|
|
|
expect { AdPlugin::HouseAdSetting.update(:topic_list_top, "") }.to change {
|
|
|
|
PluginStoreRow.count
|
|
|
|
}.by(-1)
|
|
|
|
expect(AdPlugin::HouseAdSetting.all[:topic_list_top]).to eq("")
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises error on invalid setting name" do
|
2022-12-29 07:29:26 -05:00
|
|
|
expect { AdPlugin::HouseAdSetting.update(:nope, "Click Me") }.to raise_error(
|
|
|
|
Discourse::NotFound,
|
|
|
|
)
|
|
|
|
expect(AdPlugin.pstore_get("ad-setting:nope")).to be_nil
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises error on invalid value" do
|
2022-12-29 07:29:26 -05:00
|
|
|
expect { AdPlugin::HouseAdSetting.update(:topic_list_top, "<script>") }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
|
|
|
expect(AdPlugin::HouseAdSetting.all[:topic_list_top]).to eq("")
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|