Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

Commit f272590

Browse files
committed
Spec files', multiparts' #to_h and transfers' #to_h and #to_json
1 parent 73bfb10 commit f272590

File tree

4 files changed

+231
-7
lines changed

4 files changed

+231
-7
lines changed

lib/we_transfer/remote_file.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ module RemoteFile
33
class FileMismatchError < StandardError; end
44
class NoIoError < StandardError; end
55

6-
class Multipart < ::Ks.strict(:chunks, :chunk_size)
7-
def to_h
8-
%i[chunks chunk_size].each_with_object({}) do |prop, memo|
9-
memo[prop] = send(prop)
10-
end
11-
end
12-
end
6+
class Multipart < ::Ks.strict(:chunks, :chunk_size); end
137

148
def self.upgrade(files_response:, transfer:)
159
files_response.each do |file_response|
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
require 'spec_helper'
2+
3+
describe WeTransfer::Communication do
4+
before { described_class.logger = Logger.new(nil) }
5+
6+
# In this test we use the call to #find_transfer. This is NOT exemplary, it serves only
7+
# the purpose of doing a request/response cycle to handle specific status codes (2xx, 4xx, 50x)
8+
context ".ensure_ok_status!" do
9+
10+
let!(:authentication_stub) {
11+
stub_request(:post, "#{WeTransfer::Communication::API_URL_BASE}/v2/authorize")
12+
.to_return(status: 200, body: {token: "fake-test-token"}.to_json, headers: {})
13+
}
14+
15+
context "on a successful request" do
16+
let!(:find_transfer_stub) do
17+
stub_request(:get, "#{WeTransfer::Communication::API_URL_BASE}/v2/transfers/fake-transfer-id").
18+
to_return(
19+
status: 200,
20+
body: {
21+
id: "24cd3f4ccf15232e5660052a3688c03f20190221200022",
22+
state: "uploading",
23+
message: "test transfer",
24+
url: nil,
25+
files: [
26+
{
27+
id: "fake_file_id",
28+
name: "test file",
29+
size: 8,
30+
multipart: {
31+
part_numbers: 1,
32+
chunk_size: 8
33+
},
34+
type: "file",
35+
}
36+
],
37+
expires_at: "2019-02-28T20:00:22Z",
38+
}.to_json,
39+
headers: {},
40+
)
41+
end
42+
43+
it "does not raise a CommunicationError" do
44+
expect { described_class.find_transfer('fake-transfer-id') }
45+
.to_not raise_error(WeTransfer::CommunicationError)
46+
end
47+
end
48+
49+
context "on a client error" do
50+
let!(:find_transfer_stub) do
51+
stub_request(:get, "#{WeTransfer::Communication::API_URL_BASE}/v2/transfers/fake-transfer-id").
52+
to_return(
53+
status: 405,
54+
body: '{
55+
"success":false,
56+
"message":"fake message"
57+
}',
58+
headers: {},
59+
)
60+
end
61+
62+
it "raises a CommunicationError" do
63+
expect { described_class.find_transfer('fake-transfer-id') }
64+
.to raise_error(WeTransfer::CommunicationError)
65+
end
66+
67+
it "showing the error from the API to the user" do
68+
expect { described_class.find_transfer('fake-transfer-id') }
69+
.to raise_error('fake message')
70+
end
71+
end
72+
73+
context "on a server error" do
74+
let!(:find_transfer_stub) do
75+
stub_request(:get, "#{WeTransfer::Communication::API_URL_BASE}/v2/transfers/fake-transfer-id").
76+
to_return(
77+
status: 501,
78+
body: '',
79+
headers: {},
80+
)
81+
end
82+
83+
it "raises a CommunicationError" do
84+
expect { described_class.find_transfer('fake-transfer-id') }
85+
.to raise_error(WeTransfer::CommunicationError)
86+
end
87+
88+
it "is telling we can try again" do
89+
expect { described_class.find_transfer('fake-transfer-id') }
90+
.to raise_error(%r|had a 501 code.*could retry|)
91+
end
92+
93+
context "everything else" do
94+
let(:find_transfer_stub) do
95+
stub_request(:get, "#{WeTransfer::Communication::API_URL_BASE}/v2/transfers/fake-transfer-id").
96+
to_return(
97+
status: 302,
98+
body: '',
99+
headers: {},
100+
)
101+
end
102+
103+
it "raises a CommunicationError" do
104+
expect { described_class.find_transfer('fake-transfer-id') }
105+
.to raise_error(WeTransfer::CommunicationError)
106+
end
107+
108+
it "includes the error code in the message" do
109+
expect { described_class.find_transfer('fake-transfer-id') }
110+
.to raise_error(%r|had a 302 code|)
111+
end
112+
113+
it "informs the user we have no way how to continue" do
114+
expect { described_class.find_transfer('fake-transfer-id') }
115+
.to raise_error(%r|no idea what to do with that|)
116+
end
117+
end
118+
end
119+
end
120+
end

