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. 654
      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 %>

654
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,224 +407,231 @@ 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 end
it "shows all organisations" do describe "#new" do
total_number_of_orgs = Organisation.all.count let(:request) { get "/organisations/new", headers:, params: }
expect(page).to have_link organisation.name, href: "organisations/#{organisation.id}/logs"
expect(page).to have_link unauthorised_organisation.name, href: "organisations/#{unauthorised_organisation.id}/logs"
expect(page).to have_content("#{total_number_of_orgs} total organisations")
end
it "shows a search bar" do it "shows the create organisation form" do
expect(page).to have_field("search", type: "search") 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 end
context "when viewing a specific organisation" do describe "#index" do
let(:number_of_org1_case_logs) { 2 }
let(:number_of_org2_case_logs) { 4 }
before do before do
FactoryBot.create_list(:case_log, number_of_org1_case_logs, owning_organisation_id: organisation.id, managing_organisation_id: organisation.id) get "/organisations", headers:, params: {}
FactoryBot.create_list(:case_log, number_of_org2_case_logs, owning_organisation_id: unauthorised_organisation.id, managing_organisation_id: unauthorised_organisation.id)
get "/organisations/#{organisation.id}/logs", headers:, params: {}
end end
it "only shows logs for that organisation" do it "shows the organisation list" do
expect(page).to have_content("#{number_of_org1_case_logs} total logs") expect(page).to have_content("Organisations")
organisation.case_logs.map(&:id).each do |case_log_id|
expect(page).to have_link case_log_id.to_s, href: "/logs/#{case_log_id}"
end
unauthorised_organisation.case_logs.map(&:id).each do |case_log_id|
expect(page).not_to have_link case_log_id.to_s, href: "/logs/#{case_log_id}"
end
end end
it "has filters" do it "has a create new organisation button" do
expect(page).to have_content("Filters") expect(page).to have_link("Create a new organisation", href: "/organisations/new")
expect(page).to have_content("Collection year")
end end
it "does not have specific organisation filter" do it "shows all organisations" do
expect(page).not_to have_content("Specific organisation") total_number_of_orgs = Organisation.all.count
expect(page).to have_link organisation.name, href: "organisations/#{organisation.id}/logs"
expect(page).to have_link unauthorised_organisation.name, href: "organisations/#{unauthorised_organisation.id}/logs"
expect(page).to have_content("#{total_number_of_orgs} total organisations")
end end
it "has a sub-navigation with correct tabs" do it "shows a search bar" do
expect(page).to have_css(".app-sub-navigation") expect(page).to have_field("search", type: "search")
expect(page).to have_content("About this organisation")
end end
context "when using a search query" do context "when viewing a specific organisation's logs" do
let(:logs) { FactoryBot.create_list(:case_log, 3, :completed, owning_organisation: user.organisation) } let(:number_of_org1_case_logs) { 2 }
let(:log_to_search) { FactoryBot.create(:case_log, :completed, owning_organisation: user.organisation) } let(:number_of_org2_case_logs) { 4 }
let(:log_total_count) { CaseLog.where(owning_organisation: user.organisation).count }
it "has search results in the title" do before do
get "/organisations/#{organisation.id}/logs?search=#{log_to_search.id}", headers: headers, params: {} FactoryBot.create_list(:case_log, number_of_org1_case_logs, owning_organisation_id: organisation.id, managing_organisation_id: organisation.id)
expect(page).to have_title("Your organisation (1 log matching ‘#{log_to_search.id}’ of #{log_total_count} total logs) - Submit social housing lettings and sales data (CORE) - GOV.UK") FactoryBot.create_list(:case_log, number_of_org2_case_logs, owning_organisation_id: unauthorised_organisation.id, managing_organisation_id: unauthorised_organisation.id)
get "/organisations/#{organisation.id}/logs", headers:, params: {}
end end
it "shows case logs matching the id" do it "only shows logs for that organisation" do
get "/organisations/#{organisation.id}/logs?search=#{log_to_search.id}", headers: headers, params: {} expect(page).to have_content("#{number_of_org1_case_logs} total logs")
expect(page).to have_link(log_to_search.id.to_s) organisation.case_logs.map(&:id).each do |case_log_id|
logs.each do |log| expect(page).to have_link case_log_id.to_s, href: "/logs/#{case_log_id}"
expect(page).not_to have_link(log.id.to_s)
end end
end
it "shows case logs matching the tenant code" do unauthorised_organisation.case_logs.map(&:id).each do |case_log_id|
get "/organisations/#{organisation.id}/logs?search=#{log_to_search.tenant_code}", headers: headers, params: {} expect(page).not_to have_link case_log_id.to_s, href: "/logs/#{case_log_id}"
expect(page).to have_link(log_to_search.id.to_s)
logs.each do |log|
expect(page).not_to have_link(log.id.to_s)
end end
end end
it "shows case logs matching the property reference" do it "has filters" do
get "/organisations/#{organisation.id}/logs?search=#{log_to_search.propcode}", headers: headers, params: {} expect(page).to have_content("Filters")
expect(page).to have_link(log_to_search.id.to_s) expect(page).to have_content("Collection year")
logs.each do |log|
expect(page).not_to have_link(log.id.to_s)
end
end end
it "shows case logs matching the property postcode" do it "does not have specific organisation filter" do
get "/organisations/#{organisation.id}/logs?search=#{log_to_search.postcode_full}", headers: headers, params: {} expect(page).not_to have_content("Specific organisation")
expect(page).to have_link(log_to_search.id.to_s)
logs.each do |log|
expect(page).not_to have_link(log.id.to_s)
end
end end
context "when more than one results with matching postcode" do it "has a sub-navigation with correct tabs" do
let!(:matching_postcode_log) { FactoryBot.create(:case_log, :completed, owning_organisation: user.organisation, postcode_full: log_to_search.postcode_full) } expect(page).to have_css(".app-sub-navigation")
expect(page).to have_content("About this organisation")
end
it "displays all matching logs" do context "when using a search query" do
get "/organisations/#{organisation.id}/logs?search=#{log_to_search.postcode_full}", headers: headers, params: {} let(:logs) { FactoryBot.create_list(:case_log, 3, :completed, owning_organisation: user.organisation) }
let(:log_to_search) { FactoryBot.create(:case_log, :completed, owning_organisation: user.organisation) }
let(:log_total_count) { CaseLog.where(owning_organisation: user.organisation).count }
it "has search results in the title" do
get "/organisations/#{organisation.id}/logs?search=#{log_to_search.id}", headers: headers, params: {}
expect(page).to have_title("Your organisation (1 log matching ‘#{log_to_search.id}’ of #{log_total_count} total logs) - Submit social housing lettings and sales data (CORE) - GOV.UK")
end
it "shows case logs matching the id" do
get "/organisations/#{organisation.id}/logs?search=#{log_to_search.id}", headers: headers, params: {}
expect(page).to have_link(log_to_search.id.to_s) expect(page).to have_link(log_to_search.id.to_s)
expect(page).to have_link(matching_postcode_log.id.to_s)
logs.each do |log| logs.each do |log|
expect(page).not_to have_link(log.id.to_s) expect(page).not_to have_link(log.id.to_s)
end end
end end
end
context "when there are more than 1 page of search results" do
let(:postcode) { "XX11YY" }
let(:logs) { FactoryBot.create_list(:case_log, 30, :completed, owning_organisation: user.organisation, postcode_full: postcode) }
let(:log_total_count) { CaseLog.where(owning_organisation: user.organisation).count }
it "has title with pagination details for page 1" do
get "/organisations/#{organisation.id}/logs?search=#{logs[0].postcode_full}", headers: headers, params: {}
expect(page).to have_title("Your organisation (#{logs.count} logs matching ‘#{postcode}’ of #{log_total_count} total logs) (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK")
end
it "has title with pagination details for page 2" do it "shows case logs matching the tenant code" do
get "/organisations/#{organisation.id}/logs?search=#{logs[0].postcode_full}&page=2", headers: headers, params: {} get "/organisations/#{organisation.id}/logs?search=#{log_to_search.tenant_code}", headers: headers, params: {}
expect(page).to have_title("Your organisation (#{logs.count} logs matching ‘#{postcode}’ of #{log_total_count} total logs) (page 2 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") expect(page).to have_link(log_to_search.id.to_s)
logs.each do |log|
expect(page).not_to have_link(log.id.to_s)
end
end end
end
context "when search query doesn't match any logs" do it "shows case logs matching the property reference" do
it "doesn't display any logs" do get "/organisations/#{organisation.id}/logs?search=#{log_to_search.propcode}", headers: headers, params: {}
get "/organisations/#{organisation.id}/logs?search=foobar", headers:, params: {} expect(page).to have_link(log_to_search.id.to_s)
logs.each do |log| logs.each do |log|
expect(page).not_to have_link(log.id.to_s) expect(page).not_to have_link(log.id.to_s)
end end
expect(page).not_to have_link(log_to_search.id.to_s)
end end
end
context "when search query is empty" do it "shows case logs matching the property postcode" do
it "doesn't display any logs" do get "/organisations/#{organisation.id}/logs?search=#{log_to_search.postcode_full}", headers: headers, params: {}
get "/organisations/#{organisation.id}/logs?search=", headers:, params: {} expect(page).to have_link(log_to_search.id.to_s)
logs.each do |log| logs.each do |log|
expect(page).not_to have_link(log.id.to_s) expect(page).not_to have_link(log.id.to_s)
end end
expect(page).not_to have_link(log_to_search.id.to_s)
end end
end
context "when search and filter is present" do context "when more than one results with matching postcode" do
let(:matching_postcode) { log_to_search.postcode_full } let!(:matching_postcode_log) { FactoryBot.create(:case_log, :completed, owning_organisation: user.organisation, postcode_full: log_to_search.postcode_full) }
let(:matching_status) { "in_progress" }
let!(:log_matching_filter_and_search) { FactoryBot.create(:case_log, :in_progress, owning_organisation: user.organisation, postcode_full: matching_postcode) }
it "shows only logs matching both search and filters" do it "displays all matching logs" do
get "/organisations/#{organisation.id}/logs?search=#{matching_postcode}&status[]=#{matching_status}", headers: headers, params: {} get "/organisations/#{organisation.id}/logs?search=#{log_to_search.postcode_full}", headers: headers, params: {}
expect(page).to have_content(log_matching_filter_and_search.id) expect(page).to have_link(log_to_search.id.to_s)
expect(page).not_to have_content(log_to_search.id) expect(page).to have_link(matching_postcode_log.id.to_s)
logs.each do |log| logs.each do |log|
expect(page).not_to have_content(log.id) expect(page).not_to have_link(log.id.to_s)
end
end end
end end
end
end
end
context "when viewing a specific organisation users" do context "when there are more than 1 page of search results" do
let!(:users) { FactoryBot.create_list(:user, 5, organisation:) } let(:postcode) { "XX11YY" }
let!(:different_org_users) { FactoryBot.create_list(:user, 5) } let(:logs) { FactoryBot.create_list(:case_log, 30, :completed, owning_organisation: user.organisation, postcode_full: postcode) }
let(:log_total_count) { CaseLog.where(owning_organisation: user.organisation).count }
before do it "has title with pagination details for page 1" do
get "/organisations/#{organisation.id}/users", headers:, params: {} get "/organisations/#{organisation.id}/logs?search=#{logs[0].postcode_full}", headers: headers, params: {}
end expect(page).to have_title("Your organisation (#{logs.count} logs matching ‘#{postcode}’ of #{log_total_count} total logs) (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK")
end
it "displays the name of the organisation" do it "has title with pagination details for page 2" do
expect(page).to have_content(organisation.name) get "/organisations/#{organisation.id}/logs?search=#{logs[0].postcode_full}&page=2", headers: headers, params: {}
end expect(page).to have_title("Your organisation (#{logs.count} logs matching ‘#{postcode}’ of #{log_total_count} total logs) (page 2 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK")
end
end
it "has a sub-navigation with correct tabs" do context "when search query doesn't match any logs" do
expect(page).to have_css(".app-sub-navigation") it "doesn't display any logs" do
expect(page).to have_content("Users") get "/organisations/#{organisation.id}/logs?search=foobar", headers:, params: {}
end logs.each do |log|
expect(page).not_to have_link(log.id.to_s)
end
expect(page).not_to have_link(log_to_search.id.to_s)
end
end
it "displays users for this organisation" do context "when search query is empty" do
expect(page).to have_content(user.email) it "doesn't display any logs" do
users.each do |user| get "/organisations/#{organisation.id}/logs?search=", headers:, params: {}
expect(page).to have_content(user.email) logs.each do |log|
end expect(page).not_to have_link(log.id.to_s)
end end
expect(page).not_to have_link(log_to_search.id.to_s)
end
end
it "doesn't display users for other organisations" do context "when search and filter is present" do
different_org_users.each do |different_org_user| let(:matching_postcode) { log_to_search.postcode_full }
expect(page).not_to have_content(different_org_user.email) let(:matching_status) { "in_progress" }
let!(:log_matching_filter_and_search) { FactoryBot.create(:case_log, :in_progress, owning_organisation: user.organisation, postcode_full: matching_postcode) }
it "shows only logs matching both search and filters" do
get "/organisations/#{organisation.id}/logs?search=#{matching_postcode}&status[]=#{matching_status}", headers: headers, params: {}
expect(page).to have_content(log_matching_filter_and_search.id)
expect(page).not_to have_content(log_to_search.id)
logs.each do |log|
expect(page).not_to have_content(log.id)
end
end
end
end end
end end
context "when a search parameter is passed" do context "when viewing a specific organisation's users" do
let!(:matching_user) { FactoryBot.create(:user, organisation:, name: "joe", email: "matching@example.com") } let!(:users) { FactoryBot.create_list(:user, 5, organisation:) }
let(:org_user_count) { User.where(organisation:).count } let!(:different_org_users) { FactoryBot.create_list(:user, 5) }
before do before do
get "/organisations/#{user.organisation.id}/users?search=#{search_param}" get "/organisations/#{organisation.id}/users", headers:, params: {}
end end
context "when our search string matches case" do it "displays the name of the organisation" do
let(:search_param) { "joe" } expect(page).to have_content(organisation.name)
end
it "returns only matching results" do it "has a sub-navigation with correct tabs" do
expect(page).to have_content(matching_user.name) expect(page).to have_css(".app-sub-navigation")
expect(page).not_to have_link(user.name) expect(page).to have_content("Users")
end
different_org_users.each do |different_org_user| it "displays users for this organisation" do
expect(page).not_to have_content(different_org_user.email) expect(page).to have_content(user.email)
end users.each do |user|
expect(page).to have_content(user.email)
end
end
users.each do |org_user| it "doesn't display users for other organisations" do
expect(page).not_to have_content(org_user.email) different_org_users.each do |different_org_user|
end expect(page).not_to have_content(different_org_user.email)
end end
end
it "updates the table caption" do context "when a search parameter is passed" do
expect(page).to have_content("1 user found matching ‘#{search_param}’ of #{org_user_count} total users.") let!(:matching_user) { FactoryBot.create(:user, organisation:, name: "joe", email: "matching@example.com") }
let(:org_user_count) { User.where(organisation:).count }
before do
get "/organisations/#{user.organisation.id}/users?search=#{search_param}"
end end
context "when we need case insensitive search" do context "when our search string matches case" do
let(:search_param) { "Joe" } let(:search_param) { "joe" }
it "returns only matching results" do it "returns only matching results" do
expect(page).to have_content(matching_user.name) expect(page).to have_content(matching_user.name)
@ -596,42 +649,34 @@ RSpec.describe OrganisationsController, type: :request do
it "updates the table caption" do it "updates the table caption" do
expect(page).to have_content("1 user found matching ‘#{search_param}’ of #{org_user_count} total users.") expect(page).to have_content("1 user found matching ‘#{search_param}’ of #{org_user_count} total users.")
end end
end
end
context "when our search term matches an email" do context "when we need case insensitive search" do
let(:search_param) { "matching@example.com" } let(:search_param) { "Joe" }
it "returns only matching results" do it "returns only matching results" do
expect(page).to have_content(matching_user.name) expect(page).to have_content(matching_user.name)
expect(page).not_to have_link(user.name) expect(page).not_to have_link(user.name)
different_org_users.each do |different_org_user| different_org_users.each do |different_org_user|
expect(page).not_to have_content(different_org_user.email) expect(page).not_to have_content(different_org_user.email)
end end
users.each do |org_user| users.each do |org_user|
expect(page).not_to have_content(org_user.email) expect(page).not_to have_content(org_user.email)
end end
end end
it "updates the table caption" do it "updates the table caption" do
expect(page).to have_content("1 user found matching ‘#{search_param}’ of #{org_user_count} total users.") expect(page).to have_content("1 user found matching ‘#{search_param}’ of #{org_user_count} total users.")
end
end
end end
context "when our search term matches an email and a name" do context "when our search term matches an email" do
let!(:matching_user) { FactoryBot.create(:user, organisation:, name: "Foobar", email: "some@example.com") } let(:search_param) { "matching@example.com" }
let!(:another_matching_user) { FactoryBot.create(:user, organisation:, name: "Joe", email: "foobar@example.com") }
let!(:org_user_count) { User.where(organisation:).count }
let(:search_param) { "Foobar" }
before do
get "/organisations/#{user.organisation.id}/users?search=#{search_param}"
end
it "returns only matching results" do it "returns only matching results" do
expect(page).to have_link(matching_user.name) expect(page).to have_content(matching_user.name)
expect(page).to have_link(another_matching_user.name)
expect(page).not_to have_link(user.name) expect(page).not_to have_link(user.name)
different_org_users.each do |different_org_user| different_org_users.each do |different_org_user|
@ -644,132 +689,203 @@ RSpec.describe OrganisationsController, type: :request do
end end
it "updates the table caption" do it "updates the table caption" do
expect(page).to have_content("2 users found matching ‘#{search_param}’ of #{org_user_count} total users.") expect(page).to have_content("1 user found matching ‘#{search_param}’ of #{org_user_count} total users.")
end
context "when our search term matches an email and a name" do
let!(:matching_user) { FactoryBot.create(:user, organisation:, name: "Foobar", email: "some@example.com") }
let!(:another_matching_user) { FactoryBot.create(:user, organisation:, name: "Joe", email: "foobar@example.com") }
let!(:org_user_count) { User.where(organisation:).count }
let(:search_param) { "Foobar" }
before do
get "/organisations/#{user.organisation.id}/users?search=#{search_param}"
end
it "returns only matching results" do
expect(page).to have_link(matching_user.name)
expect(page).to have_link(another_matching_user.name)
expect(page).not_to have_link(user.name)
different_org_users.each do |different_org_user|
expect(page).not_to have_content(different_org_user.email)
end
users.each do |org_user|
expect(page).not_to have_content(org_user.email)
end
end
it "updates the table caption" do
expect(page).to have_content("2 users found matching ‘#{search_param}’ of #{org_user_count} total users.")
end
end end
end end
end 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
it "displays the name of the organisation" do it "displays the name of the organisation" do
expect(page).to have_content(organisation.name) expect(page).to have_content(organisation.name)
end end
it "has a sub-navigation with correct tabs" do it "has a sub-navigation with correct tabs" do
expect(page).to have_css(".app-sub-navigation") expect(page).to have_css(".app-sub-navigation")
expect(page).to have_content("About this organisation") expect(page).to have_content("About this organisation")
end end
it "allows to edit the organisation details" do it "allows to edit the organisation details" 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
FactoryBot.create_list(:organisation, 25)
get "/organisations"
end
before do context "when on the first page" do
FactoryBot.create_list(:organisation, 25) it "has pagination links" do
allow(support_user).to receive(:need_two_factor_authentication?).and_return(false) expect(page).to have_content("Previous")
sign_in support_user expect(page).not_to have_link("Previous")
get "/organisations" expect(page).to have_content("Next")
end expect(page).to have_link("Next")
end
context "when on the first page" do it "shows which organisations are being shown on the current page" do
it "has pagination links" do expect(CGI.unescape_html(response.body)).to match("Showing <b>1</b> to <b>20</b> of <b>#{total_organisations_count}</b> organisations")
expect(page).to have_content("Previous") end
expect(page).not_to have_link("Previous")
expect(page).to have_content("Next")
expect(page).to have_link("Next")
end
it "shows which organisations are being shown on the current page" do it "has pagination in the title" do
expect(CGI.unescape_html(response.body)).to match("Showing <b>1</b> to <b>20</b> of <b>#{total_organisations_count}</b> organisations") expect(page).to have_title("Organisations (page 1 of 2)")
end end
end
it "has pagination in the title" do context "when on the second page" do
expect(page).to have_title("Organisations (page 1 of 2)") before do
end get "/organisations?page=2", headers:, params: {}
end end
context "when on the second page" do it "shows the total organisations count" do
before do expect(CGI.unescape_html(response.body)).to match("<strong>#{total_organisations_count}</strong> total organisations.")
get "/organisations?page=2", headers:, params: {} end
end
it "shows the total organisations count" do it "has pagination links" do
expect(CGI.unescape_html(response.body)).to match("<strong>#{total_organisations_count}</strong> total organisations.") expect(page).to have_content("Previous")
end expect(page).to have_link("Previous")
expect(page).to have_content("Next")
expect(page).not_to have_link("Next")
end
it "has pagination links" do it "shows which logs are being shown on the current page" do
expect(page).to have_content("Previous") expect(CGI.unescape_html(response.body)).to match("Showing <b>21</b> to <b>#{total_organisations_count}</b> of <b>#{total_organisations_count}</b> organisations")
expect(page).to have_link("Previous") end
expect(page).to have_content("Next")
expect(page).not_to have_link("Next")
end
it "shows which logs are being shown on the current page" do it "has pagination in the title" do
expect(CGI.unescape_html(response.body)).to match("Showing <b>21</b> to <b>#{total_organisations_count}</b> of <b>#{total_organisations_count}</b> organisations") expect(page).to have_title("Organisations (page 2 of 2)")
end end
end
it "has pagination in the title" do context "when searching" do
expect(page).to have_title("Organisations (page 2 of 2)") let!(:searched_organisation) { FactoryBot.create(:organisation, name: "Unusual name") }
end let!(:other_organisation) { FactoryBot.create(:organisation, name: "Some other name") }
end let(:search_param) { "Unusual" }
context "when searching" do before do
let!(:searched_organisation) { FactoryBot.create(:organisation, name: "Unusual name") } get "/organisations?search=#{search_param}"
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Some other name") } end
let(:search_param) { "Unusual" }
before do it "returns matching results" do
get "/organisations?search=#{search_param}" expect(page).to have_content(searched_organisation.name)
end expect(page).not_to have_content(other_organisation.name)
end
it "returns matching results" do it "updates the table caption" do
expect(page).to have_content(searched_organisation.name) expect(page).to have_content("1 organisation found matching ‘#{search_param}’ of 29 total organisations.")
expect(page).not_to have_content(other_organisation.name) end
end
it "updates the table caption" do it "has search in the title" do
expect(page).to have_content("1 organisation found matching ‘#{search_param}’ of 29 total organisations.") expect(page).to have_title("Organisations (1 organisation matching ‘#{search_param}’ of 29 total organisations) - Submit social housing lettings and sales data (CORE) - GOV.UK")
end end
it "has search in the title" do context "when the search term matches more than 1 result" do
expect(page).to have_title("Organisations (1 organisation matching ‘#{search_param}’ of 29 total organisations) - Submit social housing lettings and sales data (CORE) - GOV.UK") let(:search_param) { "name" }
end
context "when the search term matches more than 1 result" do it "returns matching results" do
let(:search_param) { "name" } expect(page).to have_content(searched_organisation.name)
expect(page).to have_content(other_organisation.name)
end
it "returns matching results" do it "updates the table caption" do
expect(page).to have_content(searched_organisation.name) expect(page).to have_content("2 organisations found matching ‘#{search_param}’ of 29 total organisations.")
expect(page).to have_content(other_organisation.name) end
end
it "updates the table caption" do it "has search in the title" do
expect(page).to have_content("2 organisations found matching ‘#{search_param}’ of 29 total organisations.") expect(page).to have_title("Organisations (2 organisations matching ‘#{search_param}’ of 29 total organisations) - Submit social housing lettings and sales data (CORE) - GOV.UK")
end end
end
context "when search results require pagination" do
let(:search_param) { "DLUHC" }
it "has search in the title" do it "has search and pagination in the title" do
expect(page).to have_title("Organisations (2 organisations matching ‘#{search_param}’ of 29 total organisations) - Submit social housing lettings and sales data (CORE) - GOV.UK") expect(page).to have_title("Organisations (27 organisations matching ‘#{search_param}’ of 29 total organisations) (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK")
end
end
end end
end end
end
context "when search results require pagination" do describe "#create" do
let(:search_param) { "DLUHC" } let(:name) { "Unique new org name" }
let(:address_line1) { "12 Random Street" }
it "has search and pagination in the title" do let(:address_line2) { "Manchester" }
expect(page).to have_title("Organisations (27 organisations matching ‘#{search_param}’ of 29 total organisations) (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK") let(:postcode) { "MD1 5TR" }
end 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