File: deactivate_dormant_users_worker_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (128 lines) | stat: -rw-r--r-- 4,672 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Users::DeactivateDormantUsersWorker, feature_category: :seat_cost_management do
  using RSpec::Parameterized::TableSyntax

  describe '#perform' do
    let_it_be(:dormant) { create(:user, last_activity_on: Gitlab::CurrentSettings.deactivate_dormant_users_period.days.ago.to_date) }
    let_it_be(:inactive) { create(:user, last_activity_on: nil, created_at: User::MINIMUM_DAYS_CREATED.days.ago.to_date) }
    let_it_be(:inactive_recently_created) { create(:user, last_activity_on: nil, created_at: (User::MINIMUM_DAYS_CREATED - 1).days.ago.to_date) }

    subject(:worker) { described_class.new }

    it 'does not run for SaaS', :saas do
      worker.perform

      expect_any_instance_of(::Users::DeactivateService) do |deactivation_service|
        expect(deactivation_service).not_to have_received(:execute)
      end
    end

    shared_examples 'deactivates dormant users' do
      specify do
        expect { worker.perform }
          .to change { dormant.reload.state }
          .to('deactivated')
          .and change { inactive.reload.state }
          .to('deactivated')
      end
    end

    shared_examples 'deactivates certain user types' do
      where(:user_type, :expected_state) do
        :human | 'deactivated'
        :support_bot | 'active'
        :alert_bot | 'active'
        :visual_review_bot | 'active'
        :service_user | 'deactivated'
        :ghost | 'active'
        :project_bot | 'active'
        :migration_bot | 'active'
        :security_bot | 'active'
        :automation_bot | 'active'
      end

      with_them do
        specify do
          user = create(:user, user_type: user_type, state: :active, last_activity_on: Gitlab::CurrentSettings.deactivate_dormant_users_period.days.ago.to_date)

          worker.perform

          expect_any_instance_of(::Users::DeactivateService) do |deactivation_service|
            if expected_state == 'deactivated'
              expect(deactivation_service).to receive(:execute).with(user).and_call_original
            else
              expect(deactivation_service).not_to have_received(:execute).with(user)
            end
          end

          expect(user.reload.state).to eq expected_state
        end
      end
    end

    shared_examples 'does not deactivate non-active users' do
      specify do
        human_user = create(:user, user_type: :human, state: :blocked, last_activity_on: Gitlab::CurrentSettings.deactivate_dormant_users_period.days.ago.to_date)
        service_user = create(:user, user_type: :service_user, state: :blocked, last_activity_on: Gitlab::CurrentSettings.deactivate_dormant_users_period.days.ago.to_date)

        worker.perform

        expect_any_instance_of(::Users::DeactivateService) do |deactivation_service|
          expect(deactivation_service).not_to have_received(:execute).with(human_user)
          expect(deactivation_service).not_to have_received(:execute).with(service_user)
        end
      end
    end

    shared_examples 'does not deactivate recently created users' do
      specify do
        worker.perform

        expect_any_instance_of(::Users::DeactivateService) do |deactivation_service|
          expect(deactivation_service).not_to have_received(:execute).with(inactive_recently_created)
        end
      end
    end

    context 'when automatic deactivation of dormant users is enabled' do
      before do
        stub_application_setting(deactivate_dormant_users: true)
      end

      context 'when admin mode is not enabled', :do_not_mock_admin_mode_setting do
        include_examples 'deactivates dormant users'
        include_examples 'deactivates certain user types'
        include_examples 'does not deactivate non-active users'
        include_examples 'does not deactivate recently created users'
      end

      context 'when admin mode is enabled', :request_store do
        before do
          stub_application_setting(admin_mode: true)
        end

        include_examples 'deactivates dormant users'
        include_examples 'deactivates certain user types'
        include_examples 'does not deactivate non-active users'
        include_examples 'does not deactivate recently created users'
      end
    end

    context 'when automatic deactivation of dormant users is disabled' do
      before do
        stub_application_setting(deactivate_dormant_users: false)
      end

      it 'does nothing' do
        worker.perform

        expect_any_instance_of(::Users::DeactivateService) do |deactivation_service|
          expect(deactivation_service).not_to have_received(:execute)
        end
      end
    end
  end
end