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

delete_user_worker_spec.rb « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 13a7390f5921541c278d9efbb6b5ec1e8776a1be (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe DeleteUserWorker, feature_category: :user_management do
  let!(:user)         { create(:user) }
  let!(:current_user) { create(:user) }

  it "calls the DeleteUserWorker with the params it was given" do
    expect_next_instance_of(Users::DestroyService) do |service|
      expect(service).to receive(:execute).with(user, {})
    end

    described_class.new.perform(current_user.id, user.id)
  end

  it "uses symbolized keys" do
    expect_next_instance_of(Users::DestroyService) do |service|
      expect(service).to receive(:execute).with(user, { test: "test" })
    end

    described_class.new.perform(current_user.id, user.id, { "test" => "test" })
  end

  shared_examples 'does nothing' do
    it "does not instantiate a DeleteUserWorker" do
      expect(Users::DestroyService).not_to receive(:new)

      perform
    end
  end

  context 'when user deleted their own account' do
    subject(:perform) { described_class.new.perform(current_user.id, user.id) }

    before do
      # user is blocked as part of User#delete_async
      user.block
      # custom attribute is created as part of User#delete_async
      UserCustomAttribute.set_deleted_own_account_at(user)
    end

    shared_examples 'proceeds with deletion' do
      it "proceeds with deletion" do
        expect_next_instance_of(Users::DestroyService) do |service|
          expect(service).to receive(:execute).with(user, {})
        end

        perform
      end
    end

    it_behaves_like 'proceeds with deletion'

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

      it_behaves_like 'proceeds with deletion'
    end

    shared_examples 'logs' do |reason|
      it 'logs' do
        expect(Gitlab::AppLogger).to receive(:info).with({
          message: 'Skipped own account deletion.',
          reason: reason,
          user_id: user.id,
          username: user.username
        })

        perform
      end
    end

    shared_examples 'updates the user\'s custom attributes' do
      it 'destroys the user\'s DELETED_OWN_ACCOUNT_AT custom attribute' do
        key = UserCustomAttribute::DELETED_OWN_ACCOUNT_AT
        expect { perform }.to change { user.custom_attributes.by_key(key).count }.from(1).to(0)
      end

      context 'when custom attribute is not present' do
        before do
          UserCustomAttribute.delete_all
        end

        it 'does nothing' do
          expect { perform }.not_to raise_error
        end
      end

      it 'creates a SKIPPED_ACCOUNT_DELETION_AT custom attribute for the user' do
        key = UserCustomAttribute::SKIPPED_ACCOUNT_DELETION_AT
        expect { perform }.to change { user.custom_attributes.by_key(key).count }.from(0).to(1)
      end
    end

    context 'when user is banned' do
      before do
        user.activate
        user.ban
      end

      it_behaves_like 'does nothing'
      it_behaves_like 'logs', 'User has been banned.'
      it_behaves_like 'updates the user\'s custom attributes'
    end

    context 'when user is not blocked (e.g. result of user reinstatement request)' do
      before do
        user.activate
      end

      it_behaves_like 'does nothing'
      it_behaves_like 'logs', 'User has been unblocked.'
      it_behaves_like 'updates the user\'s custom attributes'
    end
  end

  context 'when user to delete does not exist' do
    subject(:perform) { described_class.new.perform(current_user.id, non_existing_record_id) }

    it_behaves_like 'does nothing'
  end

  context 'when current user does not exist' do
    subject(:perform) { described_class.new.perform(non_existing_record_id, user.id) }

    it_behaves_like 'does nothing'
  end

  context 'when user to delete and current user do not exist' do
    subject(:perform) { described_class.new.perform(non_existing_record_id, non_existing_record_id) }

    it_behaves_like 'does nothing'
  end
end