discourse-adplugin/spec/requests/house_ad_controller_spec.rb
Osama Sayegh c2057a5056
FEATURE: Add per-ad visibility settings for anons and logged-in users (#168)
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.
2023-04-07 17:56:22 +03:00

68 lines
1.9 KiB
Ruby

# frozen_string_literal: true
require "rails_helper"
describe AdPlugin::HouseAdsController do
let(:admin) { Fabricate(:admin) }
let!(:ad) do
AdPlugin::HouseAd.create(
name: "Banner",
html: "<p>Banner</p>",
visible_to_anons: true,
visible_to_logged_in_users: false,
)
end
describe "#update" do
context "when used by admins" do
before { sign_in(admin) }
it "updates an existing ad" do
put "/admin/plugins/pluginad/house_creatives/#{ad.id}.json",
params: {
name: ad.name,
html: ad.html,
visible_to_anons: "false",
visible_to_logged_in_users: "true",
}
expect(response.status).to eq(200)
expect(response.parsed_body["house_ad"].symbolize_keys).to eq(
id: ad.id,
name: ad.name,
html: ad.html,
visible_to_anons: false,
visible_to_logged_in_users: true,
)
ad_copy = AdPlugin::HouseAd.find(ad.id)
expect(ad_copy.name).to eq(ad.name)
expect(ad_copy.html).to eq(ad.html)
expect(ad_copy.visible_to_anons).to eq(false)
expect(ad_copy.visible_to_logged_in_users).to eq(true)
end
end
context "when used by non-admins" do
before { sign_in(Fabricate(:user)) }
it "can't update ads" do
put "/admin/plugins/pluginad/house_creatives/#{ad.id}.json",
params: {
name: "non sense goes here",
html: "blah <h4cked>",
visible_to_anons: "false",
visible_to_logged_in_users: "true",
}
expect(response.status).to eq(404)
ad_copy = AdPlugin::HouseAd.find(ad.id)
expect(ad_copy.name).to eq(ad.name)
expect(ad_copy.html).to eq(ad.html)
expect(ad_copy.visible_to_anons).to eq(true)
expect(ad_copy.visible_to_logged_in_users).to eq(false)
end
end
end
end