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
@@ -22,13 +22,13
22 22 right.controller == self.class.controller_name and
23 23 (right.action == 'all' or right.action == action_name)
24 24 }
25 25 }
26 26 flash[:notice] = 'You are not authorized to view the page you requested'
27 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 29 return false
30 30 end
31 31 end
32 32
33 33 end
34 34
@@ -1,10 +1,10
1 1 class GradersController < ApplicationController
2 2
3 3
4 - before_filter :authenticate
4 + before_filter :authorization
5 5
6 6 def list
7 7 @grader_processes = GraderProcess.find(:all,
8 8 :order => 'updated_at desc')
9 9 @stalled_processes = GraderProcess.find_stalled_process
10 10 end
@@ -21,13 +21,13
21 21 def self.authenticate(login, password)
22 22 user = find_by_login(login)
23 23 return user if user && user.authenticated?(password)
24 24 end
25 25
26 26 def authenticated?(password)
27 - hashed_password == encrypt(password,salt)
27 + hashed_password == User.encrypt(password,self.salt)
28 28 end
29 29
30 30 def admin?
31 31 self.roles.detect {|r| r.name == 'admin' }
32 32 end
33 33
@@ -60,17 +60,17
60 60 end
61 61
62 62 protected
63 63 def encrypt_new_password
64 64 return if password.blank?
65 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 67 end
68 68
69 69 def password_required?
70 - hashed_password.blank? || !password.blank?
70 + self.hashed_password.blank? || !self.password.blank?
71 71 end
72 72
73 - def encrypt(string,salt)
73 + def self.encrypt(string,salt)
74 74 Digest::SHA1.hexdigest(salt + string)
75 75 end
76 76 end
@@ -1,9 +1,9
1 1 class AddTaskToGraderProcess < ActiveRecord::Migration
2 2 def self.up
3 3 add_column :grader_processes, :task_id, :integer
4 4 end
5 5
6 6 def self.down
7 - remove_column :grader_processes, :task_id, :integer
7 + remove_column :grader_processes, :task_id
8 8 end
9 9 end
@@ -6,13 +6,13
6 6 # to create the application database on another system, you should be using db:schema:load, not running
7 7 # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
8 8 # you'll amass, the slower it'll run and the greater likelihood for issues).
9 9 #
10 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 14 create_table "grader_processes", :force => true do |t|
15 15 t.string "host", :limit => 20
16 16 t.integer "pid"
17 17 t.string "mode"
18 18 t.boolean "active"
@@ -1,5 +1,11
1 1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 2 one:
3 3 id: 1
4 + name: add
5 + full_name: add_full_name
6 + available: true
4 7 two:
5 8 id: 2
9 + name: subtract
10 + full_name: subtract_full_name
11 + available: false
@@ -1,5 +1,13
1 1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 - one:
3 - id: 1
4 - two:
5 - id: 2
2 +
3 + graders_right:
4 + controller: graders
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 1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 - one:
3 - id: 1
4 - two:
5 - id: 2
2 + admin:
3 + rights: graders_right, user_admin_right, problems_right No newline at end of file
@@ -1,5 +1,18
1 1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 - one:
3 - id: 1
4 - two:
5 - id: 2
2 +
3 + <%
4 + User.public_class_method :encrypt
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 1 require File.dirname(__FILE__) + '/../test_helper'
2 2
3 3 class GradersControllerTest < ActionController::TestCase
4 - # Replace this with your real tests.
5 - def test_truth
6 - assert true
4 +
5 + fixtures :users, :roles, :rights
6 +
7 + def test_should_not_allow_new_user_to_see
8 + get :list
9 + assert_redirected_to :controller => 'main', :action => 'login'
7 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 26 end
@@ -2,17 +2,36
2 2 require 'login_controller'
3 3
4 4 # Re-raise errors caught by the controller.
5 5 class LoginController; def rescue_action(e) raise e end; end
6 6
7 7 class LoginControllerTest < Test::Unit::TestCase
8 +
9 + fixtures :users
10 +
8 11 def setup
9 12 @controller = LoginController.new
10 13 @request = ActionController::TestRequest.new
11 14 @response = ActionController::TestResponse.new
12 15 end
13 16
14 17 # Replace this with your real tests.
15 - def test_truth
16 - assert true
18 + def test_should_hide_index
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 36 end
18 37 end
@@ -2,17 +2,31
2 2 require 'main_controller'
3 3
4 4 # Re-raise errors caught by the controller.
5 5 class MainController; def rescue_action(e) raise e end; end
6 6
7 7 class MainControllerTest < Test::Unit::TestCase
8 +
9 + fixtures :problems
10 + fixtures :users
11 +
8 12 def setup
9 13 @controller = MainController.new
10 14 @request = ActionController::TestRequest.new
11 15 @response = ActionController::TestResponse.new
12 16 end
13 17
14 18 # Replace this with your real tests.
15 - def test_truth
16 - assert true
19 + def test_should_redirect_new_user_to_login
20 + get :list
21 + assert_redirected_to :action => 'login'
17 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 32 end
@@ -3,88 +3,138
3 3
4 4 # Re-raise errors caught by the controller.
5 5 class UserAdminController; def rescue_action(e) raise e end; end
6 6
7 7 class UserAdminControllerTest < Test::Unit::TestCase
8 8 fixtures :users
9 + fixtures :roles
10 + fixtures :rights
9 11
10 12 def setup
11 13 @controller = UserAdminController.new
12 14 @request = ActionController::TestRequest.new
13 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 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 41 def test_index
19 - get :index
42 + get :index, {}, {:user_id => @admin_id}
20 43 assert_response :success
21 44 assert_template 'list'
22 45 end
23 46
24 47 def test_list
25 - get :list
48 + get :list, {}, {:user_id => @admin_id}
26 49
27 50 assert_response :success
28 51 assert_template 'list'
29 52
30 53 assert_not_nil assigns(:users)
31 54 end
32 55
33 56 def test_show
34 - get :show, :id => @first_id
57 + get :show, {:id => @first_id}, {:user_id => @admin_id}
35 58
36 59 assert_response :success
37 60 assert_template 'show'
38 61
39 62 assert_not_nil assigns(:user)
40 - assert assigns(:user).valid?
41 63 end
42 64
43 65 def test_new
44 - get :new
66 + get :new, {}, {:user_id => @admin_id}
45 67
46 68 assert_response :success
47 69 assert_template 'new'
48 70
49 71 assert_not_nil assigns(:user)
50 72 end
51 73
52 - def test_create
74 + def test_create_with_correct_confirmation_password
53 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 84 assert_response :redirect
58 85 assert_redirected_to :action => 'list'
59 86
60 87 assert_equal num_users + 1, User.count
61 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 106 def test_edit
64 - get :edit, :id => @first_id
107 + get :edit, {:id => @first_id}, {:user_id => @admin_id}
65 108
66 109 assert_response :success
67 110 assert_template 'edit'
68 111
69 112 assert_not_nil assigns(:user)
70 - assert assigns(:user).valid?
71 113 end
72 114
73 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 125 assert_response :redirect
76 126 assert_redirected_to :action => 'show', :id => @first_id
77 127 end
78 128
79 129 def test_destroy
80 130 assert_nothing_raised {
81 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 135 assert_response :redirect
86 136 assert_redirected_to :action => 'list'
87 137
88 138 assert_raise(ActiveRecord::RecordNotFound) {
89 139 User.find(@first_id)
90 140 }
You need to be logged in to leave comments. Login now