File: import_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 (122 lines) | stat: -rw-r--r-- 3,902 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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WorkItems::ImportCsvService, feature_category: :team_planning do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { create(:user) }
  let_it_be(:author) { create(:user, username: 'csv_author') }
  let(:file) { fixture_file_upload('spec/fixtures/work_items_valid_types.csv') }
  let(:service) do
    uploader = FileUploader.new(project)
    uploader.store!(file)

    described_class.new(user, project, uploader)
  end

  let_it_be(:issue_type) { ::WorkItems::Type.default_issue_type }

  let(:work_items) { ::WorkItems::WorkItemsFinder.new(user, project: project).execute }
  let(:email_method) { :import_work_items_csv_email }

  subject { service.execute }

  describe '#execute', :aggregate_failures do
    context 'when user has permission' do
      before do
        project.add_reporter(user)
      end

      it_behaves_like 'importer with email notification'

      context 'when file format is valid' do
        context 'when work item types are available' do
          it 'creates the expected number of work items' do
            expect { subject }.to change { work_items.count }.by 2
          end

          it 'sets work item attributes' do
            result = subject

            expect(work_items.reload).to contain_exactly(
              have_attributes(
                title: 'Valid issue',
                work_item_type_id: issue_type.id
              ),
              have_attributes(
                title: 'Valid issue with alternate case',
                work_item_type_id: issue_type.id
              )
            )

            expect(result[:success]).to eq(2)
            expect(result[:error_lines]).to eq([])
            expect(result[:type_errors]).to be_nil
            expect(result[:parse_error]).to eq(false)
          end
        end

        context 'when csv contains work item types that are missing or not available' do
          let(:file) { fixture_file_upload('spec/fixtures/work_items_invalid_types.csv') }

          it 'creates no work items' do
            expect { subject }.not_to change { work_items.count }
          end

          it 'returns the correct result' do
            result = subject

            expect(result[:success]).to eq(0)
            expect(result[:error_lines]).to be_empty # there are problematic lines detailed below
            expect(result[:parse_error]).to eq(false)
            expect(result[:type_errors]).to match({
              blank: [4],
              disallowed: {}, # tested in the EE version
              missing: {
                "isssue" => [2],
                "issue!!" => [3]
              }
            })
          end
        end
      end

      context 'when file is missing necessary headers' do
        let(:file) { fixture_file_upload('spec/fixtures/work_items_missing_header.csv') }

        it 'creates no records' do
          result = subject

          expect(result[:success]).to eq(0)
          expect(result[:error_lines]).to eq([1])
          expect(result[:type_errors]).to be_nil
          expect(result[:parse_error]).to eq(true)
        end

        it 'creates no work items' do
          expect { subject }.not_to change { work_items.count }
        end
      end

      context 'when import_export_work_items_csv feature flag is off' do
        before do
          stub_feature_flags(import_export_work_items_csv: false)
        end

        it 'raises an error' do
          expect { subject }.to raise_error(/This feature is currently behind a feature flag and it is not available./)
        end
      end
    end

    context 'when user does not have permission' do
      before do
        project.add_guest(user)
      end

      it 'raises an error' do
        expect { subject }.to raise_error(/You do not have permission to import work items in this project/)
      end
    end
  end
end