user.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. FactoryBot.define do
  2. factory :user do
  3. transient do
  4. intro_clues { true }
  5. slug { "#{firstname}.#{lastname}".parameterize }
  6. end
  7. login { slug }
  8. firstname { Faker::Name.first_name }
  9. lastname { Faker::Name.last_name }
  10. sequence(:email) { |n| "#{slug}.#{n}@zammad.org" }
  11. password { nil }
  12. active { true }
  13. login_failed { 0 }
  14. updated_by_id { 1 }
  15. created_by_id { 1 }
  16. callback(:after_stub, :before_create) do |object, context|
  17. next if !context.intro_clues
  18. object.preferences ||= {}
  19. object.preferences[:intro] = true
  20. end
  21. factory :customer do
  22. role_ids { Role.signup_role_ids.sort }
  23. trait :with_org do
  24. organization
  25. end
  26. end
  27. factory :agent_and_customer do
  28. role_ids { Role.signup_role_ids.push( Role.find_by(name: 'Agent').id ).sort }
  29. trait :with_org do
  30. organization
  31. end
  32. end
  33. factory :agent do
  34. roles { Role.where(name: 'Agent') }
  35. end
  36. factory :admin do
  37. roles { Role.where(name: %w[Admin Agent]) }
  38. end
  39. # make given password accessible for e.g. authentication logic
  40. before(:create) do |user|
  41. password_plain = user.password
  42. user.define_singleton_method(:password_plain, -> { password_plain })
  43. end
  44. end
  45. end