diff --git a/app/helpers/user_table_helper.rb b/app/helpers/tab_nav_helper.rb similarity index 50% rename from app/helpers/user_table_helper.rb rename to app/helpers/tab_nav_helper.rb index 7726eff94..c8d8e83b9 100644 --- a/app/helpers/user_table_helper.rb +++ b/app/helpers/tab_nav_helper.rb @@ -1,4 +1,4 @@ -module UserTableHelper +module TabNavHelper include GovukLinkHelper def user_cell(user) @@ -9,4 +9,12 @@ module UserTableHelper role = "#{user.role.to_s.humanize}" [user.organisation.name, role].join("\n") end + + def tab_items(user) + items = [{ name: t("Details"), url: details_organisation_path(user.organisation) }] + if user.data_coordinator? + items << { name: t("Users"), url: users_organisation_path(user.organisation) } + end + items + end end diff --git a/app/views/layouts/organisations.html.erb b/app/views/layouts/organisations.html.erb index 4be6b090b..b316bf64f 100644 --- a/app/views/layouts/organisations.html.erb +++ b/app/views/layouts/organisations.html.erb @@ -3,10 +3,9 @@ Your organisation - <%= render TabNavigationComponent.new(items: [ - { name: t('Details'), url: details_organisation_path(@organisation) }, - { name: t('Users'), url: users_organisation_path(@organisation) }, - ]) %> + <% items = tab_items(current_user) %> + + <%= render TabNavigationComponent.new(items: items) %>

<%= content_for(:tab_title) %>

diff --git a/spec/features/organisation_spec.rb b/spec/features/organisation_spec.rb index 3a3d97b7e..dd6d120e8 100644 --- a/spec/features/organisation_spec.rb +++ b/spec/features/organisation_spec.rb @@ -3,7 +3,6 @@ require_relative "form/helpers" RSpec.describe "User Features" do include Helpers - let!(:user) { FactoryBot.create(:user) } let(:organisation) { user.organisation } let(:org_id) { organisation.id } @@ -11,33 +10,50 @@ RSpec.describe "User Features" do sign_in user end - context "Organisation page" do - it "default to organisation details" do - visit("/case-logs") - click_link("Your organisation") - expect(page).to have_content(user.organisation.name) + context "User is a data coordinator" do + let!(:user) { FactoryBot.create(:user, :data_coordinator) } + + context "Organisation page" do + it "defaults to organisation details" do + visit("/case-logs") + click_link("Your organisation") + expect(page).to have_content(user.organisation.name) + end + + it "can switch tabs" do + visit("/organisations/#{org_id}") + click_link("Users") + expect(page).to have_current_path("/organisations/#{org_id}/users") + click_link("Details") + expect(page).to have_current_path("/organisations/#{org_id}/details") + end end - it "can switch tabs" do - visit("/organisations/#{org_id}") - click_link("Users") - expect(page).to have_current_path("/organisations/#{org_id}/users") - click_link("Details") - expect(page).to have_current_path("/organisations/#{org_id}/details") + context "Organisation users" do + it "users can be added" do + visit("/organisations/#{org_id}") + click_link("Users") + click_link("Invite user") + expect(page).to have_current_path("/users/new") + expect(page).to have_content("Invite user to submit CORE data") + fill_in("user[name]", with: "New User") + fill_in("user[email]", with: "new_user@example.com") + expect { click_button("Continue") }.to change { ActionMailer::Base.deliveries.count }.by(1) + expect(page).to have_current_path("/organisations/#{org_id}/users") + end end end - context "Organisation users" do - it "users can be added" do - visit("/organisations/#{org_id}") - click_link("Users") - click_link("Invite user") - expect(page).to have_current_path("/users/new") - expect(page).to have_content("Invite user to submit CORE data") - fill_in("user[name]", with: "New User") - fill_in("user[email]", with: "new_user@example.com") - expect { click_button("Continue") }.to change { ActionMailer::Base.deliveries.count }.by(1) - expect(page).to have_current_path("/organisations/#{org_id}/users") + context "User is a data provider" do + let!(:user) { FactoryBot.create(:user) } + + context "Organisation page" do + it "can only see the details tab" do + visit("/case-logs") + click_link("Your organisation") + expect(page).to have_current_path("/organisations/#{org_id}/details") + expect(page).to have_no_link("Users") + end end end end diff --git a/spec/helpers/tab_nav_helper_spec.rb b/spec/helpers/tab_nav_helper_spec.rb new file mode 100644 index 000000000..ad68dd1a1 --- /dev/null +++ b/spec/helpers/tab_nav_helper_spec.rb @@ -0,0 +1,40 @@ +require "rails_helper" + +RSpec.describe TabNavHelper do + let(:organisation) { FactoryBot.create(:organisation) } + let(:user) { FactoryBot.build(:user, organisation: organisation) } + + describe "#user_cell" do + it "returns user link and email separated by a newline character" do + expected_html = "Danny Rojas\n#{user.email}" + expect(user_cell(user)).to match(expected_html) + end + end + + describe "#org_cell" do + it "returns the users org name and role separated by a newline character" do + expected_html = "DLUHC\nData provider" + expect(org_cell(user)).to match(expected_html) + end + end + + describe "#tab_items" do + context "user is a data_coordinator" do + let(:user) { FactoryBot.build(:user, :data_coordinator, organisation: organisation) } + it "returns details and user tabs" do + result = tab_items(user).map { |i| i[:name] } + expect(result.count).to eq(2) + expect(result.first).to match("Details") + expect(result.second).to match("Users") + end + end + + context "user is a data_provider" do + it "returns details tab only" do + result = tab_items(user).map { |i| i[:name] } + expect(result.count).to eq(1) + expect(result.first).to match("Details") + end + end + end +end diff --git a/spec/helpers/user_table_helper_spec.rb b/spec/helpers/user_table_helper_spec.rb deleted file mode 100644 index e3a4f9204..000000000 --- a/spec/helpers/user_table_helper_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -require "rails_helper" - -RSpec.describe UserTableHelper do - let(:user) { FactoryBot.build(:user) } - - describe "#user_cell" do - it "returns user link and email separated by a newline character" do - expected_html = "Danny Rojas\n#{user.email}" - expect(user_cell(user)).to match(expected_html) - end - end - - describe "#org_cell" do - it "returns the users org name and role separated by a newline character" do - expected_html = "DLUHC\nData provider" - expect(org_cell(user)).to match(expected_html) - end - end -end