strongbolt 0.3.12 → 0.3.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +56 -0
  3. data/.rubocop_todo.yml +91 -0
  4. data/Gemfile +1 -1
  5. data/Gemfile.lock +18 -2
  6. data/Rakefile +1 -1
  7. data/app/controllers/strongbolt/capabilities_controller.rb +36 -45
  8. data/app/controllers/strongbolt/roles_controller.rb +39 -47
  9. data/app/controllers/strongbolt/security_controller.rb +2 -3
  10. data/app/controllers/strongbolt/user_groups_controller.rb +48 -54
  11. data/app/controllers/strongbolt/user_groups_users_controller.rb +2 -4
  12. data/app/controllers/strongbolt_controller.rb +1 -1
  13. data/circle.yml +13 -0
  14. data/lib/generators/strongbolt/fix_generator.rb +5 -6
  15. data/lib/generators/strongbolt/fix_unique_group_members_generator.rb +2 -3
  16. data/lib/generators/strongbolt/indexes_generator.rb +3 -4
  17. data/lib/generators/strongbolt/install_generator.rb +8 -9
  18. data/lib/generators/strongbolt/templates/fix_unique_group_members.rb +1 -1
  19. data/lib/generators/strongbolt/templates/indexes.rb +1 -1
  20. data/lib/generators/strongbolt/templates/migration.rb +11 -12
  21. data/lib/generators/strongbolt/templates/strongbolt.rb +1 -1
  22. data/lib/generators/strongbolt/views_generator.rb +4 -4
  23. data/lib/strongbolt.rb +51 -54
  24. data/lib/strongbolt/base.rb +1 -1
  25. data/lib/strongbolt/bolted.rb +12 -13
  26. data/lib/strongbolt/bolted_controller.rb +46 -57
  27. data/lib/strongbolt/capabilities_role.rb +5 -5
  28. data/lib/strongbolt/capability.rb +32 -31
  29. data/lib/strongbolt/configuration.rb +18 -19
  30. data/lib/strongbolt/controllers/url_helpers.rb +5 -5
  31. data/lib/strongbolt/engine.rb +9 -9
  32. data/lib/strongbolt/errors.rb +4 -4
  33. data/lib/strongbolt/generators/migration.rb +4 -6
  34. data/lib/strongbolt/helpers.rb +5 -7
  35. data/lib/strongbolt/rails/routes.rb +4 -4
  36. data/lib/strongbolt/role.rb +11 -12
  37. data/lib/strongbolt/roles_user_group.rb +5 -5
  38. data/lib/strongbolt/rspec.rb +2 -2
  39. data/lib/strongbolt/rspec/user.rb +13 -15
  40. data/lib/strongbolt/tenantable.rb +78 -80
  41. data/lib/strongbolt/user_abilities.rb +44 -54
  42. data/lib/strongbolt/user_group.rb +8 -10
  43. data/lib/strongbolt/user_groups_user.rb +6 -6
  44. data/lib/strongbolt/version.rb +1 -1
  45. data/lib/tasks/strongbolt_tasks.rake +4 -4
  46. data/spec/controllers/strongbolt/capabilities_controller_spec.rb +28 -45
  47. data/spec/controllers/strongbolt/roles_controller_spec.rb +39 -72
  48. data/spec/controllers/strongbolt/user_groups_controller_spec.rb +34 -65
  49. data/spec/controllers/strongbolt/user_groups_users_controller_spec.rb +11 -19
  50. data/spec/controllers/without_authorization_controller_spec.rb +5 -5
  51. data/spec/dummy/app/controllers/posts_controller.rb +2 -2
  52. data/spec/dummy/app/controllers/test_controller.rb +1 -1
  53. data/spec/dummy/app/controllers/without_authorization_controller.rb +1 -1
  54. data/spec/dummy/bin/rails +1 -1
  55. data/spec/dummy/config.ru +1 -1
  56. data/spec/dummy/config/application.rb +4 -5
  57. data/spec/dummy/config/initializers/cookies_serializer.rb +1 -1
  58. data/spec/dummy/config/initializers/strongbolt.rb +2 -2
  59. data/spec/dummy/config/routes.rb +1 -3
  60. data/spec/dummy/db/migrate/20150630212236_create_strongbolt_tables.rb +9 -10
  61. data/spec/dummy/db/migrate/20150630212251_create_strongbolt_tables_indexes.rb +2 -2
  62. data/spec/dummy/db/migrate/20160531110509_fix_unique_group_members.rb +1 -1
  63. data/spec/fabricators/capability_fabricator.rb +4 -4
  64. data/spec/fabricators/role_fabricator.rb +3 -3
  65. data/spec/fabricators/user_fabricator.rb +2 -2
  66. data/spec/fabricators/user_group_fabricator.rb +3 -3
  67. data/spec/fixtures/application.rb +6 -3
  68. data/spec/fixtures/controllers.rb +1 -1
  69. data/spec/spec_helper.rb +7 -8
  70. data/spec/strongbolt/bolted_controller_spec.rb +110 -208
  71. data/spec/strongbolt/bolted_spec.rb +26 -40
  72. data/spec/strongbolt/capability_spec.rb +72 -86
  73. data/spec/strongbolt/configuration_spec.rb +33 -46
  74. data/spec/strongbolt/controllers/url_helpers_spec.rb +7 -9
  75. data/spec/strongbolt/helpers_spec.rb +14 -16
  76. data/spec/strongbolt/role_spec.rb +32 -35
  77. data/spec/strongbolt/tenantable_spec.rb +88 -86
  78. data/spec/strongbolt/user_abilities_multiple_tenants_spec.rb +29 -34
  79. data/spec/strongbolt/user_abilities_spec.rb +142 -188
  80. data/spec/strongbolt/user_group_spec.rb +14 -14
  81. data/spec/strongbolt/users_tenant_spec.rb +10 -12
  82. data/spec/strongbolt_spec.rb +53 -73
  83. data/spec/support/controller_macros.rb +1 -3
  84. data/spec/support/db_setup.rb +31 -25
  85. data/spec/support/helpers.rb +12 -12
  86. data/spec/support/transactional_specs.rb +1 -3
  87. data/strongbolt.gemspec +14 -12
  88. metadata +20 -3
