diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 00e28aac0..e0df2c2e6 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -62,9 +62,7 @@ class UsersController < ApplicationController end def new - if current_user.data_coordinator? || current_user.support? - @organisation_id = params["organisation_id"] - end + @organisation_id = params["organisation_id"] @resource = User.new end diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index 072979710..fa9fc26fc 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -40,7 +40,7 @@ :id, :name, label: { text: "Organisation", size: "m" }, - options: { disabled: [""], selected: @organisation_id ? answer_options : "" } %> + options: { disabled: [""], selected: @organisation_id ? answer_options.first : "" } %> <% end %> <% roles = current_user.assignable_roles.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> diff --git a/spec/features/organisation_spec.rb b/spec/features/organisation_spec.rb index 6e0daea63..99e3657c6 100644 --- a/spec/features/organisation_spec.rb +++ b/spec/features/organisation_spec.rb @@ -199,7 +199,7 @@ RSpec.describe "User Features" do context "when I click on Invite user and there are multiple organisations in the database" do before do FactoryBot.create_list(:organisation, 5) - click_link(text: "Invite user") + click_link(text: "Invite user") end it "has only specific organisation name in the dropdown" do diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 9b951b742..01b0d29b1 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -1593,16 +1593,31 @@ RSpec.describe UsersController, type: :request do describe "#new" do before do sign_in user + FactoryBot.create(:organisation, name: "other org") end - it "can assign support role to the new user" do - get "/users/new" - expect(page).to have_field("user-role-support-field") - end + context "when support user" do + it "can assign support role to the new user" do + get "/users/new" + expect(page).to have_field("user-role-support-field") + end - it "can assign organisation to the new user" do - get "/users/new" - expect(page).to have_field("user-organisation-id-field") + it "can assign organisation to the new user" do + get "/users/new" + expect(page).to have_field("user-organisation-id-field") + end + + it "has all organisation names in the dropdown" do + get "/users/new" + expect(page).to have_select("user-organisation-id-field", options: Organisation.pluck(:name)) + end + + context "when organisation id is present in params in there are multiple organisations in the database" do + it "has only specific organisation name in the dropdown" do + get "/users/new", params: {organisation_id: user.organisation.id} + expect(page).to have_select("user-organisation-id-field", options: [user.organisation.name]) + end + end end end end