blob: 1646c18a0ed8dde59364ecbf5aa5f39a68d8c4e6 (
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
|
# 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 wiki repository if it does not exist' do
let(:container) { create(container_type) } # rubocop:disable Rails/SaveBang
it 'creates wiki repository' do
expect { service.execute }.to change { container.wiki.repository.exists? }.to(true)
end
context 'if an error is raised creating the repository' do
it 'catches error and return gracefully' do
allow(container.wiki).to receive(:repository_exists?).and_return(false)
result = service.execute
expect(result[:status]).to eq :error
expect(result[:message]).to eq 'Error creating the wiki repository'
end
end
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
|