Testes para AttachmentsController

master
Alinson S. Xavier 16 years ago
parent 8611d1e9e6
commit f0394466e3

@ -25,7 +25,7 @@ class ApplicationController < ActionController::Base
before_filter :set_timezone before_filter :set_timezone
# Força o login para algumas áreas do sistema # Força o login para algumas áreas do sistema
before_filter :require_login, :only => [ :edit, :new, :create, :update, :delete, :destroy, :download ] before_filter :require_login, :only => [ :edit, :new, :create, :update, :delete, :destroy, :download, :undelete ]
protected protected
def rescue_action(exception) def rescue_action(exception)

@ -23,6 +23,10 @@ class AttachmentsController < ApplicationController
#after_filter :cache_sweep, :only => [ :create, :update, :destroy ] #after_filter :cache_sweep, :only => [ :create, :update, :destroy ]
def show def show
respond_to do |format|
format.html
format.xml { render :xml => @attachment }
end
end end
def new def new
@ -31,22 +35,20 @@ class AttachmentsController < ApplicationController
def create def create
@attachment.course_id = @course.id @attachment.course_id = @course.id
@attachment.description = params[:attachment][:description] @attachment.description = params[:attachment][:description]
unless params[:attachment][:file].kind_of?(String) @attachment.file_name = "blank"
unless params[:attachment][:file].nil?
@attachment.file = params[:attachment][:file] @attachment.file = params[:attachment][:file]
@attachment.file_name = params[:attachment][:file].original_filename @attachment.file_name = params[:attachment][:file].original_filename
@attachment.content_type = params[:attachment][:file].content_type @attachment.content_type = params[:attachment][:file].content_type
end end
@attachment.save!
# Verifica se o arquivo ja esta associado a outro anexo AttachmentCreateLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course)
#file_path = "#{RAILS_ROOT}/public/upload/#{@course.id}/#{@attachment.file_name}" flash[:notice] = 'Attachment created'[]
#@attachment.errors.add("file", "already exists") if File.exists?(file_path)
if @attachment.save respond_to do |format|
AttachmentCreateLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course) format.html { redirect_to course_attachment_url(@course, @attachment) }
flash[:notice] = 'Attachment created'[] format.xml { head :created, :location => course_attachment_url(@course, @attachment, :format => :xml) }
redirect_to :action => 'show', :id => @attachment.id
else
render :action => 'new'
end end
end end
@ -55,19 +57,23 @@ class AttachmentsController < ApplicationController
def update def update
@attachment.description = params[:attachment][:description] @attachment.description = params[:attachment][:description]
unless params[:attachment][:file].kind_of?(String) unless params[:attachment][:file].nil?
@attachment.file = params[:attachment][:file] @attachment.file = params[:attachment][:file]
@attachment.file_name = params[:attachment][:file].original_filename @attachment.file_name = params[:attachment][:file].original_filename
@attachment.content_type = params[:attachment][:file].content_type @attachment.content_type = params[:attachment][:file].content_type
end end
changed = @attachment.changed?
if @attachment.save if changed
AttachmentEditLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course)
@attachment.last_modified = Time.now.utc @attachment.last_modified = Time.now.utc
@attachment.save!
AttachmentEditLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course)
flash[:notice] = 'Attachment updated'[] flash[:notice] = 'Attachment updated'[]
redirect_to :action => 'show', :id => @attachment.id end
else
render :action => 'edit' respond_to do |format|
format.html { redirect_to course_attachment_url(@course, @attachment) }
format.xml { head :created, :location => course_attachment_url(@course, @attachment, :format => :xml) }
end end
end end
@ -75,8 +81,13 @@ class AttachmentsController < ApplicationController
@attachment.destroy @attachment.destroy
flash[:notice] = 'Attachment removed'[] flash[:notice] = 'Attachment removed'[]
flash[:undo] = undelete_course_attachment_url(@course, @attachment) flash[:undo] = undelete_course_attachment_url(@course, @attachment)
AttachmentDeleteLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course) AttachmentDeleteLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course)
redirect_to :controller => 'courses', :action => 'show', :id => @course
respond_to do |format|
format.html { redirect_to course_url(@course) }
format.xml { head :ok }
end
end end
def download def download

