File: generate_csv_service_spec.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 (87 lines) | stat: -rw-r--r-- 2,989 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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Import::SourceUsers::GenerateCsvService, feature_category: :importers do
  let_it_be(:namespace) { create(:namespace) }
  let_it_be(:current_user) { namespace.owner }

  let_it_be(:user_pending_assignment) { create(:import_source_user, :pending_reassignment, namespace: namespace) }
  let_it_be(:user_awaiting_approval) { create(:import_source_user, :awaiting_approval, namespace: namespace) }
  let_it_be(:rejected_user) { create(:import_source_user, :rejected, namespace: namespace) }

  subject(:service) { described_class.new(namespace, current_user: current_user) }

  describe '#execute' do
    context 'when the user is a namespace owner', :aggregate_failures do
      it 'returns spreadsheet data' do
        result = service.execute

        expect(result).to be_success

        csv_data = CSV.parse(result.payload)

        expect(csv_data.size).to eq(3)
        expect(csv_data[0]).to match_array(described_class::HEADERS)

        expect(csv_data).to include(an_array_matching([
          user_pending_assignment.source_hostname,
          user_pending_assignment.import_type,
          user_pending_assignment.source_user_identifier,
          user_pending_assignment.source_name,
          user_pending_assignment.source_username,
          '',
          ''
        ]))
      end

      it 'returns only data for this namespace' do
        other_source_user = create(:import_source_user)

        result = service.execute

        csv_data = CSV.parse(result.payload)
        source_user_identifiers = csv_data.pluck(2)

        expect(source_user_identifiers).not_to include(other_source_user.source_user_identifier)
      end

      it 'returns only data for Import::SourceUser records with a re-assignable status' do
        result = service.execute

        csv_data = CSV.parse(result.payload)

        source_user_identifiers = csv_data.pluck(2).drop(1)
        expect(source_user_identifiers).to match_array([
          user_pending_assignment.source_user_identifier,
          rejected_user.source_user_identifier
        ])
      end

      context 'and there is no data to return' do
        let(:namespace) { create(:namespace) }

        subject(:service) { described_class.new(namespace, current_user: namespace.owner) }

        it 'only returns the headers' do
          result = service.execute
          csv_data = CSV.parse(result.payload)

          expect(csv_data.size).to eq(1)
          expect(csv_data[0]).to match_array(described_class::HEADERS)
        end
      end
    end

    context 'when current user does not have permission' do
      subject(:service) { described_class.new(namespace, current_user: create(:user)) }

      it 'returns error no permissions' do
        result = service.execute

        expect(result).to be_error
        expect(result.message).to eq('You do not have permission to view import source users for this namespace')
      end
    end
  end
end