-
Notifications
You must be signed in to change notification settings - Fork 146
/
sftp_spec.rb
121 lines (101 loc) · 4.41 KB
/
sftp_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
113
114
115
116
117
118
119
120
121
require "spec_helper"
require 'net/ssh'
require 'net/sftp'
class Redcar::Project
describe Adapters::RemoteProtocols::SFTP do
let(:conn) { double('connection').as_null_object }
subject do
Adapters::RemoteProtocols::SFTP.new('server', 'user', 'secret', [])
end
before(:each) do
Net::SSH.stub!(:start).with('server', 'user', hash_including(:password => 'secret')).and_return(conn)
end
context "Public methods" do
describe '#directory?' do
it "fetches the folder if it's not the base folder" do
subject.stub!(:fetch).with("/home/fcoury").and_return([
{ :fullname => '/home/fcoury/hello_world.rb', :name => 'hello_world.rb', :type => :file },
{ :fullname => '/home/fcoury/snippets', :name => 'snippets', :type => :dir }
])
subject.directory?("/home/fcoury/hello_world.rb").should be_false
subject.directory?("/home/fcoury/snippets").should be_true
end
end
describe '#file?' do
it "fetches the folder for the file" do
subject.stub!(:fetch).with("/home/fcoury").and_return([
{ :fullname => '/home/fcoury/hello_world.rb', :name => 'hello_world.rb', :type => :file },
{ :fullname => '/home/fcoury/snippets', :name => 'snippets', :type => :dir }
])
subject.file?("/home/fcoury/hello_world.rb").should be_true
subject.file?("/home/fcoury/snippets").should be_false
end
end
describe '#load' do
let(:conn) { double('connection').as_null_object }
let(:sftp) { double('sftp connection').as_null_object }
subject do
Adapters::RemoteProtocols::SFTP.new('server', 'user', 'secret', []).tap do |protocol|
conn.stub!(:sftp).and_return(sftp)
protocol.stub!(:connection).and_return(conn)
end
end
before(:each) do
File.stub!(:open)
FileUtils.stub!(:mkdir_p)
end
it "creates the folder /tmp/hostname/file_path" do
FileUtils.should_receive(:mkdir_p).with('/tmp/server/home/fcoury')
subject.load('/home/fcoury/file.txt')
end
it "downloads the file to the local folder using SFTP" do
sftp.should_receive(:download!).with('/home/fcoury/file.txt', '/tmp/server/home/fcoury/file.txt')
subject.load('/home/fcoury/file.txt')
end
it "returns the downloaded file contents" do
file = double('File')
File.should_receive(:open).with('/tmp/server/home/fcoury/file.txt', 'rb').and_yield(file)
file.should_receive(:read).and_return "contents"
subject.load('/home/fcoury/file.txt').should == "contents"
end
end
describe '#save' do
let(:conn) { double('connection').as_null_object }
let(:sftp) { double('sftp connection').as_null_object }
subject do
Adapters::RemoteProtocols::SFTP.new('server', 'user', 'secret', []).tap do |protocol|
conn.stub!(:sftp).and_return(sftp)
protocol.stub!(:connection).and_return(conn)
end
end
before(:each) do
File.stub!(:open)
FileUtils.stub!(:mkdir_p)
end
it "write file contents" do
file = double('File')
File.should_receive(:open).with('/tmp/server/home/fcoury/file.txt', 'wb').and_yield(file)
file.should_receive(:print).with("contents")
subject.save('/home/fcoury/file.txt', 'contents')
end
it "uploads the local file to the remote path" do
sftp.should_receive(:upload!).with('/tmp/server/home/fcoury/file.txt', '/home/fcoury/file.txt')
subject.save('/home/fcoury/file.txt', 'contents')
end
it "returns the saved file" do
file = double('File').as_null_object
File.should_receive(:open).with('/tmp/server/home/fcoury/file.txt', 'wb').and_yield(file)
subject.save('/home/fcoury/file.txt', 'contents').should == file
end
end
end
context "Private methods" do
describe '#connection' do
it "delegates the connection to Remote.connect" do
Net::SSH.should_receive(:start).with('server', 'user', hash_including(:password => 'secret'))
subject.send(:connection)
end
end
end
end
end