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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Work Items', feature_category: :team_planning do
include WorkhorseHelpers
include_context 'workhorse headers'
let_it_be(:work_item) { create(:work_item) }
let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project) }
let(:file) { fixture_file_upload("spec/fixtures/#{filename}") }
before_all do
work_item.project.add_developer(current_user)
end
shared_examples 'response with 404 status' do
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
shared_examples 'safely handles uploaded files' do
it 'ensures the upload is handled safely', :aggregate_failures do
allow(Gitlab::PathTraversal).to receive(:check_path_traversal!).and_call_original
expect(Gitlab::PathTraversal).to receive(:check_path_traversal!).with(filename).at_least(:once)
expect(FileUploader).not_to receive(:cache)
subject
end
end
describe 'GET /:namespace/:project/work_items/:id' do
context 'when authenticated' do
before do
sign_in(current_user)
end
it 'renders show' do
get project_work_item_url(work_item.project, work_item.iid)
expect(response).to have_gitlab_http_status(:ok)
end
it 'has correct metadata' do
get project_work_item_url(work_item.project, work_item.iid)
expect(response.body).to include("#{work_item.title} (#{work_item.to_reference})")
expect(response.body).to include(work_item.work_item_type.name.pluralize)
end
end
context 'when user cannot read the project' do
before do
sign_in(current_user)
work_item.project.team.truncate
end
it 'renders not found' do
get project_work_item_url(work_item.project, work_item.iid)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'does not include sensitive metadata' do
get project_work_item_url(work_item.project, work_item.iid)
expect(response.body).not_to include("#{work_item.title} (#{work_item.to_reference})")
expect(response.body).not_to include(work_item.work_item_type.name.pluralize)
end
end
end
describe 'POST /:namespace/:project/work_items/import_csv' do
let(:filename) { 'work_items_valid_types.csv' }
let(:params) { { namespace_id: project.namespace.id, path: 'test' } }
subject { upload_file(file, workhorse_headers, params) }
shared_examples 'handles authorisation' do
context 'when unauthorized' do
context 'with non-member' do
let_it_be(:current_user) { create(:user) }
before do
sign_in(current_user)
end
it 'responds with error' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'with anonymous user' do
it 'responds with error' do
subject
expect(response).to have_gitlab_http_status(:found)
expect(response).to be_redirect
end
end
end
context 'when authorized' do
before do
sign_in(current_user)
project.add_reporter(current_user)
end
context 'when import/export work items feature is available and member is a reporter' do
shared_examples 'response with success status' do
it 'returns 200 status and success message' do
subject
expect(response).to have_gitlab_http_status(:success)
expect(json_response).to eq(
'message' => "Your work items are being imported. Once finished, you'll receive a confirmation email.")
end
end
it_behaves_like 'response with success status'
it_behaves_like 'safely handles uploaded files'
it 'shows error when upload fails' do
expect_next_instance_of(UploadService) do |upload_service|
expect(upload_service).to receive(:execute).and_return(nil)
end
subject
expect(json_response).to eq('errors' => 'File upload error.')
end
context 'when file extension is not csv' do
let(:filename) { 'sample_doc.md' }
it 'returns error message' do
subject
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response).to eq(
'errors' => "The uploaded file was invalid. Supported file extensions are .csv.")
end
end
end
context 'when work items import/export feature is not available' do
before do
stub_feature_flags(import_export_work_items_csv: false)
end
it_behaves_like 'response with 404 status'
end
end
end
context 'with public project' do
let_it_be(:project) { create(:project, :public) }
it_behaves_like 'handles authorisation'
end
context 'with private project' do
it_behaves_like 'handles authorisation'
end
def upload_file(file, headers = {}, params = {})
workhorse_finalize(
import_csv_project_work_items_path(project),
method: :post,
file_key: :file,
params: params.merge(file: file),
headers: headers,
send_rewritten_field: true
)
end
end
describe 'POST #authorize' do
subject do
post import_csv_authorize_project_work_items_path(project),
headers: workhorse_headers
end
before do
sign_in(current_user)
end
context 'with authorized user' do
before do
project.add_reporter(current_user)
end
context 'when work items import/export feature is enabled' do
let(:user) { current_user }
it_behaves_like 'handle uploads authorize request' do
let(:uploader_class) { FileUploader }
let(:maximum_size) { Gitlab::CurrentSettings.max_attachment_size.megabytes }
end
end
context 'when work items import/export feature is disabled' do
before do
stub_feature_flags(import_export_work_items_csv: false)
end
it_behaves_like 'response with 404 status'
end
end
context 'with unauthorized user' do
it_behaves_like 'response with 404 status'
end
end
end
|