2008-12-26 04:25:27 +00:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2008-10-25 16:42:38 +00:00
|
|
|
|
2008-11-07 17:13:41 +00:00
|
|
|
describe "attach_file" do
|
2008-04-27 17:46:33 +00:00
|
|
|
before do
|
2008-12-26 04:25:27 +00:00
|
|
|
Webrat.configuration.mode = :rails
|
2008-04-27 17:46:33 +00:00
|
|
|
@filename = __FILE__
|
2008-07-25 23:11:56 +00:00
|
|
|
@uploaded_file = mock("uploaded file")
|
2008-11-23 05:22:49 +00:00
|
|
|
ActionController::TestUploadedFile.stub!(:new => @uploaded_file)
|
2008-04-27 17:46:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should fail if no file field found" do
|
2008-11-23 05:22:49 +00:00
|
|
|
with_html <<-HTML
|
2008-11-28 05:34:35 +00:00
|
|
|
<html>
|
2008-04-27 17:46:33 +00:00
|
|
|
<form method="post" action="/widgets">
|
|
|
|
</form>
|
2008-11-28 05:34:35 +00:00
|
|
|
</html>
|
2008-11-23 05:22:49 +00:00
|
|
|
HTML
|
2008-11-23 19:08:34 +00:00
|
|
|
lambda { attach_file("Doc", "/some/path") }.should raise_error(Webrat::NotFoundError)
|
2008-04-27 17:46:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should submit empty strings for blank file fields" do
|
2008-11-23 05:22:49 +00:00
|
|
|
with_html <<-HTML
|
2008-11-28 05:34:35 +00:00
|
|
|
<html>
|
2008-04-27 17:46:33 +00:00
|
|
|
<form method="post" action="/widgets">
|
|
|
|
<input type="file" id="widget_file" name="widget[file]" />
|
|
|
|
<input type="submit" />
|
|
|
|
</form>
|
2008-11-28 05:34:35 +00:00
|
|
|
</html>
|
2008-11-23 05:22:49 +00:00
|
|
|
HTML
|
2009-04-08 00:37:26 +00:00
|
|
|
webrat_session.should_receive(:post).with("http://www.example.com/widgets", { "widget" => { "file" => "" } })
|
2008-11-23 05:22:49 +00:00
|
|
|
click_button
|
2008-04-27 17:46:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should submit the attached file" do
|
2008-11-23 05:22:49 +00:00
|
|
|
with_html <<-HTML
|
2008-11-28 05:34:35 +00:00
|
|
|
<html>
|
2008-04-27 17:46:33 +00:00
|
|
|
<form method="post" action="/widgets">
|
|
|
|
<label for="widget_file">Document</label>
|
|
|
|
<input type="file" id="widget_file" name="widget[file]" />
|
|
|
|
<input type="submit" />
|
|
|
|
</form>
|
2008-11-28 05:34:35 +00:00
|
|
|
</html>
|
2008-11-23 05:22:49 +00:00
|
|
|
HTML
|
2009-04-08 00:37:26 +00:00
|
|
|
webrat_session.should_receive(:post).with("http://www.example.com/widgets", { "widget" => { "file" => @uploaded_file } })
|
2008-11-23 05:22:49 +00:00
|
|
|
attach_file "Document", @filename
|
|
|
|
click_button
|
2008-04-27 17:46:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should support collections" do
|
2008-11-23 05:22:49 +00:00
|
|
|
with_html <<-HTML
|
2008-11-28 05:34:35 +00:00
|
|
|
<html>
|
2008-04-27 17:46:33 +00:00
|
|
|
<form method="post" action="/widgets">
|
|
|
|
<label for="widget_file1">Document</label>
|
|
|
|
<input type="file" id="widget_file1" name="widget[files][]" />
|
|
|
|
<label for="widget_file2">Spreadsheet</label>
|
|
|
|
<input type="file" id="widget_file2" name="widget[files][]" />
|
|
|
|
<input type="submit" />
|
|
|
|
</form>
|
2008-11-28 05:34:35 +00:00
|
|
|
</html>
|
2008-11-23 05:22:49 +00:00
|
|
|
HTML
|
2009-04-08 00:37:26 +00:00
|
|
|
webrat_session.should_receive(:post).with("http://www.example.com/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } })
|
2008-11-23 05:22:49 +00:00
|
|
|
attach_file "Document", @filename
|
|
|
|
attach_file "Spreadsheet", @filename
|
|
|
|
click_button
|
2008-04-27 17:46:33 +00:00
|
|
|
end
|
2008-05-21 19:27:47 +00:00
|
|
|
|
|
|
|
it "should allow the content type to be specified" do
|
2008-11-23 05:22:49 +00:00
|
|
|
with_html <<-HTML
|
2008-11-28 05:34:35 +00:00
|
|
|
<html>
|
2008-05-21 19:27:47 +00:00
|
|
|
<form method="post" action="/widgets">
|
|
|
|
<label for="person_picture">Picture</label>
|
|
|
|
<input type="file" id="person_picture" name="person[picture]" />
|
|
|
|
<input type="submit" />
|
|
|
|
</form>
|
2008-11-28 05:34:35 +00:00
|
|
|
</html>
|
2008-11-23 05:22:49 +00:00
|
|
|
HTML
|
2008-07-25 23:11:56 +00:00
|
|
|
ActionController::TestUploadedFile.should_receive(:new).with(@filename, "image/png").any_number_of_times
|
2008-11-23 05:22:49 +00:00
|
|
|
attach_file "Picture", @filename, "image/png"
|
|
|
|
click_button
|
2008-05-21 19:27:47 +00:00
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
end
|