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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Import::BulkImportsController do
let_it_be(:user) { create(:user) }
before do
sign_in(user)
end
context 'when user is signed in' do
context 'when bulk_import feature flag is enabled' do
before do
stub_feature_flags(bulk_import: true)
end
describe 'POST configure' do
context 'when no params are passed in' do
it 'clears out existing session' do
post :configure
expect(session[:bulk_import_gitlab_access_token]).to be_nil
expect(session[:bulk_import_gitlab_url]).to be_nil
expect(response).to have_gitlab_http_status(:found)
expect(response).to redirect_to(status_import_bulk_imports_url)
end
end
it 'sets the session variables' do
token = 'token'
url = 'https://gitlab.example'
post :configure, params: { bulk_import_gitlab_access_token: token, bulk_import_gitlab_url: url }
expect(session[:bulk_import_gitlab_access_token]).to eq(token)
expect(session[:bulk_import_gitlab_url]).to eq(url)
expect(response).to have_gitlab_http_status(:found)
expect(response).to redirect_to(status_import_bulk_imports_url)
end
it 'strips access token with spaces' do
token = 'token'
post :configure, params: { bulk_import_gitlab_access_token: " #{token} " }
expect(session[:bulk_import_gitlab_access_token]).to eq(token)
expect(controller).to redirect_to(status_import_bulk_imports_url)
end
end
describe 'GET status' do
let(:client) { BulkImports::Clients::HTTP.new(url: 'http://gitlab.example', token: 'token') }
describe 'serialized group data' do
let(:client_response) do
double(
parsed_response: [
{ 'id' => 1, 'full_name' => 'group1', 'full_path' => 'full/path/group1', 'web_url' => 'http://demo.host/full/path/group1' },
{ 'id' => 2, 'full_name' => 'group2', 'full_path' => 'full/path/group2', 'web_url' => 'http://demo.host/full/path/group1' }
],
headers: {
'x-next-page' => '2',
'x-page' => '1',
'x-per-page' => '20',
'x-total' => '37',
'x-total-pages' => '2'
}
)
end
let(:client_params) do
{
top_level_only: true,
min_access_level: Gitlab::Access::OWNER
}
end
before do
allow(controller).to receive(:client).and_return(client)
allow(client).to receive(:get).with('groups', client_params).and_return(client_response)
end
it 'returns serialized group data' do
get :status, format: :json
expect(json_response).to eq({ importable_data: client_response.parsed_response }.as_json)
end
it 'forwards pagination headers' do
get :status, format: :json
expect(response.headers['x-per-page']).to eq client_response.headers['x-per-page']
expect(response.headers['x-page']).to eq client_response.headers['x-page']
expect(response.headers['x-next-page']).to eq client_response.headers['x-next-page']
expect(response.headers['x-prev-page']).to eq client_response.headers['x-prev-page']
expect(response.headers['x-total']).to eq client_response.headers['x-total']
expect(response.headers['x-total-pages']).to eq client_response.headers['x-total-pages']
end
context 'when filtering' do
it 'returns filtered result' do
filter = 'test'
search_params = client_params.merge(search: filter)
expect(client).to receive(:get).with('groups', search_params).and_return(client_response)
get :status, format: :json, params: { filter: filter }
end
end
end
context 'when host url is local or not http' do
%w[https://localhost:3000 http://192.168.0.1 ftp://testing].each do |url|
before do
stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
session[:bulk_import_gitlab_access_token] = 'test'
session[:bulk_import_gitlab_url] = url
end
it 'denies network request' do
get :status
expect(controller).to redirect_to(new_group_path(anchor: 'import-group-pane'))
expect(flash[:alert]).to eq('Specified URL cannot be used: "Only allowed schemes are http, https"')
end
end
context 'when local requests are allowed' do
%w[https://localhost:3000 http://192.168.0.1].each do |url|
before do
stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)
session[:bulk_import_gitlab_access_token] = 'test'
session[:bulk_import_gitlab_url] = url
end
it 'allows network request' do
get :status
expect(response).to have_gitlab_http_status(:ok)
end
end
end
end
context 'when connection error occurs' do
before do
allow(controller).to receive(:client).and_return(client)
allow(client).to receive(:get).and_raise(BulkImports::Error)
end
it 'returns 422' do
get :status, format: :json
expect(response).to have_gitlab_http_status(:unprocessable_entity)
end
it 'clears session' do
get :status, format: :json
expect(session[:gitlab_url]).to be_nil
expect(session[:gitlab_access_token]).to be_nil
end
end
end
describe 'GET realtime_changes' do
let_it_be(:bulk_import) { create(:bulk_import, :created, user: user) }
it 'returns bulk imports created by current user' do
get :realtime_changes
expect(json_response).to eq([{ 'id' => bulk_import.id, 'status_name' => bulk_import.status_name.to_s }])
end
it 'sets a Poll-Interval header' do
get :realtime_changes
expect(response.headers['Poll-Interval']).to eq(Import::BulkImportsController::POLLING_INTERVAL.to_s)
end
end
describe 'POST create' do
let(:instance_url) { "http://fake-instance" }
let(:bulk_import) { create(:bulk_import) }
let(:pat) { "fake-pat" }
let(:bulk_import_params) do
[{ "source_type" => "group_entity",
"source_full_path" => "full_path",
"destination_name" => "destination_name",
"destination_namespace" => "root" }]
end
before do
session[:bulk_import_gitlab_access_token] = pat
session[:bulk_import_gitlab_url] = instance_url
end
it 'executes BulkImportService' do
expect_next_instance_of(
BulkImportService, user, bulk_import_params, { url: instance_url, access_token: pat }) do |service|
allow(service).to receive(:execute).and_return(ServiceResponse.success(payload: bulk_import))
end
post :create, params: { bulk_import: bulk_import_params }
expect(response).to have_gitlab_http_status(:ok)
expect(response.body).to eq({ id: bulk_import.id }.to_json)
end
it 'returns error when validation fails' do
error_response = ServiceResponse.error(message: 'Record invalid', http_status: :unprocessable_entity)
expect_next_instance_of(
BulkImportService, user, bulk_import_params, { url: instance_url, access_token: pat }) do |service|
allow(service).to receive(:execute).and_return(error_response)
end
post :create, params: { bulk_import: bulk_import_params }
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(response.body).to eq({ error: 'Record invalid' }.to_json)
end
end
end
context 'when bulk_import feature flag is disabled' do
before do
stub_feature_flags(bulk_import: false)
end
context 'POST configure' do
it 'returns 404' do
post :configure
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'GET status' do
it 'returns 404' do
get :status
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end
context 'when user is signed out' do
before do
sign_out(user)
end
context 'POST configure' do
it 'redirects to sign in page' do
post :configure
expect(response).to have_gitlab_http_status(:found)
expect(response).to redirect_to(new_user_session_path)
end
end
context 'GET status' do
it 'redirects to sign in page' do
get :status
expect(response).to have_gitlab_http_status(:found)
expect(response).to redirect_to(new_user_session_path)
end
end
end
end
|