File: build_service_shared_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (124 lines) | stat: -rw-r--r-- 3,530 bytes parent folder | download
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
# frozen_string_literal: true

RSpec.shared_examples 'organization user creation and validation in service' do
  it 'creates organization_user on the organization', :aggregate_failures do
    organization_ids = user.organization_users.map(&:organization_id)

    expect(organization_ids).to contain_exactly(organization_params[:organization_id])
  end

  context 'when organization_params is blank' do
    let(:params) { base_params.except(*organization_params.keys) }

    it 'does not create organization_user record' do
      expect(user.organization_users).to be_empty
    end
  end

  context 'when organization param is invalid' do
    let(:params) { base_params.merge(organization_id: non_existing_record_id) }

    it 'adds invalid organization user error', :aggregate_failures do
      expect(user.valid?).to be(false)
      expect(user.errors.full_messages).to include('Organization users is invalid')
    end
  end
end

RSpec.shared_examples 'common user build items' do
  it_behaves_like 'organization user creation and validation in service'

  it { is_expected.to be_valid }

  it 'sets the created_by_id' do
    expect(user.created_by_id).to eq(current_user&.id)
  end

  it 'creates personal namespace in specified organization' do
    expect(user.namespace.organization).to eq(organization)
  end

  context 'when organization_id is not in the params' do
    let(:params) { base_params.except(:organization_id) }

    it 'does not assign organization' do
      expect(user.namespace.organization).to eq(nil)
    end
  end

  context 'when user_type is provided' do
    context 'when project_bot' do
      before do
        params.merge!({ user_type: :project_bot })
      end

      it { expect(user.project_bot?).to be true }
    end

    context 'when not a project_bot' do
      before do
        params.merge!({ user_type: :alert_bot })
      end

      it { expect(user).to be_human }
    end
  end
end

RSpec.shared_examples_for 'current user not admin build items' do
  it_behaves_like 'organization user creation and validation in service'

  context 'with organization_access_level params' do
    let(:params) { base_params.merge(organization_access_level: 'owner') }

    it 'ignores parameter and use default access level' do
      organization_user_data = user.organization_users.first

      expect(organization_user_data.access_level).to eq('default')
    end
  end

  context 'when "email_confirmation_setting" application setting is set to `hard`' do
    before do
      stub_application_setting_enum('email_confirmation_setting', 'hard')
      stub_application_setting(signup_enabled?: true)
    end

    it 'does not confirm the user' do
      expect(user).not_to be_confirmed
    end
  end

  context 'when "email_confirmation_setting" application setting is set to `off`' do
    before do
      stub_application_setting_enum('email_confirmation_setting', 'off')
      stub_application_setting(signup_enabled?: true)
    end

    it 'confirms the user' do
      expect(user).to be_confirmed
    end
  end

  context 'with allowed params' do
    let(:params) do
      {
        email: 1,
        name: 1,
        password: 1,
        password_automatically_set: 1,
        username: 1,
        user_type: 'project_bot',
        organization_id: organization.id
      }
    end

    let(:user_params) { params.except(:organization_id) }

    it 'sets all allowed attributes' do
      expect(User).to receive(:new).with(hash_including(user_params)).and_call_original

      user
    end
  end
end