Tests for courses controller
This commit is contained in:
2
Gemfile
2
Gemfile
@@ -20,7 +20,7 @@ gem 'haml', '~>3.1'
|
||||
gem 'hpricot', '~>0.8'
|
||||
gem 'icalendar', '~>1.0'
|
||||
gem 'jquery-rails'
|
||||
gem 'shoulda', "~>2.11"
|
||||
gem 'shoulda', "~>3.5"
|
||||
gem 'will_paginate', "~>3.0"
|
||||
gem 'simplecov'
|
||||
|
||||
|
||||
@@ -109,7 +109,12 @@ GEM
|
||||
railties (~> 3.2.0)
|
||||
sass (>= 3.1.10)
|
||||
tilt (~> 1.3)
|
||||
shoulda (2.11.3)
|
||||
shoulda (3.5.0)
|
||||
shoulda-context (~> 1.0, >= 1.0.1)
|
||||
shoulda-matchers (>= 1.4.1, < 3.0)
|
||||
shoulda-context (1.2.1)
|
||||
shoulda-matchers (2.8.0)
|
||||
activesupport (>= 3.0.0)
|
||||
simplecov (0.10.0)
|
||||
docile (~> 1.1.0)
|
||||
json (~> 1.8)
|
||||
@@ -153,7 +158,7 @@ DEPENDENCIES
|
||||
json
|
||||
rails (~> 3.2)
|
||||
sass-rails (~> 3.2.3)
|
||||
shoulda (~> 2.11)
|
||||
shoulda (~> 3.5)
|
||||
simplecov
|
||||
sqlite3
|
||||
turn
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
|
||||
class CoursesController < ApplicationController
|
||||
|
||||
before_filter :require_admin, :only => [ :new, :create, :edit, :update, :destroy ]
|
||||
before_filter :require_login, :only => [ :enroll, :unenroll ]
|
||||
before_filter :find_course, :except => [ :index ]
|
||||
before_filter :require_admin, :only => [:new, :create, :edit, :update, :destroy]
|
||||
before_filter :require_login, :only => [:enroll, :unenroll]
|
||||
before_filter :find_course, :except => [:index]
|
||||
#after_filter :cache_sweep, :only => [ :create, :update, :destroy ]
|
||||
|
||||
def index
|
||||
@@ -27,9 +27,12 @@ class CoursesController < ApplicationController
|
||||
@period = params[:period] || App.current_period
|
||||
|
||||
if logged_in? and !@current_user.courses.empty?
|
||||
@courses = Course.find(:all, :order => 'grade asc, full_name asc', :conditions => ['period = ? and hidden = ? and id not in (?)', @period, false, @current_user.courses])
|
||||
@courses = Course.all(:order => 'grade asc, full_name asc',
|
||||
:conditions => ['period = ? and hidden = ? and id not in (?)',
|
||||
@period, false, @current_user.courses])
|
||||
else
|
||||
@courses = Course.find(:all, :order => 'grade asc, full_name asc', :conditions => ['period = ? and hidden = ?', @period, false])
|
||||
@courses = Course.all(:order => 'grade asc, full_name asc',
|
||||
:conditions => ['period = ? and hidden = ?', @period, false])
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
@@ -54,7 +57,8 @@ class CoursesController < ApplicationController
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to course_path(@course) }
|
||||
format.xml { head :created, :location => course_url(@course, :format => :xml) }
|
||||
format.xml { head :created, :location => course_url(@course,
|
||||
:format => :xml) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -105,7 +109,7 @@ class CoursesController < ApplicationController
|
||||
protected
|
||||
def find_course
|
||||
if params[:id]
|
||||
params[:id] = Course.find(:first, :conditions => ['short_name = ?', params[:id]], :order => 'period desc').id if !params[:id].is_numeric? and !Course.find_by_short_name(params[:id]).nil?
|
||||
params[:id] = Course.first(:conditions => ['short_name = ?', params[:id]], :order => 'period desc').id if !params[:id].is_numeric? and !Course.find_by_short_name(params[:id]).nil?
|
||||
end
|
||||
@course = params[:id] ? Course.find(params[:id]) : Course.new(params[:course])
|
||||
end
|
||||
|
||||
@@ -15,7 +15,7 @@ module AuthenticationSystem
|
||||
# apenas verifica se o usuário já está logado ou não. Caso não esteja, ele é redirecionado.
|
||||
def login_by_html
|
||||
if !logged_in?
|
||||
flash[:warning] = 'You must be logged in to access this section of the site'[:login_required]
|
||||
flash[:warning] = 'You must be logged in to access this section of the site'
|
||||
session[:return_to] = request.fullpath
|
||||
redirect_to login_url
|
||||
end
|
||||
|
||||
@@ -14,87 +14,86 @@
|
||||
# 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/>.
|
||||
|
||||
#require File.dirname(__FILE__) + '/../test_helper.rb'
|
||||
#require 'courses_controller'
|
||||
#
|
||||
## Re-raise errors caught by the controller.
|
||||
##class CoursesController; def rescue_action(e) raise e end; end
|
||||
#
|
||||
#class CoursesControllerTest < ActionController::TestCase
|
||||
#
|
||||
# def setup
|
||||
# @course = courses(:course_1)
|
||||
# @old_course = courses(:old_course)
|
||||
# LogEntry.delete_all
|
||||
# end
|
||||
#
|
||||
# context "An anonymous user" do
|
||||
#
|
||||
# should_request_login_on_post_to(:new, {})
|
||||
# should_request_login_on_post_to(:create, {})
|
||||
# should_request_login_on_post_to(:edit, {:id => 1})
|
||||
# should_request_login_on_post_to(:update, {:id => 1})
|
||||
# should_request_login_on_post_to(:destroy, {:id => 1})
|
||||
# should_request_login_on_post_to(:enroll, {:id => 1})
|
||||
# should_request_login_on_post_to(:unenroll, {:id => 1})
|
||||
#
|
||||
# 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 'h1', "Disciplinas #{App.current_period}"
|
||||
# assert_select 'a[href=?]', course_url(@course)
|
||||
# end
|
||||
#
|
||||
# should "choose display the selected period" do
|
||||
# get :index, :period => "1970.1"
|
||||
# assert_select 'h1', "Disciplinas 1970.1"
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# context "on get to :show" do
|
||||
# setup { get :show, :id => @course.id }
|
||||
#
|
||||
# should respond_with :success
|
||||
# should render_template 'show'
|
||||
#
|
||||
# should "display the course" do
|
||||
# assert_select 'a[href=?]', course_log_url(@course)
|
||||
# assert_select 'a[href=?]', course_news_url(@course)
|
||||
# assert_select 'a[href=?]', course_events_url(@course)
|
||||
# assert_select 'a[href=?]', new_course_event_url(@course)
|
||||
# assert_select 'a[href=?]', new_course_attachment_url(@course)
|
||||
# assert_select 'a[href=?]', new_course_wiki_instance_url(@course)
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# context "An authenticated user" do
|
||||
# setup { login_as :bob }
|
||||
# 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
|
||||
require File.dirname(__FILE__) + '/../test_helper.rb'
|
||||
require 'courses_controller'
|
||||
|
||||
# Re-raise errors caught by the controller.
|
||||
#class CoursesController; def rescue_action(e) raise e end; end
|
||||
|
||||
class CoursesControllerTest < ActionController::TestCase
|
||||
|
||||
def setup
|
||||
@course = courses(:course_1)
|
||||
@old_course = courses(:old_course)
|
||||
LogEntry.delete_all
|
||||
end
|
||||
|
||||
context "An anonymous user" do
|
||||
|
||||
should_have_access_denied_on_post_to(:new, {})
|
||||
should_have_access_denied_on_post_to(:create, {})
|
||||
should_have_access_denied_on_post_to(:edit, {:id => 1})
|
||||
should_have_access_denied_on_post_to(:update, {:id => 1})
|
||||
should_have_access_denied_on_post_to(:destroy, {:id => 1})
|
||||
should_request_login_on_post_to(:enroll, {:id => 1})
|
||||
should_request_login_on_post_to(:unenroll, {:id => 1})
|
||||
|
||||
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 'h1', "Disciplinas #{App.current_period}"
|
||||
assert_select 'a[href=?]', course_url(@course)
|
||||
end
|
||||
|
||||
should "display the selected period" do
|
||||
get :index, :period => "1970.1"
|
||||
assert_select 'h1', "Disciplinas 1970.1"
|
||||
end
|
||||
end
|
||||
|
||||
context "on get to :show" do
|
||||
setup { get :show, :id => @course.id }
|
||||
|
||||
should respond_with :success
|
||||
should render_template 'show'
|
||||
|
||||
should "display the course" do
|
||||
assert_select 'a[href=?]', course_log_url(@course)
|
||||
assert_select 'a[href=?]', course_news_url(@course)
|
||||
assert_select 'a[href=?]', course_events_url(@course)
|
||||
assert_select 'a[href=?]', new_course_event_url(@course)
|
||||
assert_select 'a[href=?]', new_course_attachment_url(@course)
|
||||
assert_select 'a[href=?]', new_course_wiki_instance_url(@course)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "An authenticated user" do
|
||||
setup { login_as :bob }
|
||||
end
|
||||
|
||||
#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
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
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
|
||||
@@ -11,3 +11,27 @@ class ActiveSupport::TestCase
|
||||
|
||||
# Add more helper methods to be used by all tests here...
|
||||
end
|
||||
|
||||
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_have_access_denied_on_post_to(action, params)
|
||||
should "have access denied on post to #{action}" do
|
||||
assert_raises AccessDenied do
|
||||
post action, params
|
||||
end
|
||||
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