Commit 947f0a6974becfa5950f92b6883eb39da2e3d5b1
1 parent
f947ef5a
Exists in
master
and in
23 other branches
ActionItem5: tested the controller to create and manage roles and the role assignment model
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@499 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
7 changed files
with
127 additions
and
28 deletions
Show diff stats
app/controllers/environment_admin/role_controller.rb
| @@ -8,16 +8,16 @@ class RoleController < EnvironmentAdminController | @@ -8,16 +8,16 @@ class RoleController < EnvironmentAdminController | ||
| 8 | end | 8 | end |
| 9 | 9 | ||
| 10 | def new | 10 | def new |
| 11 | - @role = Role.new(:name => '', :permissions => []) | 11 | + @role = Role.new |
| 12 | end | 12 | end |
| 13 | 13 | ||
| 14 | def create | 14 | def create |
| 15 | - role = Role.new(params[:role]) | ||
| 16 | - if role.save | ||
| 17 | - redirect_to :action => 'show', :id => role | 15 | + @role = Role.new(params[:role]) |
| 16 | + if @role.save | ||
| 17 | + redirect_to :action => 'show', :id => @role | ||
| 18 | else | 18 | else |
| 19 | flash[:notice] = _('Failed to create role') | 19 | flash[:notice] = _('Failed to create role') |
| 20 | - redirect_to :action => 'index' | 20 | + render :action => 'new' |
| 21 | end | 21 | end |
| 22 | end | 22 | end |
| 23 | 23 | ||
| @@ -26,9 +26,9 @@ class RoleController < EnvironmentAdminController | @@ -26,9 +26,9 @@ class RoleController < EnvironmentAdminController | ||
| 26 | end | 26 | end |
| 27 | 27 | ||
| 28 | def update | 28 | def update |
| 29 | - role = Role.find(params[:id]) | ||
| 30 | - if role.update_attributes(params[:role]) | ||
| 31 | - redirect_to :action => 'show', :id => role | 29 | + @role = Role.find(params[:id]) |
| 30 | + if @role.update_attributes(params[:role]) | ||
| 31 | + redirect_to :action => 'show', :id => @role | ||
| 32 | else | 32 | else |
| 33 | flash[:notice] = _('Failed to edit role') | 33 | flash[:notice] = _('Failed to edit role') |
| 34 | render :action => 'edit' | 34 | render :action => 'edit' |
| @@ -36,8 +36,8 @@ class RoleController < EnvironmentAdminController | @@ -36,8 +36,8 @@ class RoleController < EnvironmentAdminController | ||
| 36 | end | 36 | end |
| 37 | 37 | ||
| 38 | def destroy | 38 | def destroy |
| 39 | - role = Role.find(params[:id]) | ||
| 40 | - if role.destroy | 39 | + @role = Role.find(params[:id]) |
| 40 | + if @role.destroy | ||
| 41 | redirect_to :action => 'index' | 41 | redirect_to :action => 'index' |
| 42 | else | 42 | else |
| 43 | flash[:notice] = _('Failed to edit role') | 43 | flash[:notice] = _('Failed to edit role') |
app/models/role.rb
| @@ -10,20 +10,28 @@ class Role < ActiveRecord::Base | @@ -10,20 +10,28 @@ class Role < ActiveRecord::Base | ||
| 10 | } | 10 | } |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | + PERMISSIONS_LIST = PERMISSIONS.values.map{|h| h.keys }.flatten | ||
| 14 | + | ||
| 13 | def self.permission_name(p) | 15 | def self.permission_name(p) |
| 14 | msgid = PERMISSIONS.values.inject({}){|s,v| s.merge(v)}[p] | 16 | msgid = PERMISSIONS.values.inject({}){|s,v| s.merge(v)}[p] |
| 15 | gettext(msgid) | 17 | gettext(msgid) |
| 16 | end | 18 | end |
| 17 | - | ||
| 18 | - has_many :role_assignments | ||
| 19 | 19 | ||
| 20 | + has_many :role_assignments | ||
| 20 | serialize :permissions, Array | 21 | serialize :permissions, Array |
| 21 | - | 22 | + validates_uniqueness_of :name |
| 23 | + | ||
| 24 | + def validate | ||
| 25 | + unless (permissions - PERMISSIONS_LIST).empty? | ||
| 26 | + errors.add :permissons, 'non existent permission' | ||
| 27 | + end | ||
| 28 | + end | ||
| 29 | + | ||
| 22 | def initialize(*args) | 30 | def initialize(*args) |
| 23 | super(*args) | 31 | super(*args) |
| 24 | - permissions = [] | 32 | + self[:permissions] ||= [] |
| 25 | end | 33 | end |
| 26 | - | 34 | + |
| 27 | def has_permission?(perm) | 35 | def has_permission?(perm) |
| 28 | permissions.include?(perm) | 36 | permissions.include?(perm) |
| 29 | end | 37 | end |
app/views/role/show.rhtml
test/fixtures/roles.yml
test/functional/role_controller_test.rb
| @@ -10,9 +10,61 @@ class RoleControllerTest < Test::Unit::TestCase | @@ -10,9 +10,61 @@ class RoleControllerTest < Test::Unit::TestCase | ||
| 10 | @request = ActionController::TestRequest.new | 10 | @request = ActionController::TestRequest.new |
| 11 | @response = ActionController::TestResponse.new | 11 | @response = ActionController::TestResponse.new |
| 12 | end | 12 | end |
| 13 | + all_fixtures | ||
| 13 | 14 | ||
| 14 | - # Replace this with your real tests. | ||
| 15 | - def test_truth | ||
| 16 | - assert true | 15 | + def test_index_should_get_roles |
| 16 | + get 'index' | ||
| 17 | + assert assigns(:roles) | ||
| 18 | + end | ||
| 19 | + | ||
| 20 | + def test_show_should_fetch_role | ||
| 21 | + get 'show', :id => 1 | ||
| 22 | + assert assigns(:role) | ||
| 23 | + assert_equal 1, assigns(:role).id | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | + def test_should_create_with_valid_paramters | ||
| 27 | + Role.any_instance.stubs(:valid?).returns(true) | ||
| 28 | + post 'create' | ||
| 29 | + assert !assigns(:role).new_record? | ||
| 30 | + assert_nil flash[:notice] | ||
| 31 | + assert_response :redirect | ||
| 32 | + end | ||
| 33 | + | ||
| 34 | + def test_should_not_create_with_invalid_paramters | ||
| 35 | + Role.any_instance.stubs(:valid?).returns(false) | ||
| 36 | + post 'create' | ||
| 37 | + assert assigns(:role).new_record? | ||
| 38 | + assert_not_nil flash[:notice] | ||
| 39 | + assert_response :success | ||
| 40 | + end | ||
| 41 | + | ||
| 42 | + def test_can_edit | ||
| 43 | + get 'edit', :id => 1 | ||
| 44 | + assert_not_nil assigns(:role) | ||
| 45 | + assert_equal 1, assigns(:role).id | ||
| 46 | + end | ||
| 47 | + | ||
| 48 | + def test_should_update_to_valid_parameters | ||
| 49 | + Role.any_instance.stubs(:valid?).returns(true) | ||
| 50 | + post 'update', :id => 1 | ||
| 51 | + assert_not_nil assigns(:role) | ||
| 52 | + assert_nil flash[:notice] | ||
| 53 | + assert_response :redirect | ||
| 54 | + end | ||
| 55 | + | ||
| 56 | + def test_should_not_update_to_invalid_paramters | ||
| 57 | + Role.any_instance.stubs(:valid?).returns(false) | ||
| 58 | + post 'update', :id => 1 | ||
| 59 | + assert_not_nil assigns(:role) | ||
| 60 | + assert_not_nil flash[:notice] | ||
| 61 | + assert_response :success | ||
| 62 | + end | ||
| 63 | + | ||
| 64 | + def test_should_destroy | ||
| 65 | + assert_difference Role, :count, -1 do | ||
| 66 | + post 'destroy', :id => 1 | ||
| 67 | + assert_not_nil assigns(:role) | ||
| 68 | + end | ||
| 17 | end | 69 | end |
| 18 | end | 70 | end |
test/unit/role_assignment_test.rb
| 1 | require File.dirname(__FILE__) + '/../test_helper' | 1 | require File.dirname(__FILE__) + '/../test_helper' |
| 2 | 2 | ||
| 3 | class RoleAssignmentTest < Test::Unit::TestCase | 3 | class RoleAssignmentTest < Test::Unit::TestCase |
| 4 | - fixtures :role_assignments | 4 | + all_fixtures |
| 5 | + | ||
| 6 | + def test_has_generic_permission | ||
| 7 | + role = Role.create(:name => 'new_role', :permissions => ['permission']) | ||
| 8 | + ra = RoleAssignment.create(:role => role) | ||
| 9 | + assert ra.has_permission?('permission', nil) | ||
| 10 | + assert !ra.has_permission?('not_permitted', nil) | ||
| 11 | + end | ||
| 5 | 12 | ||
| 6 | - # Replace this with your real tests. | ||
| 7 | - def test_truth | ||
| 8 | - assert true | 13 | + def test_has_specific_permission |
| 14 | + role = Role.create(:name => 'new_role', :permissions => ['permission']) | ||
| 15 | + resource_A = Profile.create(:identifier => 'resource_a', :name => 'Resource A') | ||
| 16 | + resource_B = Profile.create(:identifier => 'resource_b', :name => 'Resource B') | ||
| 17 | + ra = RoleAssignment.create(:role => role, :resource => resource_A) | ||
| 18 | + assert ra.has_permission?('permission', resource_A) | ||
| 19 | + assert !ra.has_permission?('permission', resource_B) | ||
| 9 | end | 20 | end |
| 10 | end | 21 | end |
test/unit/role_test.rb
| 1 | require File.dirname(__FILE__) + '/../test_helper' | 1 | require File.dirname(__FILE__) + '/../test_helper' |
| 2 | 2 | ||
| 3 | class RoleTest < Test::Unit::TestCase | 3 | class RoleTest < Test::Unit::TestCase |
| 4 | - fixtures :roles | 4 | + all_fixtures |
| 5 | 5 | ||
| 6 | - # Replace this with your real tests. | ||
| 7 | - def test_truth | ||
| 8 | - assert true | 6 | + def test_role_creation |
| 7 | + assert_difference Role, :count do | ||
| 8 | + role = Role.new(:name => 'new_role') | ||
| 9 | + assert role.save | ||
| 10 | + end | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | + def test_uniqueness_of_name | ||
| 14 | + Role.create(:name => 'role_name') | ||
| 15 | + role = Role.new(:name => 'role_name') | ||
| 16 | + assert ! role.save | ||
| 17 | + end | ||
| 18 | + | ||
| 19 | + def test_name_of_permission | ||
| 20 | + assert_equal 'Edit profile', Role.permission_name('edit_profile') | ||
| 21 | + end | ||
| 22 | + | ||
| 23 | + def test_permission_setting | ||
| 24 | + role = Role.new(:name => 'permissive_role', :permissions => ['edit_profile']) | ||
| 25 | + assert role.save | ||
| 26 | + assert role.has_permission?('edit_profile') | ||
| 27 | + role.permissions << 'post_content' | ||
| 28 | + assert role.save | ||
| 29 | + assert role.has_permission?('post_content') | ||
| 30 | + assert role.has_permission?('edit_profile') | ||
| 31 | + end | ||
| 32 | + | ||
| 33 | + def test_permission_existece | ||
| 34 | + role = Role.new(:name => 'role_with_non_existent_permission') | ||
| 35 | + role.permissions << 'non_existent_permission' | ||
| 36 | + assert ! role.save | ||
| 9 | end | 37 | end |
| 10 | end | 38 | end |