cannie 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b94c55e9c803c675720227999d8ad2dd25fd28d
4
- data.tar.gz: 0d8ca482c78937b9f4b8703d1dc0a71a99bb4d39
3
+ metadata.gz: fb9f9ff798ce6036fb378e643d66ab7a6cc678e8
4
+ data.tar.gz: 4ae746f74792940b996abfd21842078b8fe794a7
5
5
  SHA512:
6
- metadata.gz: 7bb904c6afa19b0726dece42bb5a12268fa035bf8cd27a1747eb4c31e5c1e4c72b49255f69ff57b4719ae54a7f1f7ab0a2c988885d3b4d4c32905ec5ecd5f2e7
7
- data.tar.gz: 5caa12f62cafe9d7b357528656ca9c427f02c9eadfb01ec19b396271d52064560094e46640de8305912ebd18e31c591edbcf39fbeae917629b012b8fd255c9dd
6
+ metadata.gz: f7ce6ab4dfdfecca9c648bba8b28d19b8438cc15dc956098567853e54bf9d4cf4a8efece057537f6e30838a2db73d4014e2621fafaf55aa92ee12f975fc68a50
7
+ data.tar.gz: 65bd5926da7baf5da5f5329ce4a2c466dbf38236d55bfc0b64ba351abd37971604f3603d21a935e2bc3e93a5cbcc7d72e90230087d62bd70c179c4d3d1a8f335
@@ -19,8 +19,8 @@ module Cannie
19
19
  def check_permissions(options={})
20
20
  after_action(options.slice(:only, :except)) do |controller|
21
21
  next if controller.permitted?
22
- next if options[:if] && !controller.instance_eval(options[:if])
23
- next if options[:unless] && controller.instance_eval(options[:unless])
22
+ next if options[:if] && !controller.instance_eval(&options[:if])
23
+ next if options[:unless] && controller.instance_eval(&options[:unless])
24
24
  raise CheckPermissionsNotPerformed, 'Action failed the check_permissions because it does not calls permit! method. Add skip_check_permissions to bypass this check.'
25
25
  end
26
26
  end
@@ -1,3 +1,3 @@
1
1
  module Cannie
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -1,20 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
- class TestController < ActionController::Base
4
- def action
5
- end
6
- end
7
-
8
3
  describe Cannie::ControllerExtensions do
9
- subject { TestController.new }
10
-
11
- let(:before_filters) do
12
- subject.class._process_action_callbacks.select{|f| f.kind == :before}.map(&:raw_filter)
13
- end
4
+ let(:klass) {
5
+ Class.new(ActionController::Base) do
6
+ def action; end
7
+ end
8
+ }
14
9
 
15
- let(:after_filters) do
16
- subject.class._process_action_callbacks.select{|f| f.kind == :after}.map(&:raw_filter)
17
- end
10
+ subject { klass.new }
18
11
 
19
12
  let(:permissions) do
20
13
  Class.new do
@@ -30,66 +23,51 @@ describe Cannie::ControllerExtensions do
30
23
 
31
24
  describe '.check_permissions' do
32
25
  describe 'without conditions' do
33
- before do
34
- TestController.class_eval do
35
- check_permissions
36
- end
37
- end
26
+ before { klass.check_permissions }
38
27
 
39
28
  it 'raises exception if controller.permitted? evaluates to false' do
40
- expect { after_filters.first.call(subject) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
29
+ expect { subject.run_callbacks(:process_action) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
41
30
  end
42
31
 
43
32
  it 'does not raise exception if controller.permitted? evaluates to true' do
44
33
  subject.stub(:permitted?).and_return(true)
45
- expect { after_filters.first.call(subject) }.not_to raise_error
34
+ expect { subject.run_callbacks(:process_action) }.not_to raise_error
46
35
  end
47
36
  end
48
37
 
49
38
  describe 'with if condition' do
50
- before do
51
- TestController.class_eval do
52
- check_permissions if: ->{ self.var == true }
53
- end
54
- end
39
+ before { klass.check_permissions if: :condition? }
55
40
 
56
41
  it 'raises exception if :if block executed in controller scope returns true' do
57
- TestController.stub(:var).and_return(true)
58
- expect { after_filters.first.call(subject) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
42
+ subject.stub(:condition?).and_return(true)
43
+ expect { subject.run_callbacks(:process_action) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
59
44
  end
60
45
 
61
46
  it 'does not raise exception if :if block executed in controller scope returns false' do
62
- TestController.stub(:var).and_return(false)
63
- expect { after_filters.first.call(subject) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
47
+ subject.stub(:condition?).and_return(false)
48
+ expect { subject.run_callbacks(:process_action) }.not_to raise_error
64
49
  end
65
50
  end
66
51
 
67
52
  describe 'with unless condition' do
68
- before do
69
- TestController.class_eval do
70
- check_permissions unless: ->{ self.var == true }
71
- end
72
- end
53
+ before { klass.check_permissions unless: :condition? }
73
54
 
74
55
  it 'raises exception if :unless block executed in controller scope returns false' do
75
- TestController.stub(:var).and_return(true)
76
- expect { after_filters.first.call(subject) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
56
+ subject.stub(:condition?).and_return(false)
57
+ expect { subject.run_callbacks(:process_action) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
77
58
  end
78
59
 
79
60
  it 'does not raise exception if :unless block executed in controller scope returns false' do
80
- TestController.stub(:var).and_return(false)
81
- expect { after_filters.first.call(subject) }.to raise_error(Cannie::CheckPermissionsNotPerformed)
61
+ subject.stub(:condition?).and_return(true)
62
+ expect { subject.run_callbacks(:process_action) }.not_to raise_error
82
63
  end
83
64
  end
84
65
  end
85
66
 
86
67
  describe '.skip_check_permissions' do
87
68
  it 'sets @_permitted to true to bypass permissions checking' do
88
- subject.class.instance_eval do
89
- skip_check_permissions
90
- end
91
-
92
- before_filters.first.call(subject)
69
+ klass.skip_check_permissions
70
+ subject.run_callbacks(:process_action)
93
71
  expect(subject.permitted?).to be_true
94
72
  end
95
73
  end
@@ -126,4 +104,26 @@ describe Cannie::ControllerExtensions do
126
104
  expect { subject.permit! :update, on: [3,6,11] }.to raise_error(Cannie::ActionForbidden)
127
105
  end
128
106
  end
107
+
108
+ describe '#current_permissions' do
109
+ before(:all) do
110
+ Permissions = Class.new do
111
+ attr_reader :user
112
+ def initialize(user)
113
+ @user = user
114
+ end
115
+ end
116
+ end
117
+
118
+ before { subject.stub(:current_user).and_return 'User' }
119
+
120
+ it 'creates new Permissions object' do
121
+ expect(subject.current_permissions).to be_instance_of(Permissions)
122
+ end
123
+
124
+ it 'passes current_user to Permissions::new' do
125
+ subject.stub(:current_user).and_return 'User'
126
+ expect(subject.current_permissions.user).to eq('User')
127
+ end
128
+ end
129
129
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cannie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - hck