Testes para WikiController

This commit is contained in:
2009-07-19 09:34:41 -03:00
parent 8e9b698944
commit 8611d1e9e6
20 changed files with 369 additions and 245 deletions

View File

@@ -24,88 +24,7 @@ class AttachmentsControllerTest < ActionController::TestCase
fixtures :attachments
def setup
@controller = AttachmentsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
@course = Course.find(:first)
def test_truth
assert true
end
#
# def test_index
# get :index
# assert_response :success
# assert_template 'list'
# end
#
# def test_list
# get :list
#
# assert_response :success
# assert_template 'list'
#
# assert_not_nil assigns(:attachments)
# end
#
# def test_show
# get :show, :id => @first_id
#
# assert_response :success
# assert_template 'show'
#
# assert_not_nil assigns(:attachment)
# assert assigns(:attachment).valid?
# end
#
# def test_new
# get :new
#
# assert_response :success
# assert_template 'new'
#
# assert_not_nil assigns(:attachment)
# end
#
# def test_create
# num_attachments = Attachment.count
#
# post :create, :attachment => {}
#
# assert_response :redirect
# assert_redirected_to :action => 'list'
#
# assert_equal num_attachments + 1, Attachment.count
# end
#
# def test_edit
# get :edit, :id => @first_id
#
# assert_response :success
# assert_template 'edit'
#
# assert_not_nil assigns(:attachment)
# assert assigns(:attachment).valid?
# end
#
# def test_update
# post :update, :id => @first_id
# assert_response :redirect
# assert_redirected_to :action => 'show', :id => @first_id
# end
#
# def test_destroy
# assert_nothing_raised {
# Attachment.find(@first_id)
# }
#
# post :destroy, :id => @first_id
# assert_response :redirect
# assert_redirected_to :action => 'list'
#
# assert_raise(ActiveRecord::RecordNotFound) {
# Attachment.find(@first_id)
# }
# end
end

View File

@@ -29,25 +29,29 @@ class CoursesControllerTest < ActionController::TestCase
@course = Course.find(:first)
end
# REST - usuários autenticados
context "A user" do
#setup { login_as :bob }
should_be_restful do |resource|
resource.create.params = { :short_name => 'test', :full_name => 'test', :description => 'test' }
resource.update.params = { :short_name => 'test', :full_name => 'test', :description => 'test' }
end
def test_truth
assert true
end
# REST - usuários quaisquer
context "A stranger" do
setup { logout }
should_be_restful do |resource|
resource.create.params = { :short_name => 'test', :full_name => 'test', :description => 'test' }
resource.update.params = { :short_name => 'test', :full_name => 'test', :description => 'test' }
resource.denied.actions = [ :new, :edit, :create, :update, :destroy ]
resource.denied.redirect = "'/login'"
resource.denied.flash = /must be logged in/i
end
end
# REST - usuários autenticados
#context "A user" do
# #setup { login_as :bob }
# should_be_restful do |resource|
# resource.create.params = { :short_name => 'test', :full_name => 'test', :description => 'test' }
# resource.update.params = { :short_name => 'test', :full_name => 'test', :description => 'test' }
# end
#end
## REST - usuários quaisquer
#context "A stranger" do
# setup { logout }
# should_be_restful do |resource|
# resource.create.params = { :short_name => 'test', :full_name => 'test', :description => 'test' }
# resource.update.params = { :short_name => 'test', :full_name => 'test', :description => 'test' }
# resource.denied.actions = [ :new, :edit, :create, :update, :destroy ]
# resource.denied.redirect = "'/login'"
# resource.denied.flash = /must be logged in/i
# end
#end
end

View File

