Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions spec/factories/instances.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true
FactoryBot.define do
base_time = Time.zone.now.to_i
# Unique per process — see the note in user_emails.rb; host and name are both unique-constrained.
run_id = "#{Time.zone.now.to_i}-#{SecureRandom.hex(3)}"
sequence :host do |n|
"local-#{base_time}-#{n}.lvh.me"
"local-#{run_id}-#{n}.lvh.me"
end

factory :instance do
sequence(:name) { |n| "Instance-#{base_time}-#{n}" }
sequence(:name) { |n| "Instance-#{run_id}-#{n}" }
host

trait :with_learning_map_component_enabled do
Expand Down
7 changes: 5 additions & 2 deletions spec/factories/user_emails.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# frozen_string_literal: true
FactoryBot.define do
base_time = Time.zone.now.to_i
# Unique per process. Specs commit (use_transactional_fixtures is false), so a bare timestamp
# collides whenever two rspec processes start within the same second, and the second process then
# fails User::Email's uniqueness validation. The timestamp is kept for tracing leaked rows.
run_id = "#{Time.zone.now.to_i}-#{SecureRandom.hex(3)}"
sequence :email do |n|
"user_#{n}@domain-#{base_time}-name.com"
"user_#{n}@domain-#{run_id}-name.com"
end

factory :user_email, class: User::Email.name do
Expand Down
23 changes: 20 additions & 3 deletions spec/support/userstamp.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# frozen_string_literal: true
ActsAsTenant.with_tenant(Instance.default) do
# Create a global stamper for this spec run
User.stamper = User.human_users.first
RSpec.configure do |config|
# Create a global stamper for this spec run.
#
# The stamper becomes the creator (and therefore the auto-built owner course_user) of courses
# created in specs, and mail-sending specs deliver to that owner — so the stamper MUST own a
# valid email. This suite commits without cleanup (use_transactional_fixtures is false, no
# DatabaseCleaner), so if any spec removes the seeded admin's email it stays removed; the next
# process's db:seed then recreates the admin as a *new* user, leaving the lowest-id human
# (User.human_users.first) permanently without an email.
#
# Resolve the stamper by the seeded admin email (matching db/seeds and seed.rake) so it always
# owns one, and do it in before(:suite) — this runs AFTER rails_helper's top-level db:seed, so
# the admin email is guaranteed present even after such a recreation. (Setting it at file-load
# time ran before db:seed and re-froze the stale, emailless user.) Fall back to the lowest-id
# human only if that email is somehow absent.
config.before(:suite) do
ActsAsTenant.with_tenant(Instance.default) do
User.stamper = User::Email.find_by_email('test@example.org')&.user || User.human_users.first
end
end
end