Testes para AttachmentsController
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
6
test/fixtures/courses.yml
vendored
6
test/fixtures/courses.yml
vendored
@@ -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
|
||||
|
||||
1
test/fixtures/files/another_attachment.txt
vendored
Normal file
1
test/fixtures/files/another_attachment.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
Another hello world.
|
||||
1
test/fixtures/files/attachment.txt
vendored
Normal file
1
test/fixtures/files/attachment.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
Hello world.
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
15
test/shoulda_macros/custom.rb
Normal file
15
test/shoulda_macros/custom.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user