This repository has been archived by the owner on May 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
dropbox_spec.rb
112 lines (95 loc) · 3.66 KB
/
dropbox_spec.rb
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
require "spec_helper"
require "active_support/core_ext/object/blank"
require "active_support/core_ext/hash/except"
require "active_support/core_ext/hash/keys"
describe Paperclip::Storage::Dropbox, :vcr do
before do
@options = {}
end
def new_post(options = {})
Post.has_attached_file :attachment, @options
Post.new({attachment: uploaded_file("photo.jpg")}.merge(options))
end
describe "#dropbox_client" do
it "initializes a Dropbox client with credentials" do
client = new_post.attachment.dropbox_client
credentials = CREDENTIALS[:dropbox]
expect(client.session.consumer_key).to eq credentials[:app_key]
expect(client.session.consumer_secret).to eq credentials[:app_secret]
expect(client.session.access_token.key).to eq credentials[:access_token]
expect(client.session.access_token.secret).to eq credentials[:access_token_secret]
expect(client.root).to eq({"dropbox" => "dropbox", "app_folder" => "sandbox"}[credentials[:access_type]])
end
it "defaults :access_type to 'dropbox'" do
@options.update(dropbox_credentials: CREDENTIALS[:dropbox].except(:access_type))
expect(new_post.attachment.dropbox_client.root).to eq "dropbox"
end
end
describe "#flush_writes" do
it "uploads the attachment to Dropbox" do
post = new_post.tap(&:save)
expect(post.attachment.url).to be_an_existing_url
end
it "handles spaces in filenames" do
post = new_post(attachment: uploaded_file("photo with spaces.jpg")).tap(&:save)
expect(post.attachment.url).to be_an_existing_url
end
end
describe "#flush_deletes" do
it "deletes the attachment from Dropbox" do
post = new_post.tap(&:save)
url = post.attachment.url
post.destroy
expect(url).not_to be_an_existing_url
end
it "doesn't raise errors when file doesn't exist on Dropbox" do
post = new_post.tap(&:save)
post.attachment.dropbox_client.file_delete(post.attachment.path)
expect { post.destroy }.not_to raise_error
end
it "handles spaces in filenames" do
post = new_post(attachment: uploaded_file("photo with spaces.jpg")).tap(&:save)
url = post.attachment.url
post.destroy
expect(url).not_to be_an_existing_url
end
end
describe "#exists?" do
it "returns true if the file exists on Dropbox" do
post = new_post.tap(&:save)
expect(post.attachment.exists?).to be_true
end
it "returns false if the file doesn't exist on Dropbox" do
post = new_post
expect(post.attachment.exists?).to be_false
end
it "returns false if the attachment is blank" do
post = new_post(attachment: nil)
expect(post.attachment.exists?).to be_false
end
end
describe "path" do
it "adds 'Public' to path" do
@options.update(dropbox_visibility: "public")
new_post.attachment.path.include?("Public").should == true
end
it "does not add 'Public' to path" do
@options.update(dropbox_visibility: "private")
new_post.attachment.path.include?("Public").should == false
end
it "does not add 'Public' to app_folder path" do
@options.update(dropbox_credentials: CREDENTIALS[:dropbox].merge(:access_type => "app_folder"))
@options.update(dropbox_visibility: "public")
new_post.attachment.path.include?("Public").should == false
end
end
describe "#copy_to_local_file" do
it "copies file from Dropbox to a local file" do
post = new_post.tap(&:save)
destination = File.join(Bundler.root, "tmp/photo.jpg")
post.attachment.copy_to_local_file(destination)
expect(File.exists?(destination)).to be_true
File.delete(destination)
end
end
end