Welcome to mirror list, hosted at ThFree Co, Russian Federation.

inactive_projects_deletion_cron_worker_spec.rb « projects « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0e7b4ea504c500fbe1bcb3be3044478b5b36c020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::InactiveProjectsDeletionCronWorker do
  include ProjectHelpers

  describe "#perform" do
    subject(:worker) { described_class.new }

    let_it_be(:admin_user) { create(:user, :admin) }
    let_it_be(:non_admin_user) { create(:user) }
    let_it_be(:new_blank_project) do
      create_project_with_statistics.tap do |project|
        project.update!(last_activity_at: Time.current)
      end
    end

    let_it_be(:inactive_blank_project) do
      create_project_with_statistics.tap do |project|
        project.update!(last_activity_at: 13.months.ago)
      end
    end

    let_it_be(:inactive_large_project) do
      create_project_with_statistics(with_data: true, size_multiplier: 2.gigabytes)
        .tap { |project| project.update!(last_activity_at: 2.years.ago) }
    end

    let_it_be(:active_large_project) do
      create_project_with_statistics(with_data: true, size_multiplier: 2.gigabytes)
        .tap { |project| project.update!(last_activity_at: 1.month.ago) }
    end

    before do
      stub_application_setting(inactive_projects_min_size_mb: 5)
      stub_application_setting(inactive_projects_send_warning_email_after_months: 12)
      stub_application_setting(inactive_projects_delete_after_months: 14)
    end

    context 'when delete inactive projects feature is disabled' do
      before do
        stub_application_setting(delete_inactive_projects: false)
      end

      it 'does not invoke Projects::InactiveProjectsDeletionNotificationWorker' do
        expect(::Projects::InactiveProjectsDeletionNotificationWorker).not_to receive(:perform_in)
        expect(::Projects::DestroyService).not_to receive(:new)

        worker.perform
      end

      it 'does not delete the inactive projects' do
        worker.perform

        expect(inactive_large_project.reload.pending_delete).to eq(false)
      end
    end

    context 'when delete inactive projects feature is enabled' do
      before do
        stub_application_setting(delete_inactive_projects: true)
      end

      context 'when feature flag is disabled' do
        before do
          stub_feature_flags(inactive_projects_deletion: false)
        end

        it 'does not invoke Projects::InactiveProjectsDeletionNotificationWorker' do
          expect(::Projects::InactiveProjectsDeletionNotificationWorker).not_to receive(:perform_in)
          expect(::Projects::DestroyService).not_to receive(:new)

          worker.perform
        end

        it 'does not delete the inactive projects' do
          worker.perform

          expect(inactive_large_project.reload.pending_delete).to eq(false)
        end
      end

      context 'when feature flag is enabled', :clean_gitlab_redis_shared_state, :sidekiq_inline do
        let_it_be(:delay) { anything }

        before do
          stub_feature_flags(inactive_projects_deletion: true)
        end

        it 'invokes Projects::InactiveProjectsDeletionNotificationWorker for inactive projects' do
          Gitlab::Redis::SharedState.with do |redis|
            expect(redis).to receive(:hset).with('inactive_projects_deletion_warning_email_notified',
                                                 "project:#{inactive_large_project.id}", Date.current)
          end
          expect(::Projects::InactiveProjectsDeletionNotificationWorker).to receive(:perform_in).with(
            delay, inactive_large_project.id, deletion_date).and_call_original
          expect(::Projects::DestroyService).not_to receive(:new)

          worker.perform
        end

        it 'does not invoke InactiveProjectsDeletionNotificationWorker for already notified inactive projects' do
          Gitlab::Redis::SharedState.with do |redis|
            redis.hset('inactive_projects_deletion_warning_email_notified', "project:#{inactive_large_project.id}",
                       Date.current.to_s)
          end

          expect(::Projects::InactiveProjectsDeletionNotificationWorker).not_to receive(:perform_in)
          expect(::Projects::DestroyService).not_to receive(:new)

          worker.perform
        end

        it 'invokes Projects::DestroyService for projects that are inactive even after being notified' do
          Gitlab::Redis::SharedState.with do |redis|
            redis.hset('inactive_projects_deletion_warning_email_notified', "project:#{inactive_large_project.id}",
                       15.months.ago.to_date.to_s)
          end

          expect(::Projects::InactiveProjectsDeletionNotificationWorker).not_to receive(:perform_in)
          expect(::Projects::DestroyService).to receive(:new).with(inactive_large_project, admin_user, {})
                                                             .at_least(:once).and_call_original

          worker.perform

          expect(inactive_large_project.reload.pending_delete).to eq(true)

          Gitlab::Redis::SharedState.with do |redis|
            expect(redis.hget('inactive_projects_deletion_warning_email_notified',
                              "project:#{inactive_large_project.id}")).to be_nil
          end
        end
      end

      it_behaves_like 'an idempotent worker'
    end
  end
end