|
| 1 | +$LOAD_PATH.unshift(File.dirname(__FILE__)) |
| 2 | +ENV["RAILS_ENV"] = "test" |
| 3 | + |
| 4 | +if ENV["COVERAGE"] |
| 5 | + require "simplecov" |
| 6 | + SimpleCov.start "rails" |
| 7 | + SimpleCov.command_name "Unit Tests" |
| 8 | + SimpleCov.merge_timeout 3600 |
| 9 | +end |
| 10 | + |
| 11 | +require File.expand_path("../config/environment", __dir__) |
| 12 | + |
| 13 | +require "rails/test_help" |
| 14 | +require "mocha/minitest" |
| 15 | +require "factories" |
| 16 | +require "webmock/minitest" |
| 17 | +require "govuk_schemas/assert_matchers" |
| 18 | + |
| 19 | +Dir[Rails.root.join("test/support/*.rb")].sort.each { |f| require f } |
| 20 | + |
| 21 | +Mocha.configure do |c| |
| 22 | + c.stubbing_non_existent_method = :prevent |
| 23 | +end |
| 24 | + |
| 25 | +class ActiveSupport::TestCase |
| 26 | + include FactoryBot::Syntax::Methods |
| 27 | + # include ModelHelpers |
| 28 | + # include ModelStubbingHelpers |
| 29 | + # include HtmlAssertions |
| 30 | + # include I18nHelpers |
| 31 | + include PublishingApiTestHelpers |
| 32 | + # include GovukSchemas::AssertMatchers |
| 33 | + # include UrlHelpers |
| 34 | + # extend GovspeakValidationTestHelper |
| 35 | + |
| 36 | + if ENV["RUN_IN_PARALLEL"] == "false" |
| 37 | + parallelize(workers: 1) # Run tests sequentially |
| 38 | + else |
| 39 | + parallelize(workers: :number_of_processors) # Run tests in parallel |
| 40 | + end |
| 41 | + |
| 42 | + # Fix the merging of coverage reports from parallel processes when using |
| 43 | + # Rails 6 parallelization rather than parallel_tests |
| 44 | + # from https://github.com/simplecov-ruby/simplecov/issues/718#issuecomment-538201587 |
| 45 | + if ENV["COVERAGE"] |
| 46 | + parallelize_setup do |worker| |
| 47 | + SimpleCov.command_name "#{SimpleCov.command_name}-#{worker}" |
| 48 | + end |
| 49 | + |
| 50 | + parallelize_teardown do |_worker| |
| 51 | + SimpleCov.result |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + setup do |
| 56 | + Timecop.freeze(2011, 11, 11, 11, 11, 11) |
| 57 | + stub_any_publishing_api_call |
| 58 | + stub_publishing_api_publish_intent |
| 59 | + end |
| 60 | + |
| 61 | + teardown do |
| 62 | + Timecop.return |
| 63 | + end |
| 64 | + |
| 65 | + def assert_same_elements(array1, array2) |
| 66 | + assert_equal array1.to_set, array2.to_set, "Different elements in #{array1.inspect} and #{array2}.inspect" |
| 67 | + end |
| 68 | + |
| 69 | + def assert_hash_includes(hash, should_exist) |
| 70 | + assert should_exist.to_a.all? { |e| hash.to_a.include?(e) }, "#{hash} doesn't include #{should_exist}" |
| 71 | + end |
| 72 | + |
| 73 | + def assert_all_requested(array) |
| 74 | + array.each { |request| assert_requested request } |
| 75 | + end |
| 76 | + |
| 77 | + def refute_output(regex, &block) |
| 78 | + assert_output(/^(?!#{regex}).*$/, &block) |
| 79 | + end |
| 80 | + |
| 81 | + def mock_env(partial_env_hash) |
| 82 | + old_env = ENV.to_hash |
| 83 | + ENV.update partial_env_hash |
| 84 | + begin |
| 85 | + yield |
| 86 | + ensure |
| 87 | + ENV.replace old_env |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + def routes_helper |
| 92 | + @routes_helper ||= Whitehall::UrlMaker.new |
| 93 | + end |
| 94 | + |
| 95 | + def self.disable_database_queries |
| 96 | + setup do |
| 97 | + ActiveRecord::Base |
| 98 | + .connection |
| 99 | + .stubs(:select) |
| 100 | + .raises("Database queries are disabled") |
| 101 | + end |
| 102 | + teardown do |
| 103 | + ActiveRecord::Base.connection.unstub(:select) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + def self.class_for(document_type) |
| 108 | + document_type.to_s.classify.constantize |
| 109 | + end |
| 110 | + |
| 111 | + def self.class_from_test_name |
| 112 | + name.sub(/Test$/, "").constantize |
| 113 | + end |
| 114 | + |
| 115 | + def self.factory_name_from_test |
| 116 | + name.sub(/Test$/, "").underscore.to_sym |
| 117 | + end |
| 118 | + |
| 119 | + delegate :class_from_test_name, to: :class |
| 120 | + |
| 121 | + delegate :factory_name_from_test, to: :class |
| 122 | + |
| 123 | + def file_fixture(filename) |
| 124 | + File.new(Rails.root.join("test/fixtures", filename)) |
| 125 | + end |
| 126 | + |
| 127 | + def upload_fixture(filename, mime_type = nil) |
| 128 | + Rack::Test::UploadedFile.new(Rails.root.join("test/fixtures", filename), mime_type) |
| 129 | + end |
| 130 | + |
| 131 | + def assert_file_content_identical(file1, file2) |
| 132 | + FileUtils.compare_file(file1.path, file2.path) |
| 133 | + end |
| 134 | + |
| 135 | + def publish(edition) |
| 136 | + publisher = EditionPublisher.new(edition) |
| 137 | + unless publisher.perform! |
| 138 | + raise "Could not publish edition: #{publisher.failure_reason}" |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + def force_publish(edition) |
| 143 | + publisher = EditionForcePublisher.new(edition) |
| 144 | + unless publisher.perform! |
| 145 | + raise "Could not force publish edition: #{publisher.failure_reason}" |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + def fixture_path |
| 150 | + Pathname.new(Rails.root.join("test/fixtures")) |
| 151 | + end |
| 152 | +end |
| 153 | + |
| 154 | +class ActionController::TestCase |
| 155 | + # include HtmlAssertions |
| 156 | + # include AdminControllerTestHelpers |
| 157 | + # include AdminEditionControllerTestHelpers |
| 158 | + # include AdminEditionControllerScheduledPublishingTestHelpers |
| 159 | + # include AdminEditionRolesBehaviour |
| 160 | + # include AdminEditionWorldLocationsBehaviour |
| 161 | + # include DocumentControllerTestHelpers |
| 162 | + # include ControllerTestHelpers |
| 163 | + # include ResourceTestHelpers |
| 164 | + # include AtomTestHelpers |
| 165 | + # include CacheControlTestHelpers |
| 166 | + # include ViewRendering |
| 167 | + # |
| 168 | + # include Admin::EditionRoutesHelper |
| 169 | + |
| 170 | + attr_reader :current_user |
| 171 | + |
| 172 | + setup do |
| 173 | + request.env["warden"] = stub(authenticate!: false, authenticated?: false, user: nil) |
| 174 | + |
| 175 | + # In controller tests, stub out all calls to the content store. This |
| 176 | + # implies that by default we don't care about responses from this endpoint, |
| 177 | + # which is currently only used to render specialist sector links in the |
| 178 | + # header. |
| 179 | + stub_request(:get, %r{.*content-store.*/content/.*}).to_return(status: 404) |
| 180 | + stub_publishing_api_has_linkables([], document_type: "topic") |
| 181 | + stub_request(:get, %r{\A#{Plek.find('publishing-api')}/v2/links/}).to_return(body: { links: {} }.to_json) |
| 182 | + stub_request(:get, %r{\A#{Plek.find('publishing-api')}/v2/expanded-links/}).to_return(body: { links: {} }.to_json) |
| 183 | + end |
| 184 | + |
| 185 | + def login_as(role_or_user, organisation = nil) |
| 186 | + @current_user = role_or_user.is_a?(Symbol) ? create(role_or_user, organisation:) : role_or_user |
| 187 | + request.env["warden"] = stub(authenticate!: true, authenticated?: true, user: @current_user) |
| 188 | + Current.user = @current_user |
| 189 | + @current_user |
| 190 | + end |
| 191 | + |
| 192 | + def login_as_admin |
| 193 | + login_as(create(:user, name: "admin-name", email: "admin@example.com")) |
| 194 | + end |
| 195 | + |
| 196 | + def assert_login_required |
| 197 | + assert_redirected_to login_path |
| 198 | + end |
| 199 | + |
| 200 | + def json_response |
| 201 | + ActiveSupport::JSON.decode(response.body) |
| 202 | + end |
| 203 | +end |
| 204 | + |
| 205 | +class ActionDispatch::IntegrationTest |
| 206 | + include Warden::Test::Helpers |
| 207 | + |
| 208 | + def login_as(user) |
| 209 | + GDS::SSO.test_user = user |
| 210 | + super |
| 211 | + end |
| 212 | + |
| 213 | + def login_as_admin |
| 214 | + login_as(create(:gds_admin, name: "admin-name", email: "admin@example.com")) |
| 215 | + end |
| 216 | + |
| 217 | + def logout |
| 218 | + GDS::SSO.test_user = nil |
| 219 | + super |
| 220 | + end |
| 221 | + |
| 222 | + teardown do |
| 223 | + GDS::SSO.test_user = nil |
| 224 | + end |
| 225 | +end |
| 226 | + |
| 227 | +class ActionMailer::TestCase |
| 228 | + include UrlHelpers |
| 229 | +end |
| 230 | + |
| 231 | +class ActionView::TestCase |
| 232 | + def setup_view_context |
| 233 | + @view_context = @controller.view_context |
| 234 | + end |
| 235 | +end |
0 commit comments