discourse-adplugin/spec/models/house_ad_setting_spec.rb
Isaac Janzen 554f03f3da
FEATURE: Add group and category restrictions to house ads (#205)
# Description

This PR adds the ability to apply **group** and **category** restrictions to a **house ad**.

# What is included
- In order to get the group and category selectors to work within `admin/assets/javascripts/discourse/controllers/admin-plugins-house-ads-show.js` I needed to modernize the file. 
- I dropped the `bufferedProperty` implementation in favor of a vanilla ember approach
- I added `category_ids` and `group_ids` to our house ads model
- I added tests for group / category restrictions
- I added a preview button to display the house ad
- `/site.json` would return a object called `house_creatives` and a list of key value pairs that matched the ad name with the html, like so:
```js
{ AD_KEY: ad.html }
```
I need access to the category ids on the client to conditionally render the house ads so the new format will be: 
```js
{ AD_KEY: { html: ad.html, category_ids: ad.category_ids } }
```

# Screenshots
<img width="658" alt="Screenshot 2024-04-08 at 2 39 22 PM" src="https://github.com/discourse/discourse-adplugin/assets/50783505/b44b386d-65a1-4a2a-a487-d735b13357dd">

# Preview Video

https://github.com/discourse/discourse-adplugin/assets/50783505/6d0d8253-afef-4e15-b6fc-c6f696efd169
2024-04-09 11:54:11 -06:00

121 lines
3.9 KiB
Ruby

# frozen_string_literal: true
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,
group_ids: [],
category_ids: [],
)
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,
group_ids: [],
category_ids: [],
)
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" => {
html: "<whatever-anon>",
category_ids: [],
},
)
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" => {
html: "<whatever-logged-in>",
category_ids: [],
},
)
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