@@ -1,78 +1,71 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  module Strongbolt
4
-
5
4
  describe Bolted do
6
-
7
5
  #
8
6
  # Bolted?
9
7
  #
10
- describe "bolted?" do
11
-
8
+ describe 'bolted?' do
12
9
  context 'when grant is disabled' do
13
- it "should return false" do
10
+ it 'should return false' do
14
11
  without_grant do
15
12
  expect(Model.bolted?).to eq false
16
13
  end
17
14
  end
18
15
  end # End Grant disabled
19
16
 
20
- context "when no user but enabled" do
17
+ context 'when no user but enabled' do
21
18
  before do
22
19
  expect(Grant::User).to receive(:current_user)
23
20
  end
24
21
 
25
- it "should return false" do
22
+ it 'should return false' do
26
23
  expect(Model.bolted?).to eq false
27
24
  end
28
25
  end
29
26
 
30
- context "when using rails is on console" do
27
+ context 'when using rails is on console' do
31
28
  before do
32
- rails = class_double 'Rails'#, :console => true
29
+ class_double 'Rails' # , :console => true
33
30
  end
34
31
 
35
- it "should return false" do
32
+ it 'should return false' do
36
33
  expect(Model.bolted?).to eq false
37
34
  end
38
35
  end
39
-
40
36
  end
41
37
 
42
- it "should let create a model" do
38
+ it 'should let create a model' do
43
39
  expect do
44
- Model.create! name: "Cool"
40
+ Model.create! name: 'Cool'
45
41
  end.not_to raise_error
46
42
  end
47
-
48
43
  end
49
44
 
50
45
  #
51
46
  # Owned?
52
47
  #
53
48
  describe 'owned?' do
54
-
55
- context "when model is User" do
49
+ context 'when model is User' do
56
50
  let(:user) { User.create! }
57
51
 
58
- it "should be true" do
52
+ it 'should be true' do
59
53
  expect(User).to be_owned
60
54
  end
61
55
 
62
- it "should return the user id" do
56
+ it 'should return the user id' do
63
57
  expect(user.strongbolt_owner_id).to eq user.id
64
58
  end
65
59
 
66
- it "should have the right owner attribute" do
60
+ it 'should have the right owner attribute' do
67
61
  expect(User.owner_attribute).to eq :id
68
62
  end
69
63
  end
70
64
 
71
65
  context 'when model is ownable' do
72
-
73
66
  before do
