summaryrefslogtreecommitdiff
path: root/spec/helpers/users_helper_spec.rb
blob: c9dc3fcff3fb980797aba1fb2345a7632203a943 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe UsersHelper do
  include TermsHelper

  let(:user) { create(:user) }

  def filter_ee_badges(badges)
    badges.reject { |badge| badge[:text] == 'Is using seat' }
  end

  describe '#user_link' do
    subject { helper.user_link(user) }

    it "links to the user's profile" do
      is_expected.to include("href=\"#{user_path(user)}\"")
    end

    it "has the user's email as title" do
      is_expected.to include("title=\"#{user.email}\"")
    end
  end

  describe '#profile_tabs' do
    subject(:tabs) { helper.profile_tabs }

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

    context 'with public profile' do
      it 'includes all the expected tabs' do
        expect(tabs).to include(:activity, :groups, :contributed, :projects, :starred, :snippets)
      end
    end

    context 'with private profile' do
      before do
        allow(helper).to receive(:can?).with(user, :read_user_profile, nil).and_return(false)
      end

      it 'is empty' do
        expect(tabs).to be_empty
      end
    end
  end

  describe '#user_internal_regex_data' do
    using RSpec::Parameterized::TableSyntax

    where(:user_default_external, :user_default_internal_regex, :result) do
      false | nil                | { user_internal_regex_pattern: nil, user_internal_regex_options: nil }
      false | ''                 | { user_internal_regex_pattern: nil, user_internal_regex_options: nil }
      false | 'mockRegexPattern' | { user_internal_regex_pattern: nil, user_internal_regex_options: nil }
      true  | nil                | { user_internal_regex_pattern: nil, user_internal_regex_options: nil }
      true  | ''                 | { user_internal_regex_pattern: nil, user_internal_regex_options: nil }
      true  | 'mockRegexPattern' | { user_internal_regex_pattern: 'mockRegexPattern', user_internal_regex_options: 'i' }
    end

    with_them do
      before do
        stub_application_setting(user_default_external: user_default_external)
        stub_application_setting(user_default_internal_regex: user_default_internal_regex)
      end

      subject { helper.user_internal_regex_data }

      it { is_expected.to eq(result) }
    end
  end

  describe '#current_user_menu_items' do
    subject(:items) { helper.current_user_menu_items }

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

    after do
      expect(items).not_to include(:start_trial)
    end

    it 'includes all default items' do
      expect(items).to include(:help, :sign_out)
    end

    it 'includes the profile tab if the user can read themself' do
      expect(helper).to receive(:can?).with(user, :read_user, user) { true }

      expect(items).to include(:profile)
    end

    it 'includes the settings tab if the user can update themself' do
      expect(helper).to receive(:can?).with(user, :update_user, user) { true }

      expect(items).to include(:settings)
    end

    context 'when terms are enforced' do
      before do
        enforce_terms
      end

      it 'hides the profile and the settings tab' do
        expect(items).not_to include(:settings, :profile, :help)
      end
    end
  end

  describe '#user_badges_in_admin_section' do
    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    context 'with a blocked user' do
      it "returns the blocked badge" do
        blocked_user = create(:user, state: 'blocked')

        badges = helper.user_badges_in_admin_section(blocked_user)

        expect(filter_ee_badges(badges)).to eq([text: "Blocked", variant: "danger"])
      end
    end

    context 'with a pending approval user' do
      it 'returns the pending approval badge' do
        blocked_pending_approval_user = create(:user, :blocked_pending_approval)

        badges = helper.user_badges_in_admin_section(blocked_pending_approval_user)

        expect(filter_ee_badges(badges)).to eq([text: 'Pending approval', variant: 'info'])
      end
    end

    context 'with an admin user' do
      it "returns the admin badge" do
        admin_user = create(:admin)

        badges = helper.user_badges_in_admin_section(admin_user)

        expect(filter_ee_badges(badges)).to eq([text: "Admin", variant: "success"])
      end
    end

    context 'with an external user' do
      it 'returns the external badge' do
        external_user = create(:user, external: true)

        badges = helper.user_badges_in_admin_section(external_user)

        expect(filter_ee_badges(badges)).to eq([text: "External", variant: "secondary"])
      end
    end

    context 'with the current user' do
      it 'returns the "It\'s You" badge' do
        badges = helper.user_badges_in_admin_section(user)

        expect(filter_ee_badges(badges)).to eq([text: "It's you!", variant: nil])
      end
    end

    context 'with an external blocked admin' do
      it 'returns the blocked, admin and external badges' do
        user = create(:admin, state: 'blocked', external: true)

        badges = helper.user_badges_in_admin_section(user)

        expect(badges).to eq([
          { text: "Blocked", variant: "danger" },
          { text: "Admin", variant: "success" },
          { text: "External", variant: "secondary" }
        ])
      end
    end

    context 'get badges for normal user' do
      it 'returns no badges' do
        user = create(:user)

        badges = helper.user_badges_in_admin_section(user)

        expect(filter_ee_badges(badges)).to be_empty
      end
    end
  end

  describe '#can_force_email_confirmation?' do
    subject { helper.can_force_email_confirmation?(user) }

    context 'for a user that is already confirmed' do
      it { is_expected.to eq(false) }
    end

    context 'for a user that is not confirmed' do
      let(:user) { create(:user, :unconfirmed) }

      it { is_expected.to eq(true) }
    end
  end

  describe '#work_information' do
    subject { helper.work_information(user) }

    context 'when both job_title and organization are present' do
      let(:user) { build(:user, organization: 'GitLab', job_title: 'Frontend Engineer') }

      it 'returns job title concatenated with organization' do
        is_expected.to eq('Frontend Engineer at GitLab')
      end
    end

    context 'when only organization is present' do
      let(:user) { build(:user, organization: 'GitLab') }

      it "returns organization" do
        is_expected.to eq('GitLab')
      end
    end

    context 'when only job_title is present' do
      let(:user) { build(:user, job_title: 'Frontend Engineer') }

      it 'returns job title' do
        is_expected.to eq('Frontend Engineer')
      end
    end

    context 'when neither organization nor job_title are present' do
      it { is_expected.to be_nil }
    end

    context 'when user parameter is nil' do
      let(:user) { nil }

      it { is_expected.to be_nil }
    end
  end
end