-
Notifications
You must be signed in to change notification settings - Fork 121
/
spec_helper.rb
354 lines (292 loc) · 12.3 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# frozen_string_literal: true
$VERBOSE = nil unless ENV['RUBY_LOUD'] # silence loud Ruby 2.7 deprecations
ENV['RAILS_ENV'] = 'test'
ENV['DATABASE_URL'] = ENV['DATABASE_TEST_URL'] if ENV['DATABASE_TEST_URL']
# Analytics is turned off by default
ENV['HYRAX_ANALYTICS'] = 'false'
require "bundler/setup"
def ci_build?
ENV['CI']
end
require 'simplecov'
SimpleCov.root(File.expand_path('../..', __FILE__))
SimpleCov.start('rails') do
add_filter '/.internal_test_app'
add_filter '/lib/generators'
add_filter '/spec'
add_filter '/tasks'
add_filter '/lib/hyrax/version.rb'
add_filter '/lib/hyrax/engine.rb'
end
require 'factory_bot'
if ENV['IN_DOCKER']
require File.expand_path("config/environment", '../hyrax-webapp')
db_config = ActiveRecord::Base.configurations[ENV['RAILS_ENV']]
ActiveRecord::Tasks::DatabaseTasks.create(db_config)
ActiveRecord::Migrator.migrations_paths = [Pathname.new(ENV['RAILS_ROOT']).join('db', 'migrate').to_s]
ActiveRecord::Tasks::DatabaseTasks.migrate
ActiveRecord::Base.descendants.each(&:reset_column_information)
else
require 'engine_cart'
EngineCart.load_application!
end
ActiveRecord::Migration.maintain_test_schema!
require 'active_fedora/cleaner'
require 'devise'
require 'devise/version'
require 'mida'
require 'rails-controller-testing'
require 'rspec/rails'
require 'rspec/its'
require 'rspec/matchers'
require 'rspec/active_model/mocks'
require 'equivalent-xml'
require 'equivalent-xml/rspec_matchers'
require 'database_cleaner'
require 'hyrax/specs/capybara'
require 'hyrax/specs/clamav'
require 'hyrax/specs/engine_routes'
# ensure Hyrax::Schema gets loaded is resolvable for `support/` models
Hyrax::Schema # rubocop:disable Lint/Void
Valkyrie::MetadataAdapter
.register(Valkyrie::Persistence::Memory::MetadataAdapter.new, :test_adapter)
Valkyrie::MetadataAdapter
.register(Valkyrie::Persistence::Postgres::MetadataAdapter.new, :postgres_adapter)
Valkyrie::StorageAdapter.register(
Valkyrie::Storage::Disk.new(base_path: Rails.root / 'tmp' / 'test_adapter_uploads'),
:test_disk
)
# Require supporting ruby files from spec/support/ and subdirectories. Note: engine, not Rails.root context.
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f }
require 'webmock/rspec'
allowed_hosts = %w[chrome chromedriver.storage.googleapis.com fcrepo solr]
WebMock.disable_net_connect!(allow_localhost: true, allow: allowed_hosts)
require 'i18n/debug' if ENV['I18N_DEBUG']
require 'byebug' unless ci_build?
require 'hyrax/specs/shared_specs/factories/strategies/json_strategy'
require 'hyrax/specs/shared_specs/factories/strategies/valkyrie_resource'
FactoryBot.register_strategy(:valkyrie_create, ValkyrieCreateStrategy)
FactoryBot.register_strategy(:json, JsonStrategy)
FactoryBot.definition_file_paths = [File.expand_path("../factories", __FILE__)]
FactoryBot.find_definitions
require 'shoulda/matchers'
require 'shoulda/callback/matchers'
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
query_registration_targets = [
Valkyrie::MetadataAdapter.find(:test_adapter).query_service.custom_queries,
Valkyrie::MetadataAdapter.find(:postgres_adapter).query_service.custom_queries
]
[Hyrax::CustomQueries::Navigators::CollectionMembers,
Hyrax::CustomQueries::Navigators::ChildFileSetsNavigator,
Hyrax::CustomQueries::Navigators::ChildFilesetsNavigator, # deprecated, use ChildFileSetsNavigator
Hyrax::CustomQueries::Navigators::ChildWorksNavigator,
Hyrax::CustomQueries::Navigators::ParentWorkNavigator,
Hyrax::CustomQueries::FindAccessControl,
Hyrax::CustomQueries::FindCollectionsByType,
Hyrax::CustomQueries::FindManyByAlternateIds,
Hyrax::CustomQueries::FindIdsByModel,
Hyrax::CustomQueries::FindFileMetadata,
Hyrax::CustomQueries::Navigators::FindFiles].each do |handler|
query_registration_targets.each do |adapter|
adapter.register_query_handler(handler)
end
end
ActiveJob::Base.queue_adapter = :test
def clean_active_fedora_repository
return if Hyrax.config.disable_wings
ActiveFedora::Cleaner.clean!
# The JS is executed in a different thread, so that other thread
# may think the root path has already been created:
ActiveFedora.fedora.connection.send(:init_base_path)
end
RSpec.configure do |config|
config.disable_monkey_patching!
config.include Shoulda::Matchers::ActiveRecord, type: :model
config.include Shoulda::Matchers::ActiveModel, type: :form
config.include Shoulda::Callback::Matchers::ActiveModel
config.include Hyrax::Matchers
config.full_backtrace = true if ci_build?
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.fixture_path = File.expand_path("../fixtures", __FILE__)
config.file_fixture_path = File.expand_path("../fixtures", __FILE__)
config.use_transactional_fixtures = false
config.before :suite do
Hyrax::RedisEventStore.instance.then(&:flushdb)
DatabaseCleaner.clean_with(:truncation)
# Noid minting causes extra LDP requests which slow the test suite.
Hyrax.config.enable_noids = false
# setup a test group service
User.group_service = TestHydraGroupService.new
# Set a geonames username; doesn't need to be real.
Hyrax.config.geonames_username = 'hyrax-test'
# Initialize query_service class attribute by calling to avoid it sometimes being set to test_adapter
Hyrax::SolrQueryService.query_service
# disable analytics except for specs which will have proper api mocks
end
config.before :all do
Hyrax.config.analytics = false
end
config.before do |example|
if example.metadata[:type] == :feature && Capybara.current_driver != :rack_test
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.start
end
# using :workflow is preferable to :clean_repo, use the former if possible
# It's important that this comes after DatabaseCleaner.start
ensure_deposit_available_for(user) if example.metadata[:workflow] && defined?(user)
end
config.include(ControllerLevelHelpers, type: :view)
config.before(:each, type: :view) do
initialize_controller_helpers(view)
# disallow network connections to services within the stack for view specs;
# no db/metadata/index calls
WebMock.disable_net_connect!(allow_localhost: false, allow: 'chromedriver.storage.googleapis.com')
allow(Hyrax)
.to receive(:metadata_adapter)
.and_return(Valkyrie::MetadataAdapter.find(:test_adapter))
end
config.after(:each, type: :view) do
WebMock.disable_net_connect!(allow_localhost: true, allow: allowed_hosts)
end
config.before(:all, type: :feature) do
# Assets take a long time to compile. This causes two problems:
# 1) the profile will show the first feature test taking much longer than it
# normally would.
# 2) The first feature test will trigger rack-timeout
#
# Precompile the assets to prevent these issues.
visit "/assets/application.css"
visit "/assets/application.js"
end
config.after do
DatabaseCleaner.clean
# Ensuring we have a clear queue between each spec.
ActiveJob::Base.queue_adapter.enqueued_jobs = []
ActiveJob::Base.queue_adapter.performed_jobs = []
User.group_service.clear
end
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.include Shoulda::Matchers::Independent
config.include Devise::Test::ControllerHelpers, type: :controller
config.include EngineRoutes, type: :controller
config.include Warden::Test::Helpers, type: :request
config.include Warden::Test::Helpers, type: :feature
config.before(:each, type: :feature) do |example|
clean_active_fedora_repository unless
# trust that clean_repo performed the clean if present
example.metadata[:clean_repo] ||
# don't run for adapters other than wings
(example.metadata[:valkyrie_adapter].present? && example.metadata[:valkyrie_adapter] != :wings_adapter)
end
config.append_after(:each, type: :feature) do
Warden.test_reset!
Capybara.reset_sessions!
page.driver.reset!
end
config.include Capybara::RSpecMatchers, type: :input
config.include InputSupport, type: :input
config.include FactoryBot::Syntax::Methods
config.include OptionalExample
config.infer_spec_type_from_file_location!
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.formatter = 'LoggingFormatter'
config.default_formatter = 'doc' if config.files_to_run.one?
config.order = :random
Kernel.srand config.seed
config.shared_context_metadata_behavior = :apply_to_host_groups
config.filter_run_when_matching :focus
config.example_status_persistence_file_path = 'spec/examples.txt'
config.profile_examples = 10
config.before(:example, :active_fedora) do
skip("Don't test Wings") if Hyrax.config.disable_wings
end
config.before(:example, :clean_repo) do
clean_active_fedora_repository unless Hyrax.config.disable_wings
Hyrax::RedisEventStore.instance.then(&:flushdb)
# Not needed to clean the Solr core used by ActiveFedora since
# clean_active_fedora_repository will wipe that core
Hyrax::SolrService.wipe! if Hyrax.config.query_index_from_valkyrie
end
# Use this example metadata when you want to perform jobs inline during testing.
#
# describe '#my_method`, :perform_enqueued do
# ...
# end
#
# If you pass an `Array` of job classes, they will be treated as the filter list.
#
# describe '#my_method`, perform_enqueued: [MyJobClass] do
# ...
# end
#
# Limit to specific job classes with:
#
# ActiveJob::Base.queue_adapter.filter = [JobClass]
#
config.around(:example, :perform_enqueued) do |example|
ActiveJob::Base.queue_adapter.filter =
example.metadata[:perform_enqueued].try(:to_a)
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = true
example.run
ActiveJob::Base.queue_adapter.filter = nil
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = false
ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = false
end
config.before(:example, :index_adapter) do |example|
allow(Hyrax.config)
.to receive(:query_index_from_valkyrie)
.and_return(true)
adapter_name = example.metadata[:index_adapter]
allow(Hyrax)
.to receive(:index_adapter)
.and_return(Valkyrie::IndexingAdapter.find(adapter_name))
end
config.after(:example, :index_adapter) do |example|
adapter_name = example.metadata[:index_adapter]
Valkyrie::IndexingAdapter.find(adapter_name).wipe!
end
# Configure blacklight to use the valkyrie solr index
config.around(:example, index_adapter: :solr_index) do |example|
blacklight_connection_url = CatalogController.blacklight_config.connection_config[:url]
CatalogController.blacklight_config.connection_config[:url] = Valkyrie::IndexingAdapter.find(:solr_index).connection.options[:url]
Blacklight.default_index.connection = nil # force reloading of rsolr connection
example.run
CatalogController.blacklight_config.connection_config[:url] = blacklight_connection_url
Blacklight.default_index.connection = nil # force reloading of rsolr connection
end
# Prepend this before block to ensure that it runs before other before blocks like clean_repo
config.prepend_before(:example, :valkyrie_adapter) do |example|
adapter_name = example.metadata[:valkyrie_adapter]
allow(Hyrax)
.to receive(:metadata_adapter)
.and_return(Valkyrie::MetadataAdapter.find(adapter_name))
if adapter_name == :wings_adapter
skip("Don't test Wings when it is dasabled") if Hyrax.config.disable_wings
else
allow(Hyrax.config).to receive(:disable_wings).and_return(true)
hide_const("Wings") # disable_wings=true removes the Wings constant
end
end
# Prepend this before block to ensure that it runs before other before blocks like clean_repo
config.prepend_before(:example, :storage_adapter) do |example|
adapter_name = example.metadata[:storage_adapter]
allow(Hyrax)
.to receive(:storage_adapter)
.and_return(Valkyrie::StorageAdapter.find(adapter_name))
end
end