Description:
[web] added graders_right_to_admin_role, added a few functional tests: main, user_admin, graders, login git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@131 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r58:94b5b7562ca1 - - 14 files changed: 182 inserted, 39 deleted

@@ -0,0 +1,17
1 + class AddGradersRightToAdminRole < ActiveRecord::Migration
2 + def self.up
3 + admin_role = Role.find_by_name('admin')
4 +
5 + graders_right = Right.create(:name => 'graders_admin',
6 + :controller => 'graders',
7 + :action => 'all')
8 +
9 + admin_role.rights << graders_right;
10 + admin_role.save
11 + end
12 +
13 + def self.down
14 + graders_right = Right.find_by_name('graders_admin')
15 + graders_right.destroy
16 + end
17 + end
@@ -4,31 +4,31
4 class ApplicationController < ActionController::Base
4 class ApplicationController < ActionController::Base
5 # Pick a unique cookie name to distinguish our session data from others'
5 # Pick a unique cookie name to distinguish our session data from others'
6 session :session_key => '_grader_session_id'
6 session :session_key => '_grader_session_id'
7
7
8 protected
8 protected
9 def authenticate
9 def authenticate
10 unless session[:user_id]
10 unless session[:user_id]
11 redirect_to :controller => 'main', :action => 'login'
11 redirect_to :controller => 'main', :action => 'login'
12 return false
12 return false
13 end
13 end
14 return true
14 return true
15 end
15 end
16
16
17 def authorization
17 def authorization
18 return false unless authenticate
18 return false unless authenticate
19 user = User.find(session[:user_id])
19 user = User.find(session[:user_id])
20 unless user.roles.detect { |role|
20 unless user.roles.detect { |role|
21 role.rights.detect{ |right|
21 role.rights.detect{ |right|
22 right.controller == self.class.controller_name and
22 right.controller == self.class.controller_name and
23 (right.action == 'all' or right.action == action_name)
23 (right.action == 'all' or right.action == action_name)
24 }
24 }
25 }
25 }
26 flash[:notice] = 'You are not authorized to view the page you requested'
26 flash[:notice] = 'You are not authorized to view the page you requested'
27 #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login')
27 #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login')
28 - redirect_to :controller => 'login'
28 + redirect_to :controller => 'main', :action => 'login'
29 return false
29 return false
30 end
30 end
31 end
31 end
32
32
33 end
33 end
34
34
@@ -1,18 +1,18
1 class GradersController < ApplicationController
1 class GradersController < ApplicationController
2
2
3
3
4 - before_filter :authenticate
4 + before_filter :authorization
5
5
6 def list
6 def list
7 @grader_processes = GraderProcess.find(:all,
7 @grader_processes = GraderProcess.find(:all,
8 :order => 'updated_at desc')
8 :order => 'updated_at desc')
9 @stalled_processes = GraderProcess.find_stalled_process
9 @stalled_processes = GraderProcess.find_stalled_process
10 end
10 end
11
11
12 def clear
12 def clear
13 grader_proc = GraderProcess.find(params[:id])
13 grader_proc = GraderProcess.find(params[:id])
14 grader_proc.destroy if grader_proc!=nil
14 grader_proc.destroy if grader_proc!=nil
15 redirect_to :action => 'list'
15 redirect_to :action => 'list'
16 end
16 end
17
17
18 end
18 end
@@ -3,74 +3,74
3 class User < ActiveRecord::Base
3 class User < ActiveRecord::Base
4
4
5 has_and_belongs_to_many :roles
5 has_and_belongs_to_many :roles
6
6
7 has_many :test_requests, :order => "problem_id"
7 has_many :test_requests, :order => "problem_id"
8
8
9 validates_presence_of :login
9 validates_presence_of :login
10 validates_presence_of :full_name
10 validates_presence_of :full_name
11 validates_length_of :full_name, :minimum => 1
11 validates_length_of :full_name, :minimum => 1
12
12
13 validates_presence_of :password, :if => :password_required?
13 validates_presence_of :password, :if => :password_required?
14 validates_length_of :password, :within => 4..20, :if => :password_required?
14 validates_length_of :password, :within => 4..20, :if => :password_required?
15 validates_confirmation_of :password, :if => :password_required?
15 validates_confirmation_of :password, :if => :password_required?
16
16
17 attr_accessor :password
17 attr_accessor :password
18
18
19 before_save :encrypt_new_password
19 before_save :encrypt_new_password
20
20
21 def self.authenticate(login, password)
21 def self.authenticate(login, password)
22 user = find_by_login(login)
22 user = find_by_login(login)
23 return user if user && user.authenticated?(password)
23 return user if user && user.authenticated?(password)
24 end
24 end
25
25
26 def authenticated?(password)
26 def authenticated?(password)
27 - hashed_password == encrypt(password,salt)
27 + hashed_password == User.encrypt(password,self.salt)
28 end
28 end
29
29
30 def admin?
30 def admin?
31 self.roles.detect {|r| r.name == 'admin' }
31 self.roles.detect {|r| r.name == 'admin' }
32 end
32 end
33
33
34 def email_for_editing
34 def email_for_editing
35 if self.email==nil
35 if self.email==nil
36 "(unknown)"
36 "(unknown)"
37 elsif self.email==''
37 elsif self.email==''
38 "(blank)"
38 "(blank)"
39 else
39 else
40 self.email
40 self.email
41 end
41 end
42 end
42 end
43
43
44 def email_for_editing=(e)
44 def email_for_editing=(e)
45 self.email=e
45 self.email=e
46 end
46 end
47
47
48 def alias_for_editing
48 def alias_for_editing
49 if self.alias==nil
49 if self.alias==nil
50 "(unknown)"
50 "(unknown)"
51 elsif self.alias==''
51 elsif self.alias==''
52 "(blank)"
52 "(blank)"
53 else
53 else
54 self.alias
54 self.alias
55 end
55 end
56 end
56 end
57
57
58 def alias_for_editing=(e)
58 def alias_for_editing=(e)
59 self.alias=e
59 self.alias=e
60 end
60 end
61
61
62 protected
62 protected
63 def encrypt_new_password
63 def encrypt_new_password
64 return if password.blank?
64 return if password.blank?
65 self.salt = (10+rand(90)).to_s
65 self.salt = (10+rand(90)).to_s
66 - self.hashed_password = encrypt(password,salt)
66 + self.hashed_password = User.encrypt(self.password,self.salt)
67 end
67 end
68
68
69 def password_required?
69 def password_required?
70 - hashed_password.blank? || !password.blank?
70 + self.hashed_password.blank? || !self.password.blank?
71 end
71 end
72
72
73 - def encrypt(string,salt)
73 + def self.encrypt(string,salt)
74 Digest::SHA1.hexdigest(salt + string)
74 Digest::SHA1.hexdigest(salt + string)
75 end
75 end
76 end
76 end
@@ -1,9 +1,9
1 class AddTaskToGraderProcess < ActiveRecord::Migration
1 class AddTaskToGraderProcess < ActiveRecord::Migration
2 def self.up
2 def self.up
3 add_column :grader_processes, :task_id, :integer
3 add_column :grader_processes, :task_id, :integer
4 end
4 end
5
5
6 def self.down
6 def self.down
7 - remove_column :grader_processes, :task_id, :integer
7 + remove_column :grader_processes, :task_id
8 end
8 end
9 end
9 end
@@ -1,36 +1,36
1 # This file is auto-generated from the current state of the database. Instead of editing this file,
1 # This file is auto-generated from the current state of the database. Instead of editing this file,
2 # please use the migrations feature of ActiveRecord to incrementally modify your database, and
2 # please use the migrations feature of ActiveRecord to incrementally modify your database, and
3 # then regenerate this schema definition.
3 # then regenerate this schema definition.
4 #
4 #
5 # Note that this schema.rb definition is the authoritative source for your database schema. If you need
5 # Note that this schema.rb definition is the authoritative source for your database schema. If you need
6 # to create the application database on another system, you should be using db:schema:load, not running
6 # to create the application database on another system, you should be using db:schema:load, not running
7 # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
7 # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
8 # you'll amass, the slower it'll run and the greater likelihood for issues).
8 # you'll amass, the slower it'll run and the greater likelihood for issues).
9 #
9 #
10 # It's strongly recommended to check this file into your version control system.
10 # It's strongly recommended to check this file into your version control system.
11
11
12 - ActiveRecord::Schema.define(:version => 19) do
12 + ActiveRecord::Schema.define(:version => 20) do
13
13
14 create_table "grader_processes", :force => true do |t|
14 create_table "grader_processes", :force => true do |t|
15 t.string "host", :limit => 20
15 t.string "host", :limit => 20
16 t.integer "pid"
16 t.integer "pid"
17 t.string "mode"
17 t.string "mode"
18 t.boolean "active"
18 t.boolean "active"
19 t.datetime "created_at"
19 t.datetime "created_at"
20 t.datetime "updated_at"
20 t.datetime "updated_at"
21 t.integer "task_id"
21 t.integer "task_id"
22 end
22 end
23
23
24 add_index "grader_processes", ["host", "pid"], :name => "index_grader_processes_on_ip_and_pid"
24 add_index "grader_processes", ["host", "pid"], :name => "index_grader_processes_on_ip_and_pid"
25
25
26 create_table "languages", :force => true do |t|
26 create_table "languages", :force => true do |t|
27 t.string "name", :limit => 10
27 t.string "name", :limit => 10
28 t.string "pretty_name"
28 t.string "pretty_name"
29 t.string "ext", :limit => 10
29 t.string "ext", :limit => 10
30 end
30 end
31
31
32 create_table "problems", :force => true do |t|
32 create_table "problems", :force => true do |t|
33 t.string "name", :limit => 30
33 t.string "name", :limit => 30
34 t.string "full_name"
34 t.string "full_name"
35 t.integer "full_score"
35 t.integer "full_score"
36 t.date "date_added"
36 t.date "date_added"
@@ -1,5 +1,11
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 one:
2 one:
3 id: 1
3 id: 1
4 + name: add
5 + full_name: add_full_name
6 + available: true
4 two:
7 two:
5 id: 2
8 id: 2
9 + name: subtract
10 + full_name: subtract_full_name
11 + available: false
@@ -1,5 +1,13
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 - one:
2 +
3 - id: 1
3 + graders_right:
4 - two:
4 + controller: graders
5 - id: 2
5 + action: all
6 +
7 + user_admin_right:
8 + controller: user_admin
9 + action: all
10 +
11 + problems_right:
12 + controller: problems
13 + action: all
@@ -1,5 +1,3
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 - one:
2 + admin:
3 - id: 1
3 + rights: graders_right, user_admin_right, problems_right No newline at end of file
4 - two:
5 - id: 2
@@ -1,5 +1,18
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 - one:
2 +
3 - id: 1
3 + <%
4 - two:
4 + User.public_class_method :encrypt
5 - id: 2
5 +
6 + SALT = "abc"
7 + %>
8 +
9 + john:
10 + login: john
11 + hashed_password: <%= User.encrypt("hello",SALT) %>
12 + salt: <%= SALT %>
13 + mary:
14 + login: mary
15 + hashed_password: <%= User.encrypt("goodbye",SALT) %>
16 + salt: <%= SALT %>
17 + roles: admin
18 +
@@ -1,8 +1,26
1 require File.dirname(__FILE__) + '/../test_helper'
1 require File.dirname(__FILE__) + '/../test_helper'
2
2
3 class GradersControllerTest < ActionController::TestCase
3 class GradersControllerTest < ActionController::TestCase
4 - # Replace this with your real tests.
4 +
5 - def test_truth
5 + fixtures :users, :roles, :rights
6 - assert true
6 +
7 + def test_should_not_allow_new_user_to_see
8 + get :list
9 + assert_redirected_to :controller => 'main', :action => 'login'
7 end
10 end
11 +
12 + def test_should_not_allow_normal_user_to_see
13 + john = users(:john)
14 +
15 + get :list, {}, {:user_id => john.id}
16 + assert_redirected_to :controller => 'main', :action => 'login'
8 end
17 end
18 +
19 + def test_should_allow_admin_to_see
20 + mary = users(:mary)
21 +
22 + get :list, {}, {:user_id => mary.id}
23 + assert_template 'graders/list'
24 + end
25 +
26 + end
@@ -1,18 +1,37
1 require File.dirname(__FILE__) + '/../test_helper'
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'login_controller'
2 require 'login_controller'
3
3
4 # Re-raise errors caught by the controller.
4 # Re-raise errors caught by the controller.
5 class LoginController; def rescue_action(e) raise e end; end
5 class LoginController; def rescue_action(e) raise e end; end
6
6
7 class LoginControllerTest < Test::Unit::TestCase
7 class LoginControllerTest < Test::Unit::TestCase
8 +
9 + fixtures :users
10 +
8 def setup
11 def setup
9 @controller = LoginController.new
12 @controller = LoginController.new
10 @request = ActionController::TestRequest.new
13 @request = ActionController::TestRequest.new
11 @response = ActionController::TestResponse.new
14 @response = ActionController::TestResponse.new
12 end
15 end
13
16
14 # Replace this with your real tests.
17 # Replace this with your real tests.
15 - def test_truth
18 + def test_should_hide_index
16 - assert true
19 + get :index
20 + assert_redirected_to :controller => 'main', :action => 'login'
21 + end
22 +
23 + def test_should_login_user_and_set_session
24 + john = users(:john)
25 +
26 + post :login, :login => 'john', :password => "hello"
27 + assert_redirected_to :controller => 'main', :action => 'list'
28 + assert_equal john.id, session[:user_id]
29 + end
30 +
31 + def test_should_reject_user_with_wrong_password
32 + john = users(:john)
33 +
34 + post :login, :login => 'john', :password => "wrong"
35 + assert_redirected_to :controller => 'main', :action => 'login'
17 end
36 end
18 end
37 end
@@ -1,18 +1,32
1 require File.dirname(__FILE__) + '/../test_helper'
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'main_controller'
2 require 'main_controller'
3
3
4 # Re-raise errors caught by the controller.
4 # Re-raise errors caught by the controller.
5 class MainController; def rescue_action(e) raise e end; end
5 class MainController; def rescue_action(e) raise e end; end
6
6
7 class MainControllerTest < Test::Unit::TestCase
7 class MainControllerTest < Test::Unit::TestCase
8 +
9 + fixtures :problems
10 + fixtures :users
11 +
8 def setup
12 def setup
9 @controller = MainController.new
13 @controller = MainController.new
10 @request = ActionController::TestRequest.new
14 @request = ActionController::TestRequest.new
11 @response = ActionController::TestResponse.new
15 @response = ActionController::TestResponse.new
12 end
16 end
13
17
14 # Replace this with your real tests.
18 # Replace this with your real tests.
15 - def test_truth
19 + def test_should_redirect_new_user_to_login
16 - assert true
20 + get :list
21 + assert_redirected_to :action => 'login'
17 end
22 end
23 +
24 + def test_should_list_available_problems_if_logged_in
25 + john = users(:john)
26 + get :list, {}, {:user_id => john.id}
27 +
28 + assert_template 'main/list'
29 + assert_select "table tr:nth-child(2)", :text => /\(add\)/
18 end
30 end
31 +
32 + end
@@ -1,92 +1,142
1 require File.dirname(__FILE__) + '/../test_helper'
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'user_admin_controller'
2 require 'user_admin_controller'
3
3
4 # Re-raise errors caught by the controller.
4 # Re-raise errors caught by the controller.
5 class UserAdminController; def rescue_action(e) raise e end; end
5 class UserAdminController; def rescue_action(e) raise e end; end
6
6
7 class UserAdminControllerTest < Test::Unit::TestCase
7 class UserAdminControllerTest < Test::Unit::TestCase
8 fixtures :users
8 fixtures :users
9 + fixtures :roles
10 + fixtures :rights
9
11
10 def setup
12 def setup
11 @controller = UserAdminController.new
13 @controller = UserAdminController.new
12 @request = ActionController::TestRequest.new
14 @request = ActionController::TestRequest.new
13 @response = ActionController::TestResponse.new
15 @response = ActionController::TestResponse.new
14
16
15 - @first_id = users(:first).id
17 + @first_id = users(:john).id
18 + @admin_id = users(:mary).id
19 + end
20 +
21 + def test_should_not_allow_new_user_to_see
22 + get :list
23 + assert_redirected_to :controller => 'main', :action => 'login'
16 end
24 end
17
25
26 + def test_should_not_allow_normal_user_to_see
27 + john = users(:john)
28 +
29 + get :list, {}, {:user_id => john.id}
30 + assert_redirected_to :controller => 'main', :action => 'login'
31 + end
32 +
33 + def test_should_allow_admin_to_see
34 + mary = users(:mary)
35 +
36 + get :list, {}, {:user_id => mary.id}
37 + assert_template 'user_admin/list'
38 + end
39 +
40 +
18 def test_index
41 def test_index
19 - get :index
42 + get :index, {}, {:user_id => @admin_id}
20 assert_response :success
43 assert_response :success
21 assert_template 'list'
44 assert_template 'list'
22 end
45 end
23
46
24 def test_list
47 def test_list
25 - get :list
48 + get :list, {}, {:user_id => @admin_id}
26
49
27 assert_response :success
50 assert_response :success
28 assert_template 'list'
51 assert_template 'list'
29
52
30 assert_not_nil assigns(:users)
53 assert_not_nil assigns(:users)
31 end
54 end
32
55
33 def test_show
56 def test_show
34 - get :show, :id => @first_id
57 + get :show, {:id => @first_id}, {:user_id => @admin_id}
35
58
36 assert_response :success
59 assert_response :success
37 assert_template 'show'
60 assert_template 'show'
38
61
39 assert_not_nil assigns(:user)
62 assert_not_nil assigns(:user)
40 - assert assigns(:user).valid?
41 end
63 end
42
64
43 def test_new
65 def test_new
44 - get :new
66 + get :new, {}, {:user_id => @admin_id}
45
67
46 assert_response :success
68 assert_response :success
47 assert_template 'new'
69 assert_template 'new'
48
70
49 assert_not_nil assigns(:user)
71 assert_not_nil assigns(:user)
50 end
72 end
51
73
52 - def test_create
74 + def test_create_with_correct_confirmation_password
53 num_users = User.count
75 num_users = User.count
54
76
55 - post :create, :user => {}
77 + post :create, {:user => {
78 + :login => 'test',
79 + :full_name => 'hello',
80 + :password => 'abcde',
81 + :password_confirmation => 'abcde'
82 + }}, {:user_id => @admin_id}
56
83
57 assert_response :redirect
84 assert_response :redirect
58 assert_redirected_to :action => 'list'
85 assert_redirected_to :action => 'list'
59
86
60 assert_equal num_users + 1, User.count
87 assert_equal num_users + 1, User.count
61 end
88 end
62
89
90 + def test_create_with_wrong_confirmation_password
91 + num_users = User.count
92 +
93 + post :create, {:user => {
94 + :login => 'test',
95 + :full_name => 'hello',
96 + :password => 'abcde',
97 + :password_confirmation => 'abcdef'
98 + }}, {:user_id => @admin_id}
99 +
100 + assert_response :success
101 + assert_template 'new'
102 +
103 + assert_equal num_users, User.count
104 + end
105 +
63 def test_edit
106 def test_edit
64 - get :edit, :id => @first_id
107 + get :edit, {:id => @first_id}, {:user_id => @admin_id}
65
108
66 assert_response :success
109 assert_response :success
67 assert_template 'edit'
110 assert_template 'edit'
68
111
69 assert_not_nil assigns(:user)
112 assert_not_nil assigns(:user)
70 - assert assigns(:user).valid?
71 end
113 end
72
114
73 def test_update
115 def test_update
74 - post :update, :id => @first_id
116 + post :update, {
117 + :id => @first_id,
118 + :user => {
119 + :login => 'test',
120 + :full_name => 'hello',
121 + :password => 'abcde',
122 + :password_confirmation => 'abcde'
123 + }
124 + }, {:user_id => @admin_id}
75 assert_response :redirect
125 assert_response :redirect
76 assert_redirected_to :action => 'show', :id => @first_id
126 assert_redirected_to :action => 'show', :id => @first_id
77 end
127 end
78
128
79 def test_destroy
129 def test_destroy
80 assert_nothing_raised {
130 assert_nothing_raised {
81 User.find(@first_id)
131 User.find(@first_id)
82 }
132 }
83
133
84 - post :destroy, :id => @first_id
134 + post :destroy, {:id => @first_id}, {:user_id => @admin_id}
85 assert_response :redirect
135 assert_response :redirect
86 assert_redirected_to :action => 'list'
136 assert_redirected_to :action => 'list'
87
137
88 assert_raise(ActiveRecord::RecordNotFound) {
138 assert_raise(ActiveRecord::RecordNotFound) {
89 User.find(@first_id)
139 User.find(@first_id)
90 }
140 }
91 end
141 end
92 end
142 end
You need to be logged in to leave comments. Login now