74
- define_model "OwnedModel" do
75
- self.table_name = "models"
67
+ define_model 'OwnedModel' do
68
+ self.table_name = 'models'
76
69
 
77
70
  belongs_to :user
78
71
  end
@@ -80,42 +73,38 @@ module Strongbolt
80
73
 
81
74
  let(:model) { OwnedModel.create! user: User.create! }
82
75
 
83
- it "should be true" do
76
+ it 'should be true' do
84
77
  expect(OwnedModel).to be_owned
85
78
  end
86
79
 
87
- it "should return the model user id" do
80
+ it 'should return the model user id' do
88
81
  expect(model.strongbolt_owner_id).to eq model.user_id
89
82
  end
90
83
 
91
- it "should have the right owner attribute" do
84
+ it 'should have the right owner attribute' do
92
85
  expect(OwnedModel.owner_attribute).to eq :user_id
93
86
  end
94
-
95
87
  end
96
88
 
97
89
  context 'when model isnt ownable' do
98
-
99
- it "should be true" do
90
+ it 'should be true' do
100
91
  expect(UnownedModel).not_to be_owned
101
92
  end
102
93
 
103
- it "should raise error" do
94
+ it 'should raise error' do
104
95
  expect do
105
96
  UnownedModel.new.strongbolt_owner_id
106
97
  end.to raise_error ModelNotOwned
107
98
  end
108
-
109
99
  end
110
-
111
100
  end
112
101
 
113
102
  #
114
103
  # Name for authorization
115
104
  #
116
105
  describe 'name_for_authorization' do
117
- it "should default to model name" do
118
- expect(Model.send(:name_for_authorization)).to eq "Model"
106
+ it 'should default to model name' do
107
+ expect(Model.send(:name_for_authorization)).to eq 'Model'
119
108
  end
120
109
  end
121
110
 
@@ -123,14 +112,11 @@ module Strongbolt
123
112
  # Authorize as
124
113
  #
125
114
  describe 'authorize_as' do
126
-
127
- before { Model.authorize_as "ParentModel" }
115
+ before { Model.authorize_as 'ParentModel' }
128
116
  after { Model.authorize_as nil }
129
117
 
130
- it "should have changed name for authorization" do
131
- expect(Model.send(:name_for_authorization)).to eq "ParentModel"
118
+ it 'should have changed name for authorization' do
119
+ expect(Model.send(:name_for_authorization)).to eq 'ParentModel'
132
120
  end
133
-
134
121
  end
135
-
136
122
  end
@@ -1,22 +1,21 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  module Strongbolt
4
-
5
4
  describe Capability do
6
-
7
- let(:capability) { Capability.new model: "User", action: "find" }
5
+ let(:capability) { Capability.new model: 'User', action: 'find' }
8
6
 
9
7
  subject { capability }
10
8
 
11
9
  #
12
10
  # Associations
13
11
  #
14
- it { is_expected.to have_many(:capabilities_roles).class_name("Strongbolt::CapabilitiesRole")
15
- .dependent :restrict_with_exception }
12
+ it {
13
+ is_expected.to have_many(:capabilities_roles).class_name('Strongbolt::CapabilitiesRole')
14
+ .dependent :restrict_with_exception
15
+ }
16
16
  it { is_expected.to have_many(:roles).through :capabilities_roles }
17
17
  it { is_expected.to have_many(:users).through :roles }
18
18
 
19
-
20
19
  #
21
20
  # VALIDATIONS
22
21
  #
@@ -28,42 +27,39 @@ module Strongbolt
28
27
 
29
28
  it { is_expected.to validate_uniqueness_of(:action).scoped_to :model, :require_ownership, :require_tenant_access }
30
29
 
31
- it { is_expected.to validate_inclusion_of(:action).in_array %w{find create update destroy} }
30
+ it { is_expected.to validate_inclusion_of(:action).in_array %w[find create update destroy] }
32
31
 
33
- it "should ensure the model exists" do
34
- capability.model = "UserFake"
32
+ it 'should ensure the model exists' do
33
+ capability.model = 'UserFake'
35
34
  expect(capability).not_to be_valid
36
35
  end
37
36
 
38
- context "when there are roles linked to it" do
39
-
37
+ context 'when there are roles linked to it' do
40
38
  before do
41
39
  capability.save
42
40
  capability.roles << Role.create!(name: 'role')
43
41
  end
