user.rb 3.0 KB

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