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

create_attachment_service_shared_examples.rb « wikis « services « shared_examples « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 555a6d5eed0ca038e49cd633c719346a57020d55 (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
# frozen_string_literal: true

RSpec.shared_examples 'Wikis::CreateAttachmentService#execute' do |container_type|
  let(:container) { create(container_type, :wiki_repo) }
  let(:wiki) { container.wiki }

  let(:user) { create(:user) }
  let(:file_name) { 'filename.txt' }
  let(:file_path_regex) { %r{#{described_class::ATTACHMENT_PATH}/\h{32}/#{file_name}} }

  let(:file_opts) do
    {
      file_name: file_name,
      file_content: 'Content of attachment'
    }
  end

  let(:opts) { file_opts }

  let(:service) { Wikis::CreateAttachmentService.new(container: container, current_user: user, params: opts) }

  subject(:service_execute) { service.execute[:result] }

  before do
    container.add_developer(user)
  end

  context 'creates branch if it does not exists' do
    let(:branch_name) { 'new_branch' }
    let(:opts) { file_opts.merge(branch_name: branch_name) }

    it do
      expect(wiki.repository.branches).to be_empty
      expect { service.execute }.to change { wiki.repository.branches.count }.by(1)
      expect(wiki.repository.branches.first.name).to eq branch_name
    end
  end

  it 'adds file to the repository' do
    expect(wiki.repository.ls_files('HEAD')).to be_empty

    service.execute

    files = wiki.repository.ls_files('HEAD')
    expect(files.count).to eq 1
    expect(files.first).to match(file_path_regex)
  end

  context 'returns' do
    before do
      allow(SecureRandom).to receive(:hex).and_return('fixed_hex')

      service_execute
    end

    it 'returns related information', :aggregate_failures do
      expect(service_execute[:file_name]).to eq file_name
      expect(service_execute[:file_path]).to eq 'uploads/fixed_hex/filename.txt'
      expect(service_execute[:branch]).to eq wiki.default_branch
      expect(service_execute[:commit]).not_to be_empty
    end
  end
end