Skip to content

Commit

Permalink
Merge pull request #444 from bobbrodie/fix-file-open
Browse files Browse the repository at this point in the history
Resolve URI.open report
  • Loading branch information
bobbrodie authored Apr 29, 2024
2 parents b1e2b88 + 0e08cca commit 3e1deb0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
8 changes: 3 additions & 5 deletions lib/jira/resource/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def self.endpoint_name
end

def self.meta(client)
response = client.get(client.options[:rest_base_path] + '/attachment/meta')
response = client.get("#{client.options[:rest_base_path]}/attachment/meta")
parse_json(response.body)
end

Expand All @@ -42,7 +42,7 @@ def self.meta(client)
# @yieldparam [IO] file The IO object streaming the download.
def download_file(headers = {}, &block)
default_headers = client.options[:default_headers]
URI.open(content, default_headers.merge(headers), &block)
URI.parse(content).open(default_headers.merge(headers), &block)
end

# Downloads the file contents as a string object.
Expand All @@ -54,9 +54,7 @@ def download_file(headers = {}, &block)
# @param [Hash] headers Any additional headers to call Jira.
# @return [String,NilClass] The file contents.
def download_contents(headers = {})
download_file(headers) do |file|
file.read
end
download_file(headers, &:read)
end

def save!(attrs, path = url)
Expand Down
62 changes: 36 additions & 26 deletions spec/jira/resource/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
describe JIRA::Resource::Attachment do
subject(:attachment) do
JIRA::Resource::Attachment.new(
client,
issue: JIRA::Resource::Issue.new(client),
attrs: { 'author' => { 'foo' => 'bar' } }
client,
issue: JIRA::Resource::Issue.new(client),
attrs: { 'author' => { 'foo' => 'bar' } }
)
end

Expand Down Expand Up @@ -39,8 +39,8 @@

let(:response) do
double(
'response',
body: '{"enabled":true,"uploadLimit":10485760}'
'response',
body: '{"enabled":true,"uploadLimit":10485760}'
)
end

Expand All @@ -61,11 +61,10 @@

context 'there is an attachment on an issue' do
let(:client) do
JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false )
JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false)
end
let(:attachment_file_contents) { 'file contents' }
let(:file_target) { double(read: :attachment_file_contents) }
let(:attachment_url) { "https:jirahost/secure/attachment/32323/myfile.txt" }
let(:attachment_url) { 'https://localhost:2990/secure/attachment/32323/myfile.txt' }
subject(:attachment) do
JIRA::Resource::Attachment.new(
client,
Expand All @@ -74,24 +73,24 @@
)
end

before(:each) do
stub_request(:get, attachment_url).to_return(body: attachment_file_contents)
end

describe '.download_file' do
it 'passes file object to block' do
expect(URI).to receive(:open).with(attachment_url, anything).and_yield(file_target)
expect(URI).to receive(:parse).with(attachment_url).and_call_original

attachment.download_file do |file|
expect(file).to eq(file_target)
expect(file.read).to eq(attachment_file_contents)
end

end
end

describe '.download_contents' do
it 'downloads the file contents as a string' do
expect(URI).to receive(:open).with(attachment_url, anything).and_return(attachment_file_contents)

result_str = attachment.download_contents

expect(result_str).to eq(attachment_file_contents)
expect(URI).to receive(:parse).with(attachment_url).and_call_original
expect(attachment.download_contents).to eq(attachment_file_contents)
end
end
end
Expand Down Expand Up @@ -139,27 +138,34 @@
attrs: { 'author' => { 'foo' => 'bar' } }
)
end
let(:default_headers_given) { { 'authorization' => "Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2" } }
let(:default_headers_given) {
{
'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2'
}
}
let(:bearer_client) do
JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false,
default_headers: default_headers_given )
end
let(:merged_headers) do
{"Accept"=>"application/json", "X-Atlassian-Token"=>"nocheck"}.merge(default_headers_given)
{
'Accept' => 'application/json',
'X-Atlassian-Token' => 'nocheck'
}.merge(default_headers_given)
end

it 'passes the custom headers' do
expect(bearer_client.request_client).to receive(:request_multipart).with(anything, anything, merged_headers).and_return(response)
expect(bearer_client.request_client).to receive(:request_multipart)
.with(anything, anything, merged_headers)
.and_return(response)

bearer_attachment.save('file' => path_to_file)

end
end

end

describe '#save!' do
subject { attachment.save!('file' => path_to_file) }
subject { attachment.save!('file' => path_to_file) }

before do
allow(client).to receive(:post_multipart).and_return(response)
Expand Down Expand Up @@ -193,20 +199,24 @@
attrs: { 'author' => { 'foo' => 'bar' } }
)
end
let(:default_headers_given) { { 'authorization' => "Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2" } }
let(:default_headers_given) { { 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' } }
let(:bearer_client) do
JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false,
default_headers: default_headers_given )
end
let(:merged_headers) do
{"Accept"=>"application/json", "X-Atlassian-Token"=>"nocheck"}.merge(default_headers_given)
{
'Accept' => 'application/json',
'X-Atlassian-Token' => 'nocheck'
}.merge(default_headers_given)
end

it 'passes the custom headers' do
expect(bearer_client.request_client).to receive(:request_multipart).with(anything, anything, merged_headers).and_return(response)
expect(bearer_client.request_client).to receive(:request_multipart)
.with(anything, anything, merged_headers)
.and_return(response)

bearer_attachment.save!('file' => path_to_file)

end
end
end
Expand Down

0 comments on commit 3e1deb0

Please sign in to comment.