user.rb 3.2 KB

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