spec/we_transfer/transfer_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,73 @@
492492

493493
it "url?"
494494
end
495+
496+
describe "#to_h" do
497+
let(:transfer) { described_class.new(message: 'test transfer') }
498+
let(:file_stub_1) { [instance_double(WeTransfer::WeTransferFile, name: 'foo', size: 8)] }
499+
500+
it "has keys and values for id, state, url, message and files" do
501+
allow(file_stub_1)
502+
.to receive(:to_h)
503+
.and_return('fake-file-to_h')
504+
505+
allow(transfer)
506+
.to receive(:files)
507+
.and_return([file_stub_1])
508+
509+
allow(transfer)
510+
.to receive(:id)
511+
.and_return('fake-id')
512+
513+
allow(transfer)
514+
.to receive(:state)
515+
.and_return('fake-state')
516+
517+
allow(transfer)
518+
.to receive(:url)
519+
.and_return('fake-url')
520+
521+
expected = {
522+
id: "fake-id",
523+
state: "fake-state",
524+
url: "fake-url",
525+
message: "test transfer",
526+
files: ["fake-file-to_h"]
527+
}
528+
529+
expect(transfer.to_h)
530+
.to match(expected)
531+
end
532+
533+
it "calls :to_h on all files" do
534+
file_stub_2 = instance_double(WeTransfer::WeTransferFile, name: 'bar', size: 8)
535+
536+
allow(transfer)
537+
.to receive(:files)
538+
.and_return([file_stub_1, file_stub_2])
539+
540+
expect(file_stub_1)
541+
.to receive(:to_h)
542+
543+
expect(file_stub_2)
544+
.to receive(:to_h)
545+
546+
transfer.to_h
547+
end
548+
end
549+
550+
describe "#to_json" do
551+
it "converts the results of #to_h" do
552+
transfer = described_class.new(message: 'test transfer')
553+
554+
transfer_hash = { "foo" => "bar" }
555+
556+
allow(transfer)
557+
.to receive(:to_h)
558+
.and_return(transfer_hash)
559+
560+
expect(JSON.parse(transfer.to_json))
561+
.to eq(transfer_hash)
562+
end
563+
end
495564
end

spec/we_transfer/we_transfer_file_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,45 @@
5959
.to eq %|{"name":"test file","size":8}|
6060
end
6161
end
62+
63+
describe "#to_h" do
64+
let(:file) { described_class.new(name: 'foo', size: 8) }
65+
let(:multipart_stub) { instance_double(WeTransfer::RemoteFile::Multipart) }
66+
67+
it "has keys and values for id, name, size and multipart" do
68+
allow(file)
69+
.to receive(:multipart)
70+
.and_return(multipart_stub)
71+
72+
allow(file)
73+
.to receive(:id)
74+
.and_return('fake-id')
75+
76+
allow(multipart_stub)
77+
.to receive(:to_h)
78+
.and_return('fake-multipart')
79+
80+
expected = {
81+
id: 'fake-id',
82+
multipart: 'fake-multipart',
83+
size: 8,
84+
name: 'foo'
85+
86+
}
87+
88+
expect(file.to_h)
89+
.to match(expected)
90+
end
91+
92+
it "calls :to_h on multipart" do
93+
allow(file)
94+
.to receive(:multipart)
95+
.and_return(multipart_stub)
96+
97+
expect(multipart_stub)
98+
.to receive(:to_h)
99+
100+
file.to_h
101+
end
102+
end
62103
end

0 commit comments

Comments
 (0)