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

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

require 'spec_helper'

RSpec.describe Members::CreateService, :aggregate_failures, :clean_gitlab_redis_cache, :clean_gitlab_redis_shared_state, :sidekiq_inline do
  let_it_be(:source, reload: true) { create(:project) }
  let_it_be(:user) { create(:user) }
  let_it_be(:member) { create(:user) }
  let_it_be(:user_ids) { member.id.to_s }
  let_it_be(:access_level) { Gitlab::Access::GUEST }

  let(:additional_params) { { invite_source: '_invite_source_' } }
  let(:params) { { user_ids: user_ids, access_level: access_level }.merge(additional_params) }

  subject(:execute_service) { described_class.new(user, params.merge({ source: source })).execute }

  before do
    if source.is_a?(Project)
      source.add_maintainer(user)
      OnboardingProgress.onboard(source.namespace)
    else
      source.add_owner(user)
      OnboardingProgress.onboard(source)
    end
  end

  context 'when passing valid parameters' do
    it 'adds a user to members' do
      expect(execute_service[:status]).to eq(:success)
      expect(source.users).to include member
      expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(true)
    end

    context 'when executing on a group' do
      let_it_be(:source) { create(:group) }

      it 'adds a user to members' do
        expect(execute_service[:status]).to eq(:success)
        expect(source.users).to include member
        expect(OnboardingProgress.completed?(source, :user_added)).to be(true)
      end
    end
  end

  context 'when passing no user ids' do
    let(:user_ids) { '' }

    it 'does not add a member' do
      expect(execute_service[:status]).to eq(:error)
      expect(execute_service[:message]).to be_present
      expect(source.users).not_to include member
      expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
    end
  end

  context 'when passing many user ids' do
    let(:user_ids) { 1.upto(101).to_a.join(',') }

    it 'limits the number of users to 100' do
      expect(execute_service[:status]).to eq(:error)
      expect(execute_service[:message]).to be_present
      expect(source.users).not_to include member
      expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
    end
  end

  context 'when passing an invalid access level' do
    let(:access_level) { -1 }

    it 'does not add a member' do
      expect(execute_service[:status]).to eq(:error)
      expect(execute_service[:message]).to include("#{member.username}: Access level is not included in the list")
      expect(source.users).not_to include member
      expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
    end
  end

  context 'when passing an existing invite user id' do
    let(:user_ids) { create(:project_member, :invited, project: source).invite_email }

    it 'does not add a member' do
      expect(execute_service[:status]).to eq(:error)
      expect(execute_service[:message]).to eq("The member's email address has already been taken")
      expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
    end
  end

  context 'when tracking the invite source', :snowplow do
    context 'when invite_source is not passed' do
      let(:additional_params) { {} }

      it 'raises an error' do
        expect { execute_service }.to raise_error(ArgumentError, 'No invite source provided.')

        expect_no_snowplow_event
      end
    end

    context 'when invite_source is passed' do
      it 'tracks the invite source from params' do
        execute_service

        expect_snowplow_event(
          category: described_class.name,
          action: 'create_member',
          label: '_invite_source_',
          property: 'existing_user',
          user: user
        )
      end
    end

    context 'when it is a net_new_user' do
      let(:additional_params) { { invite_source: '_invite_source_', user_ids: 'email@example.org' } }

      it 'tracks the invite source from params' do
        execute_service

        expect_snowplow_event(
          category: described_class.name,
          action: 'create_member',
          label: '_invite_source_',
          property: 'net_new_user',
          user: user
        )
      end
    end
  end

  context 'when tracking the areas of focus', :snowplow do
    context 'when areas_of_focus is not passed' do
      it 'does not track' do
        execute_service

        expect_no_snowplow_event(category: described_class.name, action: 'area_of_focus')
      end
    end

    context 'when 1 areas_of_focus is passed' do
      let(:additional_params) { { invite_source: '_invite_source_', areas_of_focus: ['no_selection'] } }

      it 'tracks the areas_of_focus from params' do
        execute_service

        expect_snowplow_event(
          category: described_class.name,
          action: 'area_of_focus',
          label: 'no_selection',
          property: source.members.last.id.to_s
        )
      end

      context 'when passing many user ids' do
        let(:another_user) { create(:user) }
        let(:user_ids) { [member.id, another_user.id].join(',') }

        it 'tracks the areas_of_focus from params' do
          execute_service

          members = source.members.last(2)

          expect_snowplow_event(
            category: described_class.name,
            action: 'area_of_focus',
            label: 'no_selection',
            property: members.first.id.to_s
          )
          expect_snowplow_event(
            category: described_class.name,
            action: 'area_of_focus',
            label: 'no_selection',
            property: members.last.id.to_s
          )
        end
      end
    end

    context 'when multiple areas_of_focus are passed' do
      let(:additional_params) { { invite_source: '_invite_source_', areas_of_focus: %w[no_selection Other] } }

      it 'tracks the areas_of_focus from params' do
        execute_service

        expect_snowplow_event(
          category: described_class.name,
          action: 'area_of_focus',
          label: 'no_selection',
          property: source.members.last.id.to_s
        )
        expect_snowplow_event(
          category: described_class.name,
          action: 'area_of_focus',
          label: 'Other',
          property: source.members.last.id.to_s
        )
      end
    end
  end
end