44
42
 
45
- it "cannot delete" do
43
+ it 'cannot delete' do
46
44
  expect do
47
45
  capability.destroy
48
46
  end.to raise_error ActiveRecord::DeleteRestrictionError
49
47
  end
50
-
51
48
  end
52
49
 
53
-
54
50
  #
55
51
  # Scopes and table
56
52
  #
57
- describe "scope and table" do
53
+ describe 'scope and table' do
58
54
  before(:all) do
59
- define_model "OtherModel"
55
+ define_model 'OtherModel'
60
56
 
61
57
  @capabilities = [
62
- Capability.create!(model: "Model", action: "find"),
63
- Capability.create!(model: "Model", action: "create"),
64
- Capability.create!(model: "OtherModel", action: "find"),
65
- Capability.create!(model: "OtherModel", action: "find", require_ownership: true),
66
- Capability.create!(model: "User", action: "find")
58
+ Capability.create!(model: 'Model', action: 'find'),
59
+ Capability.create!(model: 'Model', action: 'create'),
60
+ Capability.create!(model: 'OtherModel', action: 'find'),
61
+ Capability.create!(model: 'OtherModel', action: 'find', require_ownership: true),
62
+ Capability.create!(model: 'User', action: 'find')
67
63
  ]
68
64
  end
69
65
  after(:all) { Capability.all.delete_all }
@@ -71,18 +67,17 @@ module Strongbolt
71
67
  #
72
68
  # SCOPE ORDERED
73
69
  #
74
- describe "ordered" do
75
- it "should have the scope" do
70
+ describe 'ordered' do
71
+ it 'should have the scope' do
76
72
  expect(Capability).to respond_to :ordered
77
73
  end
78
74
 
79
- describe "results" do
80
-
75
+ describe 'results' do
81
76
  let(:results) { Capability.ordered }
82
77
 
83
78
  subject { results }
84
79
 
85
- it "should have 5 elements" do
80
+ it 'should have 5 elements' do
86
81
  expect(results.size).to eq 5
87
82
  end
88
83
 
@@ -93,159 +88,150 @@ module Strongbolt
93
88
  #
94
89
  # To Table
95
90
  #
96
- describe "to_table" do
97
-
98
- it "should have the to_table" do
91
+ describe 'to_table' do
92
+ it 'should have the to_table' do
99
93
  expect(Capability).to respond_to :to_table
100
94
  end
101
95
 
102
- describe "results" do
96
+ describe 'results' do
103
97
  let(:results) { Capability.to_table }
104
98
 
105
99
  subject { results }
106
100
 
107
- it "should have 4" do
101
+ it 'should have 4' do
108
102
  expect(results.size).to eq 4
109
103
  end
110
104
 
111
- it "should have each one as a hash with the right keys" do
105
+ it 'should have each one as a hash with the right keys' do
112
106
  results.each do |permission|
113
- [:model, :require_ownership, :require_tenant_access,
114
- :find, :create, :update, :destroy].each do |attr|
115
- expect(permission).to include attr
116
- end
107
+ %i[model require_ownership require_tenant_access
108
+ find create update destroy].each do |attr|
109
+ expect(permission).to include attr
110
+ end
117
111
  end
118
112
  end
119
113
  end
120
-
121
114
  end # End to_table
122
115
 
123
116
  #
124
117
  # To Hash
125
118
  #
126
- describe "to_hash" do
127
-
128
- it "should have the to_hash" do
119
+ describe 'to_hash' do
120
+ it 'should have the to_hash' do
129
121
  expect(Capability).to respond_to :to_hash
130
122
  end
131
123
 
132
- describe "results" do
124
+ describe 'results' do
133
125
  let(:results) { Capability.to_hash }
134
126
 
135
127
  subject { results }
136
128
 
137
- it "should have 4" do
129
+ it 'should have 4' do
138
130
  expect(results.size).to eq 4
139
131
  end
140
132
 
141
- it "should have the correct keys" do
133
+ it 'should have the correct keys' do
142
134
  keys = [
143
135
  {
144
- model: "Model",
136
+ model: 'Model',
145
137
  require_ownership: false,
146
138
  require_tenant_access: true
147
139
  },
148
140
  {
149
- model: "OtherModel",
141
+ model: 'OtherModel',
150
142
  require_ownership: false,
151
143
  require_tenant_access: true
152
144
  },
153
145
  {
154
- model: "OtherModel",
146
+ model: 'OtherModel',
155
147
  require_ownership: true,
156
148
  require_tenant_access: true
157
149
  },
158
150
  {
159
- model: "User",
151
+ model: 'User',
160
152
  require_ownership: false,
161
153
  require_tenant_access: true
162
- },
154
+ }
163
155
  ]
