diff options
Diffstat (limited to 'spec/support/shared_contexts/lib/gitlab/middleware/multipart_shared_contexts.rb')
-rw-r--r-- | spec/support/shared_contexts/lib/gitlab/middleware/multipart_shared_contexts.rb | 106 |
1 files changed, 76 insertions, 30 deletions
diff --git a/spec/support/shared_contexts/lib/gitlab/middleware/multipart_shared_contexts.rb b/spec/support/shared_contexts/lib/gitlab/middleware/multipart_shared_contexts.rb index f1554ea8e9f..ec5bea34e8b 100644 --- a/spec/support/shared_contexts/lib/gitlab/middleware/multipart_shared_contexts.rb +++ b/spec/support/shared_contexts/lib/gitlab/middleware/multipart_shared_contexts.rb @@ -1,42 +1,88 @@ # frozen_string_literal: true -RSpec.shared_context 'multipart middleware context' do - let(:app) { double(:app) } - let(:middleware) { described_class.new(app) } - let(:original_filename) { 'filename' } - - # Rails 5 doesn't combine the GET/POST parameters in - # ActionDispatch::HTTP::Parameters if action_dispatch.request.parameters is set: - # https://github.com/rails/rails/blob/aea6423f013ca48f7704c70deadf2cd6ac7d70a1/actionpack/lib/action_dispatch/http/parameters.rb#L41 - def get_params(env) - req = ActionDispatch::Request.new(env) - req.GET.merge(req.POST) +# This context provides one temporary file for the multipart spec +# +# Here are the available variables: +# - uploaded_file +# - uploaded_filepath +# - filename +# - remote_id +RSpec.shared_context 'with one temporary file for multipart' do |within_tmp_sub_dir: false| + let(:uploaded_filepath) { uploaded_file.path } + + around do |example| + Tempfile.open('uploaded_file2') do |tempfile| + @uploaded_file = tempfile + @filename = 'test_file.png' + @remote_id = 'remote_id' + + example.run + end end - def post_env(rewritten_fields, params, secret, issuer) - token = JWT.encode({ 'iss' => issuer, 'rewritten_fields' => rewritten_fields }, secret, 'HS256') - Rack::MockRequest.env_for( - '/', - method: 'post', - params: params, - described_class::RACK_ENV_KEY => token - ) + attr_reader :uploaded_file, :filename, :remote_id +end + +# This context provides two temporary files for the multipart spec +# +# Here are the available variables: +# - uploaded_file +# - uploaded_filepath +# - filename +# - remote_id +# - tmp_sub_dir (only when using within_tmp_sub_dir: true) +# - uploaded_file2 +# - uploaded_filepath2 +# - filename2 +# - remote_id2 +RSpec.shared_context 'with two temporary files for multipart' do + include_context 'with one temporary file for multipart' + + let(:uploaded_filepath2) { uploaded_file2.path } + + around do |example| + Tempfile.open('uploaded_file2') do |tempfile| + @uploaded_file2 = tempfile + @filename2 = 'test_file2.png' + @remote_id2 = 'remote_id2' + + example.run + end end - def with_tmp_dir(uploads_sub_dir, storage_path = '') - Dir.mktmpdir do |dir| - upload_dir = File.join(dir, storage_path, uploads_sub_dir) - FileUtils.mkdir_p(upload_dir) + attr_reader :uploaded_file2, :filename2, :remote_id2 +end + +# This context provides three temporary files for the multipart spec +# +# Here are the available variables: +# - uploaded_file +# - uploaded_filepath +# - filename +# - remote_id +# - tmp_sub_dir (only when using within_tmp_sub_dir: true) +# - uploaded_file2 +# - uploaded_filepath2 +# - filename2 +# - remote_id2 +# - uploaded_file3 +# - uploaded_filepath3 +# - filename3 +# - remote_id3 +RSpec.shared_context 'with three temporary files for multipart' do + include_context 'with two temporary files for multipart' - allow(Rails).to receive(:root).and_return(dir) - allow(Dir).to receive(:tmpdir).and_return(File.join(Dir.tmpdir, 'tmpsubdir')) - allow(GitlabUploader).to receive(:root).and_return(File.join(dir, storage_path)) + let(:uploaded_filepath3) { uploaded_file3.path } - Tempfile.open('top-level', upload_dir) do |tempfile| - env = post_env({ 'file' => tempfile.path }, { 'file.name' => original_filename, 'file.path' => tempfile.path }, Gitlab::Workhorse.secret, 'gitlab-workhorse') + around do |example| + Tempfile.open('uploaded_file3') do |tempfile| + @uploaded_file3 = tempfile + @filename3 = 'test_file3.png' + @remote_id3 = 'remote_id3' - yield dir, env - end + example.run end end + + attr_reader :uploaded_file3, :filename3, :remote_id3 end |