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

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

require 'spec_helper'

RSpec.describe API::Entities::User do
  let_it_be(:timezone) { 'America/Los_Angeles' }

  let(:user) { create(:user, timezone: timezone) }
  let(:current_user) { create(:user) }
  let(:entity) { described_class.new(user, current_user: current_user) }

  subject { entity.as_json }

  it 'exposes correct attributes' do
    expect(subject).to include(:name, :bio, :location, :public_email, :skype, :linkedin, :twitter, :website_url, :organization, :job_title, :work_information, :pronouns)
  end

  it 'exposes created_at if the current user can read the user profile' do
    allow(Ability).to receive(:allowed?).with(current_user, :read_user_profile, user).and_return(true)

    expect(subject).to include(:created_at)
  end

  it 'does not expose created_at if the current user cannot read the user profile' do
    allow(Ability).to receive(:allowed?).with(current_user, :read_user_profile, user).and_return(false)

    expect(subject).not_to include(:created_at)
  end

  it 'exposes user as not a bot' do
    expect(subject[:bot]).to be_falsey
  end

  context 'with project bot user' do
    let(:project) { create(:project) }
    let(:user) { create(:user, :project_bot, name: 'secret') }

    before do
      project.add_maintainer(user)
    end

    it 'exposes user as a bot' do
      expect(subject[:bot]).to eq(true)
    end

    context 'when the requester is not an admin' do
      it 'does not expose project bot user name' do
        expect(subject[:name]).to eq('****')
      end
    end

    context 'when the requester is nil' do
      let(:current_user) { nil }

      it 'does not expose project bot user name' do
        expect(subject[:name]).to eq('****')
      end
    end

    context 'when the requester is a project maintainer' do
      let(:current_user) { create(:user) }

      before do
        project.add_maintainer(current_user)
      end

      it 'exposes project bot user name' do
        expect(subject[:name]).to eq('secret')
      end
    end

    context 'when the requester is an admin' do
      let(:current_user) { create(:user, :admin) }

      it 'exposes project bot user name', :enable_admin_mode do
        expect(subject[:name]).to eq('secret')
      end
    end
  end

  context 'with group bot user' do
    let(:group) { create(:group) }
    let(:user) { create(:user, :project_bot, name: 'group bot') }

    before do
      group.add_maintainer(user)
    end

    it 'exposes user as a bot' do
      expect(subject[:bot]).to eq(true)
    end

    context 'when the requester is not a group member' do
      context 'with a public group' do
        it 'exposes group bot user name' do
          expect(subject[:name]).to eq('group bot')
        end
      end

      context 'with a private group' do
        let(:group) { create(:group, :private) }

        it 'does not expose group bot user name' do
          expect(subject[:name]).to eq('****')
        end
      end
    end

    context 'when the requester is nil' do
      let(:current_user) { nil }

      it 'does not expose group bot user name' do
        expect(subject[:name]).to eq('****')
      end
    end

    context 'when the requester is a group maintainer' do
      let(:current_user) { create(:user) }

      before do
        group.add_maintainer(current_user)
      end

      it 'exposes group bot user name' do
        expect(subject[:name]).to eq('group bot')
      end
    end

    context 'when the requester is an admin' do
      let(:current_user) { create(:user, :admin) }

      it 'exposes group bot user name', :enable_admin_mode do
        expect(subject[:name]).to eq('group bot')
      end
    end
  end

  it 'exposes local_time' do
    local_time = '2:30 PM'
    expect(entity).to receive(:local_time).with(timezone).and_return(local_time)
    expect(subject[:local_time]).to eq(local_time)
  end
end