164
- results.each do |key, permission|
156
+ results.each do |key, _permission|
165
157
  expect(keys).to include key
166
158
  end
167
159
  end
168
160
 
169
- it "should have each one as a hash with the right keys" do
170
- results.each do |key, permission|
171
- [:find, :create, :update, :destroy].each do |attr|
161
+ it 'should have each one as a hash with the right keys' do
162
+ results.each do |_key, permission|
163
+ %i[find create update destroy].each do |attr|
172
164
  expect(permission).to include attr
173
165
  end
174
166
  end
175
167
  end
176
168
  end
177
-
178
169
  end # End to_hash
179
-
180
170
  end # End Scope and Table
181
171
 
182
-
183
-
184
172
  #
185
173
  # Create capability from hash
186
174
  #
187
- describe "from_hash" do
188
- let(:params) { {model: "User", require_ownership: true, require_tenant_access: false} }
175
+ describe 'from_hash' do
176
+ let(:params) { { model: 'User', require_ownership: true, require_tenant_access: false } }
189
177
 
190
178
  let(:capabilities) { Capability.from_hash params }
191
179
 
192
180
  subject { capabilities }
193
181
 
194
- context "when list of actions" do
195
- before { params[:actions] = [:find, :update] }
182
+ context 'when list of actions' do
183
+ before { params[:actions] = %i[find update] }
196
184
 
197
- it "should have 2 element2" do
198
- expect(subject.size).to eq 2
185
+ it 'should have 2 element2' do
186
+ expect(subject.size).to eq 2
199
187
  end
200
188
 
201
- it "should have the right model" do
189
+ it 'should have the right model' do
202
190
  capabilities.each do |c|
203
- expect(c.model).to eq "User"
191
+ expect(c.model).to eq 'User'
204
192
  end
205
193
  end
206
194
 
207
- it "should have the right require_ownership" do
208
- capabilities.each { |c| expect(c.require_ownership).to eq true }
195
+ it 'should have the right require_ownership' do
196
+ capabilities.each { |c| expect(c.require_ownership).to eq true }
209
197
  end
210
198
 
211
- it "should have the right require_tenant_access" do
212
- capabilities.each { |c| expect(c.require_tenant_access).to eq false }
199
+ it 'should have the right require_tenant_access' do
200
+ capabilities.each { |c| expect(c.require_tenant_access).to eq false }
213
201
  end
214
202
 
215
- it "should have the right actions" do
203
+ it 'should have the right actions' do
216
204
  capabilities.each do |c|
217
- expect(["find", "update"]).to include c.action.to_s
205
+ expect(%w[find update]).to include c.action.to_s
218
206
  end
219
207
  end
220
- end #/list of actions
208
+ end # /list of actions
221
209
 
222
- context "when list of actions" do
223
- before { params[:actions] = "find" }
210
+ context 'when list of actions' do
211
+ before { params[:actions] = 'find' }
224
212
 
225
- it "should have 1 element" do
226
- expect(subject.size).to eq 1
213
+ it 'should have 1 element' do
214
+ expect(subject.size).to eq 1
227
215
  end
228
-
229
- it "should have the right action" do
230
- expect(capabilities[0].action).to eq "find"
216
+
217
+ it 'should have the right action' do
218
+ expect(capabilities[0].action).to eq 'find'
231
219
  end
232
220
  end
233
221
 
234
- context "when :all" do
235
- before { params[:actions] = "all" }
222
+ context 'when :all' do
223
+ before { params[:actions] = 'all' }
236
224
 
237
- it "should have 4 elements" do
225
+ it 'should have 4 elements' do
238
226
  expect(subject.size).to eq 4
239
227
  end
240
228
 
241
- it "should have the right actions" do
229
+ it 'should have the right actions' do
242
230
  capabilities.each do |c|
243
231
  expect(Capability::Actions).to include c.action.to_s
244
232
  end
245
233
  end
246
234
  end
247
235
  end
248
-
249
236
  end
250
-
251
- end
237
+ end