Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create reusable sign in test helpers #1120

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions spec/support/dfe_sign_in_user_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
module DfESignInUserHelper
def sign_in_placements_user(organisations: [], with_dfe_sign_id: true, sign_in: true)
@current_user = create(:placements_user)
@current_user.update!(dfe_sign_in_uid: nil) unless with_dfe_sign_id
organisations.each do |organisation|
create(:user_membership, user: @current_user, organisation:)
end
sign_in ? sign_in_as(@current_user) : user_exists_in_dfe_sign_in(user: @current_user)
end
alias_method :given_i_am_signed_in_as_a_placements_user, :sign_in_placements_user

def sign_in_placements_support_user(with_dfe_sign_id: true, sign_in: true)
@current_user = create(:placements_support_user)
@current_user.update!(dfe_sign_in_uid: nil) unless with_dfe_sign_id
sign_in ? sign_in_as(@current_user) : user_exists_in_dfe_sign_in(user: @current_user)
end
alias_method :given_i_am_signed_in_as_a_placements_support_user, :sign_in_placements_support_user

def sign_in_as(user)
user_exists_in_dfe_sign_in(user:)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,6 @@

private

def and_i_visit_the_sign_in_path
visit sign_in_path
end

def and_i_click_sign_in
click_on "Sign in using DfE Sign In"
end

def then_i_should_see_the_mentor_name_in_the_placement_details(
mentor_name:, change_link: "Change"
)
Expand Down
12 changes: 2 additions & 10 deletions spec/system/good_job_dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

RSpec.describe "GoodJob admin dashboard", service: :placements, type: :system do
scenario "Access the dashboard as a support user" do
given_i_am_a_support_user
given_i_am_signed_in_as_a_placements_support_user
when_i_go_to_the_dashboard_path
then_i_can_see_the_dashboard
end

scenario "Cannot access the dashboard as a regular user" do
given_i_am_a_regular_user
given_i_am_signed_in_as_a_placements_user
when_i_go_to_the_dashboard_path
then_i_cannot_see_the_dashboard
end
Expand All @@ -20,14 +20,6 @@

private

def given_i_am_a_support_user
sign_in_as create(:placements_support_user)
end

def given_i_am_a_regular_user
sign_in_as create(:placements_user)
end

def when_i_go_to_the_dashboard_path
visit "/good_job"
end
Expand Down
10 changes: 2 additions & 8 deletions spec/system/infinite_redirect_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
require "rails_helper"

RSpec.describe "Infinite redirect", service: :placements, type: :system do
let!(:user) { create(:placements_support_user) }

scenario "When the support users permissions are removed they aren't stuck in an infinite loop" do
given_i_am_a_support_user
given_i_am_signed_in_as_a_placements_support_user
when_i_view_all_organisations
and_the_support_user_is_removed_as_a_support_user
and_i_click_link("Organisations")
Expand All @@ -14,16 +12,12 @@

private

def given_i_am_a_support_user
sign_in_as user
end

def when_i_view_all_organisations
visit placements_support_organisations_path
end

def and_the_support_user_is_removed_as_a_support_user
user.update!(type: "Placements::User")
@current_user.update!(type: "Placements::User")
end

def and_i_click_link(text)
Expand Down
28 changes: 1 addition & 27 deletions spec/system/placements/authenticate_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,13 @@
end

scenario "As a user who has signed in" do
given_there_is_an_existing_placements_user_with_a_school_for(anne)
when_i_visit_the_sign_in_path
and_i_click_sign_in
given_i_am_signed_in_as_a_placements_user(organisations: [school])
and_i_visit_placements_schools_details_path
then_i_am_able_to_access_the_page
end

private

def given_there_is_an_existing_user_for(user_name)
user = create(:placements_user, user_name.downcase.to_sym)
create(:user_membership, user:, organisation: school, email: user.email)
user
end

def when_i_visit_placements_schools_details_path
visit placements_school_path(school.id)
end
Expand All @@ -37,23 +29,5 @@ def then_i_am_able_to_access_the_page
expect(page).to have_content("NameSchool")
end

def when_i_visit_the_sign_in_path
visit sign_in_path
end

def when_i_click_sign_in
click_on "Sign in using DfE Sign In"
end

def given_there_is_an_existing_placements_user_with_a_school_for(user)
user_exists_in_dfe_sign_in(user:)
create(
:user_membership,
user:,
organisation: school,
)
end

alias_method :and_i_click_sign_in, :when_i_click_sign_in
alias_method :and_i_visit_placements_schools_details_path, :when_i_visit_placements_schools_details_path
end
52 changes: 18 additions & 34 deletions spec/system/placements/organisations/users/add_users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
perform_enqueued_jobs { example.run }
end

let(:anne) { create(:placements_user, :anne) }
let(:one_school) { create(:placements_school, name: "One School") }
let(:one_provider) { create(:placements_provider, name: "One Provider") }
let(:mary) { create(:placements_user, :mary) }
let(:another_school) { create(:placements_school, name: "Another School") }
let(:new_user) { create(:placements_user) }

describe "Ann invites a member successfully" do
context "with provider" do
scenario "user invites a member to a provider" do
given_i_am_logged_in_as_a_user_with_one_organisation(one_provider)
given_i_am_signed_in_as_a_placements_user(organisations: [one_provider])
when_i_click_users
then_i_see_the_users_page
then_i_see_the_users_page_for_provider(one_provider)
when_i_click_add_user
and_i_enter_valid_user_details
and_user_is_selected_in_provider_primary_navigation
Expand All @@ -38,9 +36,9 @@

