data_privacy_task_job_spec.rb 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe DataPrivacyTaskJob, type: :job do
  4. describe '#perform' do
  5. before do
  6. Setting.set('system_init_done', true)
  7. end
  8. let!(:organization) { create(:organization, name: 'test') }
  9. let!(:admin) { create(:admin) }
  10. let!(:user) { create(:customer, organization: organization) }
  11. it 'checks if the user is deleted' do
  12. create(:data_privacy_task, deletable: user)
  13. described_class.perform_now
  14. expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
  15. end
  16. it 'checks if deletion does not crash if the user is already deleted' do
  17. task = create(:data_privacy_task, deletable: user)
  18. user.destroy
  19. described_class.perform_now
  20. expect(task.reload.state).to eq('completed')
  21. end
  22. it 'checks if the organization is deleted' do
  23. create(:data_privacy_task, deletable: user)
  24. described_class.perform_now
  25. expect(organization.reload).to be_a(Organization)
  26. end
  27. it 'checks if the state is completed' do
  28. task = create(:data_privacy_task, deletable: user)
  29. described_class.perform_now
  30. expect(task.reload.state).to eq('completed')
  31. end
  32. it 'checks if the user is deleted (delete_organization=true)' do
  33. create(:data_privacy_task, deletable: user, preferences: { delete_organization: 'true' })
  34. described_class.perform_now
  35. expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
  36. end
  37. it 'checks if the organization is deleted (delete_organization=true)' do
  38. create(:data_privacy_task, deletable: user, preferences: { delete_organization: 'true' })
  39. described_class.perform_now
  40. expect { organization.reload }.to raise_error(ActiveRecord::RecordNotFound)
  41. end
  42. it 'checks if the organization is not deleted (delete_organization=true) if another user was added while task is queued' do
  43. create(:data_privacy_task, deletable: user, preferences: { delete_organization: 'true' })
  44. another_user = create(:user)
  45. another_user.update! organization: organization
  46. described_class.perform_now
  47. expect(organization.reload).to be_present
  48. end
  49. it 'checks if the organization is removed from secondary organizations for users when deleted (delete_organization=true)', aggregate_failures: true do
  50. create(:data_privacy_task, deletable: user, preferences: { delete_organization: 'true' })
  51. another_user = create(:user)
  52. another_user.organizations << organization
  53. another_user.assets({}) # make sure cache exists
  54. described_class.perform_now
  55. expect { organization.reload }.to raise_error(ActiveRecord::RecordNotFound)
  56. expect(another_user.assets({}))
  57. .not_to include(
  58. User: include(
  59. another_user.id => include(
  60. 'organization_ids' => include(organization.id)
  61. )
  62. )
  63. )
  64. end
  65. it 'checks creation of activity stream log' do
  66. create(:data_privacy_task, deletable: user, created_by: admin)
  67. travel 15.minutes
  68. described_class.perform_now
  69. expect(admin.activity_stream(20).any? { |entry| entry.type.name == 'completed' }).to be true
  70. end
  71. end
  72. end