data_privacy_task_validator_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'lib/validations/object_manager/attribute_validator/backend_examples'
  4. RSpec.describe Validations::DataPrivacyTaskValidator do
  5. subject(:task) { described_class.new }
  6. let(:deletable) { create(:customer) }
  7. let(:record) { build(:data_privacy_task, deletable: deletable) }
  8. it 'valid record passes' do
  9. task.validate(record)
  10. expect(record.errors).to be_blank
  11. end
  12. describe 'validating deletable type' do
  13. context 'when deletable is user' do
  14. let(:deletable) { create(:agent) }
  15. it 'passes' do
  16. task.validate(record)
  17. expect(record.errors).to be_blank
  18. end
  19. end
  20. context 'when deletable is ticket' do
  21. let(:deletable) { create(:ticket) }
  22. it 'passes' do
  23. task.validate(record)
  24. expect(record.errors).to be_blank
  25. end
  26. end
  27. context 'when deletable is other type' do
  28. let(:deletable) { create(:ticket_article) }
  29. it 'adds error' do
  30. task.validate(record)
  31. expect(record.errors.full_messages).to include('Data privacy task allows to delete a user or a ticket only.')
  32. end
  33. end
  34. end
  35. describe 'validating if a similar task exists' do
  36. it 'adds error if task for the same deletable exists' do
  37. create(:data_privacy_task, deletable: deletable)
  38. task.validate(record)
  39. expect(record.errors.full_messages).to include('Selected object is already queued for deletion.')
  40. end
  41. it 'passes if existing task is marked as failed' do
  42. create(:data_privacy_task, deletable: deletable, state: 'failed')
  43. task.validate(record)
  44. expect(record.errors).to be_blank
  45. end
  46. it 'passes if another task exists' do
  47. create(:data_privacy_task, deletable: create(:customer))
  48. task.validate(record)
  49. expect(record.errors).to be_blank
  50. end
  51. end
  52. describe 'validating user object' do
  53. it 'adds error if deleting current user', current_user_id: -> { deletable.id } do
  54. task.validate(record)
  55. expect(record.errors.full_messages).to include('It is not possible to delete your current account.')
  56. end
  57. context 'when deleting a system user' do
  58. let(:deletable) { User.find_by(id: 1) }
  59. it 'adds error' do
  60. task.validate(record)
  61. expect(record.errors.full_messages).to include('It is not possible to delete the system user.')
  62. end
  63. end
  64. context 'when deleting an admin' do
  65. let(:deletable) { create(:admin) }
  66. before { deletable }
  67. it 'adds error if deleting last admin user' do
  68. task.validate(record)
  69. expect(record.errors.full_messages).to include('It is not possible to delete the last account with admin permissions.')
  70. end
  71. context 'when other admin exists' do
  72. let(:other_admin) { create(:admin) }
  73. before { other_admin }
  74. it 'passes' do
  75. task.validate(record)
  76. expect(record.errors).to be_blank
  77. end
  78. it 'adds error if other admins are queued for deletion' do
  79. create(:data_privacy_task, deletable: other_admin)
  80. task.validate(record)
  81. expect(record.errors.full_messages).to include('It is not possible to delete the last account with admin permissions.')
  82. end
  83. end
  84. end
  85. end
  86. end