0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-23 18:00:29 +01:00
discourse-custom-wizard/spec/requests/custom_wizard/custom_field_extensions_spec.rb

99 Zeilen
3,2 KiB
Ruby

# frozen_string_literal: true
describe "custom field extensions" do
let!(:topic) { Fabricate(:topic) }
let!(:post) { Fabricate(:post) }
let!(:category) { Fabricate(:category) }
let!(:user) { Fabricate(:user) }
let!(:group) { Fabricate(:group, users: [user]) }
2021-09-07 14:06:13 +02:00
let(:custom_field_json) { get_wizard_fixture("custom_field/custom_fields") }
2021-09-24 11:58:42 +02:00
let(:subscription_custom_field_json) { get_wizard_fixture("custom_field/subscription_custom_fields") }
2021-03-11 07:30:15 +01:00
before do
custom_field_json['custom_fields'].each do |field_json|
custom_field = CustomWizard::CustomField.new(nil, field_json)
custom_field.save
end
end
2021-03-11 07:30:15 +01:00
it "adds topic custom fields to the show topic response" do
topic.custom_fields["topic_field_1"] = true
topic.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
get "/t/#{topic.slug}/#{topic.id}.json"
2021-03-11 07:30:15 +01:00
expect(response.status).to eq(200)
expect(response.parsed_body["topic_field_1"]).to eq(true)
end
2021-03-11 07:30:15 +01:00
it "adds post custom fields to the show post response" do
post.custom_fields["post_field_1"] = 7
post.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
get "/posts/#{post.id}.json"
2021-03-11 07:30:15 +01:00
expect(response.status).to eq(200)
expect(response.parsed_body['post_field_1']).to eq(7)
end
2021-03-11 07:30:15 +01:00
2021-09-24 11:58:42 +02:00
context "with a subscription" do
2021-09-07 14:06:13 +02:00
before do
enable_subscription("business")
2021-09-24 11:58:42 +02:00
subscription_custom_field_json['custom_fields'].each do |field_json|
2021-09-07 14:06:13 +02:00
custom_field = CustomWizard::CustomField.new(nil, field_json)
custom_field.save
end
end
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
it "adds category custom fields to the show categories response" do
category.custom_fields["category_field_1"] = { a: 1, b: 2 }
category.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
get "/c/#{category.id}/show.json"
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
expect(response.status).to eq(200)
expect(response.parsed_body["category"]["category_field_1"]).to eq({ a: 1, b: 2 }.as_json)
end
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
it "adds group custom fields to the show group response" do
group.custom_fields["group_field_1"] = "Group cf entry"
group.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
sign_in(user)
get "/groups/#{group.name}.json"
expect(response.status).to eq(200)
2021-09-07 14:06:13 +02:00
expect(response.parsed_body['group']['group_field_1']).to eq("Group cf entry")
end
context "preloaded" do
it "preloads category custom fields on site categories" do
Site.preloaded_category_custom_fields << "other_field"
category.custom_fields["category_field_1"] = { a: 1, b: 2 }
category.save_custom_fields(true)
get "/site.json"
expect(response.status).to eq(200)
site_category = response.parsed_body['categories'].select { |c| c['id'] == category.id }.first
expect(site_category["category_field_1"]).to eq({ a: 1, b: 2 }.as_json)
end
it "preloads group custom fields on group index" do
Group.preloaded_custom_field_names << "other_field"
group = Fabricate(:group)
group.custom_fields["group_field_1"] = "Group cf entry"
group.save_custom_fields(true)
get "/groups.json"
expect(response.status).to eq(200)
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
group = response.parsed_body['groups'].select { |g| g['id'] == group.id }.first
expect(group['group_field_1']).to eq("Group cf entry")
end
end
end
end