2019-05-12 22:16:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-18 17:52:59 -04:00
|
|
|
describe AdPlugin::HouseAdSetting do
|
|
|
|
let(:defaults) { AdPlugin::HouseAdSetting::DEFAULTS }
|
|
|
|
|
2023-04-07 10:56:22 -04: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
|
|
|
|
|
2023-04-07 10:56:22 -04: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
|
2023-04-07 10:56:22 -04:00
|
|
|
|
|
|
|
describe ".publish_settings" do
|
|
|
|
let!(:anon_ad) do
|
|
|
|
AdPlugin::HouseAd.create(
|
|
|
|
name: "anon-ad",
|
|
|
|
html: "<whatever-anon>",
|
|
|
|
visible_to_anons: true,
|
|
|
|
visible_to_logged_in_users: false,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:logged_in_ad) do
|
|
|
|
AdPlugin::HouseAd.create(
|
|
|
|
name: "logged-in-ad",
|
|
|
|
html: "<whatever-logged-in>",
|
|
|
|
visible_to_anons: false,
|
|
|
|
visible_to_logged_in_users: true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before { AdPlugin::HouseAdSetting.update("topic_list_top", "logged-in-ad|anon-ad") }
|
|
|
|
|
|
|
|
it "publishes different payloads to different channels for anons and logged in users" do
|
|
|
|
messages = MessageBus.track_publish { AdPlugin::HouseAdSetting.publish_settings }
|
|
|
|
expect(messages.size).to eq(2)
|
|
|
|
|
|
|
|
anon_message = messages.find { |m| m.channel == "/site/house-creatives/anonymous" }
|
|
|
|
logged_in_message = messages.find { |m| m.channel == "/site/house-creatives/logged-in" }
|
|
|
|
|
|
|
|
expect(anon_message.data[:creatives]).to eq("anon-ad" => "<whatever-anon>")
|
|
|
|
expect(anon_message.group_ids).to eq(nil)
|
|
|
|
expect(anon_message.user_ids).to eq(nil)
|
|
|
|
|
|
|
|
expect(logged_in_message.data[:creatives]).to eq("logged-in-ad" => "<whatever-logged-in>")
|
|
|
|
expect(logged_in_message.group_ids).to eq([Group::AUTO_GROUPS[:trust_level_0]])
|
|
|
|
expect(logged_in_message.user_ids).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|