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

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

require 'spec_helper'

RSpec.describe Users::AutoBanService, feature_category: :instance_resiliency do
  let_it_be_with_reload(:user) { create(:user) }
  let(:reason) { :auto_ban_reason }

  context 'when auto banning a user', :aggregate_failures do
    subject(:auto_ban_user) { described_class.new(user: user, reason: reason).execute }

    context 'when successful' do
      it 'returns success status' do
        response = auto_ban_user

        expect(response[:status]).to eq(:success)
      end

      it 'bans the user' do
        expect { auto_ban_user }.to change { user.state }.from('active').to('banned')
      end

      it 'creates a BannedUser' do
        expect { auto_ban_user }.to change { Users::BannedUser.count }.by(1)
        expect(Users::BannedUser.last.user_id).to eq(user.id)
      end

      describe 'recording a custom attribute' do
        it 'records a custom attribute' do
          expect { auto_ban_user }.to change { UserCustomAttribute.count }.by(1)
          expect(user.custom_attributes.by_key(UserCustomAttribute::AUTO_BANNED_BY).first.value).to eq(reason.to_s)
        end
      end
    end

    context 'when failed' do
      context 'when user is blocked' do
        before do
          user.block!
        end

        it 'returns state error message' do
          response = auto_ban_user

          expect(response[:status]).to eq(:error)
          expect(response[:message]).to match('State cannot transition via \"ban\"')
        end

        it 'does not modify the BannedUser record or user state' do
          expect { auto_ban_user }.not_to change { Users::BannedUser.count }
          expect { auto_ban_user }.not_to change { user.state }
        end
      end
    end
  end
end