2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Admin::UserFieldsController do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:moderator)
|
|
|
|
fab!(:user)
|
2018-06-11 12:50:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
describe "#create" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2014-09-25 23:32:08 +08:00
|
|
|
|
|
|
|
it "creates a user field" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect {
|
2018-06-11 12:50:21 +08:00
|
|
|
post "/admin/customize/user_fields.json",
|
|
|
|
params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
user_field: {
|
|
|
|
name: "hello",
|
|
|
|
description: "hello desc",
|
|
|
|
field_type: "text",
|
2024-05-23 19:18:25 +08:00
|
|
|
requirement: "on_signup",
|
2017-08-31 12:06:56 +08:00
|
|
|
},
|
2018-06-11 12:50:21 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-01-10 01:04:02 +08:00
|
|
|
}.to change(UserField, :count).by(1)
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|
2015-07-29 00:29:40 +08:00
|
|
|
|
|
|
|
it "creates a user field with options" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
2018-06-11 12:50:21 +08:00
|
|
|
post "/admin/customize/user_fields.json",
|
|
|
|
params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
user_field: {
|
|
|
|
name: "hello",
|
|
|
|
description: "hello desc",
|
|
|
|
field_type: "dropdown",
|
|
|
|
options: %w[a b c],
|
2024-05-23 19:18:25 +08:00
|
|
|
requirement: "on_signup",
|
2017-08-31 12:06:56 +08:00
|
|
|
},
|
2018-06-11 12:50:21 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-31 12:06:56 +08:00
|
|
|
end.to change(UserField, :count).by(1)
|
2015-07-29 00:29:40 +08:00
|
|
|
|
|
|
|
expect(UserFieldOption.count).to eq(3)
|
|
|
|
end
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08: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
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user_field)
|
2022-11-03 11:42:44 +08:00
|
|
|
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2014-09-25 23:32:08 +08:00
|
|
|
|
|
|
|
it "returns a list of user fields" do
|
2018-06-11 12:50:21 +08:00
|
|
|
get "/admin/customize/user_fields.json"
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
json = response.parsed_body
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["user_fields"]).to be_present
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08: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
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user_field)
|
2022-11-03 11:42:44 +08:00
|
|
|
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2014-09-25 23:32:08 +08:00
|
|
|
|
2014-10-09 02:38:18 +08:00
|
|
|
it "deletes the user field" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect {
|
2018-06-11 12:50:21 +08:00
|
|
|
delete "/admin/customize/user_fields/#{user_field.id}.json"
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-01-10 01:04:02 +08:00
|
|
|
}.to change(UserField, :count).by(-1)
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08: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
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user_field)
|
2022-11-03 11:42:44 +08:00
|
|
|
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2014-09-25 23:32:08 +08:00
|
|
|
|
2014-10-09 02:38:18 +08:00
|
|
|
it "updates the user field" do
|
2018-06-11 12:50:21 +08:00
|
|
|
put "/admin/customize/user_fields/#{user_field.id}.json",
|
|
|
|
params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
user_field: {
|
|
|
|
name: "fraggle",
|
|
|
|
field_type: "confirm",
|
|
|
|
description: "muppet",
|
2024-05-23 19:18:25 +08:00
|
|
|
requirement: "optional",
|
2017-08-31 12:06:56 +08:00
|
|
|
},
|
2018-06-11 12:50:21 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2024-05-23 19:18:25 +08:00
|
|
|
expect(user_field.reload).to have_attributes(
|
|
|
|
name: "fraggle",
|
|
|
|
field_type: "confirm",
|
|
|
|
required?: false,
|
|
|
|
)
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|
2015-07-29 00:29:40 +08:00
|
|
|
|
|
|
|
it "updates the user field options" do
|
2018-06-11 12:50:21 +08:00
|
|
|
put "/admin/customize/user_fields/#{user_field.id}.json",
|
|
|
|
params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
user_field: {
|
|
|
|
name: "fraggle",
|
|
|
|
field_type: "dropdown",
|
|
|
|
description: "muppet",
|
|
|
|
options: %w[hello hello world],
|
|
|
|
},
|
2018-06-11 12:50:21 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-07-29 00:29:40 +08: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-18 01:01:15 +08:00
|
|
|
|
|
|
|
it "keeps options when updating the user field" do
|
2018-06-11 12:50:21 +08:00
|
|
|
put "/admin/customize/user_fields/#{user_field.id}.json",
|
|
|
|
params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
user_field: {
|
|
|
|
name: "fraggle",
|
|
|
|
field_type: "dropdown",
|
|
|
|
description: "muppet",
|
|
|
|
options: %w[hello hello world],
|
|
|
|
position: 1,
|
|
|
|
},
|
2018-06-11 12:50:21 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-08-18 01:01:15 +08:00
|
|
|
user_field.reload
|
|
|
|
expect(user_field.user_field_options.size).to eq(2)
|
|
|
|
|
2018-06-11 12:50:21 +08:00
|
|
|
put "/admin/customize/user_fields/#{user_field.id}.json",
|
|
|
|
params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
user_field: {
|
|
|
|
name: "fraggle",
|
|
|
|
field_type: "dropdown",
|
|
|
|
description: "muppet",
|
|
|
|
position: 2,
|
|
|
|
},
|
2018-06-11 12:50:21 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-08-18 01:01:15 +08:00
|
|
|
user_field.reload
|
|
|
|
expect(user_field.user_field_options.size).to eq(2)
|
|
|
|
end
|
2021-06-08 01:34:01 +08: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-23 02:00:04 +08:00
|
|
|
type: DirectoryColumn.types[:user_field],
|
2021-06-08 01:34:01 +08: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 23:32:08 +08:00
|
|
|
end
|
2022-11-03 11:42:44 +08: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 23:32:08 +08:00
|
|
|
end
|
|
|
|
end
|