discourse/plugins/discourse-adplugin/spec/serializer/house_ad_serializer_spec.rb
Juan David Martínez Cubillos cc7f4743a5
FEATURE: Implement ad impressions tracking (#35797)
**Description**

This PR implements these features:
 - Impressions tracking for HouseAds and External Ads
 - Click tracking for HouseAds and External Ads
- Reports of HouseAds performance, ad impressions by user, general ad
impressions, conversion reports by ad and placement, conversion reports
by ad type, and conversion reports by placement.

As part of implementing these features, the HouseAd model had to be
pulled out of the Plugin rows table into its own table. Refactoring had
to be done to account for these changes.

---------

Co-authored-by: Bannon Tanner <bannon.n.tanner@gmail.com>
2025-12-01 12:17:25 -05:00

121 lines
3.5 KiB
Ruby

# frozen_string_literal: true
describe AdPlugin::HouseAdSerializer do
fab!(:user)
fab!(:house_ad) do
Fabricate(
:house_ad,
name: "Test Ad",
html: "<div>Test HTML</div>",
visible_to_logged_in_users: true,
visible_to_anons: false,
)
end
fab!(:category)
fab!(:group)
let(:guardian) { Guardian.new(user) }
before { enable_current_plugin }
describe "basic serialization" do
it "serializes basic attributes" do
serializer = AdPlugin::HouseAdSerializer.new(house_ad, root: false)
json = serializer.as_json
expect(json[:id]).to eq(house_ad.id)
expect(json[:name]).to eq("Test Ad")
expect(json[:html]).to eq("<div>Test HTML</div>")
expect(json[:visible_to_logged_in_users]).to eq(true)
expect(json[:visible_to_anons]).to eq(false)
expect(json[:created_at]).to be_present
expect(json[:updated_at]).to be_present
end
it "does not include groups by default" do
house_ad.update!(group_ids: [group.id])
serializer = AdPlugin::HouseAdSerializer.new(house_ad, root: false)
json = serializer.as_json
expect(json).not_to have_key(:groups)
end
it "does not include categories by default" do
house_ad.update!(category_ids: [category.id])
serializer = AdPlugin::HouseAdSerializer.new(house_ad, root: false)
json = serializer.as_json
expect(json).not_to have_key(:categories)
end
end
context "with include_groups option" do
it "includes empty array when no groups" do
serializer =
AdPlugin::HouseAdSerializer.new(
house_ad,
scope: guardian,
root: false,
include_groups: true,
)
json = serializer.as_json
expect(json[:groups]).to eq([])
end
it "includes multiple groups" do
group2 = Fabricate(:group)
house_ad.update!(group_ids: [group.id, group2.id])
serializer =
AdPlugin::HouseAdSerializer.new(
house_ad,
scope: guardian,
root: false,
include_groups: true,
)
json = serializer.as_json
expect(json[:groups].length).to eq(2)
expect(json[:groups].map { |g| g[:id] }).to contain_exactly(group.id, group2.id)
end
end
context "with include_categories option" do
it "includes empty array when no categories" do
serializer = AdPlugin::HouseAdSerializer.new(house_ad, root: false, include_categories: true)
json = serializer.as_json
expect(json[:categories]).to eq([])
end
it "includes multiple categories" do
category2 = Fabricate(:category)
house_ad.update!(category_ids: [category.id, category2.id])
serializer = AdPlugin::HouseAdSerializer.new(house_ad, root: false, include_categories: true)
json = serializer.as_json
expect(json[:categories].length).to eq(2)
expect(json[:categories].map { |c| c[:id] }).to contain_exactly(category.id, category2.id)
end
end
context "with both groups and categories" do
it "includes both when both options are set" do
house_ad.update!(category_ids: [category.id], group_ids: [group.id])
serializer =
AdPlugin::HouseAdSerializer.new(
house_ad,
scope: guardian,
root: false,
include_categories: true,
include_groups: true,
)
json = serializer.as_json
expect(json[:categories]).to be_present
expect(json[:categories].length).to eq(1)
expect(json[:groups]).to be_present
expect(json[:groups].length).to eq(1)
end
end
end