2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2014-09-25 23:32:08 +08:00
|
|
|
|
|
|
|
describe Admin::UserFieldsController do
|
|
|
|
it "is a subclass of AdminController" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(Admin::UserFieldsController < Admin::AdminController).to eq(true)
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2018-06-11 12:50:21 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
2014-09-25 23:32:08 +08:00
|
|
|
|
2018-06-11 12:50:21 +08:00
|
|
|
describe '#create' do
|
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' }
|
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: ['a', 'b', 'c']
|
|
|
|
}
|
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
|
|
|
|
|
2018-06-11 12:50:21 +08:00
|
|
|
describe '#index' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user_field) { Fabricate(:user_field) }
|
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
|
|
|
|
|
2018-06-11 12:50:21 +08:00
|
|
|
describe '#destroy' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user_field) { Fabricate(:user_field) }
|
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
|
|
|
|
|
2018-06-11 12:50:21 +08:00
|
|
|
describe '#update' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user_field) { Fabricate(:user_field) }
|
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' }
|
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)
|
2014-09-25 23:32:08 +08:00
|
|
|
user_field.reload
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user_field.name).to eq('fraggle')
|
|
|
|
expect(user_field.field_type).to eq('confirm')
|
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: ['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: ['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
|
|
|
|
end
|
|
|
|
end
|