Skip to content

Commit

Permalink
Test file upload
Browse files Browse the repository at this point in the history
This additional test ensures that the file uploading API works.

Refs sul-dlss/happy-heron#3075
  • Loading branch information
edsu committed Jun 9, 2023
1 parent 45e7b45 commit f6e71fc
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions spec/requests/direct_uploads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
require 'rails_helper'

RSpec.describe 'Direct upload' do
let(:json) do
'{"blob":{"filename":"Gemfile.lock","byte_size":1751,"checksum":"vQ0xN+GwJBg9iEAcD4v73g==",' \
'"content_type":"text/html"}}'
end
let(:filename) { 'test.txt' }
let(:data) { 'text' }
let(:checksum) { OpenSSL::Digest::MD5.base64digest(data) }
let(:byte_size) { data.length }
let(:content_type) { 'text/plain' }
let(:json) { JSON.dump({ blob: { filename:, byte_size:, checksum:, content_type: } }) }

context 'when unauthorized' do
it 'returns 401' do
Expand All @@ -25,4 +27,54 @@
expect(response).to be_successful
end
end

context 'when uploading a file' do
it 'returns 204' do
post '/v1/direct_uploads',
params: json,
headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{jwt}" }

expect(response).to be_successful
expect(response.media_type).to eq 'application/json'
direct_upload = response.parsed_body

signed_id = direct_upload['signed_id']
expect(signed_id).to be_truthy

direct_upload_uri = URI.parse(direct_upload['direct_upload']['url'])
expect(direct_upload_uri.path).to start_with('/v1/disk/')

put direct_upload_uri.path,
params: data,
headers: { 'Content-Type' => content_type, 'Authorization' => "Bearer #{jwt}" }

expect(response).to have_http_status(:no_content) # Status: 204
end
end

context 'when uploading a invalid json file with application/json' do
let(:content_type) { 'application/json' }

it 'returns 204' do
post '/v1/direct_uploads',
params: json,
headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{jwt}" }

expect(response).to be_successful
expect(response.media_type).to eq 'application/json'
direct_upload = response.parsed_body

signed_id = direct_upload['signed_id']
expect(signed_id).to be_truthy

direct_upload_uri = URI.parse(direct_upload['direct_upload']['url'])
expect(direct_upload_uri.path).to start_with('/v1/disk/')

put direct_upload_uri.path,
params: data,
headers: { 'Content-Type' => content_type, 'Authorization' => "Bearer #{jwt}" }

expect(response).to have_http_status(:no_content) # Status: 204
end
end
end

0 comments on commit f6e71fc

Please sign in to comment.