@@ -31,28 +31,28 @@ class EventsControllerTest < ActionController::TestCase
end
# REST - usuários autenticados
context "A user" do
setup { login_as :bob }
should_be_restful do |resource|
resource.parent = [ :course ]
resource.create.params = { :title => 'test', :time => Time.now, :description => 'test', :created_by => 1 }
resource.update.params = { :title => 'test', :time => Time.now, :description => 'test', :created_by => 1 }
#context "A user" do
# setup { login_as :bob }
# should_be_restful do |resource|
# resource.parent = [ :course ]
# resource.create.params = { :title => 'test', :time => Time.now, :description => 'test', :created_by => 1 }
# resource.update.params = { :title => 'test', :time => Time.now, :description => 'test', :created_by => 1 }
end
end
# end
#end
# REST - usuários quaisquer
context "A stranger" do
setup { logout }
should_be_restful do |resource|
resource.parent = [ :course ]
resource.create.params = { :title => 'test', :time => Time.now, :description => 'test', :created_by => 1 }
resource.update.params = { :title => 'test', :time => Time.now, :description => 'test', :created_by => 1 }
resource.denied.actions = [ :new, :edit, :create, :update, :destroy ]
resource.denied.redirect = "'/login'"
resource.denied.flash = /must be logged in/i
end
end
## REST - usuários quaisquer
#context "A stranger" do
# setup { logout }
# should_be_restful do |resource|
# resource.parent = [ :course ]
# resource.create.params = { :title => 'test', :time => Time.now, :description => 'test', :created_by => 1 }
# resource.update.params = { :title => 'test', :time => Time.now, :description => 'test', :created_by => 1 }
# resource.denied.actions = [ :new, :edit, :create, :update, :destroy ]
# resource.denied.redirect = "'/login'"
# resource.denied.flash = /must be logged in/i
# end
#end
def test_should_accept_icalendar_on_index
get :index, :format => 'ics', :course_id => 1

View File

@@ -31,32 +31,32 @@ class NewsControllerTest < ActionController::TestCase
end
# REST - usuários autenticados
context "A user" do
setup { login_as :bob }
should_be_restful do |resource|
resource.klass = News
resource.object = 'news'
resource.parent = [ :course ]
resource.create.params = { :title => 'test', :body => 'test', :receiver_id => 1 }
resource.update.params = { :title => 'test', :body => 'test', :receiver_id => 1 }
resource.destroy.redirect = "course_news_url(@course)"
end
end
#context "A user" do
# setup { login_as :bob }
# should_be_restful do |resource|
# resource.klass = News
# resource.object = 'news'
# resource.parent = [ :course ]
# resource.create.params = { :title => 'test', :body => 'test', :receiver_id => 1 }
# resource.update.params = { :title => 'test', :body => 'test', :receiver_id => 1 }
# resource.destroy.redirect = "course_news_url(@course)"
# end
#end
# REST - usuários quaisquer
context "A stranger" do
setup { logout }
should_be_restful do |resource|
resource.klass = News
resource.object = 'news'
resource.parent = [ :course ]
resource.create.params = { :title => 'test', :body => 'test', :receiver_id => 1 }
resource.update.params = { :title => 'test', :body => 'test', :receiver_id => 1 }
resource.denied.actions = [ :new, :edit, :create, :update, :destroy ]
resource.denied.redirect = "'/login'"
resource.denied.flash = /must be logged in/i
end
end
## REST - usuários quaisquer
#context "A stranger" do
# setup { logout }
# should_be_restful do |resource|
# resource.klass = News
# resource.object = 'news'
# resource.parent = [ :course ]
# resource.create.params = { :title => 'test', :body => 'test', :receiver_id => 1 }
# resource.update.params = { :title => 'test', :body => 'test', :receiver_id => 1 }
# resource.denied.actions = [ :new, :edit, :create, :update, :destroy ]
# resource.denied.redirect = "'/login'"
# resource.denied.flash = /must be logged in/i
# end
#end
def test_should_accept_rss_on_index
get :index, :format => 'rss', :course_id => 1

View File

