data_retention_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Data retention rules for object cleanup from the system with the scheduler (#4838)', type: :system do
  4. let(:user) { create(:customer, organization: organization) }
  5. let(:organization) { create(:organization) }
  6. let(:ticket) { create(:ticket, customer: user) }
  7. let(:article) { create(:'ticket/article', :inbound_email, ticket: ticket) }
  8. before do
  9. travel_to 6.months.ago - 1.day
  10. article
  11. travel_back
  12. end
  13. shared_examples 'deleting object via data privacy task' do |object_name|
  14. before do
  15. visit '#manage/job'
  16. click 'a', text: 'New Scheduler'
  17. in_modal do
  18. fill_in 'Name', with: 'Delete users older than 6 months'
  19. set_select_field_value 'object', object_name
  20. scroll_into_view('.object_selector')
  21. within '.object_selector' do
  22. set_condition(1, 'Last contact', 'before (relative)', value: ['6'], range: ['Month(s)'])
  23. end
  24. scroll_into_view('.object_perform_action')
  25. within '.object_perform_action' do
  26. set_condition(1, 'Action', nil, value_action: 'Add a data privacy deletion task')
  27. end
  28. click_on 'Submit'
  29. end
  30. run_last_job_and_perform_last_deletion_task
  31. end
  32. it 'deletes user via data privacy task', if: object_name == 'User' do
  33. expect(User).not_to exist(user.id)
  34. expect(Organization).to exist(organization.id)
  35. expect(Ticket).not_to exist(ticket.id)
  36. end
  37. it 'deletes ticket via data privacy task', if: object_name == 'Ticket' do
  38. expect(Ticket).not_to exist(ticket.id)
  39. expect(User).to exist(user.id)
  40. expect(Organization).to exist(organization.id)
  41. end
  42. end
  43. context 'with user object' do
  44. let(:deletable) { user }
  45. it_behaves_like 'deleting object via data privacy task', 'User'
  46. end
  47. context 'with ticket object' do
  48. let(:deletable) { ticket }
  49. it_behaves_like 'deleting object via data privacy task', 'Ticket'
  50. end
  51. def run_last_job_and_perform_last_deletion_task
  52. job = Job.last
  53. # Workaround to always run the scheduler job without checking its executable state and if the current time applies.
  54. allow(job).to receive_messages(executable?: true, in_timeplan?: true)
  55. job.run
  56. DataPrivacyTask.last.perform
  57. end
  58. def set_attribute_selector(row_number, value)
  59. attribute_selector = find(".js-filterElement:nth-child(#{row_number}) .js-attributeSelector select")
  60. option = attribute_selector.find('option', text: value, exact_text: true)
  61. option.select_option
  62. end
  63. def set_operator_selector(row_number, value)
  64. operator_selector = find(".js-filterElement:nth-child(#{row_number}) .js-operator select")
  65. option = operator_selector.find('option', text: value, exact_text: true)
  66. option.select_option
  67. end
  68. def set_value_action(row_number, value)
  69. row_number += 1 if has_css?('.horizontal-filters .alert')
  70. value_action_selector = find(".js-filterElement:nth-child(#{row_number}) .js-value select")
  71. option = value_action_selector.find('option', text: value, exact_text: true)
  72. option.select_option
  73. end
  74. def set_value_selector(row_number, value)
  75. value_selector = find(".js-filterElement:nth-child(#{row_number}) select.js-value")
  76. value.each do |v|
  77. option = value_selector.find('option', text: v, exact_text: true)
  78. option.select_option
  79. end
  80. end
  81. def set_range_selector(row_number, value)
  82. range_selector = find(".js-filterElement:nth-child(#{row_number}) select.js-range")
  83. value.each do |v|
  84. option = range_selector.find('option', text: v, exact_text: true)
  85. option.select_option
  86. end
  87. end
  88. def set_condition(row_number, attribute, operator, value: nil, range: nil, value_action: nil)
  89. set_attribute_selector(row_number, attribute) if attribute.present?
  90. set_operator_selector(row_number, operator) if operator.present?
  91. if !value_action.nil?
  92. set_value_action(row_number, value_action)
  93. end
  94. return if value.nil?
  95. set_value_selector(row_number, value)
  96. return if range.nil?
  97. set_range_selector(row_number, range)
  98. end
  99. end