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

93 Zeilen
2,8 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-03-11 07:30:15 +01:00
let(:custom_field_json) {
JSON.parse(File.open(
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/custom_field/custom_fields.json"
).read)
}
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 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
get "/c/#{category.id}/show.json"
2021-03-11 07:30:15 +01: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
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
sign_in(user)
get "/groups/#{group.name}.json"
expect(response.status).to eq(200)
expect(response.parsed_body['group']['group_field_1']).to eq("Group cf entry")
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
context "preloaded" do
it "preloads category custom fields on site categories" do
Site.preloaded_category_custom_fields << "other_field"
2021-03-11 07:30:15 +01:00
category.custom_fields["category_field_1"] = { a: 1, b: 2 }
category.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
get "/site.json"
expect(response.status).to eq(200)
2021-03-11 07:30:15 +01:00
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
2021-03-11 07:30:15 +01:00
it "preloads group custom fields on group index" do
Group.preloaded_custom_field_names << "other_field"
2021-03-11 07:30:15 +01:00
group = Fabricate(:group)
group.custom_fields["group_field_1"] = "Group cf entry"
group.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
get "/groups.json"
expect(response.status).to eq(200)
2021-03-11 07:30:15 +01: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