Browse Source

[CLDC-1659] Create managing agents (#956)

* Add managing agent

* tidy up specs

* Address comments

* linter
pull/958/head v0.2.16
Jack S 2 years ago committed by GitHub
parent
commit
38f1cada5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 45
      app/controllers/organisation_relationships_controller.rb
  2. 2
      app/views/organisation_relationships/add_housing_provider.html.erb
  3. 34
      app/views/organisation_relationships/add_managing_agent.html.erb
  4. 48
      app/views/organisation_relationships/managing_agents.html.erb
  5. 2
      config/routes.rb
  6. 473
      spec/requests/organisation_relationships_controller_spec.rb
  7. 312
      spec/requests/organisations_controller_spec.rb

45
app/controllers/organisation_relationships_controller.rb

@ -23,9 +23,11 @@ class OrganisationRelationshipsController < ApplicationController
def managing_agents def managing_agents
managing_agents = organisation.managing_agents managing_agents = organisation.managing_agents
unpaginated_filtered_managing_agents = filtered_collection(managing_agents, search_term) unpaginated_filtered_managing_agents = filtered_collection(managing_agents, search_term)
organisations = Organisation.where.not(id: @organisation.id).pluck(:id, :name)
respond_to do |format| respond_to do |format|
format.html do format.html do
@pagy, @managing_agents = pagy(unpaginated_filtered_managing_agents) @pagy, @managing_agents = pagy(unpaginated_filtered_managing_agents)
@organisations = organisations
@searched = search_term.presence @searched = search_term.presence
@total_count = managing_agents.size @total_count = managing_agents.size
render "organisation_relationships/managing_agents", layout: "application" render "organisation_relationships/managing_agents", layout: "application"
@ -42,6 +44,15 @@ class OrganisationRelationshipsController < ApplicationController
end end
end end
def add_managing_agent
@organisations = Organisation.where.not(id: @organisation.id).pluck(:id, :name)
respond_to do |format|
format.html do
render "organisation_relationships/add_managing_agent", layout: "application"
end
end
end
def create_housing_provider def create_housing_provider
child_organisation_id = @organisation.id child_organisation_id = @organisation.id
parent_organisation_id = related_organisation_id parent_organisation_id = related_organisation_id
@ -61,21 +72,47 @@ class OrganisationRelationshipsController < ApplicationController
redirect_to housing_providers_organisation_path(related_organisation_id:) redirect_to housing_providers_organisation_path(related_organisation_id:)
end end
def create_managing_agent
parent_organisation_id = @organisation.id
child_organisation_id = related_organisation_id
relationship_type = OrganisationRelationship::MANAGING
if related_organisation_id.empty?
@organisation.errors.add :related_organisation_id, "You must choose a managing agent"
@organisations = Organisation.where.not(id: parent_organisation_id).pluck(:id, :name)
render "organisation_relationships/add_managing_agent"
return
elsif OrganisationRelationship.exists?(child_organisation_id:, parent_organisation_id:, relationship_type:)
@organisation.errors.add :related_organisation_id, "You have already added this managing agent"
@organisations = Organisation.where.not(id: parent_organisation_id).pluck(:id, :name)
render "organisation_relationships/add_managing_agent"
return
end
create!(child_organisation_id:, parent_organisation_id:, relationship_type:)
redirect_to managing_agents_organisation_path(related_organisation_id:)
end
private
def create!(child_organisation_id:, parent_organisation_id:, relationship_type:) def create!(child_organisation_id:, parent_organisation_id:, relationship_type:)
@resource = OrganisationRelationship.new(child_organisation_id:, parent_organisation_id:, relationship_type:) @resource = OrganisationRelationship.new(child_organisation_id:, parent_organisation_id:, relationship_type:)
@resource.save! @resource.save!
end end
private def create(child_organisation_id, parent_organisation_id, relationship_type)
@resource = OrganisationRelationship.new(child_organisation_id:, parent_organisation_id:, relationship_type:)
def organisation @resource.save!
@organisation ||= Organisation.find(params[:id])
end end
def related_organisation_id def related_organisation_id
params["organisation"]["related_organisation_id"] params["organisation"]["related_organisation_id"]
end end
def organisation
@organisation ||= Organisation.find(params[:id])
end
def search_term def search_term
params["search"] params["search"]
end end

2
app/views/organisation_relationships/add_housing_provider.html.erb

@ -3,8 +3,6 @@
<%= render partial: "organisations/headings", locals: { main: @organisation.name, sub: nil } %> <%= render partial: "organisations/headings", locals: { main: @organisation.name, sub: nil } %>
<%= render SubNavigationComponent.new(items: secondary_items(request.path, @organisation.id)) %> <%= render SubNavigationComponent.new(items: secondary_items(request.path, @organisation.id)) %>
<h2 class="govuk-visually-hidden">Add Housing Provider</h2> <h2 class="govuk-visually-hidden">Add Housing Provider</h2>
<% end %>
<% if current_user.support? %>
<%= govuk_back_link(href: :back) %> <%= govuk_back_link(href: :back) %>
<%= render partial: "organisations/headings", locals: { main: "What is the name of this organisation's housing provider?", sub: nil } %> <%= render partial: "organisations/headings", locals: { main: "What is the name of this organisation's housing provider?", sub: nil } %>
<p class="govuk-body">Start typing to search for a housing provider</p> <p class="govuk-body">Start typing to search for a housing provider</p>

34
app/views/organisation_relationships/add_managing_agent.html.erb

@ -0,0 +1,34 @@
<%= form_with model: @organisation, url: managing_agents_organisation_path, method: "post", local: true do |f| %>
<% if current_user.support? %>
<%= render partial: "organisations/headings", locals: { main: @organisation.name, sub: nil } %>
<%= render SubNavigationComponent.new(items: secondary_items(request.path, @organisation.id)) %>
<h2 class="govuk-visually-hidden">Add Managing Agent</h2>
<%= govuk_back_link(href: :back) %>
<%= render partial: "organisations/headings", locals: { main: "What is the name of this organisation's managing agent?", sub: nil } %>
<p class="govuk-body">Start typing to search for a managing agent</p>
<% else %>
<% content_for :before_content do %>
<%= govuk_back_link(href: :back) %>
<% end %>
<%= render partial: "organisations/headings", locals: { main: "What is the name of your managing agent?", sub: nil } %>
<p class="govuk-body">Start typing to search for your managing agent</p>
<% end %>
<% answer_options = { "" => "Select an option" } %>
<% @organisations.each do |organisation| %>
<% answer_options[organisation[0]] = organisation[1] %>
<% end %>
<%= render partial: "organisation_relationships/related_organisation_select_question", locals: {
question: Form::Question.new("", { "answer_options" => answer_options }, nil),
f:,
} %>
<%= f.govuk_submit "Add" %>
<%= govuk_details(summary_text: "Can't find the managing agent you're looking for?") do %>
<ul class="govuk-list govuk-list--bullet">
<li>Double check the spelling and try again</li>
<li>Type the first few letters to see the suggestions</li>
<li>If you still can't find it,
<%= govuk_link_to("contact the DLUHC service desk", "https://digital.dclg.gov.uk/jira/servicedesk/customer/portal/4/group/21", rel: "noreferrer noopener", target: "_blank") %>
</li>
</ul>
<% end %>
<% end %>

48
app/views/organisation_relationships/managing_agents.html.erb

@ -1,23 +1,47 @@
<% item_label = format_label(@pagy.count, "managing agents") %> <% item_label = format_label(@pagy.count, "managing agents") %>
<% title = "Managing agents" %>
<% content_for :title, title %> <% if current_user.data_coordinator? %>
<% if params["related_organisation_id"] %>
<%= govuk_notification_banner(
title_text: "Success",
success: true, title_heading_level: 3,
title_id: "swanky-notifications"
) do |notification_banner|
notification_banner.heading(text: "#{Organisation.find(params['related_organisation_id']).name} is now one of this organisation's managing agents")
end %>
<% end %>
<% end %>
<% if current_user.support? %> <% if current_user.support? %>
<%= render partial: "organisations/headings", locals: { main: @organisation.name, sub: nil } %> <%= render partial: "organisations/headings", locals: { main: @organisation.name, sub: nil } %>
<%= render SubNavigationComponent.new( <%= render SubNavigationComponent.new(
items: secondary_items(request.path, @organisation.id), items: secondary_items(request.path, @organisation.id),
) %> ) %>
<h2 class="govuk-visually-hidden">managing agents</h2> <h2 class="govuk-visually-hidden">Managing Agents</h2>
<p class="govuk-body">A managing agent can submit logs for your organisation.</p> <% if params["related_organisation_id"] %>
<%= govuk_notification_banner(
title_text: "Success",
success: true, title_heading_level: 3,
title_id: "swanky-notifications"
) do |notification_banner|
notification_banner.heading(text: "#{Organisation.find(params['related_organisation_id']).name} is now one of this organisation's managing agents")
end %>
<% end %>
<p class="govuk-body">A managing agent can submit logs for this organisation.</p>
<% if @total_count == 0 %>
<p class="govuk-body">This organisation does not currently have any managing agents.</p>
<% end %>
<% else %> <% else %>
<%= render partial: "organisations/headings", locals: { main: "Your managing agents", sub: nil } %> <%= render partial: "organisations/headings", locals: { main: "This organisation managing agents", sub: current_user.organisation.name } %>
<p class="govuk-body">A managing agent can submit logs for this organisation.</p>
<p class="govuk-body">A managing agent can submit logs for your organisation.</p> <% if @total_count == 0 %>
<% end %> <p class="govuk-body">This organisation does not currently have any managing agents.</p>
<% if @total_count == 0 %> <% end %>
<p class="govuk-body">You do not currently have any managing agents.</p>
<% end %> <% end %>
<% if current_user.data_coordinator? || current_user.support? %> <% if current_user.support? %>
<%= govuk_button_link_to "Add a managing agent", managing_agents_organisation_path(organisation_id: @organisation.id), html: { method: :get } %> <%= govuk_button_link_to "Add a managing agent", managing_agents_add_organisation_path(organisation_id: @organisation.id), html: { method: :get } %>
<% elsif current_user.data_coordinator? %>
<%= govuk_button_link_to "Add a managing agent", managing_agents_add_organisation_path, html: { method: :get } %>
<% end %> <% end %>
<% if @total_count != 0 %> <% if @total_count != 0 %>
<%= render SearchComponent.new(current_user:, search_label: "Search for a managing agent", value: @searched) %> <%= render SearchComponent.new(current_user:, search_label: "Search for a managing agent", value: @searched) %>

2
config/routes.rb

@ -83,6 +83,8 @@ Rails.application.routes.draw do
get "housing-providers/add", to: "organisation_relationships#add_housing_provider" get "housing-providers/add", to: "organisation_relationships#add_housing_provider"
post "housing-providers", to: "organisation_relationships#create_housing_provider" post "housing-providers", to: "organisation_relationships#create_housing_provider"
get "managing-agents", to: "organisation_relationships#managing_agents" get "managing-agents", to: "organisation_relationships#managing_agents"
get "managing-agents/add", to: "organisation_relationships#add_managing_agent"
post "managing-agents", to: "organisation_relationships#create_managing_agent"
end end
end end

473
spec/requests/organisation_relationships_controller_spec.rb

@ -0,0 +1,473 @@
require "rails_helper"
RSpec.describe OrganisationRelationshipsController, type: :request do
let(:organisation) { user.organisation }
let!(:unauthorised_organisation) { FactoryBot.create(:organisation) }
let(:headers) { { "Accept" => "text/html" } }
let(:page) { Capybara::Node::Simple.new(response.body) }
context "when user is signed in" do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
context "with a data coordinator user" do
before do
sign_in user
end
context "when accessing the housing providers tab" do
context "with an organisation that the user belongs to" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let!(:other_org_housing_provider) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end
it "shows the tab navigation" do
expected_html = "<nav class=\"app-primary-navigation\""
expect(response.body).to include(expected_html)
end
it "shows an add housing provider button" do
expect(page).to have_link("Add a housing provider")
end
it "shows a table of housing providers" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(housing_provider.name)
end
it "shows only housing providers for the current user's organisation" do
expect(page).to have_content(housing_provider.name)
expect(page).not_to have_content(other_org_housing_provider.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total housing providers")
end
context "when adding a housing provider" do
before do
get "/organisations/#{organisation.id}/housing-providers/add", headers:, params: {}
end
it "has the correct header" do
expect(response.body).to include("What is the name of your housing provider?")
end
it "shows an add button" do
expect(page).to have_button("Add")
end
end
end
context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do
before do
get "/organisations/#{unauthorised_organisation.id}/housing-providers", headers:, params: {}
end
it "returns not found 404 from users page" do
expect(response).to have_http_status(:not_found)
end
end
end
context "when accessing the managing agents tab" do
context "with an organisation that the user belongs to" do
let!(:managing_agent) { FactoryBot.create(:organisation) }
let!(:other_org_managing_agent) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end
it "shows the tab navigation" do
expected_html = "<nav class=\"app-primary-navigation\""
expect(response.body).to include(expected_html)
end
it "shows an add managing-agent button" do
expect(page).to have_link("Add a managing agent")
end
it "shows a table of managing-agents" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(managing_agent.name)
end
it "shows only managing-agents for the current user's organisation" do
expect(page).to have_content(managing_agent.name)
expect(page).not_to have_content(other_org_managing_agent.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total agents")
end
end
context "when adding a managing agent" do
before do
get "/organisations/#{organisation.id}/managing-agents/add", headers:, params: {}
end
it "has the correct header" do
expect(response.body).to include("What is the name of your managing agent?")
end
end
context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do
before do
get "/organisations/#{unauthorised_organisation.id}/managing-agents", headers:, params: {}
end
it "returns not found 404 from users page" do
expect(response).to have_http_status(:not_found)
end
end
end
describe "organisation_relationships#create_housing_provider" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let(:params) do
{
"organisation": {
"related_organisation_id": housing_provider.id,
},
}
end
let(:request) { post "/organisations/#{organisation.id}/housing-providers", headers:, params: }
it "creates a new organisation relationship" do
expect { request }.to change(OrganisationRelationship, :count).by(1)
end
it "sets the organisation relationship attributes correctly" do
request
expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id, relationship_type: OrganisationRelationship::OWNING)
end
it "redirects to the organisation list" do
request
expect(response).to redirect_to("/organisations/#{organisation.id}/housing-providers?related_organisation_id=#{housing_provider.id}")
end
end
describe "organisation_relationships#create_managing_agent" do
let!(:managing_agent) { FactoryBot.create(:organisation) }
let(:params) do
{
"organisation": {
"related_organisation_id": managing_agent.id,
},
}
end
let(:request) { post "/organisations/#{organisation.id}/managing-agents", headers:, params: }
it "creates a new organisation relationship" do
expect { request }.to change(OrganisationRelationship, :count).by(1)
end
it "sets the organisation relationship attributes correctly" do
request
expect(OrganisationRelationship).to exist(parent_organisation_id: organisation.id, child_organisation_id: managing_agent.id, relationship_type: OrganisationRelationship::MANAGING)
end
it "redirects to the organisation list" do
request
expect(response).to redirect_to("/organisations/#{organisation.id}/managing-agents?related_organisation_id=#{managing_agent.id}")
end
end
end
context "with a data provider user" do
let(:user) { FactoryBot.create(:user) }
before do
sign_in user
end
context "when accessing the housing providers tab" do
context "with an organisation that the user belongs to" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let!(:other_org_housing_provider) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end
it "shows the tab navigation" do
expected_html = "<nav class=\"app-primary-navigation\""
expect(response.body).to include(expected_html)
end
it "doesn't show an add housing provider button" do
expect(page).not_to have_link("Add a housing provider")
end
it "shows a table of housing providers" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(housing_provider.name)
end
it "shows only housing providers for the current user's organisation" do
expect(page).to have_content(housing_provider.name)
expect(page).not_to have_content(other_org_housing_provider.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total housing providers")
end
end
context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do
before do
get "/organisations/#{unauthorised_organisation.id}/housing-providers", headers:, params: {}
end
it "returns not found 404 from users page" do
expect(response).to have_http_status(:not_found)
end
end
end
context "when accessing the managing agents tab" do
context "with an organisation that the user belongs to" do
let!(:managing_agent) { FactoryBot.create(:organisation) }
let!(:other_org_managing_agent) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end
it "shows the tab navigation" do
expected_html = "<nav class=\"app-primary-navigation\""
expect(response.body).to include(expected_html)
end
it "doesn't show an add managing agent button" do
expect(page).not_to have_link("Add a managing agent")
end
it "shows a table of managing agents" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(managing_agent.name)
end
it "shows only managing agents for the current user's organisation" do
expect(page).to have_content(managing_agent.name)
expect(page).not_to have_content(other_org_managing_agent.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total agents")
end
end
context "when adding a managing agent" do
before do
get "/organisations/#{organisation.id}/managing-agents/add", headers:, params: {}
end
it "has the correct header" do
expect(response.body).to include("What is the name of your managing agent?")
end
end
context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do
before do
get "/organisations/#{unauthorised_organisation.id}/managing-agents", headers:, params: {}
end
it "returns not found 404 from users page" do
expect(response).to have_http_status(:not_found)
end
end
end
end
context "with a support user" do
let(:user) { FactoryBot.create(:user, :support) }
before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
end
describe "organisation_relationships#create_housing_provider" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let(:params) do
{
"organisation": {
"related_organisation_id": housing_provider.id,
},
}
end
let(:request) { post "/organisations/#{organisation.id}/housing-providers", headers:, params: }
it "creates a new organisation relationship" do
expect { request }.to change(OrganisationRelationship, :count).by(1)
end
it "sets the organisation relationship attributes correctly" do
request
expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id, relationship_type: OrganisationRelationship::OWNING)
end
it "redirects to the organisation list" do
request
expect(response).to redirect_to("/organisations/#{organisation.id}/housing-providers?related_organisation_id=#{housing_provider.id}")
end
end
describe "organisation_relationships#create_managing_agent" do
let!(:managing_agent) { FactoryBot.create(:organisation) }
let(:params) do
{
"organisation": {
"related_organisation_id": managing_agent.id,
},
}
end
let(:request) { post "/organisations/#{organisation.id}/managing-agents", headers:, params: }
it "creates a new organisation relationship" do
expect { request }.to change(OrganisationRelationship, :count).by(1)
end
it "sets the organisation relationship attributes correctly" do
request
expect(OrganisationRelationship).to exist(parent_organisation_id: organisation.id, child_organisation_id: managing_agent.id, relationship_type: OrganisationRelationship::MANAGING)
end
it "redirects to the organisation list" do
request
expect(response).to redirect_to("/organisations/#{organisation.id}/managing-agents?related_organisation_id=#{managing_agent.id}")
end
end
context "when viewing a specific organisation's housing providers" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let!(:other_org_housing_provider) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end
it "displays the name of the organisation" do
expect(page).to have_content(organisation.name)
end
it "has a sub-navigation with correct tabs" do
expect(page).to have_css(".app-sub-navigation")
expect(page).to have_content("Users")
end
it "shows a table of housing providers" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(housing_provider.name)
end
it "shows only housing providers for this organisation" do
expect(page).to have_content(housing_provider.name)
expect(page).not_to have_content(other_org_housing_provider.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total housing providers")
end
context "when adding a housing provider" do
before do
get "/organisations/#{organisation.id}/housing-providers/add", headers:, params: {}
end
it "has the correct header" do
expect(response.body).to include("What is the name of this organisation&#39;s housing provider?")
end
it "shows an add button" do
expect(page).to have_button("Add")
end
end
end
context "when viewing a specific organisation's managing agents" do
let!(:managing_agent) { FactoryBot.create(:organisation) }
let!(:other_org_managing_agent) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end
it "displays the name of the organisation" do
expect(page).to have_content(organisation.name)
end
it "has a sub-navigation with correct tabs" do
expect(page).to have_css(".app-sub-navigation")
expect(page).to have_content("Users")
end
it "shows a table of managing agents" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(managing_agent.name)
end
it "shows only managing agents for this organisation" do
expect(page).to have_content(managing_agent.name)
expect(page).not_to have_content(other_org_managing_agent.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total agents")
end
context "when adding a housing provider" do
before do
get "/organisations/#{organisation.id}/managing-agents/add", headers:, params: {}
end
it "has the correct header" do
expect(response.body).to include("What is the name of this organisation&#39;s managing agent?")
end
it "shows an add button" do
expect(page).to have_button("Add")
end
end
end
end
end
end

312
spec/requests/organisations_controller_spec.rb

@ -284,116 +284,6 @@ RSpec.describe OrganisationsController, type: :request do
end end
end end
context "when accessing the housing providers tab" do
context "with an organisation that the user belongs to" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let!(:other_org_housing_provider) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end
it "shows the tab navigation" do
expected_html = "<nav class=\"app-primary-navigation\""
expect(response.body).to include(expected_html)
end
it "shows an add housing provider button" do
expect(page).to have_link("Add a housing provider")
end
it "shows a table of housing providers" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(housing_provider.name)
end
it "shows only housing providers for the current user's organisation" do
expect(page).to have_content(housing_provider.name)
expect(page).not_to have_content(other_org_housing_provider.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total housing providers")
end
context "when adding a housing provider" do
before do
get "/organisations/#{organisation.id}/housing-providers/add", headers:, params: {}
end
it "has the correct header" do
expect(response.body).to include("What is the name of your housing provider?")
end
it "shows an add button" do
expect(page).to have_button("Add")
end
end
end
context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do
before do
get "/organisations/#{unauthorised_organisation.id}/housing-providers", headers:, params: {}
end
it "returns not found 404 from users page" do
expect(response).to have_http_status(:not_found)
end
end
end
context "when accessing the managing agents tab" do
context "with an organisation that the user belongs to" do
let!(:managing_agent) { FactoryBot.create(:organisation) }
let!(:other_org_managing_agent) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end
it "shows the tab navigation" do
expected_html = "<nav class=\"app-primary-navigation\""
expect(response.body).to include(expected_html)
end
it "shows an add managing-agent button" do
expect(page).to have_link("Add a managing agent")
end
it "shows a table of managing-agents" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(managing_agent.name)
end
it "shows only managing-agents for the current user's organisation" do
expect(page).to have_content(managing_agent.name)
expect(page).not_to have_content(other_org_managing_agent.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total agents")
end
end
context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do
before do
get "/organisations/#{unauthorised_organisation.id}/managing-agents", headers:, params: {}
end
it "returns not found 404 from users page" do
expect(response).to have_http_status(:not_found)
end
end
end
describe "#edit" do describe "#edit" do
context "with an organisation that the user belongs to" do context "with an organisation that the user belongs to" do
before do before do
@ -534,34 +424,6 @@ RSpec.describe OrganisationsController, type: :request do
expect { request }.not_to change(Organisation, :count) expect { request }.not_to change(Organisation, :count)
end end
end end
describe "organisation_relationships#create_housing_provider" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let(:params) do
{
"organisation": {
"related_organisation_id": housing_provider.id,
},
}
end
let(:request) { post "/organisations/#{organisation.id}/housing-providers", headers:, params: }
it "creates a new organisation relationship" do
expect { request }.to change(OrganisationRelationship, :count).by(1)
end
it "sets the organisation relationship attributes correctly" do
request
expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id, relationship_type: OrganisationRelationship::OWNING)
end
it "redirects to the organisation list" do
request
expect(response).to redirect_to("/organisations/#{organisation.id}/housing-providers?related_organisation_id=#{housing_provider.id}")
end
end
end end
context "with a data provider user" do context "with a data provider user" do
@ -616,102 +478,6 @@ RSpec.describe OrganisationsController, type: :request do
end end
end end
context "when accessing the housing providers tab" do
context "with an organisation that the user belongs to" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let!(:other_org_housing_provider) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end
it "shows the tab navigation" do
expected_html = "<nav class=\"app-primary-navigation\""
expect(response.body).to include(expected_html)
end
it "doesn't show an add housing provider button" do
expect(page).not_to have_link("Add a housing provider")
end
it "shows a table of housing providers" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(housing_provider.name)
end
it "shows only housing providers for the current user's organisation" do
expect(page).to have_content(housing_provider.name)
expect(page).not_to have_content(other_org_housing_provider.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total housing providers")
end
end
context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do
before do
get "/organisations/#{unauthorised_organisation.id}/housing-providers", headers:, params: {}
end
it "returns not found 404 from users page" do
expect(response).to have_http_status(:not_found)
end
end
end
context "when accessing the managing agents tab" do
context "with an organisation that the user belongs to" do
let!(:managing_agent) { FactoryBot.create(:organisation) }
let!(:other_org_managing_agent) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing])
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end
it "shows the tab navigation" do
expected_html = "<nav class=\"app-primary-navigation\""
expect(response.body).to include(expected_html)
end
it "doesn't show an add managing agent button" do
expect(page).not_to have_link("Add a managing agent")
end
it "shows a table of managing agents" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(managing_agent.name)
end
it "shows only managing agents for the current user's organisation" do
expect(page).to have_content(managing_agent.name)
expect(page).not_to have_content(other_org_managing_agent.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total agents")
end
end
context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do
before do
get "/organisations/#{unauthorised_organisation.id}/managing-agents", headers:, params: {}
end
it "returns not found 404 from users page" do
expect(response).to have_http_status(:not_found)
end
end
end
describe "#edit" do describe "#edit" do
before do before do
get "/organisations/#{organisation.id}/edit", headers:, params: {} get "/organisations/#{organisation.id}/edit", headers:, params: {}
@ -1194,56 +960,6 @@ RSpec.describe OrganisationsController, type: :request do
end end
end end
context "when viewing a specific organisation's housing providers" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let!(:other_org_housing_provider) { FactoryBot.create(:organisation, name: "Foobar LTD") }
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning])
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end
it "displays the name of the organisation" do
expect(page).to have_content(organisation.name)
end
it "has a sub-navigation with correct tabs" do
expect(page).to have_css(".app-sub-navigation")
expect(page).to have_content("Users")
end
it "shows a table of housing providers" do
expected_html = "<table class=\"govuk-table\""
expect(response.body).to include(expected_html)
expect(response.body).to include(housing_provider.name)
end
it "shows only housing providers for this organisation" do
expect(page).to have_content(housing_provider.name)
expect(page).not_to have_content(other_org_housing_provider.name)
end
it "shows the pagination count" do
expect(page).to have_content("1 total housing providers")
end
context "when adding a housing provider" do
before do
get "/organisations/#{organisation.id}/housing-providers/add", headers:, params: {}
end
it "has the correct header" do
expect(response.body).to include("What is the name of this organisation&#39;s housing provider?")
end
it "shows an add button" do
expect(page).to have_button("Add")
end
end
end
context "when there are more than 20 organisations" do context "when there are more than 20 organisations" do
let(:total_organisations_count) { Organisation.all.count } let(:total_organisations_count) { Organisation.all.count }
@ -1401,34 +1117,6 @@ RSpec.describe OrganisationsController, type: :request do
end end
end end
end end
describe "organisation_relationships#create_housing_provider" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let(:params) do
{
"organisation": {
"related_organisation_id": housing_provider.id,
},
}
end
let(:request) { post "/organisations/#{organisation.id}/housing-providers", headers:, params: }
it "creates a new organisation relationship" do
expect { request }.to change(OrganisationRelationship, :count).by(1)
end
it "sets the organisation relationship attributes correctly" do
request
expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id, relationship_type: OrganisationRelationship::OWNING)
end
it "redirects to the organisation list" do
request
expect(response).to redirect_to("/organisations/#{organisation.id}/housing-providers?related_organisation_id=#{housing_provider.id}")
end
end
end end
end end

Loading…
Cancel
Save