data_privacy_task_spec.rb 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. require 'rails_helper'
  2. RSpec.describe DataPrivacyTask, type: :model do
  3. describe '.perform' do
  4. let(:organization) { create(:organization, name: 'test') }
  5. let!(:admin) { create(:admin) }
  6. let(:user) { create(:customer, organization: organization) }
  7. it 'blocks other objects than user objects' do
  8. expect { create(:data_privacy_task, deletable: create(:chat)) }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Deletable is not a User')
  9. end
  10. it 'blocks the multiple deletion tasks for the same user' do
  11. create(:data_privacy_task, deletable: user)
  12. expect { create(:data_privacy_task, deletable: user) }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Deletable has an existing DataPrivacyTask queued')
  13. end
  14. it 'blocks deletion task for user id 1' do
  15. expect { create(:data_privacy_task, deletable: User.find(1)) }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Deletable is undeletable system User with ID 1')
  16. end
  17. it 'blocks deletion task for yourself' do
  18. UserInfo.current_user_id = user.id
  19. expect { create(:data_privacy_task, deletable: user) }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Deletable is your current account')
  20. end
  21. it 'blocks deletion task for last admin' do
  22. expect { create(:data_privacy_task, deletable: admin) }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Deletable is last account with admin permissions')
  23. end
  24. it 'allows deletion task for last two admins' do
  25. create(:admin)
  26. admin = create(:admin)
  27. expect(create(:data_privacy_task, deletable: admin)).to be_truthy
  28. end
  29. it 'sets the failed state when task failed' do
  30. task = create(:data_privacy_task, deletable: user)
  31. user.destroy
  32. task.perform
  33. expect(task.reload.state).to eq('failed')
  34. end
  35. it 'sets an error message when task failed' do
  36. task = create(:data_privacy_task, deletable: user)
  37. user.destroy
  38. task.perform
  39. expect(task.reload.preferences[:error]).to eq("ERROR: #<ActiveRecord::RecordNotFound: Couldn't find User with 'id'=#{user.id}>")
  40. end
  41. end
  42. describe '#prepare_deletion_preview' do
  43. let(:organization) { create(:organization, name: 'Zammad GmbH') }
  44. let(:user) { create(:customer, organization: organization, email: 'secret@example.com') }
  45. let(:task) { create(:data_privacy_task, deletable: user) }
  46. context 'when storing user data' do
  47. let(:pseudonymous_data) do
  48. {
  49. 'firstname' => 'N*e',
  50. 'lastname' => 'B*n',
  51. 'email' => 's*t@e*e.com',
  52. 'organization' => 'Z*d G*H',
  53. }
  54. end
  55. it 'creates pseudonymous representation' do
  56. expect(task[:preferences][:user]).to eq(pseudonymous_data)
  57. end
  58. end
  59. context 'when User is owner of Tickets' do
  60. let!(:owner_tickets) { create_list(:ticket, 3, owner: user) }
  61. it 'stores the numbers' do
  62. expect(task[:preferences][:owner_tickets]).to eq(owner_tickets.reverse.map(&:number))
  63. end
  64. end
  65. context 'when User is customer of Tickets' do
  66. let!(:customer_tickets) { create_list(:ticket, 3, customer: user) }
  67. it 'stores the numbers' do
  68. expect(task[:preferences][:customer_tickets]).to eq(customer_tickets.reverse.map(&:number))
  69. end
  70. end
  71. end
  72. end