diff --git a/spec/controllers/admin/case_logs_controller_spec.rb b/spec/controllers/admin/case_logs_controller_spec.rb index 7a27443a1..c17580083 100644 --- a/spec/controllers/admin/case_logs_controller_spec.rb +++ b/spec/controllers/admin/case_logs_controller_spec.rb @@ -3,10 +3,6 @@ require_relative "../../support/devise" require_relative "../../request_helper" describe Admin::CaseLogsController, type: :controller do - before do - RequestHelper.stub_http_requests - end - render_views let(:page) { Capybara::Node::Simple.new(response.body) } let(:resource_title) { "Logs" } diff --git a/spec/controllers/admin/dashboard_controller_spec.rb b/spec/controllers/admin/dashboard_controller_spec.rb index f23255201..0273fec79 100644 --- a/spec/controllers/admin/dashboard_controller_spec.rb +++ b/spec/controllers/admin/dashboard_controller_spec.rb @@ -3,10 +3,6 @@ require_relative "../../support/devise" require_relative "../../request_helper" describe Admin::DashboardController, type: :controller do - before do - RequestHelper.stub_http_requests - end - render_views let(:page) { Capybara::Node::Simple.new(response.body) } let(:resource_title) { "Dashboard" } diff --git a/spec/features/form/accessible_autocomplete_spec.rb b/spec/features/form/accessible_autocomplete_spec.rb index ef6e5b319..74719f142 100644 --- a/spec/features/form/accessible_autocomplete_spec.rb +++ b/spec/features/form/accessible_autocomplete_spec.rb @@ -15,7 +15,6 @@ RSpec.describe "Accessible Automcomplete" do end before do - RequestHelper.stub_http_requests sign_in user end diff --git a/spec/features/form/check_answers_page_spec.rb b/spec/features/form/check_answers_page_spec.rb index c51ddfa48..535ea5f72 100644 --- a/spec/features/form/check_answers_page_spec.rb +++ b/spec/features/form/check_answers_page_spec.rb @@ -33,7 +33,6 @@ RSpec.describe "Form Check Answers Page" do let(:id) { case_log.id } before do - RequestHelper.stub_http_requests sign_in user end diff --git a/spec/features/form/conditional_questions_spec.rb b/spec/features/form/conditional_questions_spec.rb index 9cc6215f5..7a70f610b 100644 --- a/spec/features/form/conditional_questions_spec.rb +++ b/spec/features/form/conditional_questions_spec.rb @@ -16,7 +16,6 @@ RSpec.describe "Form Conditional Questions" do let(:id) { case_log.id } before do - RequestHelper.stub_http_requests sign_in user end diff --git a/spec/features/form/form_navigation_spec.rb b/spec/features/form/form_navigation_spec.rb index daef5f58a..a3b5c5742 100644 --- a/spec/features/form/form_navigation_spec.rb +++ b/spec/features/form/form_navigation_spec.rb @@ -24,7 +24,6 @@ RSpec.describe "Form Navigation" do end before do - RequestHelper.stub_http_requests sign_in user end diff --git a/spec/features/form/page_routing_spec.rb b/spec/features/form/page_routing_spec.rb index ed0bf8aa7..8a33ad389 100644 --- a/spec/features/form/page_routing_spec.rb +++ b/spec/features/form/page_routing_spec.rb @@ -17,7 +17,6 @@ RSpec.describe "Form Page Routing" do let(:validator) { case_log._validators[nil].first } before do - RequestHelper.stub_http_requests allow(validator).to receive(:validate_pregnancy).and_return(true) sign_in user end diff --git a/spec/features/form/progressive_total_field_spec.rb b/spec/features/form/progressive_total_field_spec.rb index a04b4ff10..1151a6fc1 100644 --- a/spec/features/form/progressive_total_field_spec.rb +++ b/spec/features/form/progressive_total_field_spec.rb @@ -15,7 +15,6 @@ RSpec.describe "Accessible Automcomplete" do end before do - RequestHelper.stub_http_requests sign_in user end diff --git a/spec/features/form/saving_data_spec.rb b/spec/features/form/saving_data_spec.rb index 6db932d13..0e75aae0e 100644 --- a/spec/features/form/saving_data_spec.rb +++ b/spec/features/form/saving_data_spec.rb @@ -32,7 +32,6 @@ RSpec.describe "Form Saving Data" do end before do - RequestHelper.stub_http_requests sign_in user end diff --git a/spec/features/form/validations_spec.rb b/spec/features/form/validations_spec.rb index 246f6af30..6f922951a 100644 --- a/spec/features/form/validations_spec.rb +++ b/spec/features/form/validations_spec.rb @@ -4,7 +4,6 @@ require_relative "../../request_helper" RSpec.describe "validations" do before do - RequestHelper.stub_http_requests sign_in user end diff --git a/spec/helpers/form_page_error_helper_spec.rb b/spec/helpers/form_page_error_helper_spec.rb index 2130eb787..63c321cf2 100644 --- a/spec/helpers/form_page_error_helper_spec.rb +++ b/spec/helpers/form_page_error_helper_spec.rb @@ -2,10 +2,6 @@ require "rails_helper" require_relative "../request_helper" RSpec.describe FormPageErrorHelper do - before do - RequestHelper.stub_http_requests - end - describe "#remove_other_page_errors" do context "when non base other questions are removed" do let!(:case_log) { FactoryBot.create(:case_log, :in_progress) } diff --git a/spec/helpers/tasklist_helper_spec.rb b/spec/helpers/tasklist_helper_spec.rb index 578edf7c6..c11c9fbb9 100644 --- a/spec/helpers/tasklist_helper_spec.rb +++ b/spec/helpers/tasklist_helper_spec.rb @@ -2,10 +2,6 @@ require "rails_helper" require_relative "../request_helper" RSpec.describe TasklistHelper do - before do - RequestHelper.stub_http_requests - end - let(:empty_case_log) { FactoryBot.create(:case_log) } let(:case_log) { FactoryBot.create(:case_log, :in_progress) } diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 82744db29..0b4a371ff 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -5,10 +5,6 @@ RSpec.describe CaseLog do let(:owning_organisation) { FactoryBot.create(:organisation) } let(:managing_organisation) { owning_organisation } - before do - RequestHelper.stub_http_requests - end - describe "#form" do let(:case_log) { FactoryBot.build(:case_log) } let(:case_log_2) { FactoryBot.build(:case_log, startdate: Time.zone.local(2022, 1, 1)) } diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index a1b4f011a..f2f304d10 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -2,10 +2,6 @@ require "rails_helper" require_relative "../request_helper" RSpec.describe Organisation, type: :model do - before do - RequestHelper.stub_http_requests - end - describe "#new" do let(:user) { FactoryBot.create(:user) } let(:organisation) { user.organisation } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index f24280a5c..e4c94c976 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -2,10 +2,6 @@ require "rails_helper" require_relative "../request_helper" RSpec.describe User, type: :model do - before do - RequestHelper.stub_http_requests - end - describe "#new" do let(:user) { FactoryBot.create(:user) } let(:other_organisation) { FactoryBot.create(:organisation) } diff --git a/spec/request_helper.rb b/spec/request_helper.rb index c2d30fe2d..6aa43422e 100644 --- a/spec/request_helper.rb +++ b/spec/request_helper.rb @@ -1,7 +1,5 @@ require "webmock/rspec" -WebMock.disable_net_connect!(allow_localhost: true) - module RequestHelper def self.stub_http_requests WebMock.stub_request(:get, /api.postcodes.io/) @@ -9,4 +7,8 @@ module RequestHelper WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/email/) .to_return(status: 200, body: "", headers: {}) end + + def self.real_http_requests + WebMock.allow_net_connect! + end end diff --git a/spec/requests/case_logs_controller_spec.rb b/spec/requests/case_logs_controller_spec.rb index 9fef47719..3afdbbee6 100644 --- a/spec/requests/case_logs_controller_spec.rb +++ b/spec/requests/case_logs_controller_spec.rb @@ -20,7 +20,6 @@ RSpec.describe CaseLogsController, type: :request do end before do - RequestHelper.stub_http_requests allow(ENV).to receive(:[]) allow(ENV).to receive(:[]).with("API_USER").and_return(api_username) allow(ENV).to receive(:[]).with("API_KEY").and_return(api_password) @@ -139,7 +138,6 @@ RSpec.describe CaseLogsController, type: :request do let(:headers) { { "Accept" => "text/html" } } before do - RequestHelper.stub_http_requests sign_in user get "/logs", headers: headers, params: {} end diff --git a/spec/requests/soft_validations_controller_spec.rb b/spec/requests/soft_validations_controller_spec.rb index b5ee6f5a5..1f55dea13 100644 --- a/spec/requests/soft_validations_controller_spec.rb +++ b/spec/requests/soft_validations_controller_spec.rb @@ -6,10 +6,6 @@ RSpec.describe SoftValidationsController, type: :request do let(:url) { "/logs/#{case_log.id}/net-income/soft-validations" } let(:user) { FactoryBot.create(:user) } - before do - RequestHelper.stub_http_requests - end - context "when a user is not signed in" do let(:case_log) { FactoryBot.create(:case_log, :in_progress) } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e559ad024..c50bec62f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,6 +16,7 @@ require "factory_bot" require "simplecov" +require "request_helper" SimpleCov.start "rails" do add_filter "/bin/" @@ -108,4 +109,7 @@ RSpec.configure do |config| # Kernel.srand config.seed config.include FactoryBot::Syntax::Methods + + config.before { RequestHelper.stub_http_requests } + config.after { RequestHelper.real_http_requests } end diff --git a/spec/views/case_log_index_view_spec.rb b/spec/views/case_log_index_view_spec.rb index ba12c059d..f75dfb443 100644 --- a/spec/views/case_log_index_view_spec.rb +++ b/spec/views/case_log_index_view_spec.rb @@ -2,10 +2,6 @@ require "rails_helper" require_relative "../request_helper" RSpec.describe "case_logs/index" do - before do - RequestHelper.stub_http_requests - end - let(:in_progress_log) { FactoryBot.create(:case_log, :in_progress) } let(:completed_log) { FactoryBot.create(:case_log, :completed) } diff --git a/spec/views/form/page_view_spec.rb b/spec/views/form/page_view_spec.rb index e75f650cf..5cdeb12e5 100644 --- a/spec/views/form/page_view_spec.rb +++ b/spec/views/form/page_view_spec.rb @@ -1,5 +1,4 @@ require "rails_helper" -require_relative "../../request_helper" RSpec.describe "form/page" do let(:case_log) { FactoryBot.create(:case_log, :in_progress) } @@ -19,7 +18,6 @@ RSpec.describe "form/page" do end before do - RequestHelper.stub_http_requests assign(:case_log, case_log) assign(:page, page) assign(:subsection, subsection)