From abdfae3e4ab05b2c4cf0a45a0c634fec1fbf6545 Mon Sep 17 00:00:00 2001 From: Jamie Date: Fri, 22 Dec 2023 13:56:52 +0000 Subject: [PATCH] Changes to API service to get individual providers based on code --- .env.example | 1 + adr/00003-testing-practices.md | 28 +++--- .../provider_suggestions_controller.rb | 20 +++-- .../support/providers_controller.rb | 15 ++-- app/helpers/routes_helper.rb | 4 +- app/models/dfe_sign_in_user.rb | 4 +- app/models/provider.rb | 2 +- app/services/accredited_provider_api.rb | 39 ++++++-- app/services/gias_csv_importer.rb | 2 +- .../support/providers/check.html.erb | 26 +++--- bin/bundle | 2 +- config/application.rb | 2 +- config/environments/development.rb | 4 +- config/environments/test.rb | 2 +- config/initializers/better_html.rb | 2 +- config/initializers/omniauth.rb | 2 +- config/initializers/persona.rb | 6 +- config/routes/placements.rb | 2 +- lib/hosting_environment.rb | 2 +- lib/tasks/auto_annotate_models.rake | 2 +- spec/decorators/persona_decorator_spec.rb | 2 +- .../sign_in_as_a_claims_user_persona_spec.rb | 8 +- ...gn_in_as_a_placements_user_persona_spec.rb | 8 +- spec/lib/hosting_environment_spec.rb | 4 +- spec/models/dfe_sign_in_user_spec.rb | 30 +++---- spec/models/provider_spec.rb | 2 +- spec/models/school_spec.rb | 2 +- spec/models/service_update_spec.rb | 14 +-- spec/models/user_spec.rb | 2 +- spec/requests/sessions_spec.rb | 2 +- spec/services/accredited_provider_api_spec.rb | 89 ++++++++++++++----- spec/services/gias_csv_importer_spec.rb | 4 +- spec/support/capybara.rb | 16 ++-- .../support_user_adds_a_provider_spec.rb | 38 ++++++-- spec/tasks/gias_update_spec.rb | 2 +- spec/views/erb_safety_check_spec.rb | 2 +- 36 files changed, 240 insertions(+), 152 deletions(-) diff --git a/.env.example b/.env.example index 8ef4c87bb2..a327598ef3 100644 --- a/.env.example +++ b/.env.example @@ -5,3 +5,4 @@ SIGN_IN_METHOD=persona PLACEMENTS_HOST=placements.localhost CLAIMS_HOST=claims.localhost GIAS_CSV_BASE_URL=https://ea-edubase-api-prod.azurewebsites.net/edubase/downloads/public +PUBLISH_BASE_URL=https://www.publish-teacher-training-courses.service.gov.uk diff --git a/adr/00003-testing-practices.md b/adr/00003-testing-practices.md index 35820fb81c..9784f8cae4 100644 --- a/adr/00003-testing-practices.md +++ b/adr/00003-testing-practices.md @@ -19,13 +19,13 @@ Use the following test types for our classes, modules, and functionalities. ```ruby RSpec.describe User, type: :model do subject { build(:user) } - + describe ".class_method" do it "does something" do expect(User.class_method).to do_something end end - + describe "#instance_method" do it "does something" do expect(subject.instance_method).to do_something @@ -41,7 +41,7 @@ Use the following test types for our classes, modules, and functionalities. expect(User.class_method).to do_something end end - + describe "#instance_method" do it "does something" do expect(subject.instance_method).to do_something @@ -59,11 +59,11 @@ Use the following test types for our classes, modules, and functionalities. given_i_am_on_the_landing_page i_can_see_something end - + def given_i_am_on_the_landing_page visit "/" end - + def i_can_see_something expect(page).to have_content("Something") end @@ -81,25 +81,19 @@ Use the following test types for our classes, modules, and functionalities. # Base happy path first it "returns a list of users" do get :index - - expected_json = [ - { - id: 1, - first_name: "John", - last_name: "Doe" - } - ] - + + expected_json = [{ id: 1, first_name: "John", last_name: "Doe" }] + expect(response.body).to eq(expected_json) end - + # Happy path variants next context "when given a 'name' query parameter" do it "returns a list of users filtered by name" do # Assertion end end - + # Error paths context "without authentication" do it "returns a 401 error" do @@ -107,7 +101,7 @@ Use the following test types for our classes, modules, and functionalities. end end end - + context "POST /users" do it "returns a list of users" do get :index diff --git a/app/controllers/placements/support/provider_suggestions_controller.rb b/app/controllers/placements/support/provider_suggestions_controller.rb index 5e92442495..f7fd56032b 100644 --- a/app/controllers/placements/support/provider_suggestions_controller.rb +++ b/app/controllers/placements/support/provider_suggestions_controller.rb @@ -1,7 +1,7 @@ class Placements::Support::ProviderSuggestionsController < Placements::Support::ApplicationController def index accredited_providers = AccreditedProviderApi.call - filtered_providers = filter_providers(accredited_providers, params[:query]) + filtered_providers = filter_providers(accredited_providers, query_params) providers = filtered_providers.map { |provider| formatted_provider(provider) } render json: providers @@ -9,25 +9,29 @@ def index private + def query_params + params.require(:query) + end + def filter_providers(providers, query) return providers if query.blank? downcase_query = query.downcase providers.select do |provider| [ - provider["attributes"]["name"], - provider["attributes"]["postcode"], - provider["attributes"]["urn"], - provider["attributes"]["ukprn"] + provider.dig("attributes", "name"), + provider.dig("attributes", "postcode"), + provider.dig("attributes", "urn"), + provider.dig("attributes", "ukprn"), ].any? { |attribute| attribute&.downcase&.include?(downcase_query) } end end def formatted_provider(provider) { - id: provider["id"], - name: provider["attributes"]["name"], - code: provider["attributes"]["code"] + id: provider.fetch("id"), + name: provider.dig("attributes", "name"), + code: provider.dig("attributes", "code"), } end end diff --git a/app/controllers/placements/support/providers_controller.rb b/app/controllers/placements/support/providers_controller.rb index 04a0d6f310..2fc5ffc170 100644 --- a/app/controllers/placements/support/providers_controller.rb +++ b/app/controllers/placements/support/providers_controller.rb @@ -4,7 +4,7 @@ def new end def create - @provider = Provider.find_or_initialize_by(provider_params) + @provider = Provider.new(provider_params) if @provider.save redirect_to placements_support_organisations_path else @@ -14,15 +14,10 @@ def create def check @provider = Provider.new(provider_code: params[:accredited_provider_id]) - if @provider.valid? - accredited_providers = AccreditedProviderApi.call - @selected_provider = - accredited_providers.find do |provider| - provider["attributes"]["code"] == params[:accredited_provider_id] - end - else - render :new - end + @provider_details = AccreditedProviderApi.call(@provider.provider_code) + return if @provider.valid? && @provider_details.present? + + render :new end private diff --git a/app/helpers/routes_helper.rb b/app/helpers/routes_helper.rb index f916d14868..2d7db43836 100644 --- a/app/helpers/routes_helper.rb +++ b/app/helpers/routes_helper.rb @@ -2,14 +2,14 @@ module RoutesHelper def root_path { claims: claims_root_path, - placements: placements_root_path + placements: placements_root_path, }.fetch current_service end def support_root_path { claims: root_path, # TODO: claims support path in another PR - placements: placements_support_root_path + placements: placements_support_root_path, }.fetch current_service end end diff --git a/app/models/dfe_sign_in_user.rb b/app/models/dfe_sign_in_user.rb index 4ae7efd6b1..f46cef7c89 100644 --- a/app/models/dfe_sign_in_user.rb +++ b/app/models/dfe_sign_in_user.rb @@ -21,7 +21,7 @@ def self.begin_session!(session, omniauth_payload) "email" => omniauth_payload["info"]["email"], # "dfe_sign_in_uid" => omniauth_payload["uid"], "first_name" => omniauth_payload["info"]["first_name"], - "last_name" => omniauth_payload["info"]["last_name"] + "last_name" => omniauth_payload["info"]["last_name"], # "last_active_at" => Time.zone.now, # "id_token" => omniauth_payload["credentials"]["id_token"], # "provider" => omniauth_payload["provider"], @@ -39,7 +39,7 @@ def self.load_from_session(session) last_name: dfe_sign_in_session["last_name"], # id_token: dfe_sign_in_session["id_token"], # provider: dfe_sign_in_session["provider"], - service: session["service"] + service: session["service"], ) end diff --git a/app/models/provider.rb b/app/models/provider.rb index 3c05770053..791afa3bd7 100644 --- a/app/models/provider.rb +++ b/app/models/provider.rb @@ -16,6 +16,6 @@ class Provider < ApplicationRecord validates :provider_code, uniqueness: { case_sensitive: false, - message: "Provider already exists!" + message: "Provider already exists!", } end diff --git a/app/services/accredited_provider_api.rb b/app/services/accredited_provider_api.rb index 998f895d04..2ea9d733c3 100644 --- a/app/services/accredited_provider_api.rb +++ b/app/services/accredited_provider_api.rb @@ -1,20 +1,47 @@ class AccreditedProviderApi include ServicePattern + def initialize(code = nil) + @code = code + end + + attr_reader :code + def call + code.present? ? provider_list(code) : provider_details + end + + private + + def provider_list(code) Rails .cache - .fetch("accredited_provider_api", expires_in: 24.hours) do - response = HTTParty.get(provider_url) + .fetch("accredited_provider_details_#{code}", expires_in: 24.hours) do + response = HTTParty.get(provider_details_url(code)) response = JSON.parse(response.to_s) response["data"] end end - private + def provider_details + Rails + .cache + .fetch("all_accredited_providers", expires_in: 24.hours) do + response = HTTParty.get(all_providers_url) + response = JSON.parse(response.to_s) + response["data"] + end + end + + def all_providers_url + "#{ENV["PUBLISH_BASE_URL"]}/api/public/v1/recruitment_cycles/#{next_year}/providers?filter[is_accredited_body]=true" + end + + def provider_details_url(code) + "#{ENV["PUBLISH_BASE_URL"]}/api/public/v1/recruitment_cycles/#{next_year}/providers/#{code}" + end - def provider_url - year = Time.current.next_year.year - "https://www.publish-teacher-training-courses.service.gov.uk/api/public/v1/recruitment_cycles/#{year}/providers?filter[is_accredited_body]=true" + def next_year + Time.current.next_year.year end end diff --git a/app/services/gias_csv_importer.rb b/app/services/gias_csv_importer.rb index 439057049e..ac2a4fb226 100644 --- a/app/services/gias_csv_importer.rb +++ b/app/services/gias_csv_importer.rb @@ -32,7 +32,7 @@ def call address2: school["Locality"].presence, address3: school["Address3"].presence, website: school["SchoolWebsite"].presence, - telephone: school["TelephoneNum"].presence + telephone: school["TelephoneNum"].presence, } end diff --git a/app/views/placements/support/providers/check.html.erb b/app/views/placements/support/providers/check.html.erb index ceaf2489bc..046ece39b3 100644 --- a/app/views/placements/support/providers/check.html.erb +++ b/app/views/placements/support/providers/check.html.erb @@ -5,7 +5,7 @@
<%= form_with(model: @provider, url: placements_support_providers_path, method: "post", data: {turbo: false}) do |f| %> - <%= f.hidden_field :provider_code, value: @selected_provider["attributes"]["code"] %> + <%= f.hidden_field :provider_code, value: @provider_details.dig("attributes", "code") %>
@@ -39,7 +39,7 @@ <%= t(".accredited_provider_id") %>
- <%= @selected_provider["attributes"]["accredited_provider_id"] %> + <%= @provider_details.dig("attributes", "accredited_provider_id") %>
@@ -51,7 +51,7 @@ <%= t(".email") %>
- <%= @selected_provider["attributes"]["email"] %> + <%= @provider_details.dig("attributes", "email") %>
@@ -60,7 +60,7 @@ <%= t(".telephone") %>
- <%= @selected_provider["attributes"]["telephone"] %> + <%= @provider_details.dig("attributes", "telephone") %>
@@ -69,7 +69,7 @@ <%= t(".website") %>
- <%= @selected_provider["attributes"]["website"] %> + <%= @provider_details.dig("attributes", "website") %>
@@ -79,17 +79,17 @@

