user.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. FactoryBot.define do
  3. factory :user do
  4. transient do
  5. intro_clues { true }
  6. slug { "#{firstname}.#{lastname}".parameterize }
  7. end
  8. login { slug }
  9. firstname { Faker::Name.unique.first_name }
  10. lastname { Faker::Name.unique.last_name }
  11. sequence(:email) { |n| "#{slug}.#{n}@zammad.org" }
  12. password { nil }
  13. active { true }
  14. login_failed { 0 }
  15. updated_by_id { 1 }
  16. created_by_id { 1 }
  17. callback(:after_stub, :before_create) do |object, context|
  18. next if !context.intro_clues
  19. object.preferences ||= {}
  20. object.preferences[:intro] = true
  21. end
  22. factory :customer do
  23. role_ids { Role.signup_role_ids.sort }
  24. end
  25. factory :agent_and_customer do
  26. role_ids { Role.signup_role_ids.push(Role.find_by(name: 'Agent').id).sort }
  27. end
  28. factory :agent do
  29. roles { Role.where(name: 'Agent') }
  30. end
  31. factory :admin do
  32. roles { Role.where(name: %w[Admin Agent]) }
  33. end
  34. factory :admin_only do
  35. roles { Role.where(name: %w[Admin]) }
  36. end
  37. trait :with_valid_password do
  38. password { generate(:password_valid) }
  39. end
  40. trait :without_email do
  41. sequence(:login) { |n| "login_#{slug}.#{n}" }
  42. sequence(:email) { nil }
  43. end
  44. # make given password accessible for e.g. authentication logic
  45. before(:create) do |user|
  46. password_plain = user.password
  47. user.define_singleton_method(:password_plain, -> { password_plain })
  48. end
  49. trait :groupable do
  50. transient do
  51. group { nil }
  52. end
  53. after(:create) do |user, context|
  54. Array(context.group).each do |group|
  55. user.groups << group
  56. end
  57. end
  58. end
  59. trait :preferencable do
  60. transient do
  61. notification_group_ids { [] }
  62. end
  63. preferences do
  64. {
  65. 'notification_config' => {
  66. 'matrix' => {
  67. 'create' => { 'criteria' => { 'owned_by_me' => true, 'owned_by_nobody' => true }, 'channel' => { 'email' => true, 'online' => true } },
  68. 'update' => { 'criteria' => { 'owned_by_me' => true, 'owned_by_nobody' => true }, 'channel' => { 'email' => true, 'online' => true } },
  69. 'reminder_reached' => { 'criteria' => { 'owned_by_me' => true, 'owned_by_nobody' => true }, 'channel' => { 'email' => true, 'online' => true } },
  70. 'escalation' => { 'criteria' => { 'owned_by_me' => true, 'owned_by_nobody' => true }, 'channel' => { 'email' => true, 'online' => true } },
  71. },
  72. 'group_ids' => notification_group_ids
  73. }
  74. }
  75. end
  76. end
  77. trait :ooo do
  78. transient do
  79. ooo_agent { nil }
  80. end
  81. out_of_office { true }
  82. out_of_office_start_at { 1.day.ago }
  83. out_of_office_end_at { 1.day.from_now }
  84. out_of_office_replacement_id { ooo_agent.id }
  85. end
  86. trait :with_org do
  87. organization
  88. end
  89. end
  90. sequence(:password_valid) do |n|
  91. "SOme-pass#{n}"
  92. end
  93. end