Files
codeocean/spec/factories/shared_traits.rb
Sebastian Serth bcbb2d2eb6 Generate truly random email addresses in factory
Previously, it could happen under some (random) circumstances that a series of users got the same user name. This is caused by the usage of Forgery for generating a name. Forgery itself picks a random name from a list, and there is no check that two calls would indeed deliver two different names.

In order to solve the issue, we generate a truly unique email address for specs, so that the problem won't arise again.
2023-11-11 13:40:28 +01:00

37 lines
1.0 KiB
Ruby

# frozen_string_literal: true
FactoryBot.define do
%i[admin external_user teacher].each do |factory_name|
trait :"created_by_#{factory_name}" do
user factory: factory_name
end
end
trait :generated_email do
sequence(:email) {|n| "#{name.underscore.tr(' ', '.')}.#{n}@example.org" }
end
trait :generated_user_name do
name { Forgery(:name).full_name }
end
[ExternalUser, InternalUser].each do |klass|
trait :"singleton_#{klass.name.underscore}" do
initialize_with { klass.where(email:).first_or_create }
end
end
trait :member_of_study_group do
after(:create) do |user, evaluator|
# Do not create a study group if already passed
if user.study_groups.blank?
study_group = create(:study_group)
user.study_groups << study_group
end
user.study_group_memberships.update(role: 'teacher') if evaluator.teacher_in_study_group
user.store_current_study_group_id(user.study_group_memberships.first.study_group_id)
end
end
end