- <%= @selected_provider["attributes"]["street_address_1"] %> + <%= @provider_details.dig("attributes", "street_address_1") %>
- <%= @selected_provider["attributes"]["street_address_2"] %> + <%= @provider_details.dig("attributes", "street_address_2") %>
- <%= @selected_provider["attributes"]["street_address_3"] %> + <%= @provider_details.dig("attributes", "street_address_3") %>
- <%= @selected_provider["attributes"]["city"] %> + <%= @provider_details.dig("attributes", "city") %>
- <%= @selected_provider["attributes"]["county"] %> + <%= @provider_details.dig("attributes", "county") %>
- <%= @selected_provider["attributes"]["postcode"] %> + <%= @provider_details.dig("attributes", "postcode") %>

diff --git a/bin/bundle b/bin/bundle index e7b4ab397f..f762277c87 100755 --- a/bin/bundle +++ b/bin/bundle @@ -102,7 +102,7 @@ m = require_error = activation_error_handling { require "bundler/version" } if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?( - Gem::Version.new(Bundler::VERSION) + Gem::Version.new(Bundler::VERSION), ) return end diff --git a/config/application.rb b/config/application.rb index 2aeb8a349f..700c90e186 100644 --- a/config/application.rb +++ b/config/application.rb @@ -33,7 +33,7 @@ class Application < Rails::Application config.autoload_lib(ignore: %w[]) config.assets.paths << Rails.root.join( - "node_modules/govuk-frontend/dist/govuk/assets" + "node_modules/govuk-frontend/dist/govuk/assets", ) config.autoload_paths += %W[#{config.root}/app/assets/components] diff --git a/config/environments/development.rb b/config/environments/development.rb index 735c2d6306..fb54819e24 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -25,7 +25,7 @@ config.cache_store = :memory_store config.public_file_server.headers = { - "Cache-Control" => "public, max-age=#{2.days.to_i}" + "Cache-Control" => "public, max-age=#{2.days.to_i}", } else config.action_controller.perform_caching = false @@ -35,7 +35,7 @@ config.action_mailer.delivery_method = :notify config.action_mailer.notify_settings = { - api_key: ENV.fetch("GOVUK_NOTIFY_API_KEY") + api_key: ENV.fetch("GOVUK_NOTIFY_API_KEY"), } # Store uploaded files on the local file system (see config/storage.yml for options). diff --git a/config/environments/test.rb b/config/environments/test.rb index b8cfb2842b..aad4bd68ab 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -19,7 +19,7 @@ # Configure public file server for tests with Cache-Control for performance. config.public_file_server.enabled = true config.public_file_server.headers = { - "Cache-Control" => "public, max-age=#{1.hour.to_i}" + "Cache-Control" => "public, max-age=#{1.hour.to_i}", } # Show full error reports and disable caching. diff --git a/config/initializers/better_html.rb b/config/initializers/better_html.rb index 957f6f86a5..37d889823b 100644 --- a/config/initializers/better_html.rb +++ b/config/initializers/better_html.rb @@ -3,7 +3,7 @@ BetterHtml.config = BetterHtml::Config.new( - YAML.safe_load(File.read(Rails.root.join(".better-html.yml"))) + YAML.safe_load(File.read(Rails.root.join(".better-html.yml"))), ) BetterHtml.configure do |config| diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index 433415332b..1899588c75 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -9,7 +9,7 @@ provider( :developer, fields: %i[uid email first_name last_name], - uid_field: :uid + uid_field: :uid, ) end when "dfe-sign-in" diff --git a/config/initializers/persona.rb b/config/initializers/persona.rb index c60e4af2e0..d4833c9aa8 100644 --- a/config/initializers/persona.rb +++ b/config/initializers/persona.rb @@ -6,15 +6,15 @@ { first_name: "Patricia", last_name: "Adebayo", - email: "patricia@example.com" + email: "patricia@example.com", }, { first_name: "Mary", last_name: "Lawson", email: "mary@example.com" }, { first_name: "Colin", last_name: "Chapman", email: "colin@example.com", - support_user: true - } + support_user: true, + }, ].push((DEVELOPER_PERSONA if defined?(DEVELOPER_PERSONA))).compact.freeze PERSONA_EMAILS = PERSONAS.map { |persona| persona[:email] } diff --git a/config/routes/placements.rb b/config/routes/placements.rb index 91b6d1c6cc..4690a0a36b 100644 --- a/config/routes/placements.rb +++ b/config/routes/placements.rb @@ -1,7 +1,7 @@ scope module: :placements, as: :placements, constraints: { - host: ENV["PLACEMENTS_HOST"] + host: ENV["PLACEMENTS_HOST"], } do root to: "pages#index" diff --git a/lib/hosting_environment.rb b/lib/hosting_environment.rb index bcd5d541c8..05b845b63e 100644 --- a/lib/hosting_environment.rb +++ b/lib/hosting_environment.rb @@ -1,7 +1,7 @@ module HostingEnvironment PRODUCTION_BANNER_NAME = { claims: "beta", - placements: "beta" + placements: "beta", }.with_indifferent_access.freeze def self.name(current_service) diff --git a/lib/tasks/auto_annotate_models.rake b/lib/tasks/auto_annotate_models.rake index 6d2fa9fea7..43a5ccb407 100644 --- a/lib/tasks/auto_annotate_models.rake +++ b/lib/tasks/auto_annotate_models.rake @@ -52,7 +52,7 @@ if Rails.env.development? "trace" => "false", "wrapper_open" => nil, "wrapper_close" => nil, - "with_comment" => "true" + "with_comment" => "true", ) end diff --git a/spec/decorators/persona_decorator_spec.rb b/spec/decorators/persona_decorator_spec.rb index 7f8b645646..3e69318723 100644 --- a/spec/decorators/persona_decorator_spec.rb +++ b/spec/decorators/persona_decorator_spec.rb @@ -11,7 +11,7 @@ context "when the persona is Patricia" do it "returns orange" do expect(build(:persona, :patricia).decorate.type_tag_colour).to eq( - "orange" + "orange", ) end end diff --git a/spec/features/personas/sign_in_as_a_claims_user_persona_spec.rb b/spec/features/personas/sign_in_as_a_claims_user_persona_spec.rb index 926cbf3cc5..b8626302bf 100644 --- a/spec/features/personas/sign_in_as_a_claims_user_persona_spec.rb +++ b/spec/features/personas/sign_in_as_a_claims_user_persona_spec.rb @@ -70,7 +70,7 @@ def then_i_see_persona_details_for_anne page_has_persona_content( first_name: "Anne", last_name: "Wilson", - email: "anne_wilson@example.org" + email: "anne_wilson@example.org", ) end @@ -78,7 +78,7 @@ def then_i_see_persona_details_for_patricia page_has_persona_content( first_name: "Patricia", last_name: "Adebayo", - email: "patricia@example.com" + email: "patricia@example.com", ) end @@ -86,7 +86,7 @@ def then_i_see_persona_details_for_mary page_has_persona_content( first_name: "Mary", last_name: "Lawson", - email: "mary@example.com" + email: "mary@example.com", ) end @@ -94,7 +94,7 @@ def then_i_see_persona_details_for_colin page_has_persona_content( first_name: "Colin", last_name: "Chapman", - email: "colin@example.com" + email: "colin@example.com", ) end diff --git a/spec/features/personas/sign_in_as_a_placements_user_persona_spec.rb b/spec/features/personas/sign_in_as_a_placements_user_persona_spec.rb index 0606f6cc91..069e7a5a00 100644 --- a/spec/features/personas/sign_in_as_a_placements_user_persona_spec.rb +++ b/spec/features/personas/sign_in_as_a_placements_user_persona_spec.rb @@ -85,7 +85,7 @@ def then_i_see_persona_details_for_anne page_has_persona_content( first_name: "Anne", last_name: "Wilson", - email: "anne_wilson@example.org" + email: "anne_wilson@example.org", ) end @@ -93,7 +93,7 @@ def then_i_see_persona_details_for_patricia page_has_persona_content( first_name: "Patricia", last_name: "Adebayo", - email: "patricia@example.com" + email: "patricia@example.com", ) end @@ -101,7 +101,7 @@ def then_i_see_persona_details_for_mary page_has_persona_content( first_name: "Mary", last_name: "Lawson", - email: "mary@example.com" + email: "mary@example.com", ) end @@ -109,7 +109,7 @@ def then_i_see_persona_details_for_colin page_has_persona_content( first_name: "Colin", last_name: "Chapman", - email: "colin@example.com" + email: "colin@example.com", ) end diff --git a/spec/lib/hosting_environment_spec.rb b/spec/lib/hosting_environment_spec.rb index bd8c8490a9..4f1f767e0b 100644 --- a/spec/lib/hosting_environment_spec.rb +++ b/spec/lib/hosting_environment_spec.rb @@ -26,14 +26,14 @@ it "returns the banner description of the hosting environment for claims" do current_service = "claims" expect(described_class.banner_description(current_service)).to eq( - "Make a complaint or give feedback" + "Make a complaint or give feedback", ) end it "returns the banner description of the hosting environment for placements" do current_service = "placements" expect(described_class.banner_description(current_service)).to eq( - "Make a complaint or give feedback" + "Make a complaint or give feedback", ) end end diff --git a/spec/models/dfe_sign_in_user_spec.rb b/spec/models/dfe_sign_in_user_spec.rb index 536e62db2f..1f56e2cecc 100644 --- a/spec/models/dfe_sign_in_user_spec.rb +++ b/spec/models/dfe_sign_in_user_spec.rb @@ -8,8 +8,8 @@ "info" => { "first_name" => "Example", "last_name" => "User", - "email" => "example_user@example.com" - } + "email" => "example_user@example.com", + }, } DfESignInUser.begin_session!(session, omniauth_payload) @@ -18,9 +18,9 @@ "dfe_sign_in_user" => { "first_name" => "Example", "last_name" => "User", - "email" => "example_user@example.com" - } - } + "email" => "example_user@example.com", + }, + }, ) end end @@ -31,9 +31,9 @@ "dfe_sign_in_user" => { "first_name" => "Example", "last_name" => "User", - "email" => "example_user@example.com" + "email" => "example_user@example.com", }, - "service" => :placements + "service" => :placements, } dfe_sign_in_user = DfESignInUser.load_from_session(session) @@ -53,9 +53,9 @@ "dfe_sign_in_user" => { "first_name" => claims_user.first_name, "last_name" => claims_user.last_name, - "email" => claims_user.email + "email" => claims_user.email, }, - "service" => :claims + "service" => :claims, } dfe_sign_in_user = DfESignInUser.load_from_session(session) @@ -73,9 +73,9 @@ "dfe_sign_in_user" => { "first_name" => placements_user.first_name, "last_name" => placements_user.last_name, - "email" => placements_user.email + "email" => placements_user.email, }, - "service" => :placements + "service" => :placements, } dfe_sign_in_user = DfESignInUser.load_from_session(session) @@ -91,9 +91,9 @@ "dfe_sign_in_user" => { "first_name" => support_user.first_name, "last_name" => support_user.last_name, - "email" => support_user.email + "email" => support_user.email, }, - "service" => :placements + "service" => :placements, } dfe_sign_in_user = DfESignInUser.load_from_session(session) @@ -110,8 +110,8 @@ "dfe_sign_in_user" => { "first_name" => "Example", "last_name" => "User", - "email" => "example_user@example.com" - } + "email" => "example_user@example.com", + }, } DfESignInUser.end_session!(session) diff --git a/spec/models/provider_spec.rb b/spec/models/provider_spec.rb index 50015ec1bd..608660e042 100644 --- a/spec/models/provider_spec.rb +++ b/spec/models/provider_spec.rb @@ -20,7 +20,7 @@ it { is_expected.to validate_presence_of(:provider_code) } it do is_expected.to validate_uniqueness_of( - :provider_code + :provider_code, ).case_insensitive.with_message("Provider already exists!") end end diff --git a/spec/models/school_spec.rb b/spec/models/school_spec.rb index 5fe6ff44cd..6cde162974 100644 --- a/spec/models/school_spec.rb +++ b/spec/models/school_spec.rb @@ -21,7 +21,7 @@ context "associations" do it do should belong_to(:gias_school).with_foreign_key(:urn).with_primary_key( - :urn + :urn, ) end end diff --git a/spec/models/service_update_spec.rb b/spec/models/service_update_spec.rb index d90c94c54a..929c580a6d 100644 --- a/spec/models/service_update_spec.rb +++ b/spec/models/service_update_spec.rb @@ -11,9 +11,9 @@ { date: "2023-12-14", title: "Claim Update", - content: "Some content" - } - ] + content: "Some content", + }, + ], ) updates = ServiceUpdate.where(service: :claims) expect(updates.length).to eq(1) @@ -34,9 +34,9 @@ { date: "2023-12-14", title: "Placement Update", - content: "Some content" - } - ] + content: "Some content", + }, + ], ) updates = ServiceUpdate.where(service: :placements) expect(updates.length).to eq(1) @@ -60,7 +60,7 @@ it "returns placements YAML file path" do file_path = ServiceUpdate.file_path(service: :placements) expect(file_path).to eq( - Rails.root.join("db/placements_service_updates.yml") + Rails.root.join("db/placements_service_updates.yml"), ) end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d0933156c4..e77cc1e008 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -24,7 +24,7 @@ it { is_expected.to validate_presence_of(:email) } it do is_expected.to validate_uniqueness_of(:email).scoped_to( - :service + :service, ).case_insensitive end it { is_expected.to validate_presence_of(:first_name) } diff --git a/spec/requests/sessions_spec.rb b/spec/requests/sessions_spec.rb index 4d85561a6d..782c21c418 100644 --- a/spec/requests/sessions_spec.rb +++ b/spec/requests/sessions_spec.rb @@ -14,7 +14,7 @@ params: { first_name: placements_user.first_name, last_name: placements_user.last_name, - email: placements_user.email + email: placements_user.email, } follow_redirect! diff --git a/spec/services/accredited_provider_api_spec.rb b/spec/services/accredited_provider_api_spec.rb index 1fc4e86723..0a28c526e0 100644 --- a/spec/services/accredited_provider_api_spec.rb +++ b/spec/services/accredited_provider_api_spec.rb @@ -1,12 +1,12 @@ require "rails_helper" RSpec.describe AccreditedProviderApi do - subject { described_class.call } - before do + next_year = Time.current.next_year.year + stub_request( :get, - "https://www.publish-teacher-training-courses.service.gov.uk/api/public/v1/recruitment_cycles/2024/providers?filter%5Bis_accredited_body%5D=true" + "https://www.publish-teacher-training-courses.service.gov.uk/api/public/v1/recruitment_cycles/2024/providers?filter%5Bis_accredited_body%5D=true", ).to_return( status: 200, body: { @@ -15,34 +15,77 @@ "id" => 123, "attributes" => { name: "Provider 1", - code: "Prov1" - } + code: "Prov1", + }, + }, + { + "id" => 234, + "attributes" => { + name: "Provider 2", + code: "Prov2", + }, + }, + ], + }.to_json, + ) + + stub_request( + :get, + "https://www.publish-teacher-training-courses.service.gov.uk/api/public/v1/recruitment_cycles/#{next_year}/providers/Prov1", + ).to_return( + status: 200, + body: { + "data" => { + "id" => 123, + "attributes" => { + name: "Provider 1", + code: "Prov1", }, - { "id" => 234, "attributes" => { name: "Provider 2", code: "Prov2" } } - ] - }.to_json + }, + }.to_json, ) end - it "returns provider data from the publish-teacher-training-courses api" do - response = subject - expect(response).to match_array( - [ + context "when not given a code" do + subject { described_class.call } + + it "returns a list of providers from the publish-teacher-training-courses api" do + response = subject + expect(response).to match_array( + [ + { + "id" => 123, + "attributes" => { + "name" => "Provider 1", + "code" => "Prov1", + }, + }, + { + "id" => 234, + "attributes" => { + "name" => "Provider 2", + "code" => "Prov2", + }, + }, + ], + ) + end + end + + context "when given a code" do + subject { described_class.call("Prov1") } + + it "returns details for a provider matching that code" do + response = subject + expect(response).to eq( { "id" => 123, "attributes" => { "name" => "Provider 1", - "code" => "Prov1" - } + "code" => "Prov1", + }, }, - { - "id" => 234, - "attributes" => { - "name" => "Provider 2", - "code" => "Prov2" - } - } - ] - ) + ) + end end end diff --git a/spec/services/gias_csv_importer_spec.rb b/spec/services/gias_csv_importer_spec.rb index 73d3e579e3..59894b0f18 100644 --- a/spec/services/gias_csv_importer_spec.rb +++ b/spec/services/gias_csv_importer_spec.rb @@ -15,8 +15,8 @@ it "logs messages to STDOUT" do expect { subject }.to output( match(/Done!/).and(match(/Invalid rows - /)).and( - match(/Row 5 is invalid/) - ) + match(/Row 5 is invalid/), + ), ).to_stdout end end diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index 42db0a4e70..489aff4cf0 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -1,8 +1,8 @@ -require 'capybara/rspec' +require "capybara/rspec" # Use different Capybara ports when running tests in parallel -if ENV['TEST_ENV_NUMBER'] - Capybara.server_port = 9887 + ENV['TEST_ENV_NUMBER'].to_i +if ENV["TEST_ENV_NUMBER"] + Capybara.server_port = 9887 + ENV["TEST_ENV_NUMBER"].to_i end Capybara.always_include_port = true @@ -10,11 +10,11 @@ Capybara.register_driver :chrome_headless do |app| options = Selenium::WebDriver::Chrome::Options.new - options.add_argument('--headless') unless ENV['HEADLESS'] == 'false' - options.add_argument('--no-sandbox') - options.add_argument('--disable-dev-shm-usage') - options.add_argument('--disable-gpu') - options.add_argument('--window-size=1400,1400') + options.add_argument("--headless") unless ENV["HEADLESS"] == "false" + options.add_argument("--no-sandbox") + options.add_argument("--disable-dev-shm-usage") + options.add_argument("--disable-gpu") + options.add_argument("--window-size=1400,1400") Capybara::Selenium::Driver.new(app, browser: :chrome, options:) end diff --git a/spec/system/placements/providers/support_user_adds_a_provider_spec.rb b/spec/system/placements/providers/support_user_adds_a_provider_spec.rb index c8f4653616..d20689732d 100644 --- a/spec/system/placements/providers/support_user_adds_a_provider_spec.rb +++ b/spec/system/placements/providers/support_user_adds_a_provider_spec.rb @@ -2,9 +2,11 @@ RSpec.feature "Placements / Providers / Support User adds a Provider" do before do + next_year = Time.current.next_year.year + stub_request( :get, - "https://www.publish-teacher-training-courses.service.gov.uk/api/public/v1/recruitment_cycles/2024/providers?filter%5Bis_accredited_body%5D=true" + "https://www.publish-teacher-training-courses.service.gov.uk/api/public/v1/recruitment_cycles/#{next_year}/providers?filter%5Bis_accredited_body%5D=true", ).to_return( status: 200, body: { @@ -13,12 +15,34 @@ "id" => 123, "attributes" => { name: "Provider 1", - code: "Prov1" - } + code: "Prov1", + }, + }, + { + "id" => 234, + "attributes" => { + name: "Provider 2", + code: "Prov2", + }, + }, + ], + }.to_json, + ) + + stub_request( + :get, + "https://www.publish-teacher-training-courses.service.gov.uk/api/public/v1/recruitment_cycles/#{next_year}/providers/Prov1", + ).to_return( + status: 200, + body: { + "data" => { + "id" => 123, + "attributes" => { + name: "Provider 1", + code: "Prov1", }, - { "id" => 234, "attributes" => { name: "Provider 2", code: "Prov2" } } - ] - }.to_json + }, + }.to_json, ) given_i_sign_in_as_colin @@ -113,7 +137,7 @@ def given_a_provider_already_exists(code:) def then_i_see_an_error(error_message) expect(page.find(".govuk-error-summary")).to have_content( - "There is a problem" + "There is a problem", ) expect(page.find(".govuk-error-summary")).to have_content(error_message) end diff --git a/spec/tasks/gias_update_spec.rb b/spec/tasks/gias_update_spec.rb index 84df49cbfa..c687653787 100644 --- a/spec/tasks/gias_update_spec.rb +++ b/spec/tasks/gias_update_spec.rb @@ -11,7 +11,7 @@ tempfile = Tempfile.new("foo") expect(Down).to receive(:download).with( - "#{ENV["GIAS_CSV_BASE_URL"]}/#{gias_filename}" + "#{ENV["GIAS_CSV_BASE_URL"]}/#{gias_filename}", ).and_return(tempfile) expect(GiasCsvImporter).to receive(:call).with(tempfile.path) diff --git a/spec/views/erb_safety_check_spec.rb b/spec/views/erb_safety_check_spec.rb index caf14de7a3..7300a7f0f4 100644 --- a/spec/views/erb_safety_check_spec.rb +++ b/spec/views/erb_safety_check_spec.rb @@ -7,7 +7,7 @@ erb_glob = Rails.root.join( - "app/views/**/{*.htm,*.html,*.htm.erb,*.html.erb,*.html+*.erb}" + "app/views/**/{*.htm,*.html,*.htm.erb,*.html.erb,*.html+*.erb}", ) Dir[erb_glob].each do |filename|