DEV: Refactored and moved Edit Directory Column tests out of directory_columns_controller_spec (#22022)

This commit is contained in:
Juan David Martínez Cubillos 2023-06-08 18:00:01 -05:00 committed by GitHub
parent 17e81018a2
commit e08f713668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 100 deletions

View File

@ -16,70 +16,4 @@ RSpec.describe DirectoryColumnsController do
)
end
end
describe "#edit-index" do
fab!(:public_user_field) { Fabricate(:user_field, show_on_profile: true) }
fab!(:private_user_field) do
Fabricate(:user_field, show_on_profile: false, show_on_user_card: false)
end
it "creates directory column records for public user fields" do
sign_in(admin)
expect { get "/edit-directory-columns.json" }.to change { DirectoryColumn.count }.by(1)
end
it "returns a 403 when not logged in as staff member" do
sign_in(user)
get "/edit-directory-columns.json"
expect(response.status).to eq(404)
end
end
describe "#update" do
let(:first_directory_column_id) { DirectoryColumn.first.id }
let(:second_directory_column_id) { DirectoryColumn.second.id }
let(:params) do
{
directory_columns: {
"0": {
id: first_directory_column_id,
enabled: false,
position: 1,
},
"1": {
id: second_directory_column_id,
enabled: true,
position: 1,
},
},
}
end
it "updates exising directory columns" do
sign_in(admin)
expect { put "/edit-directory-columns.json", params: params }.to change {
DirectoryColumn.find(first_directory_column_id).enabled
}.from(true).to(false)
end
it "does not let all columns be disabled" do
sign_in(admin)
bad_params = params
bad_params[:directory_columns][:"1"][:enabled] = false
put "/edit-directory-columns.json", params: bad_params
expect(response.status).to eq(400)
end
it "returns a 404 when not logged in as a staff member" do
sign_in(user)
put "/edit-directory-columns.json", params: params
expect(response.status).to eq(404)
end
end
end

View File

@ -4,40 +4,58 @@ require "rspec"
RSpec.describe EditDirectoryColumnsController do
fab!(:admin) { Fabricate(:admin) }
fab!(:normal_user) { Fabricate(:user) }
let!(:payload) do
{
directory_columns: {
"0": {
id: "1",
enabled: "true",
position: "2",
},
"1": {
id: "2",
enabled: "true",
position: "1",
},
},
format: "json",
}
end
fab!(:user) { Fabricate(:user) }
describe "#update" do
describe "when user is an admin or moderator" do
before { sign_in(admin) }
describe "user saves a new configuration" do
it "logs the new information using StaffActionLogger" do
put edit_directory_columns_path(params: payload)
staff_log = UserHistory.last
let(:first_directory_column_id) { DirectoryColumn.first.id }
let(:second_directory_column_id) { DirectoryColumn.second.id }
let!(:payload) do
{
directory_columns: {
"0": {
id: first_directory_column_id,
enabled: "false",
position: "2",
},
"1": {
id: second_directory_column_id,
enabled: "true",
position: "1",
},
},
format: "json",
}
end
expect(staff_log.custom_type).to eq("update_directory_columns")
describe "#update" do
describe "when user is an admin or moderator" do
before { sign_in(admin) }
describe "user saves a new configuration" do
it "logs the new information using StaffActionLogger" do
expect { put edit_directory_columns_path(params: payload) }.to change {
DirectoryColumn.find(first_directory_column_id).enabled
}.from(true).to(false)
staff_log = UserHistory.last
expect(staff_log.custom_type).to eq("update_directory_columns")
end
it "does not let all columns be disabled" do
sign_in(admin)
bad_params = payload
bad_params[:directory_columns][:"1"][:enabled] = "false"
put edit_directory_columns_path(params: bad_params)
expect(response.status).to eq(400)
end
end
end
end
describe "when user is not an admin or moderator" do
before { sign_in(normal_user) }
before { sign_in(user) }
describe "user saves a new configuration" do
it "does not allow saving" do
put edit_directory_columns_path(params: payload)
@ -49,15 +67,22 @@ RSpec.describe EditDirectoryColumnsController do
end
describe "#index" do
describe "when user is not an admin or moderator" do
before { sign_in(normal_user) }
describe "user checks current configuration" do
it "does not allow the configuration to load" do
get edit_directory_columns_path << ".json"
fab!(:public_user_field) { Fabricate(:user_field, show_on_profile: true) }
fab!(:private_user_field) do
Fabricate(:user_field, show_on_profile: false, show_on_user_card: false)
end
expect(response.status).to eq(403)
end
end
it "creates directory column records for public user fields" do
sign_in(admin)
expect { get "/edit-directory-columns.json" }.to change { DirectoryColumn.count }.by(1)
end
it "returns a 403 when not logged in as staff member" do
sign_in(user)
get "/edit-directory-columns.json"
expect(response.status).to eq(403)
end
end
end