socialforge/test/functional/courses_controller_test.rb

160 lines
4.7 KiB
Ruby
Raw Normal View History

2014-07-16 14:35:54 +08:00
require File.expand_path('../../test_helper', __FILE__)
class CoursesControllerTest < ActionController::TestCase
2014-07-29 12:05:24 +08:00
fixtures :courses,
:course_infos,
:course_statuses,
:users,
:user_extensions,
2014-07-30 10:18:36 +08:00
:bids,
:homework_for_courses,
:journals_for_messages,
2014-07-29 12:05:24 +08:00
:schools,
:roles,
:members,
:member_roles,
:enabled_modules,
:boards,
:messages
2014-07-16 14:35:54 +08:00
def setup
@request.session[:user_id] = nil
@request.session[:ctime] = Time.now.to_i
@request.session[:atime] = Time.now.to_i
Setting.default_language = 'en'
end
# Get :index
2014-07-16 14:35:54 +08:00
def test_index_by_anonymous_should_not_show_private_projects
get :index
assert_response :success
assert_template 'index'
courses = assigns(:courses)
assert_not_nil courses
assert courses.all?(&:is_public?)
end
# Get :new
2014-07-16 17:46:17 +08:00
# 人员添加课程的权限是不属于任何角色
def test_new_course_anyone_temporary
2014-07-29 12:05:24 +08:00
@request.session[:user_id] = 20
2014-07-16 17:46:17 +08:00
Role.find_by_name("Non member").add_permission! :add_course #Non member
2014-07-16 14:35:54 +08:00
get :new
assert_response :success
2014-07-16 17:46:17 +08:00
assert_template :new
end
# post :create
2014-07-17 09:52:56 +08:00
def test_create_course_with_access_control
2014-07-29 12:05:24 +08:00
@request.session[:user_id] = 20
2014-07-16 17:46:17 +08:00
Role.find_by_name("Non member").add_permission! :add_course #Non member
2014-07-17 09:52:56 +08:00
course_name = 'course_one'
2014-07-16 17:46:17 +08:00
post :create,
:class_period => '32',
:time => '2014',
:term => 'spring',
:course => {
2014-07-17 09:52:56 +08:00
:name => course_name, # names can't contain space.
2014-07-16 17:46:17 +08:00
:password => '1234',
:description => 'description',
2014-07-17 09:52:56 +08:00
:is_public => '1',
2014-07-16 17:46:17 +08:00
:course_type => '1'
}
2014-07-17 09:52:56 +08:00
2014-07-16 17:46:17 +08:00
assert_response :found
2014-07-17 09:52:56 +08:00
course = Course.find_by_name(course_name)
red_url = "courses/#{course.id}/settings"
assert_match %r(#{red_url}), @response.redirect_url
# 创建成功跳转settings方法会带参数一枚故一下方法失败
# assert_redirected_to "courses/#{course.id}/settings"
2014-07-16 14:35:54 +08:00
end
# post :create 403
2014-07-17 09:52:56 +08:00
def test_create_course_without_access_control
2014-07-29 12:05:24 +08:00
@request.session[:user_id] = 20
2014-07-17 09:52:56 +08:00
#Role.find_by_name("Non member").add_permission! :add_course #Non member
course_name = 'course_one'
post :create,
:class_period => '32',
:time => '2014',
:term => 'spring',
:course => {
:name => course_name, # names can't contain space.
:password => '1234',
:description => 'description',
:is_public => '1',
:course_type => '1'
}
assert_response :forbidden
end
2014-07-30 10:18:36 +08:00
test ':homework get by non-member is access' do
get :homework, {id: 1}
assert_response :success
end
test ':feedback Get by non-member is access' do
get :feedback, {id: 1}
assert_response :success
# 检测留言是否在其中以及不报错
jfms = JournalsForMessage.where(jour_type: 'Course', jour_id: 1, m_parent_id: nil).reorder('id DESC')
jfms.take(5).each do |j|
assert_select "li#word_li_#{j.id}"
end
end
test ':homework get by non-member' do
get :homework, {id: 1}
assert_response :success
course = Course.find(1)
assert course.homeworks.any?
course.homeworks.each do |homework|
assert_select '.bid_path', homework.name
end
end
test ':new_homework by no user should not new_homework course_001' do
get :new_homework, {id: 1}
assert_response 403
end
test ':new_homework by loged & allowed user should get new_homework course_001' do
user_id = 25
member = Member.create(user_id: user_id, project_id: -1, course_id: 1, roles: [Role.last])
MemberRole.create(member_id: member.id, role_id: 3)
@request.session[:user_id] = user_id
get :new_homework, {id: 1}
assert_response :success
assert_template 'new_homework'
end
test ':member by ano' do
get :member, {id: 1}
assert_response :success
assert_template 'member'
assert assigns(:members).any?
end
2014-07-17 09:52:56 +08:00
2014-07-16 14:35:54 +08:00
# test "#index by non-admin user with view_time_entries permission should show overall spent time link" do
# @request.session[:user_id] = 3
# get :index
# assert_template 'index'
# assert_select 'a[href=?]', '/time_entries'
# end
#
# test "#index by non-admin user without view_time_entries permission should not show overall spent time link" do
# Role.find(2).remove_permission! :view_time_entries
# Role.non_member.remove_permission! :view_time_entries
# Role.anonymous.remove_permission! :view_time_entries
# @request.session[:user_id] = 3
#
# get :index
# assert_template 'index'
# assert_select 'a[href=?]', '/time_entries', 0
# end
end