2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Admin::UserFieldsController do
|
2022-11-02 23:42:44 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-06-11 00:50:21 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
describe '#create' do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2014-09-25 11:32:08 -04:00
|
|
|
|
|
|
|
it "creates a user field" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect {
|
2018-06-11 00:50:21 -04:00
|
|
|
post "/admin/customize/user_fields.json", params: {
|
2017-08-31 00:06:56 -04:00
|
|
|
user_field: { name: 'hello', description: 'hello desc', field_type: 'text' }
|
2018-06-11 00:50:21 -04:00
|
|
|
}
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-01-09 12:04:02 -05:00
|
|
|
}.to change(UserField, :count).by(1)
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
2015-07-28 12:29:40 -04:00
|
|
|
|
|
|
|
it "creates a user field with options" do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
2018-06-11 00:50:21 -04:00
|
|
|
post "/admin/customize/user_fields.json", params: {
|
2017-08-31 00:06:56 -04:00
|
|
|
user_field: {
|
|
|
|
name: 'hello',
|
|
|
|
description: 'hello desc',
|
|
|
|
field_type: 'dropdown',
|
|
|
|
options: ['a', 'b', 'c']
|
|
|
|
}
|
2018-06-11 00:50:21 -04:00
|
|
|
}
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-31 00:06:56 -04:00
|
|
|
end.to change(UserField, :count).by(1)
|
2015-07-28 12:29:40 -04:00
|
|
|
|
|
|
|
expect(UserFieldOption.count).to eq(3)
|
|
|
|
end
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "user field creation not allowed" do
|
|
|
|
it "prevents creation with a 404 response" do
|
|
|
|
expect do
|
|
|
|
post "/admin/customize/user_fields.json", params: {
|
|
|
|
user_field: { name: 'hello', description: 'hello desc', field_type: 'text' }
|
|
|
|
}
|
|
|
|
end.not_to change { UserField.count }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "user field creation not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "user field creation not allowed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#index' do
|
|
|
|
fab!(:user_field) { Fabricate(:user_field) }
|
|
|
|
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2014-09-25 11:32:08 -04:00
|
|
|
|
|
|
|
it "returns a list of user fields" do
|
2018-06-11 00:50:21 -04:00
|
|
|
get "/admin/customize/user_fields.json"
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json['user_fields']).to be_present
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "user fields inaccessible" do
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/customize/user_fields.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
expect(response.parsed_body['user_fields']).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "user fields inaccessible"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "user fields inaccessible"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#destroy' do
|
|
|
|
fab!(:user_field) { Fabricate(:user_field) }
|
|
|
|
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2014-09-25 11:32:08 -04:00
|
|
|
|
2014-10-08 14:38:18 -04:00
|
|
|
it "deletes the user field" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect {
|
2018-06-11 00:50:21 -04:00
|
|
|
delete "/admin/customize/user_fields/#{user_field.id}.json"
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-01-09 12:04:02 -05:00
|
|
|
}.to change(UserField, :count).by(-1)
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "user field deletion not allowed" do
|
|
|
|
it "prevents deletion with a 404 response" do
|
|
|
|
expect do
|
|
|
|
delete "/admin/customize/user_fields/#{user_field.id}.json"
|
|
|
|
end.not_to change { UserField.count }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "user field deletion not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "user field deletion not allowed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#update' do
|
|
|
|
fab!(:user_field) { Fabricate(:user_field) }
|
|
|
|
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2014-09-25 11:32:08 -04:00
|
|
|
|
2014-10-08 14:38:18 -04:00
|
|
|
it "updates the user field" do
|
2018-06-11 00:50:21 -04:00
|
|
|
put "/admin/customize/user_fields/#{user_field.id}.json", params: {
|
2017-08-31 00:06:56 -04:00
|
|
|
user_field: { name: 'fraggle', field_type: 'confirm', description: 'muppet' }
|
2018-06-11 00:50:21 -04:00
|
|
|
}
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2014-09-25 11:32:08 -04:00
|
|
|
user_field.reload
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(user_field.name).to eq('fraggle')
|
|
|
|
expect(user_field.field_type).to eq('confirm')
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
2015-07-28 12:29:40 -04:00
|
|
|
|
|
|
|
it "updates the user field options" do
|
2018-06-11 00:50:21 -04:00
|
|
|
put "/admin/customize/user_fields/#{user_field.id}.json", params: {
|
2017-08-31 00:06:56 -04:00
|
|
|
user_field: {
|
|
|
|
name: 'fraggle',
|
|
|
|
field_type: 'dropdown',
|
|
|
|
description: 'muppet',
|
|
|
|
options: ['hello', 'hello', 'world']
|
|
|
|
}
|
2018-06-11 00:50:21 -04:00
|
|
|
}
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-07-28 12:29:40 -04:00
|
|
|
user_field.reload
|
|
|
|
expect(user_field.name).to eq('fraggle')
|
|
|
|
expect(user_field.field_type).to eq('dropdown')
|
|
|
|
expect(user_field.user_field_options.size).to eq(2)
|
|
|
|
end
|
2015-08-17 13:01:15 -04:00
|
|
|
|
|
|
|
it "keeps options when updating the user field" do
|
2018-06-11 00:50:21 -04:00
|
|
|
put "/admin/customize/user_fields/#{user_field.id}.json", params: {
|
2017-08-31 00:06:56 -04:00
|
|
|
user_field: {
|
|
|
|
name: 'fraggle',
|
|
|
|
field_type: 'dropdown',
|
|
|
|
description: 'muppet',
|
|
|
|
options: ['hello', 'hello', 'world'],
|
|
|
|
position: 1
|
|
|
|
}
|
2018-06-11 00:50:21 -04:00
|
|
|
}
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-08-17 13:01:15 -04:00
|
|
|
user_field.reload
|
|
|
|
expect(user_field.user_field_options.size).to eq(2)
|
|
|
|
|
2018-06-11 00:50:21 -04:00
|
|
|
put "/admin/customize/user_fields/#{user_field.id}.json", params: {
|
2017-08-31 00:06:56 -04:00
|
|
|
user_field: {
|
|
|
|
name: 'fraggle',
|
|
|
|
field_type: 'dropdown',
|
|
|
|
description: 'muppet',
|
|
|
|
position: 2
|
|
|
|
}
|
2018-06-11 00:50:21 -04:00
|
|
|
}
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-08-17 13:01:15 -04:00
|
|
|
user_field.reload
|
|
|
|
expect(user_field.user_field_options.size).to eq(2)
|
|
|
|
end
|
2021-06-07 13:34:01 -04:00
|
|
|
|
|
|
|
it "removes directory column record if not public" do
|
|
|
|
next_position = DirectoryColumn.maximum("position") + 1
|
|
|
|
DirectoryColumn.create(
|
|
|
|
user_field_id: user_field.id,
|
|
|
|
enabled: false,
|
2021-06-22 14:00:04 -04:00
|
|
|
type: DirectoryColumn.types[:user_field],
|
2021-06-07 13:34:01 -04:00
|
|
|
position: next_position
|
|
|
|
)
|
|
|
|
expect {
|
|
|
|
put "/admin/customize/user_fields/#{user_field.id}.json", params: {
|
|
|
|
user_field: { show_on_profile: false, show_on_user_card: false, searchable: true }
|
|
|
|
}
|
|
|
|
}.to change { DirectoryColumn.count }.by(-1)
|
|
|
|
end
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
2022-11-02 23:42:44 -04:00
|
|
|
|
|
|
|
shared_examples "user field update not allowed" do
|
|
|
|
it "prevents updates with a 404 response" do
|
|
|
|
user_field.reload
|
|
|
|
original_name = user_field.name
|
|
|
|
|
|
|
|
put "/admin/customize/user_fields/#{user_field.id}.json", params: {
|
|
|
|
user_field: { name: 'fraggle', field_type: 'confirm', description: 'muppet' }
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
|
|
|
|
user_field.reload
|
|
|
|
expect(user_field.name).to eq(original_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "user field update not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "user field update not allowed"
|
|
|
|
end
|
2014-09-25 11:32:08 -04:00
|
|
|
end
|
|
|
|
end
|