@ -73,9 +73,9 @@ class WikiController < ApplicationController
@wiki_page.user_id = session[:user_id] @wiki_page.user_id = session[:user_id]
@wiki_page.course_id = @course.id @wiki_page.course_id = @course.id
changed = @wiki_page.changed? changed = @wiki_page.changed?
@wiki_page.save!
if changed if changed
@wiki_page.save!
WikiEditLogEntry.create!(:target_id => @wiki_page.id, :user => @current_user, :course => @course, :version => @wiki_page.version) WikiEditLogEntry.create!(:target_id => @wiki_page.id, :user => @current_user, :course => @course, :version => @wiki_page.version)
flash[:notice] = "Wiki page updated"[] flash[:notice] = "Wiki page updated"[]
end end

@ -31,6 +31,7 @@ class Attachment < ActiveRecord::Base
def file=(new_file) def file=(new_file)
@tmp_file = new_file @tmp_file = new_file
self.size = new_file.size self.size = new_file.size
self.content_type ||= "application/octet-stream"
end end
protected protected

@ -8,7 +8,7 @@
%p %p
%dl %dl
%dt Arquivo %dt Arquivo
%dd= link_to h(@attachment.file_name), download_course_attachment_url %dd= link_to h(@attachment.file_name), download_course_attachment_url(@course, @attachment)
%dt Descrição %dt Descrição
%dd= h(@attachment.description) %dd= h(@attachment.description)

@ -6,7 +6,7 @@ App.webmaster_email = "webmaster@wikiufc.gelsol.org"
App.default_host = "wikiufc.gelsol.org" App.default_host = "wikiufc.gelsol.org"
App.base_path = "" App.base_path = ""
App.current_period = "2008.2" App.current_period = "2009.2"
# Limites # Limites
App.max_upload_file_size = 5.megabytes App.max_upload_file_size = 5.megabytes

@ -21,3 +21,9 @@ course_1:
description: Description goes here description: Description goes here
period: <%= App.current_period %> period: <%= App.current_period %>
old_course:
id: 2
short_name: old_course
full_name: Old Course
description: Description goes here
period: 1970.1

@ -0,0 +1 @@
Another hello world.

@ -0,0 +1 @@
Hello world.

