Tests for attachments controller

master
Alinson S. Xavier 10 years ago
parent 0cb363a314
commit 65c8cd05e2

@ -14,146 +14,145 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
#require File.dirname(__FILE__) + '/../test_helper' require File.dirname(__FILE__) + '/../test_helper'
#require 'attachments_controller' require 'attachments_controller'
#
## Re-raise errors caught by the controller. # Re-raise errors caught by the controller.
#class AttachmentsController; def rescue_action(e) raise e end; end class AttachmentsController; def rescue_action(e) raise e end; end
#
#class AttachmentsControllerTest < ActionController::TestCase class AttachmentsControllerTest < ActionController::TestCase
# fixtures :attachments fixtures :attachments
#
# def setup def setup
# @course = Course.find(:first) @course = Course.first
# @data = fixture_file_upload('/files/attachment.txt', 'text/plain') @data = fixture_file_upload('/files/attachment.txt', 'text/plain')
# @att = @course.attachments.create(:file => @data, :file_name => 'attachment.txt', @att = @course.attachments.create(:file => @data, :file_name => 'attachment.txt',
# :description => 'hello world', :path => "", :front_page => true) :description => 'hello world', :path => "", :front_page => true)
# @att.save! @att.save!
# end end
#
# def teardown def teardown
# @data.close! @data.close!
# end end
#
# context "An anonymous user" do context "An anonymous user" do
#
# should_request_login_on_post_to(:new, {:course_id => 1}) should_request_login_on_post_to(:new, {:course_id => 1})
# should_request_login_on_post_to(:create, {:course_id => 1}) should_request_login_on_post_to(:create, {:course_id => 1})
# should_request_login_on_post_to(:edit, {:course_id => 1, :id => 1}) should_request_login_on_post_to(:edit, {:course_id => 1, :id => 1})
# should_request_login_on_post_to(:update, {:course_id => 1, :id => 1}) should_request_login_on_post_to(:update, {:course_id => 1, :id => 1})
# should_request_login_on_post_to(:destroy, {:course_id => 1, :id => 1}) should_request_login_on_post_to(:destroy, {:course_id => 1, :id => 1})
#
# context "on get to :show" do context "on get to :show" do
# setup { get :show, :course_id => @course.id, :id => @att.id } setup { get :show, :course_id => @course.id, :id => @att.id }
#
# should respond_with :success should respond_with :success
#
# should "link to the attachment" do should "link to the attachment" do
# assert_select 'a[href=?]', download_course_attachment_url(@course, @att) assert_select 'a[href=?]', download_course_attachment_url(@course, @att)
# end end
# end end
# end end
#
# context "An authenticated user" do context "An authenticated user" do
# setup { login_as :bob } setup { login_as :bob }
#
# context "on get to :new" do context "on get to :new" do
# setup { get :new, :course_id => @course.id } setup { get :new, :course_id => @course.id }
# #should render_a_form #should render_a_form
# should respond_with :success should respond_with :success
# end end
#
# context "on post to :create" do context "on post to :create" do
# setup do setup do
# assert_nil @course.attachments.find_by_description('test') assert_nil @course.attachments.find_by_description('test')
# post :create, :course_id => @course.id, :attachment => { :description => 'test', :file => @data, :path => "", :front_page => 't' } post :create, :course_id => @course.id, :attachment => { :description => 'test', :file => @data, :path => "", :front_page => 't' }
# @att = @course.attachments.find_by_description('test') @att = @course.attachments.find_by_description('test')
# end end
#
# should "create a new attachment" do should "create a new attachment" do
# assert @att assert @att
# end end
#
# should set_the_flash.to(/created/i) should set_flash.to(/created/i)
# should redirect_to('the attachment') { course_attachment_url(@course, @att) } should redirect_to('the attachment') { course_attachment_url(@course, @att) }
# should_create_log_entry {[ AttachmentCreateLogEntry, @att.id, users(:bob).id ]} should_create_log_entry {[ AttachmentCreateLogEntry, @att.id, users(:bob).id ]}
# end end
#
# context "on get to :edit" do context "on get to :edit" do
# setup { get :edit, :course_id => @course.id, :id => @att.id } setup { get :edit, :course_id => @course.id, :id => @att.id }
# #should render_a_form #should render_a_form
# should render_template 'edit' should render_template 'edit'
# end end
#
# context "on post to :update" do context "on post to :update" do
# context "with unmodified data" do context "with unmodified data" do
# setup do setup do
# post :update, :course_id => @course.id, :id => @att.id, :attachment => { :description => @att.description, :path => "", :front_page => 't' } post :update, :course_id => @course.id, :id => @att.id, :attachment => { :description => @att.description, :path => "", :front_page => 't' }
# end end
#
# should_not set_the_flash should_not set_flash
# should redirect_to('the attachment') { course_attachment_url(@course, @att) } should redirect_to('the attachment') { course_attachment_url(@course, @att) }
#
# should "not create a new log entry" do should "not create a new log entry" do
# assert_nil AttachmentEditLogEntry.find(:first, :conditions => { :target_id => @att.id }) assert_nil AttachmentEditLogEntry.first(:conditions => { :target_id => @att.id })
# end end
# end end
#
# context "with new description only" do context "with new description only" do
# setup do setup do
# post :update, :course_id => @course.id, :id => @att.id, :attachment => { :description => 'new description', :front_page => 't' } post :update, :course_id => @course.id, :id => @att.id, :attachment => { :description => 'new description', :front_page => 't' }
# end end
# should set_the_flash.to(/updated/i) should set_flash.to(/updated/i)
# should redirect_to('the attachment') { course_attachment_url(@course, @att) } should redirect_to('the attachment') { course_attachment_url(@course, @att) }
# should_create_log_entry {[ AttachmentEditLogEntry, @att.id, users(:bob).id ]} should_create_log_entry {[ AttachmentEditLogEntry, @att.id, users(:bob).id ]}
# end end
#
# context "with new file" do context "with new file" do
# setup do setup do
# @new_data = fixture_file_upload('/files/another_attachment.txt', 'plain/text') @new_data = fixture_file_upload('/files/another_attachment.txt', 'plain/text')
# post :update, :course_id => @course.id, :id => @att.id, :attachment => { :data => @new_data, :front_page => 't' } post :update, :course_id => @course.id, :id => @att.id, :attachment => { :data => @new_data, :front_page => 't' }
# end end
# teardown do teardown do
# @new_data.close! @new_data.close!
# end end
# should set_the_flash.to(/updated/i) should set_flash.to(/updated/i)
# should redirect_to('the attachment') { course_attachment_url(@course, @att) } should redirect_to('the attachment') { course_attachment_url(@course, @att) }
# should_create_log_entry {[ AttachmentEditLogEntry, @att.id, users(:bob).id ]} should_create_log_entry {[ AttachmentEditLogEntry, @att.id, users(:bob).id ]}
# end end
# end end
#
# context "on post to :destroy" do context "on post to :destroy" do
# setup { post :destroy, :course_id => @course.id, :id => @att.id } setup { post :destroy, :course_id => @course.id, :id => @att.id }
#
# should set_the_flash.to(/removed/i) should set_flash.to(/removed/i)
# should redirect_to('the course page'){ course_url(@course) } should redirect_to('the course page'){ course_url(@course) }
# should_create_log_entry {[ AttachmentDeleteLogEntry, @att.id, users(:bob).id ]} should_create_log_entry {[ AttachmentDeleteLogEntry, @att.id, users(:bob).id ]}
#
# should "destroy the attachment" do should "destroy the attachment" do
# @att = Attachment.find_with_deleted(@att.id) @att = Attachment.with_deleted.find(@att.id)
# assert @att.deleted? assert @att.deleted?
# end end
# end end
#
# #context "on post to :undelete" do #context "on post to :undelete" do
# # setup do # setup do
# # @att.destroy # @att.destroy
# # post :undelete, :course_id => @course.id, :id => @att.id # post :undelete, :course_id => @course.id, :id => @att.id
# # end # end
#
# # should set_the_flash.to(/restored/i) # should set_flash.to(/restored/i)
# # should redirect_to('the attachment'){ course_attachment_url(@course, @att) } # should redirect_to('the attachment'){ course_attachment_url(@course, @att) }
# # should_create_log_entry {[ AttachmentRestoreLogEntry, @att.id, users(:bob).id ]} # should_create_log_entry {[ AttachmentRestoreLogEntry, @att.id, users(:bob).id ]}
#
# # should "restore the attachment" do # should "restore the attachment" do
# # assert Attachment.find(@att.id) # assert Attachment.find(@att.id)
# # end # end
# #end #end
#
# context "on get to :download" do context "on get to :download" do
# setup { get :download, :course_id => @course.id, :id => @att.id } setup { get :download, :course_id => @course.id, :id => @att.id }
# should respond_with :success should respond_with :success
# end end
# end end
#end end
#

@ -10,6 +10,15 @@ class ActiveSupport::TestCase
fixtures :all fixtures :all
# Add more helper methods to be used by all tests here... # Add more helper methods to be used by all tests here...
def login_as(user)
@request.session[:user_id] = users(user).id
@request.env["HTTP_AUTHORIZATION"] = user ? "Basic #{Base64.encode64("#{users(user).login}:test")}" : nil
end
def logout
@request.session[:user_id] = nil
@request.env["HTTP_AUTHORIZATION"] = nil
end
end end
class Test::Unit::TestCase class Test::Unit::TestCase
@ -31,7 +40,7 @@ class Test::Unit::TestCase
def self.should_create_log_entry(&block) def self.should_create_log_entry(&block)
should "create log entry" do should "create log entry" do
log_entry_class, target_id, user_id = instance_eval(&block) log_entry_class, target_id, user_id = instance_eval(&block)
assert log_entry_class.find(:first, :conditions => { :user_id => user_id, :target_id => target_id }) assert log_entry_class.find(:first, :conditions => {:user_id => user_id, :target_id => target_id})
end end
end end
end end