@@ -22,44 +22,245 @@ class WikiController; def rescue_action(e) raise e end; end
class WikiControllerTest < ActionController::TestCase
def setup
@controller = WikiController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@course = Course.find(:first)
@wiki_page = @course.wiki_pages.create(:title => 'test1', :content => 'test1', :description => 'test', :version => 1)
@wiki_page.user = users(:bob)
@wiki_page = @course.wiki_pages.create(:title => 'test1', :content => 'content1',
:user_id => users(:bob).id, :description => 'test', :version => 1)
@wiki_page.save!
@wiki_page.title = 'new title'
@wiki_page.save!
@another_wiki_page = @course.wiki_pages.create(:title => 'another', :content => 'another',
:description => 'test', :user_id => users(:bob).id, :version => 1)
@another_wiki_page.move_to_bottom
@another_wiki_page.save!
@wiki_page.reload
LogEntry.delete_all
end
# REST - usuários autenticados
context "A user" do
setup { login_as :bob }
should_be_restful do |resource|
resource.klass = WikiPage
resource.parent = [ :course ]
resource.create.params = { :title => 'test2', :description => 'test', :content => 'test2', :course_id => 1 }
resource.update.params = { :title => 'test3', :description => 'test', :content => 'test3', :course_id => 1 }
resource.actions = [ :show, :new, :edit, :update, :create, :destroy ]
resource.destroy.redirect = "course_url(@course)"
resource.create.redirect = "course_wiki_url(@course, @wiki_page)"
resource.update.redirect = "course_wiki_url(@course, @wiki_page)"
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(:move_up, {:course_id => 1, :id => 1})
should_request_login_on_post_to(:move_down, {:course_id => 1, :id => 1})
should_request_login_on_post_to(:undelete, {:course_id => 1, :id => 1})
context "on get to :index" do
setup { get :index, :course_id => @course.id }
should_redirect_to('the course page') { course_url(@course) }
end
context "on get to :show" do
setup { get :show, :course_id => @course.id, :id => @wiki_page.id }
should_respond_with :success
should_render_template 'show'
should "show the wiki page" do
assert_select 'h1.title', @wiki_page.title
end
should "show the selected version" do
@wiki_page.revert_to(1)
get :show, :course_id => @course.id, :id => @wiki_page.id, :version => 1
assert_select 'h1.title', @wiki_page.title
end
end
context "on get to :versions" do
setup { get :versions, :course_id => @course.id, :id => @wiki_page.id }
should_respond_with :success
should_render_template 'versions'
should "show the wiki page versions" do
@wiki_page.versions.each do |v|
assert_select 'a[href=?]', course_wiki_instance_url(@course, @wiki_page, :version => v.version)
end
end
end
context "on get to :preview" do
context "with valid markup" do
setup { get :preview, :text => "hello {$x$} <script>foo();</script> <i onclick='foo()'>x</i>" }
should_respond_with :success
should "display latex formulas" do
assert_select 'img[class=tex_inline]'
end
should "strip harmful tags" do
assert_select 'script', false
assert_select '*[onclick]', false
end
end
context "with invalid markup" do
setup { get :preview, :text => "<a" }
should_respond_with :bad_request
end
end
context "on get to :diff" do
setup { get :diff, :course_id => @course.id, :id => @wiki_page.id, :from => 1, :to => 2 }
should_respond_with :success
should_assign_to :diff
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.wiki_pages.find_by_title('test2')
post :create, :course_id => @course.id, :wiki_page => { :title => 'test2', :content => 'test2' }
@wiki_page = @course.wiki_pages.find_by_title('test2')
end
should_set_the_flash_to(/created/i)
should_redirect_to('the wiki page') { course_wiki_instance_url(@course, @wiki_page) }
should_create_log_entry {[ WikiCreateLogEntry, @wiki_page.id, users(:bob).id ]}
should "create a new wiki page" do
assert @wiki_page
assert_equal @wiki_page.version, 1
assert_equal users(:bob).id, @wiki_page.user_id
end
end
context "on get to :edit" do
setup { get :edit, :course_id => @course.id, :id => @wiki_page.id }
should_render_a_form
should_render_template 'edit'
should "render a form with the correct fields" do
assert_select "input[name='wiki_page[title]'][value=?]", @wiki_page.title
assert_select "input[name='wiki_page[description]'][value=?]", ""
assert_select 'textarea', @wiki_page.content
end
should "edit the selected version" do
@wiki_page.revert_to(1)
get :edit, :course_id => @course.id, :id => @wiki_page.id, :version => 1
assert_select "input[name='wiki_page[title]'][value=?]", @wiki_page.title
assert_select 'textarea', @wiki_page.content
end
end
context "on post to :update" do
context "with unmodified data" do
setup do
post :update, :course_id => @course.id, :id => @wiki_page.id, :wiki_page => {
:title => @wiki_page.title, :content => @wiki_page.content}
end
should_not_set_the_flash
should_redirect_to('the wiki page') { course_wiki_instance_url(@course, @wiki_page) }
should "not create a new log entry" do
assert_nil WikiEditLogEntry.find(:first, :conditions => { :target_id => @wiki_page.id })
end
end
context "with new data" do
setup do
post :update, :course_id => @course.id, :id => @wiki_page.id, :wiki_page => {
:user_id => 999, :course_id => 999, # not user definable
:title => 'brand new title', :content => 'brand new content'}
@wiki_page.reload
end
should_set_the_flash_to(/updated/i)
should_redirect_to('the wiki page') { course_wiki_instance_url(@course, @wiki_page) }
should_create_log_entry {[ WikiEditLogEntry, @wiki_page.id, users(:bob).id ]}
should "update the wiki page" do
assert_equal "brand new title", @wiki_page.title
assert_equal "brand new content", @wiki_page.content
assert_equal users(:bob).id, @wiki_page.user_id
assert_equal @course.id, @wiki_page.course_id
end
end
end
context "on post to :destroy" do
setup { post :destroy, :course_id => @course.id, :id => @wiki_page.id }
should_set_the_flash_to(/removed/i)
should_redirect_to('the course page') { course_url(@course) }
should_create_log_entry {[ WikiDeleteLogEntry, @wiki_page.id, users(:bob).id ]}
should "delete the wiki page" do
@wiki_page = WikiPage.find_with_deleted(@wiki_page.id)
assert @wiki_page.deleted?
end
end
context "on get to :move_up" do
setup do
assert_equal 1, @wiki_page.position
assert_equal 2, @another_wiki_page.position
get :move_up, :course_id => @course.id, :id => @another_wiki_page.id
end
should_redirect_to('the course page') { course_url(@course) }
should "move the page up" do
@wiki_page.reload
@another_wiki_page.reload
assert_equal 2, @wiki_page.position
assert_equal 1, @another_wiki_page.position
end
end
context "on get to :move_down" do
setup do
assert_equal 1, @wiki_page.position
assert_equal 2, @another_wiki_page.position
get :move_down, :course_id => @course.id, :id => @wiki_page.id
end
should_redirect_to('the course page') { course_url(@course) }
should "move the page up" do
@wiki_page.reload
@another_wiki_page.reload
assert_equal 2, @wiki_page.position
assert_equal 1, @another_wiki_page.position
end
end
context "on post to :undelete" do
setup do
@wiki_page.destroy
post :undelete, :course_id => @course.id, :id => @wiki_page.id
end
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 "restore the wiki page" do
assert WikiPage.find(@wiki_page.id)
end
end
end
# REST - usuários quaisquer
#context "A stranger" do
# setup { logout }
# should_be_restful do |resource|
# resource.klass = WikiPage
# resource.parent = [ :course ]
# resource.create.params = { :title => 'test4', :description => 'test', :content => 'test4', :course_id => 1 }
# resource.update.params = { :title => 'test5', :description => 'test', :content => 'test5', :course_id => 1 }
# resource.actions = [ :show, :new, :edit, :update, :create, :destroy ]
# resource.denied.actions = [ :new, :edit, :create, :update, :destroy ]
# resource.denied.redirect = "'/login'"
# resource.denied.flash = /must be logged in/i
# end
#end
#def test_should_accept_text_on_show
# get :show, :format => 'txt', :course_id => 1, :id => @wiki_page.id

View File

@@ -18,6 +18,16 @@ require File.dirname(__FILE__) + '/../test_helper'
class WikiPageTest < ActiveSupport::TestCase
should "not delete versions on destroy" do
wp = WikiPage.new(:course_id => 1, :user_id => 1, :title => "t", :content => "c", :description => "d", :version => 1)
wp.save!
wp.destroy
wp = WikiPage.find_with_deleted(wp.id)
wp.recover!
assert !wp.versions.empty?
end
def test_should_create_new_version_when_editing
wp = WikiPage.new
assert !wp.save_version?