@ -25,6 +25,134 @@ class AttachmentsControllerTest < ActionController::TestCase
def setup def setup
@course = Course.find(:first) @course = Course.find(:first)
@data = fixture_file_upload('/files/attachment.txt', 'text/plain')
@att = @course.attachments.create(:file => @data, :file_name => 'attachment.txt', :description => 'hello world')
@att.save!
end
def teardown
@data.close!
end
context "An anonymous user" do
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(:edit, {: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(:undelete, {:course_id => 1, :id => 1})
context "on get to :show" do
setup { get :show, :course_id => @course.id, :id => @att.id }
should_respond_with :success
should "link to the attachment" do
assert_select 'a[href=?]', download_course_attachment_url(@course, @att)
end
end
end
context "An authenticated user" do
setup { login_as :bob }
context "on get to :new" do
setup { get :new, :course_id => @course.id }
should_render_a_form
should_respond_with :success
end
context "on post to :create" do
setup do
assert_nil @course.attachments.find_by_description('test')
post :create, :course_id => @course.id, :attachment => { :description => 'test', :file => @data }
@att = @course.attachments.find_by_description('test')
end
should "create a new attachment" do
assert @att
end
should_set_the_flash_to(/created/i)
should_redirect_to('the attachment') { course_attachment_url(@course, @att) }
should_create_log_entry {[ AttachmentCreateLogEntry, @att.id, users(:bob).id ]}
end
context "on get to :edit" do
setup { get :edit, :course_id => @course.id, :id => @att.id }
should_render_a_form
should_render_template 'edit'
end
context "on post to :update" do
context "with unmodified data" do
setup do
post :update, :course_id => @course.id, :id => @att.id, :attachment => { :description => @att.description }
end
should_not_set_the_flash
should_redirect_to('the attachment') { course_attachment_url(@course, @att) }
should "not create a new log entry" do
assert_nil AttachmentEditLogEntry.find(:first, :conditions => { :target_id => @att.id })
end
end
context "with new description only" do
setup do
post :update, :course_id => @course.id, :id => @att.id, :attachment => { :description => 'new description' }
end
should_set_the_flash_to(/updated/i)
should_redirect_to('the attachment') { course_attachment_url(@course, @att) }
should_create_log_entry {[ AttachmentEditLogEntry, @att.id, users(:bob).id ]}
end
context "with new file" do
setup do
@new_data = fixture_file_upload('/files/another_attachment.txt', 'plain/text')
post :update, :course_id => @course.id, :id => @att.id, :attachment => { :data => @new_data }
end
teardown do
@new_data.close!
end
should_set_the_flash_to(/updated/i)
should_redirect_to('the attachment') { course_attachment_url(@course, @att) }
should_create_log_entry {[ AttachmentEditLogEntry, @att.id, users(:bob).id ]}
end
end
context "on post to :destroy" do
setup { post :destroy, :course_id => @course.id, :id => @att.id }
should_set_the_flash_to(/removed/i)
should_redirect_to('the course page'){ course_url(@course) }
should_create_log_entry {[ AttachmentDeleteLogEntry, @att.id, users(:bob).id ]}
should "destroy the attachment" do
@att = Attachment.find_with_deleted(@att.id)
assert @att.deleted?
end
end
context "on post to :undelete" do
setup do
@att.destroy
post :undelete, :course_id => @course.id, :id => @att.id
end
should_set_the_flash_to(/restored/i)
should_redirect_to('the attachment'){ course_attachment_url(@course, @att) }
should_create_log_entry {[ AttachmentRestoreLogEntry, @att.id, users(:bob).id ]}
should "restore the attachment" do
assert Attachment.find(@att.id)
end
end
context "on get to :download" do
setup { get :download, :course_id => @course.id, :id => @att.id }
should_respond_with :success
end
end end
end end

@ -23,14 +23,22 @@ require 'courses_controller'
class CoursesControllerTest < ActionController::TestCase class CoursesControllerTest < ActionController::TestCase
def setup def setup
@controller = CoursesController.new @course = courses(:course_1)
@request = ActionController::TestRequest.new @old_course = courses(:old_course)
@response = ActionController::TestResponse.new LogEntry.delete_all
@course = Course.find(:first)
end end
def test_truth context "An anonymous user" do
assert true context "on get to :index" do
setup { get :index }
should_respond_with :success
should_render_template 'index'
should "display the course list" do
assert_select 'a[href=?]', course_url(@course)
end
end
end end
# REST - usuários autenticados # REST - usuários autenticados

@ -251,6 +251,7 @@ class WikiControllerTest < ActionController::TestCase
post :undelete, :course_id => @course.id, :id => @wiki_page.id post :undelete, :course_id => @course.id, :id => @wiki_page.id
end end
should_set_the_flash_to(/restored/i)
should_redirect_to('the wiki page') { course_wiki_instance_url(@course, @wiki_page) } should_redirect_to('the wiki page') { course_wiki_instance_url(@course, @wiki_page) }
should_create_log_entry {[ WikiRestoreLogEntry, @wiki_page.id, users(:bob).id ]} should_create_log_entry {[ WikiRestoreLogEntry, @wiki_page.id, users(:bob).id ]}
@ -261,7 +262,6 @@ class WikiControllerTest < ActionController::TestCase
end end
#def test_should_accept_text_on_show #def test_should_accept_text_on_show
# get :show, :format => 'txt', :course_id => 1, :id => @wiki_page.id # get :show, :format => 'txt', :course_id => 1, :id => @wiki_page.id
# assert_formatted_response :text # assert_formatted_response :text

@ -0,0 +1,15 @@
class Test::Unit::TestCase
def self.should_request_login_on_post_to(action, params)
should "request login on post to #{action}" do
post action, params
assert_redirected_to login_url
end
end
def self.should_create_log_entry(&block)
should "create log entry" do
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 })
end
end
end