context "with school" do
scenario "user invites a member to a school" do
given_i_am_logged_in_as_a_user_with_one_organisation(one_school)
given_i_am_signed_in_as_a_placements_user(organisations: [one_school])
when_i_click_users
then_i_see_the_users_page
then_i_see_the_users_page_for_school(one_school)
and_user_is_selected_in_school_primary_navigation
when_i_click_add_user
and_user_is_selected_in_school_primary_navigation
Expand All @@ -60,14 +58,10 @@
end

describe "Mary invites a members to second organisation" do
before "user is sent an invitation" do
create(:user_membership, user: mary, organisation: one_school)
create(:user_membership, user: mary, organisation: another_school)
create(:user_membership, user: mary, organisation: one_provider)
end

scenario "user adds a user to multiple organisations" do
given_i_am_logged_in_as_a_user_with_multiple_organisations
given_i_am_signed_in_as_a_placements_user(
organisations: [one_provider, one_school, another_school],
)
and_user_is_already_assigned_to_a_school
when_i_navigate_to_that_schools_users
then_i_see_the_user_on_that_schools_user_list
Expand All @@ -83,16 +77,16 @@
end

scenario "user tries to submit invalid form" do
given_i_am_logged_in_as_a_user_with_one_organisation(one_school)
given_i_am_signed_in_as_a_placements_user(organisations: [one_school])
when_i_click_users
then_i_see_the_users_page
then_i_see_the_users_page_for_school(one_school)
when_i_click_add_user
and_try_to_submit_invalid_form_data
then_i_see_form_errors
end

scenario "user tries to add an existing user to the organisation" do
given_i_am_logged_in_as_a_user_with_one_organisation(one_school)
given_i_am_signed_in_as_a_placements_user(organisations: [one_school])
and_user_is_already_assigned_to_a_school
when_i_try_to_add_the_user_to_the_same_school
then_i_see_the_email_taken_error
Expand All @@ -106,14 +100,14 @@
}
end

before { given_i_am_logged_in_as_a_user_with_one_organisation(one_school) }
before { given_i_am_signed_in_as_a_placements_user(organisations: [one_school]) }

it "persists the user details for each tab upon refresh" do
windows.each do |window, details|
within_window window do
visit placements_school_placements_path(one_school)
when_i_click_users
then_i_see_the_users_page
then_i_see_the_users_page_for_school(one_school)
and_user_is_selected_in_school_primary_navigation
when_i_click_add_user
and_user_is_selected_in_school_primary_navigation
Expand Down Expand Up @@ -159,19 +153,6 @@ def then_i_see_the_email_taken_error
expect(page).to have_content("Email address already in use")
end

def given_i_am_logged_in_as_a_user_with_one_organisation(organisation)
user_exists_in_dfe_sign_in(user: anne)
create(:user_membership, user: anne, organisation:)
visit sign_in_path
click_on "Sign in using DfE Sign In"
end

def given_i_am_logged_in_as_a_user_with_multiple_organisations
user_exists_in_dfe_sign_in(user: mary)
visit sign_in_path
click_on "Sign in using DfE Sign In"
end

def and_user_is_already_assigned_to_a_school
create(:user_membership, user: new_user, organisation: one_school)
end
Expand Down Expand Up @@ -220,9 +201,12 @@ def when_i_click_users
click_on "Users"
end

def then_i_see_the_users_page
expect(page).to have_content "Anne Wilson"
expect(page).to have_content "[email protected]"
def then_i_see_the_users_page_for_school(school)
expect(page).to have_current_path placements_school_users_path(school), ignore_query: true
end

def then_i_see_the_users_page_for_provider(provider)
expect(page).to have_current_path placements_provider_users_path(provider), ignore_query: true
end

def when_i_click_add_user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
let(:mary) { create(:placements_user, :mary) }

before do
[anne, mary].each do |user|
create(:user_membership, user:, organisation: school)
end
create(:user_membership, user: anne, organisation: school)
given_i_am_signed_in_as_a_placements_user(organisations: [school])
end

scenario "user is removed from a school" do
given_i_am_signed_in_as_mary
and_i_visit_the_user_page(anne)
when_i_click_on("Delete user")
then_i_am_asked_to_confirm(anne, school)
Expand All @@ -33,8 +31,7 @@
end

scenario "I try to remove myself from a school" do
given_i_am_signed_in_as_mary
when_i_try_to_visit_the_remove_path_for(mary, school)
when_i_try_to_visit_the_remove_path_for(@current_user, school)
then_i_am_redirected_back
end
end
Expand All @@ -45,13 +42,11 @@
let(:mary) { create(:placements_user, :mary) }

before "message is sent to user" do
[anne, mary].each do |user|
create(:user_membership, user:, organisation: provider)
end
create(:user_membership, user: anne, organisation: provider)
given_i_am_signed_in_as_a_placements_user(organisations: [provider])
end

scenario "user is removed from a provider" do
given_i_am_signed_in_as_mary
and_i_visit_the_user_page(anne)
when_i_click_on("Delete user")
then_i_am_asked_to_confirm(anne, provider)
Expand All @@ -65,20 +60,13 @@
end

scenario "I try to remove myself from a school" do
given_i_am_signed_in_as_mary
when_i_try_to_visit_the_remove_path_for(mary, provider)
when_i_try_to_visit_the_remove_path_for(@current_user, provider)
then_i_am_redirected_back
end
end

private

def given_i_am_signed_in_as_mary
user_exists_in_dfe_sign_in(user: mary)
visit sign_in_path
click_on "Sign in using DfE Sign In"
end

def email_removal_notification(email, _organisation)
ActionMailer::Base.deliveries.find do |delivery|
delivery.to.include?(email) && delivery.subject == "You have been removed from Manage school placements"
Expand Down
Loading