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
200
201
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::Import::GitlabProjects::CreateProjectFromRemoteFileService do
let(:remote_url) { 'https://external.file.path/file' }
let(:params) do
{
path: 'path',
namespace: user.namespace,
name: 'name',
remote_import_url: remote_url
}
end
let_it_be(:user) { create(:user) }
subject { described_class.new(user, params) }
shared_examples 'successfully import' do |content_type|
it 'creates a project and returns a successful response' do
stub_headers_for(remote_url, {
'content-type' => content_type,
'content-length' => '10'
})
response = nil
expect { response = subject.execute }
.to change(Project, :count).by(1)
expect(response).to be_success
expect(response.http_status).to eq(:ok)
expect(response.payload).to be_instance_of(Project)
expect(response.payload.name).to eq('name')
expect(response.payload.path).to eq('path')
expect(response.payload.namespace).to eq(user.namespace)
end
end
it_behaves_like 'successfully import', 'application/gzip'
it_behaves_like 'successfully import', 'application/x-tar'
context 'when the file url is invalid' do
it 'returns an erred response with the reason of the failure' do
stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
params[:remote_import_url] = 'https://localhost/file'
response = nil
expect { response = subject.execute }
.not_to change(Project, :count)
expect(response).not_to be_success
expect(response.http_status).to eq(:bad_request)
expect(response.message).to eq('Requests to localhost are not allowed')
end
end
context 'validate file type' do
it 'returns erred response when the file type is not informed' do
stub_headers_for(remote_url, { 'content-length' => '10' })
response = nil
expect { response = subject.execute }
.not_to change(Project, :count)
expect(response).not_to be_success
expect(response.http_status).to eq(:bad_request)
expect(response.message)
.to eq("Missing 'ContentType' header")
end
it 'returns erred response when the file type is not allowed' do
stub_headers_for(remote_url, {
'content-type' => 'application/js',
'content-length' => '10'
})
response = nil
expect { response = subject.execute }
.not_to change(Project, :count)
expect(response).not_to be_success
expect(response.http_status).to eq(:bad_request)
expect(response.message)
.to eq("Remote file content type 'application/js' not allowed. (Allowed content types: application/gzip, application/x-tar)")
end
end
context 'validate content type' do
it 'returns erred response when the file size is not informed' do
stub_headers_for(remote_url, { 'content-type' => 'application/gzip' })
response = nil
expect { response = subject.execute }
.not_to change(Project, :count)
expect(response).not_to be_success
expect(response.http_status).to eq(:bad_request)
expect(response.message)
.to eq("Missing 'ContentLength' header")
end
it 'returns error response when the file size is a text' do
stub_headers_for(remote_url, {
'content-type' => 'application/gzip',
'content-length' => 'some text'
})
response = nil
expect { response = subject.execute }
.not_to change(Project, :count)
expect(response).not_to be_success
expect(response.http_status).to eq(:bad_request)
expect(response.message)
.to eq("Missing 'ContentLength' header")
end
it 'returns erred response when the file is larger then allowed' do
stub_headers_for(remote_url, {
'content-type' => 'application/gzip',
'content-length' => 11.gigabytes.to_s
})
response = nil
expect { response = subject.execute }
.not_to change(Project, :count)
expect(response).not_to be_success
expect(response.http_status).to eq(:bad_request)
expect(response.message)
.to eq('Remote file larger than limit. (limit 10 GB)')
end
end
it 'does not validate content-type or content-length when the file is stored in AWS-S3' do
stub_headers_for(remote_url, {
'Server' => 'AmazonS3',
'x-amz-request-id' => 'Something'
})
response = nil
expect { response = subject.execute }
.to change(Project, :count)
expect(response).to be_success
expect(response.http_status).to eq(:ok)
end
context 'when required parameters are not provided' do
let(:params) { {} }
it 'returns an erred response with the reason of the failure' do
stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
response = nil
expect { response = subject.execute }
.not_to change(Project, :count)
expect(response).not_to be_success
expect(response.http_status).to eq(:bad_request)
expect(response.message).to eq("Parameter 'path' is required")
expect(subject.errors.full_messages).to match_array([
"Missing 'ContentLength' header",
"Missing 'ContentType' header",
"Parameter 'namespace' is required",
"Parameter 'path' is required",
"Parameter 'remote_import_url' is required"
])
end
end
context 'when the project is invalid' do
it 'returns an erred response with the reason of the failure' do
create(:project, namespace: user.namespace, path: 'path')
stub_headers_for(remote_url, {
'content-type' => 'application/gzip',
'content-length' => '10'
})
response = nil
expect { response = subject.execute }
.not_to change(Project, :count)
expect(response).not_to be_success
expect(response.http_status).to eq(:bad_request)
expect(response.message).to eq('Path has already been taken')
end
end
def stub_headers_for(url, headers = {})
allow(Gitlab::HTTP)
.to receive(:head)
.with(url)
.and_return(double(headers: headers))
end
end
|