add_internal_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::User::AddInternal do
  4. subject(:service) { described_class.new(current_user:) }
  5. let(:current_user) { create(:admin) }
  6. let(:send_invite) { false }
  7. let(:user_data_email) { 'dummy@zammad.com' }
  8. let(:user_data) do
  9. {
  10. email: user_data_email,
  11. firstname: 'Bender',
  12. lastname: 'Rodríguez',
  13. }
  14. end
  15. describe 'creating a user' do
  16. it 'creates a user with valid data' do
  17. user = service.execute(user_data:)
  18. expect(user)
  19. .to be_persisted
  20. .and(have_attributes(**user_data))
  21. end
  22. it 'sets default roles' do
  23. user = service.execute(user_data:)
  24. expect(user.roles).to match_array(Role.find_by(name: 'Customer'))
  25. end
  26. it 'creates a user with given roles' do
  27. user_data[:roles] = [Role.find_by(name: 'Admin')]
  28. user = service.execute(user_data:)
  29. expect(user.roles).to match_array(Role.find_by(name: 'Admin'))
  30. end
  31. context 'with non-admin user' do
  32. let(:current_user) { create(:agent) }
  33. it 'filters sensitive inputs' do
  34. user_data[:roles] = [Role.find_by(name: 'Admin')]
  35. user = service.execute(user_data:)
  36. expect(user.roles).to match_array(Role.find_by(name: 'Customer'))
  37. end
  38. end
  39. it 'raises an error if email is already taken' do
  40. existing_user = create(:user)
  41. user_data[:email] = existing_user.email
  42. expect { service.execute(user_data:) }
  43. .to raise_error(
  44. ActiveRecord::RecordInvalid,
  45. "Validation failed: Email address '#{existing_user.email}' is already used for another user."
  46. )
  47. end
  48. it 'creates an email-less user' do
  49. user_data[:email] = nil
  50. user = service.execute(user_data:)
  51. expect(user)
  52. .to be_persisted
  53. .and(have_attributes(**user_data))
  54. end
  55. it 'creates a user with the given groups access' do
  56. group = create(:group)
  57. user_data[:roles] = [Role.find_by(name: 'Agent')] # ticket.agent is required for group access
  58. user_data[:group_ids_access_map] = { group.id => %w[read change] }
  59. user = service.execute(user_data:)
  60. expect(user.group_ids_access_map).to include(group.id => match_array(%w[read change]))
  61. end
  62. end
  63. describe 'sending invite email' do
  64. before do
  65. allow(NotificationFactory::Mailer).to receive(:notification)
  66. end
  67. context 'when send_invite is true' do
  68. let(:send_invite) { true }
  69. it 'sends invite' do
  70. service.execute(user_data:, send_invite:)
  71. expect(NotificationFactory::Mailer)
  72. .to have_received(:notification)
  73. .with(include(user: have_attributes(email: user_data_email)))
  74. end
  75. context 'when user is email-less' do
  76. it 'does not send invite' do
  77. user_data.delete :email
  78. service.execute(user_data:, send_invite:)
  79. expect(NotificationFactory::Mailer)
  80. .not_to have_received(:notification)
  81. end
  82. end
  83. end
  84. context 'when send_invite is false' do
  85. let(:send_invite) { false }
  86. it 'does not send invite' do
  87. service.execute(user_data:, send_invite:)
  88. expect(NotificationFactory::Mailer).not_to have_received(:notification)
  89. end
  90. end
  91. end
  92. end