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
@@ -25,7 +25,7
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
@@ -1,7 +1,7
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,
@@ -24,7 +24,7
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?
@@ -63,14 +63,14
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
@@ -4,6 +4,6
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
@@ -9,7 +9,7
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
@@ -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'
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 +
8 end
26 end
@@ -5,6 +5,9
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
@@ -12,7 +15,23
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
@@ -5,6 +5,10
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
@@ -12,7 +16,17
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\)/
30 + end
31 +
18 end
32 end
@@ -6,23 +6,46
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'
@@ -31,17 +54,16
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'
@@ -49,10 +71,15
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'
@@ -60,18 +87,41
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
@@ -81,7 +131,7
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
You need to be logged in to leave comments. Login now