mirror of
https://github.com/discourse/discourse-adplugin.git
synced 2025-02-20 02:29:31 +00:00
This commit adds 2 new settings to house ads to control whether an ad is shown to anonymous users and logged in users. Existing ads that were created before this feature will default to true for both settings; i.e., they will remain to be visible to both anonymous and logged-in users, but it will be possible to change the settings. Turning off both settings will effectively disable the ad completely.
109 lines
3.7 KiB
Ruby
109 lines
3.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "rails_helper"
|
|
|
|
describe AdPlugin::HouseAdSetting do
|
|
let(:defaults) { AdPlugin::HouseAdSetting::DEFAULTS }
|
|
|
|
describe ".all" do
|
|
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
|
|
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))
|
|
end
|
|
end
|
|
|
|
describe ".update" do
|
|
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
|
|
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")
|
|
end
|
|
|
|
it "can update an existing override" do
|
|
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")
|
|
end
|
|
|
|
it "removes ad names that don't exist" do
|
|
AdPlugin::HouseAdSetting.update(:topic_list_top, "Coupon|Banner|Donate")
|
|
expect(AdPlugin::HouseAdSetting.all[:topic_list_top]).to eq("Banner|Donate")
|
|
end
|
|
|
|
it "can reset to default" do
|
|
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("")
|
|
end
|
|
|
|
it "raises error on invalid setting name" do
|
|
expect { AdPlugin::HouseAdSetting.update(:nope, "Click Me") }.to raise_error(
|
|
Discourse::NotFound,
|
|
)
|
|
expect(AdPlugin.pstore_get("ad-setting:nope")).to be_nil
|
|
end
|
|
|
|
it "raises error on invalid value" do
|
|
expect { AdPlugin::HouseAdSetting.update(:topic_list_top, "<script>") }.to raise_error(
|
|
Discourse::InvalidParameters,
|
|
)
|
|
expect(AdPlugin::HouseAdSetting.all[:topic_list_top]).to eq("")
|
|
end
|
|
end
|
|
|
|
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
|
|
end
|