consul 0.12.2 → 0.12.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of consul might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/consul.gemspec +1 -1
- data/lib/consul/version.rb +1 -1
- metadata +2 -126
- data/spec/rails-2.3/Gemfile +0 -13
- data/spec/rails-2.3/Gemfile.lock +0 -65
- data/spec/rails-2.3/Rakefile +0 -11
- data/spec/rails-2.3/app_root/app +0 -1
- data/spec/rails-2.3/app_root/config/boot.rb +0 -128
- data/spec/rails-2.3/app_root/config/database.yml +0 -21
- data/spec/rails-2.3/app_root/config/environment.rb +0 -14
- data/spec/rails-2.3/app_root/config/environments/in_memory.rb +0 -0
- data/spec/rails-2.3/app_root/config/environments/mysql.rb +0 -0
- data/spec/rails-2.3/app_root/config/environments/postgresql.rb +0 -0
- data/spec/rails-2.3/app_root/config/environments/sqlite.rb +0 -0
- data/spec/rails-2.3/app_root/config/environments/sqlite3.rb +0 -0
- data/spec/rails-2.3/app_root/config/initializers/fix_missing_source_file.rb +0 -1
- data/spec/rails-2.3/app_root/config/preinitializer.rb +0 -20
- data/spec/rails-2.3/app_root/config/routes.rb +0 -21
- data/spec/rails-2.3/app_root/db +0 -1
- data/spec/rails-2.3/app_root/log/.gitignore +0 -1
- data/spec/rails-2.3/rcov.opts +0 -2
- data/spec/rails-2.3/spec.opts +0 -4
- data/spec/rails-2.3/spec/spec_helper.rb +0 -36
- data/spec/rails-3.0/.rspec +0 -2
- data/spec/rails-3.0/Gemfile +0 -11
- data/spec/rails-3.0/Gemfile.lock +0 -126
- data/spec/rails-3.0/Rakefile +0 -11
- data/spec/rails-3.0/app_root/.gitignore +0 -4
- data/spec/rails-3.0/app_root/app +0 -1
- data/spec/rails-3.0/app_root/config/application.rb +0 -31
- data/spec/rails-3.0/app_root/config/boot.rb +0 -13
- data/spec/rails-3.0/app_root/config/database.yml +0 -4
- data/spec/rails-3.0/app_root/config/environment.rb +0 -5
- data/spec/rails-3.0/app_root/config/environments/test.rb +0 -35
- data/spec/rails-3.0/app_root/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/rails-3.0/app_root/config/initializers/inflections.rb +0 -10
- data/spec/rails-3.0/app_root/config/initializers/mime_types.rb +0 -5
- data/spec/rails-3.0/app_root/config/initializers/secret_token.rb +0 -7
- data/spec/rails-3.0/app_root/config/initializers/session_store.rb +0 -8
- data/spec/rails-3.0/app_root/config/routes.rb +0 -3
- data/spec/rails-3.0/app_root/db +0 -1
- data/spec/rails-3.0/app_root/lib/tasks/.gitkeep +0 -0
- data/spec/rails-3.0/app_root/log/.gitkeep +0 -0
- data/spec/rails-3.0/app_root/script/rails +0 -6
- data/spec/rails-3.0/rcov.opts +0 -2
- data/spec/rails-3.0/spec/spec_helper.rb +0 -23
- data/spec/rails-3.2/.rspec +0 -2
- data/spec/rails-3.2/Gemfile +0 -11
- data/spec/rails-3.2/Gemfile.lock +0 -135
- data/spec/rails-3.2/Rakefile +0 -11
- data/spec/rails-3.2/app_root/.gitignore +0 -4
- data/spec/rails-3.2/app_root/app +0 -1
- data/spec/rails-3.2/app_root/config/application.rb +0 -31
- data/spec/rails-3.2/app_root/config/boot.rb +0 -13
- data/spec/rails-3.2/app_root/config/database.yml +0 -4
- data/spec/rails-3.2/app_root/config/environment.rb +0 -5
- data/spec/rails-3.2/app_root/config/environments/test.rb +0 -35
- data/spec/rails-3.2/app_root/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/rails-3.2/app_root/config/initializers/inflections.rb +0 -10
- data/spec/rails-3.2/app_root/config/initializers/mime_types.rb +0 -5
- data/spec/rails-3.2/app_root/config/initializers/secret_token.rb +0 -7
- data/spec/rails-3.2/app_root/config/initializers/session_store.rb +0 -8
- data/spec/rails-3.2/app_root/config/routes.rb +0 -3
- data/spec/rails-3.2/app_root/db +0 -1
- data/spec/rails-3.2/app_root/log/.gitignore +0 -1
- data/spec/rails-3.2/rcov.opts +0 -2
- data/spec/rails-3.2/spec/spec_helper.rb +0 -27
- data/spec/rails-4.1/.rspec +0 -2
- data/spec/rails-4.1/Gemfile +0 -12
- data/spec/rails-4.1/Gemfile.lock +0 -134
- data/spec/rails-4.1/Rakefile +0 -12
- data/spec/rails-4.1/app_root/.gitignore +0 -16
- data/spec/rails-4.1/app_root/app +0 -1
- data/spec/rails-4.1/app_root/bin/bundle +0 -3
- data/spec/rails-4.1/app_root/bin/rails +0 -8
- data/spec/rails-4.1/app_root/bin/rake +0 -8
- data/spec/rails-4.1/app_root/bin/spring +0 -18
- data/spec/rails-4.1/app_root/config/application.rb +0 -28
- data/spec/rails-4.1/app_root/config/boot.rb +0 -4
- data/spec/rails-4.1/app_root/config/database.yml +0 -4
- data/spec/rails-4.1/app_root/config/environment.rb +0 -5
- data/spec/rails-4.1/app_root/config/environments/development.rb +0 -37
- data/spec/rails-4.1/app_root/config/environments/production.rb +0 -83
- data/spec/rails-4.1/app_root/config/environments/test.rb +0 -39
- data/spec/rails-4.1/app_root/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/rails-4.1/app_root/config/initializers/cookies_serializer.rb +0 -3
- data/spec/rails-4.1/app_root/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/rails-4.1/app_root/config/initializers/inflections.rb +0 -16
- data/spec/rails-4.1/app_root/config/initializers/mime_types.rb +0 -4
- data/spec/rails-4.1/app_root/config/initializers/session_store.rb +0 -3
- data/spec/rails-4.1/app_root/config/initializers/wrap_parameters.rb +0 -14
- data/spec/rails-4.1/app_root/config/locales/en.yml +0 -23
- data/spec/rails-4.1/app_root/config/routes.rb +0 -3
- data/spec/rails-4.1/app_root/config/secrets.yml +0 -22
- data/spec/rails-4.1/app_root/db +0 -1
- data/spec/rails-4.1/log/test.log +0 -0
- data/spec/rails-4.1/rcov.opts +0 -2
- data/spec/rails-4.1/spec/spec_helper.rb +0 -30
- data/spec/shared/app_root/app/controllers/application_controller.rb +0 -16
- data/spec/shared/app_root/app/controllers/cakes_controller.rb +0 -55
- data/spec/shared/app_root/app/controllers/client_notes_controller.rb +0 -13
- data/spec/shared/app_root/app/controllers/colors_controller.rb +0 -10
- data/spec/shared/app_root/app/controllers/dashboards_controller.rb +0 -20
- data/spec/shared/app_root/app/controllers/risks_controller.rb +0 -7
- data/spec/shared/app_root/app/controllers/songs_controller.rb +0 -14
- data/spec/shared/app_root/app/controllers/users_controller.rb +0 -13
- data/spec/shared/app_root/app/models/client.rb +0 -9
- data/spec/shared/app_root/app/models/deal.rb +0 -3
- data/spec/shared/app_root/app/models/deal/item.rb +0 -3
- data/spec/shared/app_root/app/models/note.rb +0 -7
- data/spec/shared/app_root/app/models/power.rb +0 -133
- data/spec/shared/app_root/app/models/song.rb +0 -6
- data/spec/shared/app_root/app/models/user.rb +0 -8
- data/spec/shared/app_root/db/migrate/001_create_users.rb +0 -13
- data/spec/shared/app_root/db/migrate/002_create_clients.rb +0 -13
- data/spec/shared/app_root/db/migrate/003_create_notes.rb +0 -13
- data/spec/shared/app_root/db/migrate/004_create_songs.rb +0 -14
- data/spec/shared/consul/active_record_spec.rb +0 -37
- data/spec/shared/consul/controllers/cakes_controller_spec.rb +0 -61
- data/spec/shared/consul/controllers/client_notes_controller_spec.rb +0 -25
- data/spec/shared/consul/controllers/colors_controller_spec.rb +0 -29
- data/spec/shared/consul/controllers/dashboards_controller_spec.rb +0 -26
- data/spec/shared/consul/controllers/risks_controller_spec.rb +0 -9
- data/spec/shared/consul/controllers/songs_controller_spec.rb +0 -13
- data/spec/shared/consul/controllers/users_controller_spec.rb +0 -25
- data/spec/shared/consul/power_spec.rb +0 -688
- data/spec/shared/consul/util_spec.rb +0 -17
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ClientNotesController, :type => :controller do
|
4
|
-
|
5
|
-
it 'should map powers with context' do
|
6
|
-
@client1 = Client.create!
|
7
|
-
@client1_note1 = @client1.notes.create!
|
8
|
-
@client2 = Client.create!
|
9
|
-
@client2_note1 = @client2.notes.create!
|
10
|
-
controller.stub(
|
11
|
-
:current_power => Power.new,
|
12
|
-
:client => @client1
|
13
|
-
)
|
14
|
-
controller.send(:note_scope).to_a.should == [@client1_note1]
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'should fail if a context is missing' do
|
18
|
-
controller.stub(
|
19
|
-
:current_power => Power.new,
|
20
|
-
:client => nil
|
21
|
-
)
|
22
|
-
expect { controller.send(:note_scope) }.to raise_error(Consul::MissingContext)
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ColorsController, :type => :controller do
|
4
|
-
|
5
|
-
it "should allow multiple .power directives" do
|
6
|
-
controller.stub :current_power => Power.new(:red => true, :blue => false)
|
7
|
-
expect { get :show, :id => '1' }.to raise_error(Consul::Powerless)
|
8
|
-
controller.stub :current_power => Power.new(:red => false, :blue => true)
|
9
|
-
expect { get :show, :id => '1' }.to raise_error(Consul::Powerless)
|
10
|
-
controller.stub :current_power => Power.new(:red => true, :blue => true)
|
11
|
-
expect { get :show, :id => '1' }.to_not raise_error
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'should be able to map multiple powers to methods' do
|
15
|
-
controller.stub :current_power => Power.new(:red => true, :blue => false)
|
16
|
-
controller.send(:red_scope).should == 'red'
|
17
|
-
controller.send(:blue_scope).should be_nil
|
18
|
-
controller.stub :current_power => Power.new(:red => false, :blue => true)
|
19
|
-
controller.send(:red_scope).should be_nil
|
20
|
-
controller.send(:blue_scope).should == 'blue'
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'should make a mapped power method private' do
|
24
|
-
controller.stub :current_power => Power.new
|
25
|
-
expect { controller.red_scope }.to raise_error(NoMethodError)
|
26
|
-
expect { controller.send(:red_scope) }.to_not raise_error
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe DashboardsController, :type => :controller do
|
4
|
-
|
5
|
-
it "should not raise an error if the checked power is given" do
|
6
|
-
expect { get :show }.to_not raise_error
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'should define a method #current_power that returns the Power' do
|
10
|
-
controller.should_receive(:observe).with(kind_of(Power))
|
11
|
-
get :show
|
12
|
-
end
|
13
|
-
|
14
|
-
it "should set the current power before the request, and nilify it after the request" do
|
15
|
-
controller.send(:current_power).should be_nil
|
16
|
-
Power.should_receive_and_execute(:current=).ordered.with(kind_of(Power))
|
17
|
-
Power.should_receive_and_execute(:current=).ordered.with(nil)
|
18
|
-
get :show
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should nilify the current power even if the action raises an error' do
|
22
|
-
expect { post :error }.to raise_error(/error during action/)
|
23
|
-
Power.current.should be_nil
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
@@ -1,9 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe RisksController, :type => :controller do
|
4
|
-
|
5
|
-
it "should raise an error it includes Consul::Controller but forgets to define current_power do ... end" do
|
6
|
-
expect { put :show, :id => '1' }.to raise_error(Consul::UnreachablePower)
|
7
|
-
end
|
8
|
-
|
9
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe SongsController, :type => :controller do
|
4
|
-
|
5
|
-
it 'should allow to skip a required power check' do
|
6
|
-
expect { get :show, :id => '1' }.to_not raise_error
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should raise an error if an action is not checked against a power" do
|
10
|
-
expect { put :update, :id => '1' }.to raise_error(Consul::UncheckedPower)
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe UsersController, :type => :controller do
|
4
|
-
|
5
|
-
it "should raise an error if the checked power is not given" do
|
6
|
-
expect { get :update, :id => '1' }.to raise_error(Consul::Powerless)
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'should allow to map actions to another power using the :map option' do
|
10
|
-
expect { get :show, :id => '1' }.to_not raise_error
|
11
|
-
end
|
12
|
-
|
13
|
-
|
14
|
-
#describe '.power_name_for_action' do
|
15
|
-
#
|
16
|
-
# it 'should return the name of the power for the given action (feature request from devolute)' do
|
17
|
-
# UsersController.power_name_for_action(:show).should == :always_true
|
18
|
-
# UsersController.power_name_for_action('show').should == :always_true
|
19
|
-
# UsersController.power_name_for_action(:update).should == :always_false
|
20
|
-
# UsersController.power_name_for_action('update').should == :always_false
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
#end
|
24
|
-
|
25
|
-
end
|
@@ -1,688 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'thread'
|
3
|
-
|
4
|
-
describe Consul::Power do
|
5
|
-
|
6
|
-
before :each do
|
7
|
-
@user = User.create!
|
8
|
-
@deleted_client = Client.create!(:deleted => true)
|
9
|
-
@client1 = Client.create!
|
10
|
-
@client1_note1 = @client1.notes.create!
|
11
|
-
@client1_note2 = @client1.notes.create!
|
12
|
-
@client2 = Client.create!
|
13
|
-
@client2_note1 = @client2.notes.create!
|
14
|
-
@client2_note2 = @client2.notes.create!
|
15
|
-
end
|
16
|
-
|
17
|
-
describe 'integration scenario' do
|
18
|
-
|
19
|
-
it 'should work with real records' do
|
20
|
-
Client.active.should == [@client1, @client2]
|
21
|
-
@client1.notes.should == [@client1_note1, @client1_note2]
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'nil powers' do
|
27
|
-
|
28
|
-
describe 'query methods' do
|
29
|
-
|
30
|
-
context 'when no record is given' do
|
31
|
-
|
32
|
-
it 'should return false' do
|
33
|
-
@user.role = 'guest'
|
34
|
-
@user.power.clients.should be_nil
|
35
|
-
@user.power.clients?.should == false
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
context 'with a given record' do
|
41
|
-
|
42
|
-
it 'should return false' do
|
43
|
-
client = Client.create!
|
44
|
-
@user.role = 'guest'
|
45
|
-
@user.power.clients.should be_nil
|
46
|
-
@user.power.client?(client).should == false
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
describe 'bang methods' do
|
54
|
-
|
55
|
-
context 'when no record is given' do
|
56
|
-
|
57
|
-
it 'should raise Consul::Powerless when the power returns nil' do
|
58
|
-
@user.role = 'guest'
|
59
|
-
@user.power.clients.should be_nil
|
60
|
-
expect { @user.power.clients! }.to raise_error(Consul::Powerless)
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
context 'with a given record' do
|
66
|
-
|
67
|
-
it 'should raise Consul::Powerless when' do
|
68
|
-
client = Client.create!
|
69
|
-
@user.role = 'guest'
|
70
|
-
@user.power.clients.should be_nil
|
71
|
-
expect { @user.power.client!(client) }.to raise_error(Consul::Powerless)
|
72
|
-
end
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'scope powers' do
|
81
|
-
|
82
|
-
it 'should return the registered scope' do
|
83
|
-
@user.power.clients.all.should == [@client1, @client2]
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'should allow to register scopes with arguments' do
|
87
|
-
@user.power.client_notes(@client1).should == [@client1_note1, @client1_note2]
|
88
|
-
end
|
89
|
-
|
90
|
-
describe 'query methods' do
|
91
|
-
|
92
|
-
context 'when no record is given' do
|
93
|
-
|
94
|
-
it 'should return true if the power returns a scope (which might or might not match records)' do
|
95
|
-
@user.power.clients?.should == true
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'should not trigger a query if the power returns a has many association' do
|
99
|
-
Note.count.should > 0 # show that we have records in the database
|
100
|
-
@client1.reload
|
101
|
-
@user.power.client_notes?(@client1)
|
102
|
-
@client1.notes.loaded?.should == false
|
103
|
-
end
|
104
|
-
|
105
|
-
end
|
106
|
-
|
107
|
-
context 'with a given record' do
|
108
|
-
|
109
|
-
it 'should return true if the record belongs to the scope' do
|
110
|
-
@user.power.client?(@client1).should == true
|
111
|
-
end
|
112
|
-
|
113
|
-
it 'should return false if the record does not belong to the scope' do
|
114
|
-
@user.power.client?(@deleted_client).should == false
|
115
|
-
end
|
116
|
-
|
117
|
-
it 'should only trigger a single query for multiple checks on the same scope' do
|
118
|
-
@user.power.should_receive(:database_touched).once
|
119
|
-
@user.power.client?(@client1)
|
120
|
-
@user.power.client?(@deleted_client)
|
121
|
-
end
|
122
|
-
|
123
|
-
it 'should trigger a query only if the scope defines a condition' do
|
124
|
-
@user.power.should_receive(:database_touched).once
|
125
|
-
@user.power.client?(@client1)
|
126
|
-
end
|
127
|
-
|
128
|
-
it 'should not trigger a query if the scope defines no conditions' do
|
129
|
-
@user.power.should_not_receive(:database_touched)
|
130
|
-
@user.power.all_client?(@client1)
|
131
|
-
end
|
132
|
-
|
133
|
-
it 'should always trigger a query if a returned model has a default scope, even if it defines no additional conditions' do
|
134
|
-
@user.power.should_receive(:database_touched).once
|
135
|
-
@user.power.song?(Song.new)
|
136
|
-
end
|
137
|
-
|
138
|
-
it 'should trigger query if a returned model has a default scope and defines additional conditions' do
|
139
|
-
@user.power.should_receive(:database_touched).once
|
140
|
-
@user.power.recent_song?(Song.new)
|
141
|
-
end
|
142
|
-
|
143
|
-
it 'should work with powers that have arguments' do
|
144
|
-
@user.power.client_note?(@client1, @client1_note1).should == true
|
145
|
-
@user.power.client_note?(@client1, @client2_note1).should == false
|
146
|
-
end
|
147
|
-
|
148
|
-
context 'optimization through additional definition of a Ruby method' do
|
149
|
-
|
150
|
-
it 'should not affect collection access' do
|
151
|
-
@user.power.fast_clients.to_a.should =~ [@client1, @client2]
|
152
|
-
@user.power.fast_client_ids.to_a.should =~ [@client1.id, @client2.id]
|
153
|
-
@user.power.fast_clients?.should == true
|
154
|
-
end
|
155
|
-
|
156
|
-
it 'should no longer access the database when checking inclusion of a single record' do
|
157
|
-
@user.power.should_not_receive(:database_touched)
|
158
|
-
@user.power.fast_client?(@client1).should == true
|
159
|
-
@user.power.fast_client?(@deleted_client).should == false
|
160
|
-
end
|
161
|
-
|
162
|
-
it 'is used by and bang methods' do
|
163
|
-
@user.power.should_not_receive(:database_touched)
|
164
|
-
expect { @user.power.fast_client!(@client1) }.to_not raise_error
|
165
|
-
expect { @user.power.fast_client!(@deleted_client) }.to raise_error(Consul::Powerless)
|
166
|
-
end
|
167
|
-
|
168
|
-
it 'works with parametrized powers' do
|
169
|
-
@user.power.should_not_receive(:database_touched)
|
170
|
-
@user.power.fast_client_note?(@client1, @client1_note1).should == true
|
171
|
-
@user.power.fast_client_note?(@client1, @client2_note1).should == false
|
172
|
-
expect { @user.power.fast_client_note!(@client1, @client1_note1) }.to_not raise_error
|
173
|
-
expect { @user.power.fast_client_note!(@client1, @client2_note1) }.to raise_error(Consul::Powerless)
|
174
|
-
end
|
175
|
-
|
176
|
-
it 'works when only the optimized power is defined, without a collection power' do
|
177
|
-
@user.power.should_not_receive(:database_touched)
|
178
|
-
@user.power.fast_client_note_without_collection?(@client1, @client1_note1).should == true
|
179
|
-
@user.power.fast_client_note_without_collection?(@client1, @client2_note1).should == false
|
180
|
-
end
|
181
|
-
|
182
|
-
end
|
183
|
-
|
184
|
-
end
|
185
|
-
|
186
|
-
#context 'when a power with arguments is given insufficient context' do
|
187
|
-
#
|
188
|
-
# it 'should raise an error' do
|
189
|
-
# expect { @user.power.client_notes? }.to raise_error(ArgumentError)
|
190
|
-
# end
|
191
|
-
#
|
192
|
-
#end
|
193
|
-
|
194
|
-
end
|
195
|
-
|
196
|
-
describe 'bang methods' do
|
197
|
-
|
198
|
-
context 'when no record is given' do
|
199
|
-
|
200
|
-
it 'should not raise Consul::Powerless when the power returns a scope (which might or might not match records)' do
|
201
|
-
expect { @user.power.clients! }.to_not raise_error
|
202
|
-
end
|
203
|
-
|
204
|
-
end
|
205
|
-
|
206
|
-
context 'with a given record' do
|
207
|
-
|
208
|
-
it 'should raise Consul::Powerless when record belongs is inside the scope' do
|
209
|
-
expect { @user.power.client!(@deleted_client) }.to raise_error(Consul::Powerless)
|
210
|
-
end
|
211
|
-
|
212
|
-
it 'should not raise Consul::Powerless when the record is outside a scope' do
|
213
|
-
expect { @user.power.client!(@client1) }.to_not raise_error
|
214
|
-
end
|
215
|
-
|
216
|
-
it 'should work with scopes that have arguments' do
|
217
|
-
expect { @user.power.client_note!(@client1, @client1_note1) }.to_not raise_error
|
218
|
-
expect { @user.power.client_note!(@client1, @client2_note1) }.to raise_error(Consul::Powerless)
|
219
|
-
end
|
220
|
-
|
221
|
-
end
|
222
|
-
|
223
|
-
end
|
224
|
-
|
225
|
-
describe 'retrieving scope_ids' do
|
226
|
-
|
227
|
-
it 'should return record ids that match the registered scope' do
|
228
|
-
@user.power.client_ids.should == [@client1.id, @client2.id]
|
229
|
-
end
|
230
|
-
|
231
|
-
it 'should cache scope ids' do
|
232
|
-
@user.power.should_receive(:clients).once.and_return(double('scope', :construct_finder_sql => 'SELECT 1', :to_sql => 'SELECT 1').as_null_object)
|
233
|
-
2.times { @user.power.client_ids }
|
234
|
-
end
|
235
|
-
|
236
|
-
it 'should return ids when the scope joins another table (bugfix)' do
|
237
|
-
expect { @user.power.note_ids }.to_not raise_error
|
238
|
-
end
|
239
|
-
|
240
|
-
it 'should work with scopes that have arguments' do
|
241
|
-
@user.power.client_note_ids(@client1).should =~ [@client1_note1.id, @client1_note2.id]
|
242
|
-
end
|
243
|
-
|
244
|
-
end
|
245
|
-
|
246
|
-
end
|
247
|
-
|
248
|
-
context 'collection powers' do
|
249
|
-
|
250
|
-
it 'should return the registered collection' do
|
251
|
-
@user.power.key_figures.should == %w[amount working_costs]
|
252
|
-
end
|
253
|
-
|
254
|
-
describe 'query methods' do
|
255
|
-
|
256
|
-
context 'when no record is given' do
|
257
|
-
|
258
|
-
it 'should return true if the returns an enumerable (which might or might not be empty)' do
|
259
|
-
@user.power.key_figures?.should == true
|
260
|
-
end
|
261
|
-
|
262
|
-
it 'should return false if the power returns nil' do
|
263
|
-
@user.role = 'guest'
|
264
|
-
@user.power.key_figures.should be_nil
|
265
|
-
@user.power.key_figures?.should == false
|
266
|
-
end
|
267
|
-
|
268
|
-
end
|
269
|
-
|
270
|
-
context 'with a given record' do
|
271
|
-
|
272
|
-
it 'should return true if the power contains the given record' do
|
273
|
-
@user.power.key_figure?('amount').should == true
|
274
|
-
end
|
275
|
-
|
276
|
-
it 'should return false if the power does not contain the given record' do
|
277
|
-
@user.power.key_figure?('xyz').should == false
|
278
|
-
end
|
279
|
-
|
280
|
-
end
|
281
|
-
|
282
|
-
end
|
283
|
-
|
284
|
-
describe 'bang methods' do
|
285
|
-
|
286
|
-
context 'when no record is given' do
|
287
|
-
|
288
|
-
it 'should not raise Consul::Powerless if the power returns an enumerable (which might or might not be empty)' do
|
289
|
-
expect { @user.power.key_figures! }.to_not raise_error
|
290
|
-
end
|
291
|
-
|
292
|
-
it 'should raise Consul::Powerless if the power returns nil' do
|
293
|
-
@user.role = 'guest'
|
294
|
-
@user.power.key_figures.should be_nil
|
295
|
-
expect { @user.power.key_figures! }.to raise_error(Consul::Powerless)
|
296
|
-
end
|
297
|
-
|
298
|
-
end
|
299
|
-
|
300
|
-
context 'with a given record' do
|
301
|
-
|
302
|
-
it 'should not raise Consul::Powerless if the power contains the given record' do
|
303
|
-
expect { @user.power.key_figure?('amount') }.to_not raise_error
|
304
|
-
end
|
305
|
-
|
306
|
-
it 'should raise Consul::Powerless if the power does not contain the given record' do
|
307
|
-
expect { @user.power.key_figure!('xyz') }.to raise_error(Consul::Powerless)
|
308
|
-
end
|
309
|
-
end
|
310
|
-
|
311
|
-
end
|
312
|
-
|
313
|
-
end
|
314
|
-
|
315
|
-
context 'boolean powers' do
|
316
|
-
|
317
|
-
it 'should return the registered value' do
|
318
|
-
@user.power.always_true.should == true
|
319
|
-
@user.power.always_false.should == false
|
320
|
-
end
|
321
|
-
|
322
|
-
describe 'query methods' do
|
323
|
-
|
324
|
-
context 'when no record is given' do
|
325
|
-
|
326
|
-
it 'should return true when the queried power returns true' do
|
327
|
-
@user.power.always_true?.should == true
|
328
|
-
end
|
329
|
-
|
330
|
-
it 'should return false when the queried power returns false' do
|
331
|
-
@user.power.always_false?.should == false
|
332
|
-
end
|
333
|
-
|
334
|
-
it 'should return false when the queried power returns nil' do
|
335
|
-
@user.power.always_nil?.should == false
|
336
|
-
end
|
337
|
-
|
338
|
-
end
|
339
|
-
|
340
|
-
context 'with a given record' do
|
341
|
-
|
342
|
-
#it 'should raise Consul::NoCollection' do
|
343
|
-
# expect { @user.power.always_true?('foo') }.to raise_error(Consul::NoCollection)
|
344
|
-
#end
|
345
|
-
|
346
|
-
end
|
347
|
-
|
348
|
-
end
|
349
|
-
|
350
|
-
describe 'bang methods' do
|
351
|
-
|
352
|
-
context 'when no record is given' do
|
353
|
-
|
354
|
-
it 'should not raise Consul::Powerless when the power returns true' do
|
355
|
-
expect { @user.power.always_true! }.to_not raise_error
|
356
|
-
end
|
357
|
-
|
358
|
-
it 'should raise Consul::Powerless when the power returns false' do
|
359
|
-
expect { @user.power.always_false! }.to raise_error(Consul::Powerless)
|
360
|
-
end
|
361
|
-
|
362
|
-
it 'should raise Consul::Powerless when the power returns nil' do
|
363
|
-
expect { @user.power.always_nil! }.to raise_error(Consul::Powerless)
|
364
|
-
end
|
365
|
-
|
366
|
-
end
|
367
|
-
|
368
|
-
context 'with a given record' do
|
369
|
-
|
370
|
-
#it 'should raise Consul::NoCollection' do
|
371
|
-
# expect { @user.power.always_true!('foo') }.to raise_error(Consul::NoCollection)
|
372
|
-
#end
|
373
|
-
|
374
|
-
end
|
375
|
-
|
376
|
-
end
|
377
|
-
|
378
|
-
|
379
|
-
end
|
380
|
-
|
381
|
-
context 'powers of other types' do
|
382
|
-
|
383
|
-
it 'should return the registered value' do
|
384
|
-
@user.power.api_key.should == 'secret-api-key'
|
385
|
-
end
|
386
|
-
|
387
|
-
describe 'query methods' do
|
388
|
-
|
389
|
-
context 'when no record is given' do
|
390
|
-
|
391
|
-
it 'should return true if the power is not nil' do
|
392
|
-
@user.power.api_key?.should == true
|
393
|
-
end
|
394
|
-
|
395
|
-
it 'should return false if the power is nil' do
|
396
|
-
@user.role = 'guest'
|
397
|
-
@user.power.api_key.should be_nil
|
398
|
-
@user.power.api_key?.should == false
|
399
|
-
end
|
400
|
-
|
401
|
-
end
|
402
|
-
|
403
|
-
context 'with a given record' do
|
404
|
-
|
405
|
-
#it 'should raise Consul::NoCollection' do
|
406
|
-
# expect { @user.power.api_key?('foo') }.to raise_error(Consul::NoCollection)
|
407
|
-
#end
|
408
|
-
|
409
|
-
end
|
410
|
-
|
411
|
-
end
|
412
|
-
|
413
|
-
describe 'bang methods' do
|
414
|
-
|
415
|
-
context 'when no record is given' do
|
416
|
-
|
417
|
-
it 'should not raise Consul::Powerless if the power is not nil' do
|
418
|
-
expect { @user.power.api_key! }.to_not raise_error
|
419
|
-
end
|
420
|
-
|
421
|
-
it 'should raise Consul::powerless if the power is nil' do
|
422
|
-
@user.role = 'guest'
|
423
|
-
@user.power.api_key.should be_nil
|
424
|
-
expect { @user.power.api_key! }.to raise_error(Consul::Powerless)
|
425
|
-
end
|
426
|
-
|
427
|
-
end
|
428
|
-
|
429
|
-
context 'with a given record' do
|
430
|
-
|
431
|
-
#it 'should raise Consul::NoCollection' do
|
432
|
-
# expect { @user.power.api_key!('foo') }.to raise_error(Consul::NoCollection)
|
433
|
-
#end
|
434
|
-
|
435
|
-
end
|
436
|
-
|
437
|
-
end
|
438
|
-
|
439
|
-
end
|
440
|
-
|
441
|
-
describe '.power' do
|
442
|
-
|
443
|
-
it 'should allow to define multiple powers at once' do
|
444
|
-
@user.power.shorthand1.should == 'shorthand'
|
445
|
-
@user.power.shorthand2.should == 'shorthand'
|
446
|
-
@user.power.shorthand3.should == 'shorthand'
|
447
|
-
end
|
448
|
-
|
449
|
-
end
|
450
|
-
|
451
|
-
describe '.current' do
|
452
|
-
|
453
|
-
it 'should provide a class method to set and get the current Power' do
|
454
|
-
Power.current = 'current power'
|
455
|
-
Power.current.should == 'current power'
|
456
|
-
Power.current = nil
|
457
|
-
Power.current.should be_nil
|
458
|
-
end
|
459
|
-
|
460
|
-
end
|
461
|
-
|
462
|
-
describe '.current=' do
|
463
|
-
|
464
|
-
it 'should set the given Power for the current thread only' do
|
465
|
-
Thread.new do
|
466
|
-
Power.current = 'some power'
|
467
|
-
end.join
|
468
|
-
Power.current.should be_nil
|
469
|
-
end
|
470
|
-
|
471
|
-
it 'should keep multiple powers separated' do
|
472
|
-
FrontendPower = Class.new(Power)
|
473
|
-
BackendPower = Class.new(Power)
|
474
|
-
frontend_power = FrontendPower.new
|
475
|
-
backend_power = BackendPower.new
|
476
|
-
FrontendPower.current = frontend_power
|
477
|
-
BackendPower.current = backend_power
|
478
|
-
|
479
|
-
FrontendPower.current.should == frontend_power
|
480
|
-
BackendPower.current.should == backend_power
|
481
|
-
end
|
482
|
-
|
483
|
-
end
|
484
|
-
|
485
|
-
describe '.with_power' do
|
486
|
-
|
487
|
-
it 'should provide the given power as current power for the duration of the block' do
|
488
|
-
spy = double
|
489
|
-
inner_power = Power.new('inner')
|
490
|
-
Power.current = 'outer power'
|
491
|
-
spy.should_receive(:observe).with(inner_power)
|
492
|
-
Power.with_power(inner_power) do
|
493
|
-
spy.observe(Power.current)
|
494
|
-
end
|
495
|
-
Power.current.should == 'outer power'
|
496
|
-
Power.current = nil # clean up for subsequent specs -- too bad we can't use .with_power :)
|
497
|
-
end
|
498
|
-
|
499
|
-
it 'should restore an existing power even if the block raises an error' do
|
500
|
-
begin
|
501
|
-
inner_power = Power.new('inner')
|
502
|
-
Power.current = 'outer power'
|
503
|
-
Power.with_power(inner_power) do
|
504
|
-
raise ZeroDivisionError
|
505
|
-
end
|
506
|
-
rescue ZeroDivisionError
|
507
|
-
# do nothing
|
508
|
-
end
|
509
|
-
Power.current.should == 'outer power'
|
510
|
-
Power.current = nil # clean up for subsequent specs -- too bad we can't use .with_power :)
|
511
|
-
end
|
512
|
-
|
513
|
-
it 'should call instantiate a new Power if the given argument is not already a power' do
|
514
|
-
spy = double
|
515
|
-
Power.should_receive(:new).with('argument').and_return('instantiated power')
|
516
|
-
spy.should_receive(:observe).with('instantiated power')
|
517
|
-
Power.with_power('argument') do
|
518
|
-
spy.observe(Power.current)
|
519
|
-
end
|
520
|
-
end
|
521
|
-
|
522
|
-
it 'should not instantiate a new Power if the given argument is nil' do
|
523
|
-
spy = double
|
524
|
-
Power.should_not_receive(:new)
|
525
|
-
spy.should_receive(:observe).with(nil)
|
526
|
-
Power.with_power(nil) do
|
527
|
-
spy.observe(Power.current)
|
528
|
-
end
|
529
|
-
end
|
530
|
-
|
531
|
-
end
|
532
|
-
|
533
|
-
describe '#for_model' do
|
534
|
-
|
535
|
-
it 'should return the power corresponding to the given model' do
|
536
|
-
@user.power.for_model(Deal).should == 'deals power'
|
537
|
-
end
|
538
|
-
|
539
|
-
it 'should return the correct power for a namespaced model' do
|
540
|
-
@user.power.for_model(Deal::Item).should == 'deal_items power'
|
541
|
-
end
|
542
|
-
|
543
|
-
it 'should allow to prefix the power with an adjective' do
|
544
|
-
@user.power.for_model(:updatable, Deal).should == 'updatable_deals power'
|
545
|
-
end
|
546
|
-
|
547
|
-
end
|
548
|
-
|
549
|
-
describe '.for_model' do
|
550
|
-
|
551
|
-
context 'when Power.current is present' do
|
552
|
-
|
553
|
-
it 'should return the power corresponding to the given model' do
|
554
|
-
Power.with_power(@user.power) do
|
555
|
-
Power.for_model(Deal).should == 'deals power'
|
556
|
-
end
|
557
|
-
end
|
558
|
-
|
559
|
-
it 'should allow to prefix the power with an adjective' do
|
560
|
-
Power.with_power(@user.power) do
|
561
|
-
Power.for_model(:updatable, Deal).should == 'updatable_deals power'
|
562
|
-
end
|
563
|
-
end
|
564
|
-
|
565
|
-
end
|
566
|
-
|
567
|
-
context 'when Power.current is nil' do
|
568
|
-
|
569
|
-
it 'should return the given model' do
|
570
|
-
Power.for_model(Deal).should == Deal
|
571
|
-
end
|
572
|
-
|
573
|
-
it 'should return the given model even if the model was prefixed with an adjective' do
|
574
|
-
Power.for_model(:updatable, Deal).should == Deal
|
575
|
-
end
|
576
|
-
|
577
|
-
end
|
578
|
-
|
579
|
-
end
|
580
|
-
|
581
|
-
describe '#include_model?' do
|
582
|
-
|
583
|
-
it 'should return if the given model corresponds to a non-nil power' do
|
584
|
-
@user.role = 'guest'
|
585
|
-
@user.power.include_model?(Client).should == false
|
586
|
-
@user.role = 'admin'
|
587
|
-
@user.power.include_model?(Client).should == true
|
588
|
-
end
|
589
|
-
|
590
|
-
end
|
591
|
-
|
592
|
-
describe '.include_model?' do
|
593
|
-
|
594
|
-
context 'when Power.current is present' do
|
595
|
-
|
596
|
-
it 'should return whether the given model corresponds to a non-nil power' do
|
597
|
-
Power.with_power(@user.power) do
|
598
|
-
@user.role = 'guest'
|
599
|
-
Power.include_model?(Deal).should == false
|
600
|
-
@user.role = 'admin'
|
601
|
-
Power.include_model?(Deal).should == true
|
602
|
-
end
|
603
|
-
end
|
604
|
-
end
|
605
|
-
|
606
|
-
context 'when Power.current is nil' do
|
607
|
-
|
608
|
-
it 'should return true' do
|
609
|
-
Power.include_model?(Deal).should == true
|
610
|
-
end
|
611
|
-
|
612
|
-
end
|
613
|
-
|
614
|
-
end
|
615
|
-
|
616
|
-
describe '#for_record' do
|
617
|
-
|
618
|
-
it 'should return the power corresponding to the class of the given record' do
|
619
|
-
@user.power.for_record(Deal.new).should == 'deals power'
|
620
|
-
end
|
621
|
-
|
622
|
-
end
|
623
|
-
|
624
|
-
describe '.for_record' do
|
625
|
-
|
626
|
-
context 'when Power.current is present' do
|
627
|
-
|
628
|
-
it 'should return the power corresponding to the class of the given record' do
|
629
|
-
Power.with_power(@user.power) do
|
630
|
-
Power.for_record(Deal.new).should == 'deals power'
|
631
|
-
end
|
632
|
-
end
|
633
|
-
|
634
|
-
it 'should allow to prefix the power with an adjective' do
|
635
|
-
Power.with_power(@user.power) do
|
636
|
-
Power.for_record(:updatable, Deal.new).should == 'updatable_deals power'
|
637
|
-
end
|
638
|
-
end
|
639
|
-
|
640
|
-
end
|
641
|
-
|
642
|
-
context 'when Power.current is nil' do
|
643
|
-
|
644
|
-
it 'should return true' do
|
645
|
-
Power.for_record(Deal.new).should == Deal
|
646
|
-
end
|
647
|
-
|
648
|
-
it 'should return true even if the model was prefixed with an adjective' do
|
649
|
-
Power.for_record(:updatable, Deal.new).should == Deal
|
650
|
-
end
|
651
|
-
|
652
|
-
end
|
653
|
-
|
654
|
-
end
|
655
|
-
|
656
|
-
describe '#include_record?' do
|
657
|
-
|
658
|
-
it 'should return if the given record is included in the power corresponding to the class of the given record' do
|
659
|
-
@user.power.include_record?(@deleted_client).should == false
|
660
|
-
@user.power.include_record?(@client1).should == true
|
661
|
-
end
|
662
|
-
|
663
|
-
end
|
664
|
-
|
665
|
-
describe '.include_record?' do
|
666
|
-
|
667
|
-
context 'when Power.current is present' do
|
668
|
-
|
669
|
-
it 'should return whether the given record is included in the the power corresponding to the class of the given record' do
|
670
|
-
Power.with_power(@user.power) do
|
671
|
-
Power.include_record?(@deleted_client).should == false
|
672
|
-
Power.include_record?(@client1).should == true
|
673
|
-
end
|
674
|
-
end
|
675
|
-
end
|
676
|
-
|
677
|
-
context 'when Power.current is nil' do
|
678
|
-
|
679
|
-
it 'should return true' do
|
680
|
-
Power.include_record?(Deal.new).should == true
|
681
|
-
end
|
682
|
-
|
683
|
-
end
|
684
|
-
|
685
|
-
end
|
686
|
-
|
687
|
-
|
688
|
-
end
|