Skip to content

Commit

Permalink
Simplify authentication for development
Browse files Browse the repository at this point in the history
This removes the need to enter username/password credentials when
testing or development and only requires the service_open feature flag
to be specified in production.
  • Loading branch information
thomasleese committed Mar 27, 2024
1 parent 3578f25 commit f1585b1
Show file tree
Hide file tree
Showing 66 changed files with 25 additions and 205 deletions.
29 changes: 9 additions & 20 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class ApplicationController < ActionController::Base
default_form_builder GOVUKDesignSystemFormBuilder::FormBuilder
layout "two_thirds"

before_action :authenticate,
unless: -> { FeatureFlags::FeatureFlag.active?(:service_open) }
before_action :authenticate_support!, unless: :service_open?

def current_user
nil
Expand All @@ -20,25 +19,15 @@ def current_user

private

def authenticate
valid_credentials = [
{
username: ENV.fetch("SUPPORT_USERNAME", "support"),
password: ENV.fetch("SUPPORT_PASSWORD", "support"),
},
]

if FeatureFlags::FeatureFlag.active?(:staff_test_user)
valid_credentials.push(
{
username: ENV.fetch("TEST_USERNAME", "test"),
password: ENV.fetch("TEST_PASSWORD", "test"),
},
)
end

def authenticate_support!
authenticate_or_request_with_http_basic do |username, password|
valid_credentials.include?({ username:, password: })
username == ENV.fetch("SUPPORT_USERNAME") &&
password == ENV.fetch("SUPPORT_PASSWORD")
end
end

def service_open?
Rails.env.development? || Rails.env.test? ||
FeatureFlags::FeatureFlag.active?(:service_open)
end
end
2 changes: 1 addition & 1 deletion app/controllers/staff/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Staff::OmniauthCallbacksController < Devise::OmniauthCallbacksController
skip_before_action :authenticate
skip_before_action :authenticate_support!

def azure_activedirectory_v2
auth = request.env["omniauth.auth"]
Expand Down
7 changes: 0 additions & 7 deletions config/feature_flags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ feature_flags:
author: Richard Pattinson
description: Allow users to sign in using accounts in Active Directory.

staff_test_user:
author: David Feetenby
description: >
Add extra user with access to the eligibility checker for user research.
When service_open is deactivated, and this flag is enabled, the user will
have full access to the service. Should be inactive on production.
teacher_applications:
author: Thomas Leese
description: >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require "rails_helper"

RSpec.describe AssessorInterface::UploadsController, type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

let(:staff) { create(:staff, :with_assess_permission, :confirmed) }
let(:application_form) { create(:application_form) }

Expand Down
2 changes: 0 additions & 2 deletions spec/controllers/history_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require "rails_helper"

RSpec.describe HistoryController, type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

