summaryrefslogtreecommitdiff
path: root/spec/ruby/library/net/ftp/login_spec.rb
blob: 9768d9cf3310cf911099f8c628b1cae7e9d414f7 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require File.expand_path('../../../../spec_helper', __FILE__)
require File.expand_path('../spec_helper', __FILE__)
require File.expand_path('../fixtures/server', __FILE__)

describe "Net::FTP#login" do
  before :each do
    @server = NetFTPSpecs::DummyFTP.new
    @server.serve_once

    @ftp = Net::FTP.new
    @ftp.connect(@server.hostname, @server.server_port)
  end

  after :each do
    @ftp.quit rescue nil
    @ftp.close
    @server.stop
  end

  describe "when passed no arguments" do
    it "sends the USER command with 'anonymous' as name to the server" do
      @ftp.login
      @server.login_user.should == "anonymous"
    end

    it "sends 'anonymous@' as a password when required" do
      @server.should_receive(:user).and_respond("331 User name okay, need password.")
      @ftp.login
      @server.login_pass.should == "anonymous@"
    end

    it "raises a Net::FTPReplyError when the server requests an account" do
      @server.should_receive(:user).and_respond("331 User name okay, need password.")
      @server.should_receive(:pass).and_respond("332 Need account for login.")
      lambda { @ftp.login }.should raise_error(Net::FTPReplyError)
    end
  end

  describe "when passed name" do
    it "sends the USER command with the passed name to the server" do
      @ftp.login("rubyspec")
      @server.login_user.should == "rubyspec"
    end

    it "raises a Net::FTPReplyError when the server requests a password, but none was given" do
      @server.should_receive(:user).and_respond("331 User name okay, need password.")
      lambda { @ftp.login("rubyspec") }.should raise_error(Net::FTPReplyError)
    end

    it "raises a Net::FTPReplyError when the server requests an account, but none was given" do
      @server.should_receive(:user).and_respond("331 User name okay, need password.")
      @server.should_receive(:pass).and_respond("332 Need account for login.")
      lambda { @ftp.login("rubyspec") }.should raise_error(Net::FTPReplyError)
    end
  end

  describe "when passed name, password" do
    it "sends the USER command with the passed name to the server" do
      @ftp.login("rubyspec", "rocks")
      @server.login_user.should == "rubyspec"
    end

    it "sends the passed password when required" do
      @server.should_receive(:user).and_respond("331 User name okay, need password.")
      @ftp.login("rubyspec", "rocks")
      @server.login_pass.should == "rocks"
    end

    it "raises a Net::FTPReplyError when the server requests an account" do
      @server.should_receive(:user).and_respond("331 User name okay, need password.")
      @server.should_receive(:pass).and_respond("332 Need account for login.")
      lambda { @ftp.login("rubyspec", "rocks") }.should raise_error(Net::FTPReplyError)
    end
  end

  describe "when passed name, password, account" do
    it "sends the USER command with the passed name to the server" do
      @ftp.login("rubyspec", "rocks", "account")
      @server.login_user.should == "rubyspec"
    end

    it "sends the passed password when required" do
      @server.should_receive(:user).and_respond("331 User name okay, need password.")
      @ftp.login("rubyspec", "rocks", "account")
      @server.login_pass.should == "rocks"
    end

    it "sends the passed account when required" do
      @server.should_receive(:user).and_respond("331 User name okay, need password.")
      @server.should_receive(:pass).and_respond("332 Need account for login.")
      @ftp.login("rubyspec", "rocks", "account")
      @server.login_acct.should == "account"
    end
  end

  describe "when the USER command fails" do
    it "raises a Net::FTPPermError when the response code is 500" do
      @server.should_receive(:user).and_respond("500 Syntax error, command unrecognized.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end

    it "raises a Net::FTPPermError when the response code is 501" do
      @server.should_receive(:user).and_respond("501 Syntax error in parameters or arguments.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end

    it "raises a Net::FTPPermError when the response code is 502" do
      @server.should_receive(:user).and_respond("502 Command not implemented.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end

    it "raises a Net::FTPTempError when the response code is 421" do
      @server.should_receive(:user).and_respond("421 Service not available, closing control connection.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPTempError)
    end

    it "raises a Net::FTPPermError when the response code is 530" do
      @server.should_receive(:user).and_respond("530 Not logged in.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end
  end

  describe "when the PASS command fails" do
    before :each do
      @server.should_receive(:user).and_respond("331 User name okay, need password.")
    end

    it "does not raise an Error when the response code is 202" do
      @server.should_receive(:pass).and_respond("202 Command not implemented, superfluous at this site.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should_not raise_error
    end

    it "raises a Net::FTPPermError when the response code is 500" do
      @server.should_receive(:pass).and_respond("500 Syntax error, command unrecognized.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end

    it "raises a Net::FTPPermError when the response code is 501" do
      @server.should_receive(:pass).and_respond("501 Syntax error in parameters or arguments.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end

    it "raises a Net::FTPPermError when the response code is 502" do
      @server.should_receive(:pass).and_respond("502 Command not implemented.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end

    it "raises a Net::FTPTempError when the response code is 421" do
      @server.should_receive(:pass).and_respond("421 Service not available, closing control connection.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPTempError)
    end

    it "raises a Net::FTPPermError when the response code is 530" do
      @server.should_receive(:pass).and_respond("530 Not logged in.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end
  end

  describe "when the ACCT command fails" do
    before :each do
      @server.should_receive(:user).and_respond("331 User name okay, need password.")
      @server.should_receive(:pass).and_respond("332 Need account for login.")
    end

    it "does not raise an Error when the response code is 202" do
      @server.should_receive(:acct).and_respond("202 Command not implemented, superfluous at this site.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should_not raise_error
    end

    it "raises a Net::FTPPermError when the response code is 500" do
      @server.should_receive(:acct).and_respond("500 Syntax error, command unrecognized.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end

    it "raises a Net::FTPPermError when the response code is 501" do
      @server.should_receive(:acct).and_respond("501 Syntax error in parameters or arguments.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end

    it "raises a Net::FTPPermError when the response code is 502" do
      @server.should_receive(:acct).and_respond("502 Command not implemented.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end

    it "raises a Net::FTPTempError when the response code is 421" do
      @server.should_receive(:acct).and_respond("421 Service not available, closing control connection.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPTempError)
    end

    it "raises a Net::FTPPermError when the response code is 530" do
      @server.should_receive(:acct).and_respond("530 Not logged in.")
      lambda { @ftp.login("rubyspec", "rocks", "account") }.should raise_error(Net::FTPPermError)
    end
  end
end