summaryrefslogtreecommitdiff
path: root/spec/helpers/admin/user_actions_helper_spec.rb
blob: d945b13cad64556e33b7d17fdfa3ff1784d0d3ae (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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Admin::UserActionsHelper do
  describe '#admin_actions' do
    let_it_be(:current_user) { build(:user) }

    subject { helper.admin_actions(user) }

    before do
      allow(helper).to receive(:current_user).and_return(current_user)
      allow(helper).to receive(:can?).with(current_user, :destroy_user, user).and_return(true)
    end

    context 'the user is a bot' do
      let_it_be(:user) { build(:user, :bot) }

      it { is_expected.to be_empty }
    end

    context 'the current user and user are the same' do
      let_it_be(:user) { build(:user) }
      let_it_be(:current_user) { user }

      it { is_expected.to contain_exactly("edit") }
    end

    context 'the user is a standard user' do
      let_it_be(:user) { create(:user) }

      it { is_expected.to contain_exactly("edit", "block", "ban", "deactivate", "delete", "delete_with_contributions") }
    end

    context 'the user is an admin user' do
      let_it_be(:user) { create(:user, :admin) }

      it { is_expected.to contain_exactly("edit", "block", "ban", "deactivate", "delete", "delete_with_contributions") }
    end

    context 'the user is blocked by LDAP' do
      let_it_be(:user) { create(:omniauth_user, :ldap_blocked) }

      it { is_expected.to contain_exactly("edit", "ldap", "delete", "delete_with_contributions") }
    end

    context 'the user is blocked pending approval' do
      let_it_be(:user) { create(:user, :blocked_pending_approval) }

      it { is_expected.to contain_exactly("edit", "approve", "reject") }
    end

    context 'the user is blocked' do
      let_it_be(:user) { create(:user, :blocked) }

      it { is_expected.to contain_exactly("edit", "unblock", "delete", "delete_with_contributions") }
    end

    context 'the user is deactivated' do
      let_it_be(:user) { create(:user, :deactivated) }

      it { is_expected.to contain_exactly("edit", "block", "ban", "activate", "delete", "delete_with_contributions") }
    end

    context 'the user is locked' do
      let_it_be(:user) { create(:user) }

      before do
        user.lock_access!
      end

      it {
        is_expected.to contain_exactly(
          "edit",
          "block",
          "ban",
          "deactivate",
          "unlock",
          "delete",
          "delete_with_contributions"
        )
      }
    end

    context 'the user is banned' do
      let_it_be(:user) { create(:user, :banned) }

      it { is_expected.to contain_exactly("edit", "unban", "delete", "delete_with_contributions") }
    end

    context 'the current_user does not have permission to delete the user' do
      let_it_be(:user) { build(:user) }

      before do
        allow(helper).to receive(:can?).with(current_user, :destroy_user, user).and_return(false)
      end

      it { is_expected.to contain_exactly("edit", "block", "ban", "deactivate") }
    end

    context 'the user is a sole owner of a group' do
      let_it_be(:group) { create(:group) }
      let_it_be(:user) { create(:user) }

      before do
        group.add_owner(user)
      end

      it { is_expected.to contain_exactly("edit", "block", "ban", "deactivate") }
    end

    context 'the user is a bot' do
      let_it_be(:user) { create(:user, :bot) }

      it { is_expected.to match_array([]) }
    end

    context 'when `ban_user_feature_flag` is disabled' do
      before do
        stub_feature_flags(ban_user_feature_flag: false)
      end

      context 'the user is a standard user' do
        let_it_be(:user) { create(:user) }

        it { is_expected.not_to include("ban") }
      end

      context 'the user is banned' do
        let_it_be(:user) { create(:user, :banned) }

        it { is_expected.not_to include("unban") }
      end
    end
  end
end