Browse Source

CLDC-1281: Enable support users to create organisations (#636)

* Allow support users to create organisations

* Lint

* Add registration number field

* Lint

* Check params are set correctly
pull/644/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
922b8d13c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      app/controllers/organisations_controller.rb
  2. 4
      app/views/organisations/index.html.erb
  3. 65
      app/views/organisations/new.html.erb
  4. 134
      spec/requests/organisations_controller_spec.rb

28
app/controllers/organisations_controller.rb

@ -4,8 +4,8 @@ class OrganisationsController < ApplicationController
include Modules::SearchFilter include Modules::SearchFilter
before_action :authenticate_user! before_action :authenticate_user!
before_action :find_resource, except: [:index] before_action :find_resource, except: %i[index new create]
before_action :authenticate_scope! before_action :authenticate_scope!, except: [:index]
def index def index
redirect_to organisation_path(current_user.organisation) unless current_user.support? redirect_to organisation_path(current_user.organisation) unless current_user.support?
@ -27,7 +27,7 @@ class OrganisationsController < ApplicationController
if current_user.support? if current_user.support?
render "users", layout: "application" render "users", layout: "application"
else else
render "/users/index" render "users/index"
end end
end end
@ -35,6 +35,20 @@ class OrganisationsController < ApplicationController
render "show" render "show"
end end
def new
@resource = Organisation.new
render "new", layout: "application"
end
def create
organisation = Organisation.create(org_params)
if organisation.persisted?
redirect_to organisations_path
else
render :new, status: :unprocessable_entity
end
end
def edit def edit
if current_user.data_coordinator? || current_user.support? if current_user.data_coordinator? || current_user.support?
render "edit", layout: "application" render "edit", layout: "application"
@ -74,7 +88,7 @@ class OrganisationsController < ApplicationController
private private
def org_params def org_params
params.require(:organisation).permit(:name, :address_line1, :address_line2, :postcode, :phone) params.require(:organisation).permit(:name, :address_line1, :address_line2, :postcode, :phone, :holds_own_stock, :provider_type, :housing_registration_no)
end end
def search_term def search_term
@ -82,7 +96,11 @@ private
end end
def authenticate_scope! def authenticate_scope!
render_not_found if current_user.organisation != @organisation && !current_user.support? if %w[create new].include? action_name
head :unauthorized and return unless current_user.support?
elsif current_user.organisation != @organisation && !current_user.support?
render_not_found
end
end end
def find_resource def find_resource

4
app/views/organisations/index.html.erb

@ -7,6 +7,10 @@
<% content_for :title, title %> <% content_for :title, title %>
<% if current_user.support? %>
<%= govuk_button_link_to "Create a new organisation", new_organisation_path, html: { method: :get } %>
<% end %>
<%= render SearchComponent.new(current_user:, search_label: "Search by organisation name", value: @searched) %> <%= render SearchComponent.new(current_user:, search_label: "Search by organisation name", value: @searched) %>
<hr class="govuk-section-break govuk-section-break--visible govuk-section-break--m"> <hr class="govuk-section-break govuk-section-break--visible govuk-section-break--m">

65
app/views/organisations/new.html.erb

@ -0,0 +1,65 @@
<% content_for :title, "Create a new organisation" %>
<% content_for :before_content do %>
<%= govuk_back_link(
text: "Back",
href: :back,
) %>
<% end %>
<%= form_for(@resource, as: :organisation, html: { method: :post }) do |f| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= f.govuk_error_summary %>
<h1 class="govuk-heading-l">
<%= content_for(:title) %>
</h1>
<%= f.govuk_text_field :name,
label: { size: "m" },
autocomplete: "name" %>
<%= f.govuk_text_field :address_line1,
label: { text: "Address line 1", size: "m" },
autocomplete: "address-line1" %>
<%= f.govuk_text_field :address_line2,
label: { text: "Address line 2", size: "m" },
autocomplete: "address-line2" %>
<%= f.govuk_text_field :postcode,
label: { size: "m" },
autocomplete: "postal-code",
width: 10 %>
<%= f.govuk_phone_field :phone,
label: { text: "Telephone number", size: "m" },
autocomplete: "tel",
width: 20 %>
<%= f.govuk_text_field :housing_registration_no,
label: { text: "Regulator of Social Housing registration number", size: "m" },
width: 10 %>
<% null_option = [OpenStruct.new(id: "", name: "Select an option")] %>
<% types = Organisation::PROVIDER_TYPE.map { |key, _val| OpenStruct.new(id: key, name: Organisation::DISPLAY_PROVIDER_TYPE[key]) } %>
<% type_answer_options = null_option + types %>
<%= f.govuk_collection_select :provider_type,
type_answer_options,
:id,
:name,
label: { text: "Organisation type", size: "m" },
options: { disabled: [""], selected: @resource.provider_type || "" } %>
<%= f.govuk_collection_radio_buttons :holds_own_stock,
[OpenStruct.new(id: true, name: "Yes"), OpenStruct.new(id: false, name: "No")],
:id,
:name,
legend: { text: "Does the organisation hold it's own stock?", size: "m" } %>
<%= f.govuk_submit "Create organisation" %>
</div>
</div>
<% end %>

134
spec/requests/organisations_controller_spec.rb

@ -251,6 +251,52 @@ RSpec.describe OrganisationsController, type: :request do
expect(response).to redirect_to("/logs") expect(response).to redirect_to("/logs")
end end
end end
describe "#index" do
before do
get "/organisations", headers:, params:
end
it "redirects to the user's organisation" do
expect(response).to redirect_to("/organisations/#{user.organisation.id}")
end
end
describe "#new" do
let(:request) { get "/organisations/new", headers:, params: }
it "returns 401 unauthorized" do
request
expect(response).to have_http_status(:unauthorized)
end
end
describe "#create" do
let(:params) do
{
"organisation": {
name: "new organisation",
address_line1: "12 Random Street",
address_line2: "Manchester",
postcode: "MD1 5TR",
phone: "011101101",
provider_type: "LA",
holds_own_stock: "true",
housing_registration_no: "7917937",
},
}
end
let(:request) { post "/organisations", headers:, params: }
it "returns 401 unauthorized" do
request
expect(response).to have_http_status(:unauthorized)
end
it "does not create an organisation" do
expect { request }.not_to change(Organisation, :count)
end
end
end end
context "with a data provider user" do context "with a data provider user" do
@ -361,7 +407,34 @@ RSpec.describe OrganisationsController, type: :request do
before do before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false) allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user sign_in user
get "/organisations" end
describe "#new" do
let(:request) { get "/organisations/new", headers:, params: }
it "shows the create organisation form" do
request
expect(page).to have_field("organisation[name]")
expect(page).to have_field("organisation[phone]")
expect(page).to have_field("organisation[provider_type]")
expect(page).to have_field("organisation[address_line1]")
expect(page).to have_field("organisation[address_line2]")
expect(page).to have_field("organisation[postcode]")
expect(page).to have_field("organisation[holds_own_stock]")
end
end
describe "#index" do
before do
get "/organisations", headers:, params: {}
end
it "shows the organisation list" do
expect(page).to have_content("Organisations")
end
it "has a create new organisation button" do
expect(page).to have_link("Create a new organisation", href: "/organisations/new")
end end
it "shows all organisations" do it "shows all organisations" do
@ -375,7 +448,7 @@ RSpec.describe OrganisationsController, type: :request do
expect(page).to have_field("search", type: "search") expect(page).to have_field("search", type: "search")
end end
context "when viewing a specific organisation" do context "when viewing a specific organisation's logs" do
let(:number_of_org1_case_logs) { 2 } let(:number_of_org1_case_logs) { 2 }
let(:number_of_org2_case_logs) { 4 } let(:number_of_org2_case_logs) { 4 }
@ -519,7 +592,7 @@ RSpec.describe OrganisationsController, type: :request do
end end
end end
context "when viewing a specific organisation users" do context "when viewing a specific organisation's users" do
let!(:users) { FactoryBot.create_list(:user, 5, organisation:) } let!(:users) { FactoryBot.create_list(:user, 5, organisation:) }
let!(:different_org_users) { FactoryBot.create_list(:user, 5) } let!(:different_org_users) { FactoryBot.create_list(:user, 5) }
@ -651,7 +724,7 @@ RSpec.describe OrganisationsController, type: :request do
end end
end end
context "when viewing a specific organisation details" do context "when viewing a specific organisation's details" do
before do before do
get "/organisations/#{organisation.id}/details", headers:, params: {} get "/organisations/#{organisation.id}/details", headers:, params: {}
end end
@ -669,17 +742,12 @@ RSpec.describe OrganisationsController, type: :request do
expect(page).to have_link("Change", count: 3) expect(page).to have_link("Change", count: 3)
end end
end end
end
context "when there are more than 20 organisations" do context "when there are more than 20 organisations" do
let(:support_user) { FactoryBot.create(:user, :support) }
let(:total_organisations_count) { Organisation.all.count } let(:total_organisations_count) { Organisation.all.count }
before do before do
FactoryBot.create_list(:organisation, 25) FactoryBot.create_list(:organisation, 25)
allow(support_user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in support_user
get "/organisations" get "/organisations"
end end
@ -774,4 +842,52 @@ RSpec.describe OrganisationsController, type: :request do
end end
end end
end end
describe "#create" do
let(:name) { "Unique new org name" }
let(:address_line1) { "12 Random Street" }
let(:address_line2) { "Manchester" }
let(:postcode) { "MD1 5TR" }
let(:phone) { "011101101" }
let(:provider_type) { "LA" }
let(:holds_own_stock) { "true" }
let(:housing_registration_no) { "7917937" }
let(:params) do
{
"organisation": {
name:,
address_line1:,
address_line2:,
postcode:,
phone:,
provider_type:,
holds_own_stock:,
housing_registration_no:,
},
}
end
let(:request) { post "/organisations", headers:, params: }
it "creates a new organisation" do
expect { request }.to change(Organisation, :count).by(1)
end
it "sets the organisation attributes correctly" do
request
organisation = Organisation.find_by(housing_registration_no:)
expect(organisation.name).to eq(name)
expect(organisation.address_line1).to eq(address_line1)
expect(organisation.address_line2).to eq(address_line2)
expect(organisation.postcode).to eq(postcode)
expect(organisation.phone).to eq(phone)
expect(organisation.holds_own_stock).to be true
end
it "redirects to the organisation list" do
request
expect(response).to redirect_to("/organisations")
end
end
end
end
end end

Loading…
Cancel
Save