describe "GET back" do
let(:default) { "/fallback" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require "rails_helper"

RSpec.describe TeacherInterface::AgeRangeController, type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

let(:teacher) { create(:teacher) }
let(:application_form) { create(:application_form, teacher:) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require "rails_helper"

RSpec.describe TeacherInterface::DocumentsController, type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

let(:teacher) { create(:teacher) }
let(:application_form) { create(:application_form, teacher:) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require "rails_helper"

RSpec.describe TeacherInterface::EnglishLanguageController, type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

let(:teacher) { create(:teacher) }
let(:application_form) { create(:application_form, teacher:) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

RSpec.describe TeacherInterface::PersonalInformationController,
type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

let(:teacher) { create(:teacher) }
let(:application_form) { create(:application_form, teacher:) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require "rails_helper"

RSpec.describe TeacherInterface::QualificationsController, type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

let(:teacher) { create(:teacher) }
let!(:application_form) { create(:application_form, teacher:) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

RSpec.describe TeacherInterface::RegistrationNumberController,
type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

let(:teacher) { create(:teacher) }
let(:application_form) { create(:application_form, teacher:) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require "rails_helper"

RSpec.describe TeacherInterface::SubjectsController, type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

let(:teacher) { create(:teacher) }
let(:application_form) { create(:application_form, teacher:) }

Expand Down
2 changes: 0 additions & 2 deletions spec/controllers/teacher_interface/uploads_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require "rails_helper"

RSpec.describe TeacherInterface::UploadsController, type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

let(:teacher) { create(:teacher) }
let(:application_form) { create(:application_form, teacher:) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require "rails_helper"

RSpec.describe TeacherInterface::WorkHistoriesController, type: :controller do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

let(:teacher) { create(:teacher) }
let(:application_form) { create(:application_form, teacher:) }

Expand Down
10 changes: 2 additions & 8 deletions spec/requests/staff_sign_in_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
require "rails_helper"

RSpec.describe "Staff sign in", type: :request do
before do
FeatureFlags::FeatureFlag.activate(:service_open)
FeatureFlags::FeatureFlag.activate(:sign_in_with_active_directory)
end
before { FeatureFlags::FeatureFlag.activate(:sign_in_with_active_directory) }

after do
FeatureFlags::FeatureFlag.deactivate(:service_open)
FeatureFlags::FeatureFlag.deactivate(:sign_in_with_active_directory)
end
after { FeatureFlags::FeatureFlag.deactivate(:sign_in_with_active_directory) }

shared_examples "an Azure login" do
it "redirects to Azure login" do
Expand Down
2 changes: 0 additions & 2 deletions spec/requests/throttling_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "rails_helper"

RSpec.describe "Throttling", rack_attack: true do
before { FeatureFlags::FeatureFlag.activate(:service_open) }

shared_examples "throttled" do |path|
context path do
subject(:cache_count) do
Expand Down
33 changes: 4 additions & 29 deletions spec/support/system_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
# frozen_string_literal: true

module SystemHelpers
include PageHelpers
include Warden::Test::Helpers

def given_the_service_is_open
FeatureFlags::FeatureFlag.activate(:service_open)
end

def given_the_service_is_closed
FeatureFlags::FeatureFlag.deactivate(:service_open)
end

def given_the_service_allows_teacher_applications
FeatureFlags::FeatureFlag.activate(:teacher_applications)
end

def given_an_eligible_eligibility_check(country_check:)
country = create(:country, :with_national_region, code: "GB-SCT")
country.regions.first.update!(
Expand Down Expand Up @@ -80,8 +70,8 @@ def given_i_am_authorized_as_an_assessor_user
user =
create(
:staff,
:with_assess_permission,
:confirmed,
:with_assess_permission,
name: "Authorized User",
)
given_i_am_authorized_as_a_user(user)
Expand All @@ -91,8 +81,8 @@ def given_i_am_authorized_as_an_admin_user
user =
create(
:staff,
:with_verify_permission,
:confirmed,
:with_verify_permission,
name: "Authorized User",
)
given_i_am_authorized_as_a_user(user)
Expand Down Expand Up @@ -123,27 +113,12 @@ def given_malware_scanning_is_enabled(scan_result: "No threats found")
allow(stubbed_service).to receive(:call).and_return(stubbed_response)
end

def when_i_am_authorized_as_a_test_user
page.driver.basic_authorize(
ENV.fetch("TEST_USERNAME", "test"),
ENV.fetch("TEST_PASSWORD", "test"),
)
end

def when_i_sign_out
sign_out @user
end

alias_method :then_i_sign_out, :when_i_sign_out

def given_the_test_user_is_disabled
FeatureFlags::FeatureFlag.deactivate(:staff_test_user)
end

def given_the_test_user_is_enabled
FeatureFlags::FeatureFlag.activate(:staff_test_user)
end

def when_i_choose_yes
choose "Yes", visible: false
end
Expand Down
5 changes: 1 addition & 4 deletions spec/system/assessor_interface/assigning_assessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
require "rails_helper"

RSpec.describe "Assigning an assessor", type: :system do
before do
given_the_service_is_open
given_there_is_an_application_form
end
before { given_there_is_an_application_form }

it "assigns an assessor" do
given_i_am_authorized_as_an_assessor_user
Expand Down
1 change: 0 additions & 1 deletion spec/system/assessor_interface/authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

RSpec.describe "Assessor authentication", type: :system do
it "allows signing in and signing out" do
given_the_service_is_open
given_staff_exist

when_i_visit_the(:assessor_applications_page)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
require "rails_helper"

RSpec.describe "Assessor change application form name", type: :system do
before do
given_the_service_is_open
given_there_is_an_application_form
end
before { given_there_is_an_application_form }

it "checks manage applications permission" do
given_i_am_authorized_as_a_user(assessor)
Expand Down
5 changes: 1 addition & 4 deletions spec/system/assessor_interface/change_work_history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
require "rails_helper"

RSpec.describe "Assessor change work history", type: :system do
before do
given_the_service_is_open
given_there_is_an_application_form
end
before { given_there_is_an_application_form }

it "checks manage applications permission" do
given_i_am_authorized_as_a_user(assessor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

RSpec.describe "Assessor check submitted details", type: :system do
before do
given_the_service_is_open
given_there_is_an_application_form
given_i_am_authorized_as_an_assessor_user
end
Expand Down
4 changes: 0 additions & 4 deletions spec/system/assessor_interface/completing_assessment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
end

it "award" do
given_the_service_is_open
given_i_am_authorized_as_an_assessor_user
given_there_is_an_awardable_application_form(%i[old_regs])
given_i_can_request_dqt_api
Expand Down Expand Up @@ -85,7 +84,6 @@
end

it "verify" do
given_the_service_is_open
given_i_am_authorized_as_an_assessor_user
given_there_is_an_awardable_application_form_with_work_history

Expand Down Expand Up @@ -139,7 +137,6 @@
end

it "verify with reduced evidence" do
given_the_service_is_open
given_i_am_authorized_as_an_assessor_user
given_there_is_an_awardable_application_form_with_reduced_evidence

Expand Down Expand Up @@ -179,7 +176,6 @@
end

it "decline" do
given_the_service_is_open
given_i_am_authorized_as_an_assessor_user
given_there_is_a_declinable_application_form

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

RSpec.describe "Assessor confirms English language section", type: :system do
it "exemption via citizenship in the Personal Information section" do
given_the_service_is_open
given_there_is_an_application_form
and_the_application_states_english_language_exemption_by_citizenship
given_i_am_authorized_as_an_assessor_user
Expand Down Expand Up @@ -39,7 +38,6 @@
end

it "exemption via qualification in the Qualifications section" do
given_the_service_is_open
given_there_is_an_application_form
and_the_application_states_english_language_exemption_by_qualification
given_i_am_authorized_as_an_assessor_user
Expand Down Expand Up @@ -72,7 +70,6 @@
end

it "confirmation of proficiency by SELT from approved provider" do
given_the_service_is_open
given_there_is_an_application_form
and_the_application_english_language_proof_method_is_provider
given_i_am_authorized_as_an_assessor_user
Expand All @@ -91,7 +88,6 @@
end

it "confirmation of proficiency by medium of instruction document" do
given_the_service_is_open
given_there_is_an_application_form
and_the_application_english_language_proof_method_is_moi
given_i_am_authorized_as_an_assessor_user
Expand Down
1 change: 0 additions & 1 deletion spec/system/assessor_interface/creating_note_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

RSpec.describe "Creating a note", type: :system do
it "creates a note" do
given_the_service_is_open
given_i_am_authorized_as_an_assessor_user
given_there_is_an_application_form

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
RSpec.describe "Assessor views duplicate applicant's application form",
type: :system do
it "displays information about the duplicate applicant" do
given_the_service_is_open
given_i_am_authorized_as_an_assessor_user
given_there_is_an_application_form
and_the_applicant_matches_a_record_in_dqt
Expand Down
Loading

0 comments on commit f1585b1

Please sign in to comment.