cirandas.net

ref: master

vendor/plugins/access_control/test/acts_as_accessor_test.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'test/unit'
require_relative 'test_helper'

class ActAsAccessorTest < Test::Unit::TestCase

  def setup
    RoleAssignment.attr_accessible :accessor
  end

  def test_can_have_role_in_respect_to_an_resource
    res = AccessControlTestResource.create!(:name => 'bla')
    a = AccessControlTestAccessor.create!(:name => 'ze')
    role = Role.create!(:name => 'just_a_member', :permissions => ['bli'])
    assert a.add_role(role, res)
    assert a.has_permission?('bli', res)
  end

  def test_can_have_a_global_role
    r = AccessControlTestResource.create!(:name => 'bla')
    a = AccessControlTestAccessor.create!(:name => 'ze')
    member_role = Role.create!(:name => 'just_a_moderator', :permissions => ['bli'])
    assert a.add_role(member_role, r)
    assert a.has_permission?('bli', r)
  end

  def test_add_role
    res = AccessControlTestResource.create!(:name => 'bla')
    a = AccessControlTestAccessor.create!(:name => 'ze')
    role = Role.create!(:name => 'just_a_content_author', :permissions => ['bli'])
    assert a.add_role(role, res)
    assert a.role_assignments.map{|ra|[ra.role, ra.accessor, ra.resource]}.include?([role, a, res])
  end

  def test_remove_role
    res = AccessControlTestResource.create!(:name => 'bla')
    a = AccessControlTestAccessor.create!(:name => 'ze')
    role = Role.create!(:name => 'just_an_author', :permissions => ['bli'])
    ra = RoleAssignment.create!(:accessor => a, :role_id => role.id, :resource_id => res.id)

    assert a.role_assignments.include?(ra)
    a.remove_role(role, res)
    a.reload
    assert !a.role_assignments.map{|ra|[ra.role, ra.accessor, ra.resource]}.include?([role, a, res])
  end

  def test_do_not_add_role_twice
    res = AccessControlTestResource.create!(:name => 'bla')
    a = AccessControlTestAccessor.create!(:name => 'ze')
    role = Role.create!(:name => 'a_content_author', :permissions => ['bli'])
    assert a.add_role(role, res)
    assert !a.add_role(role, res)
    assert a.role_assignments.map{|ra|[ra.role, ra.accessor, ra.resource]}.include?([role, a, res])
  end

  def test_do_not_remove_inexistent_role
    res = AccessControlTestResource.create!(:name => 'bla')
    a = AccessControlTestAccessor.create!(:name => 'ze')
    role = Role.create!(:name => 'an_author', :permissions => ['bli'])

    assert !a.role_assignments.map{|ra|[ra.role, ra.accessor, ra.resource]}.include?([role, a, res])
    assert !a.remove_role(role, res)
  end

end