From f0394466e3cbbffaf6ce75ff3d585e93c1fbe582 Mon Sep 17 00:00:00 2001 From: Alinson Santos Date: Thu, 6 Aug 2009 10:10:37 -0300 Subject: [PATCH] Testes para AttachmentsController --- app/controllers/application_controller.rb | 2 +- app/controllers/attachments_controller.rb | 45 +++--- app/controllers/wiki_controller.rb | 2 +- app/models/attachment.rb | 1 + app/views/attachments/show.html.haml | 2 +- config/application.rb | 2 +- test/fixtures/courses.yml | 6 + test/fixtures/files/another_attachment.txt | 1 + test/fixtures/files/attachment.txt | 1 + .../functional/attachments_controller_test.rb | 128 ++++++++++++++++++ test/functional/courses_controller_test.rb | 20 ++- test/functional/wiki_controller_test.rb | 2 +- test/shoulda_macros/custom.rb | 15 ++ 13 files changed, 199 insertions(+), 28 deletions(-) create mode 100644 test/fixtures/files/another_attachment.txt create mode 100644 test/fixtures/files/attachment.txt create mode 100644 test/shoulda_macros/custom.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 136b738..7a7fa00 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -25,7 +25,7 @@ class ApplicationController < ActionController::Base before_filter :set_timezone # 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 def rescue_action(exception) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 1619803..f2a64d6 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -23,6 +23,10 @@ class AttachmentsController < ApplicationController #after_filter :cache_sweep, :only => [ :create, :update, :destroy ] def show + respond_to do |format| + format.html + format.xml { render :xml => @attachment } + end end def new @@ -31,22 +35,20 @@ class AttachmentsController < ApplicationController def create @attachment.course_id = @course.id @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_name = params[:attachment][:file].original_filename @attachment.content_type = params[:attachment][:file].content_type end + @attachment.save! - # Verifica se o arquivo ja esta associado a outro anexo - #file_path = "#{RAILS_ROOT}/public/upload/#{@course.id}/#{@attachment.file_name}" - #@attachment.errors.add("file", "already exists") if File.exists?(file_path) + AttachmentCreateLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course) + flash[:notice] = 'Attachment created'[] - if @attachment.save - AttachmentCreateLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course) - flash[:notice] = 'Attachment created'[] - redirect_to :action => 'show', :id => @attachment.id - else - render :action => 'new' + 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 @@ -55,19 +57,23 @@ class AttachmentsController < ApplicationController def update @attachment.description = params[:attachment][:description] - unless params[:attachment][:file].kind_of?(String) + unless params[:attachment][:file].nil? @attachment.file = params[:attachment][:file] @attachment.file_name = params[:attachment][:file].original_filename @attachment.content_type = params[:attachment][:file].content_type end + changed = @attachment.changed? - if @attachment.save - AttachmentEditLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course) + if changed @attachment.last_modified = Time.now.utc + @attachment.save! + AttachmentEditLogEntry.create!(:target_id => @attachment.id, :user => @current_user, :course => @course) flash[:notice] = 'Attachment updated'[] - redirect_to :action => 'show', :id => @attachment.id - else - render :action => 'edit' + end + + 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 @@ -75,8 +81,13 @@ class AttachmentsController < ApplicationController @attachment.destroy flash[:notice] = 'Attachment removed'[] flash[:undo] = undelete_course_attachment_url(@course, @attachment) + 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 def download diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 7843b0e..a3000ed 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -73,9 +73,9 @@ class WikiController < ApplicationController @wiki_page.user_id = session[:user_id] @wiki_page.course_id = @course.id changed = @wiki_page.changed? - @wiki_page.save! if changed + @wiki_page.save! WikiEditLogEntry.create!(:target_id => @wiki_page.id, :user => @current_user, :course => @course, :version => @wiki_page.version) flash[:notice] = "Wiki page updated"[] end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 8e7e42b..20ec5e6 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -31,6 +31,7 @@ class Attachment < ActiveRecord::Base def file=(new_file) @tmp_file = new_file self.size = new_file.size + self.content_type ||= "application/octet-stream" end protected diff --git a/app/views/attachments/show.html.haml b/app/views/attachments/show.html.haml index b851447..d8435ac 100644 --- a/app/views/attachments/show.html.haml +++ b/app/views/attachments/show.html.haml @@ -8,7 +8,7 @@ %p %dl %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 %dd= h(@attachment.description) diff --git a/config/application.rb b/config/application.rb index 313f2f4..aa0e1bc 100644 --- a/config/application.rb +++ b/config/application.rb @@ -6,7 +6,7 @@ App.webmaster_email = "webmaster@wikiufc.gelsol.org" App.default_host = "wikiufc.gelsol.org" App.base_path = "" -App.current_period = "2008.2" +App.current_period = "2009.2" # Limites App.max_upload_file_size = 5.megabytes diff --git a/test/fixtures/courses.yml b/test/fixtures/courses.yml index 3d46a47..7f4aa54 100644 --- a/test/fixtures/courses.yml +++ b/test/fixtures/courses.yml @@ -21,3 +21,9 @@ course_1: description: Description goes here period: <%= App.current_period %> +old_course: + id: 2 + short_name: old_course + full_name: Old Course + description: Description goes here + period: 1970.1 diff --git a/test/fixtures/files/another_attachment.txt b/test/fixtures/files/another_attachment.txt new file mode 100644 index 0000000..e331e8f --- /dev/null +++ b/test/fixtures/files/another_attachment.txt @@ -0,0 +1 @@ +Another hello world. diff --git a/test/fixtures/files/attachment.txt b/test/fixtures/files/attachment.txt new file mode 100644 index 0000000..18249f3 --- /dev/null +++ b/test/fixtures/files/attachment.txt @@ -0,0 +1 @@ +Hello world. diff --git a/test/functional/attachments_controller_test.rb b/test/functional/attachments_controller_test.rb index 12bdd19..a0fb9ef 100644 --- a/test/functional/attachments_controller_test.rb +++ b/test/functional/attachments_controller_test.rb @@ -25,6 +25,134 @@ class AttachmentsControllerTest < ActionController::TestCase def setup @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 diff --git a/test/functional/courses_controller_test.rb b/test/functional/courses_controller_test.rb index 095499d..92b18c0 100644 --- a/test/functional/courses_controller_test.rb +++ b/test/functional/courses_controller_test.rb @@ -23,14 +23,22 @@ require 'courses_controller' class CoursesControllerTest < ActionController::TestCase def setup - @controller = CoursesController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - @course = Course.find(:first) + @course = courses(:course_1) + @old_course = courses(:old_course) + LogEntry.delete_all end - def test_truth - assert true + context "An anonymous user" do + 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 # REST - usuários autenticados diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index ccab036..6a5544a 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -251,6 +251,7 @@ class WikiControllerTest < ActionController::TestCase post :undelete, :course_id => @course.id, :id => @wiki_page.id end + should_set_the_flash_to(/restored/i) should_redirect_to('the wiki page') { course_wiki_instance_url(@course, @wiki_page) } should_create_log_entry {[ WikiRestoreLogEntry, @wiki_page.id, users(:bob).id ]} @@ -261,7 +262,6 @@ class WikiControllerTest < ActionController::TestCase end - #def test_should_accept_text_on_show # get :show, :format => 'txt', :course_id => 1, :id => @wiki_page.id # assert_formatted_response :text diff --git a/test/shoulda_macros/custom.rb b/test/shoulda_macros/custom.rb new file mode 100644 index 0000000..51e074c --- /dev/null +++ b/test/shoulda_macros/custom.rb @@ -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