generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Front-end work for allowing users to switch organisations
We have a new front-end component, a dropdown menu organisation switcher, which will be displayed for a user with a non-zero number of `addtional_organisations` and will allow them to switch their organisation. (There is no functionality currently to populate a given user's `additional_organisations` - this would need to be done manually. By default a user will have zero `additional_organisations` and this component will be hidden.) Once switched, the user will see reports, exports, and activities for that organisation. They can switch back to their original organisation in the same way. We store the user's switched organisation in the session as `current_user_organisation`, and we also write this into `Current.user_organisation` so we can easily access it in the `User` model where we override the `#organisation` method in order to retrieve the data.
- Loading branch information
1 parent
2fa7106
commit e7a0490
Showing
14 changed files
with
270 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class OrganisationSessionController < ApplicationController | ||
include Secured | ||
|
||
def update | ||
desired_organisation_id = params[:current_user_organisation] | ||
|
||
if desired_organisation_id | ||
if current_user.additional_organisations.pluck(:id).include?(desired_organisation_id) || | ||
current_user.primary_organisation.id == desired_organisation_id | ||
session[:current_user_organisation] = desired_organisation_id | ||
end | ||
end | ||
redirect_to current_user.service_owner? ? request.referer : root_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
- if authenticated? && current_user.additional_organisations? | ||
.govuk-grid-row | ||
.govuk-grid-column-full{:class => "govuk-!-padding-top-4"} | ||
=form_with url: organisation_session_path, method: :patch do |form| | ||
=form.label t("organisation_switcher.label"), class: "govuk-label", for: "current_user_organisation" | ||
=form.select :current_user_organisation, options_for_select(@organisation_list.pluck(:name, :id), Current.user_organisation || current_user.organisation.id), {}, class: "govuk-select" | ||
=form.submit t("organisation_switcher.submit"), class: "govuk-button govuk-!-margin-bottom-1", "data-module": "govuk-button" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
en: | ||
organisation_switcher: | ||
label: "Available organisations" | ||
submit: "Set organisation" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe OrganisationSessionController do | ||
let(:user) { create(:partner_organisation_user, organisation: organisation) } | ||
let(:organisation) { create(:partner_organisation) } | ||
let(:other_organisation) { create(:partner_organisation) } | ||
|
||
before do | ||
allow(controller).to receive(:current_user).and_return(user) | ||
|
||
user.additional_organisations << [create(:partner_organisation), create(:partner_organisation)] | ||
end | ||
|
||
describe "#update" do | ||
it "sets the session `current_user_organisation` to the user's primary organisation's ID" do | ||
put :update, params: build_params(user.primary_organisation.id) | ||
|
||
expect(session[:current_user_organisation]).to eq(user.primary_organisation.id) | ||
end | ||
|
||
it "sets the session `current_user_organisation` to an ID from the user's additional organisations" do | ||
id = user.additional_organisations.pluck(:id).sample | ||
|
||
put :update, params: build_params(id) | ||
|
||
expect(session[:current_user_organisation]).to eq(id) | ||
end | ||
|
||
it "does not set the session `current_user_organisation` to a random ID" do | ||
random_id = SecureRandom.uuid | ||
|
||
put :update, params: build_params(random_id) | ||
|
||
expect(session[:current_user_organisation]).not_to eq(random_id) | ||
end | ||
|
||
it "does not set the session `current_user_organisation` to an organisation ID not in the user's additional organisations" do | ||
put :update, params: build_params(other_organisation.id) | ||
|
||
expect(session[:current_user_organisation]).not_to eq(other_organisation.id) | ||
end | ||
|
||
def build_params(id) | ||
{current_user_organisation: id} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
RSpec.feature "Users can switch organisation" do | ||
context "when the user has additional organisations" do | ||
let(:user_with_additional_organisations) do | ||
org1 = create(:partner_organisation) | ||
org2 = create(:partner_organisation) | ||
user = create(:partner_organisation_user) | ||
user.additional_organisations << [org1, org2] | ||
user | ||
end | ||
|
||
before do | ||
authenticate!(user: user_with_additional_organisations) | ||
visit root_path | ||
end | ||
after { logout } | ||
|
||
scenario "the organisation switcher dropdown is shown" do | ||
expect(page).to have_content t("organisation_switcher.label") | ||
end | ||
|
||
scenario "the organisation switcher dropdown is populated correctly" do | ||
user_with_additional_organisations.all_organisations.each do |org| | ||
expect(page).to have_content org.name | ||
end | ||
end | ||
|
||
scenario "the user can switch organisation" do | ||
expect(page).to have_select("current_user_organisation", selected: user_with_additional_organisations.primary_organisation.name) | ||
|
||
additional_org_name = user_with_additional_organisations.additional_organisations.first.name | ||
|
||
select(additional_org_name, from: "current_user_organisation") | ||
click_on t("organisation_switcher.submit") | ||
|
||
expect(page).to have_select("current_user_organisation", selected: additional_org_name) | ||
end | ||
|
||
scenario "the nav links have the correct organisation ID when the user has switched organisation" do | ||
additional_org = user_with_additional_organisations.additional_organisations.first | ||
|
||
activities_href = organisation_activities_path(organisation_id: user_with_additional_organisations.primary_organisation.id) | ||
exports_href = exports_organisation_path(id: user_with_additional_organisations.primary_organisation.id) | ||
|
||
expect(page).to have_link(href: activities_href) | ||
expect(page).to have_link(href: exports_href) | ||
|
||
select(additional_org.name, from: "current_user_organisation") | ||
click_on t("organisation_switcher.submit") | ||
|
||
updated_activities_href = organisation_activities_path(organisation_id: additional_org.id) | ||
updated_exports_href = exports_organisation_path(id: additional_org.id) | ||
|
||
expect(page).to have_link(href: updated_activities_href) | ||
expect(page).to have_link(href: updated_exports_href) | ||
end | ||
|
||
scenario "the Activities page shows the correct content when the user has switched organisation" do | ||
additional_org = user_with_additional_organisations.additional_organisations.first | ||
|
||
select(additional_org.name, from: "current_user_organisation") | ||
click_on t("organisation_switcher.submit") | ||
|
||
updated_activities_href = organisation_activities_path(organisation_id: additional_org.id) | ||
|
||
visit(updated_activities_href) | ||
|
||
expect(page).to have_content(t("page_title.activity.index")) | ||
end | ||
|
||
scenario "the Exports page shows the correct content when the user has switched organisation" do | ||
additional_org = user_with_additional_organisations.additional_organisations.first | ||
|
||
select(additional_org.name, from: "current_user_organisation") | ||
click_on t("organisation_switcher.submit") | ||
|
||
updated_exports_href = exports_organisation_path(id: additional_org.id) | ||
|
||
visit(updated_exports_href) | ||
|
||
expect(page).to have_content(t("page_title.export.organisation.show", name: additional_org.name)) | ||
end | ||
|
||
scenario "the nav links have the primary organisation ID when the user has switched organisation and switched back" do | ||
activities_href = organisation_activities_path(organisation_id: user_with_additional_organisations.primary_organisation.id) | ||
exports_href = exports_organisation_path(id: user_with_additional_organisations.primary_organisation.id) | ||
|
||
additional_org = user_with_additional_organisations.additional_organisations.first | ||
|
||
select(additional_org.name, from: "current_user_organisation") | ||
click_on t("organisation_switcher.submit") | ||
|
||
updated_exports_href = exports_organisation_path(id: additional_org.id) | ||
|
||
visit(updated_exports_href) | ||
|
||
select(user_with_additional_organisations.primary_organisation.name, from: "current_user_organisation") | ||
click_on t("organisation_switcher.submit") | ||
|
||
expect(page).to have_link(href: activities_href) | ||
expect(page).to have_link(href: exports_href) | ||
end | ||
end | ||
|
||
context "when the user has no additional organisations" do | ||
let(:user) { create(:partner_organisation_user) } | ||
|
||
before { authenticate!(user:) } | ||
after { logout } | ||
|
||
scenario "the organisation switcher dropdown is not shown" do | ||
visit root_path | ||
|
||
expect(page).not_to have_content t("organisation_switcher.label") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters