strongbolt 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +33 -0
  3. data/.gitignore +18 -0
  4. data/.rspec +1 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/Gemfile +4 -0
  8. data/Gemfile.lock +130 -0
  9. data/LICENSE.txt +22 -0
  10. data/README.md +182 -0
  11. data/Rakefile +1 -0
  12. data/app/assets/javascripts/strongbolt.js +1 -0
  13. data/app/assets/javascripts/strongbolt/role-capabilities.js +80 -0
  14. data/app/controllers/strongbolt/capabilities_controller.rb +77 -0
  15. data/app/controllers/strongbolt/roles_controller.rb +92 -0
  16. data/app/controllers/strongbolt/security_controller.rb +8 -0
  17. data/app/controllers/strongbolt/user_groups_controller.rb +76 -0
  18. data/app/controllers/strongbolt/user_groups_users_controller.rb +35 -0
  19. data/app/controllers/strongbolt_controller.rb +2 -0
  20. data/app/views/strongbolt/_menu.html.erb +13 -0
  21. data/app/views/strongbolt/capabilities/index.html.erb +53 -0
  22. data/app/views/strongbolt/capabilities/show.html.erb +53 -0
  23. data/app/views/strongbolt/roles/_capabilities.html.erb +47 -0
  24. data/app/views/strongbolt/roles/_capability.html.erb +21 -0
  25. data/app/views/strongbolt/roles/_form.html.erb +12 -0
  26. data/app/views/strongbolt/roles/edit.html.erb +14 -0
  27. data/app/views/strongbolt/roles/index.html.erb +54 -0
  28. data/app/views/strongbolt/roles/new.html.erb +11 -0
  29. data/app/views/strongbolt/roles/show.html.erb +52 -0
  30. data/app/views/strongbolt/user_groups/_form.html.erb +12 -0
  31. data/app/views/strongbolt/user_groups/edit.html.erb +14 -0
  32. data/app/views/strongbolt/user_groups/index.html.erb +46 -0
  33. data/app/views/strongbolt/user_groups/new.html.erb +13 -0
  34. data/app/views/strongbolt/user_groups/show.html.erb +88 -0
  35. data/lib/generators/strongbolt/fix_generator.rb +23 -0
  36. data/lib/generators/strongbolt/indexes_generator.rb +19 -0
  37. data/lib/generators/strongbolt/install_generator.rb +29 -0
  38. data/lib/generators/strongbolt/templates/fix.rb +5 -0
  39. data/lib/generators/strongbolt/templates/indexes.rb +21 -0
  40. data/lib/generators/strongbolt/templates/migration.rb +73 -0
  41. data/lib/generators/strongbolt/templates/strongbolt.rb +45 -0
  42. data/lib/generators/strongbolt/views_generator.rb +26 -0
  43. data/lib/strongbolt.rb +219 -0
  44. data/lib/strongbolt/base.rb +7 -0
  45. data/lib/strongbolt/bolted.rb +125 -0
  46. data/lib/strongbolt/bolted_controller.rb +297 -0
  47. data/lib/strongbolt/capabilities_role.rb +15 -0
  48. data/lib/strongbolt/capability.rb +165 -0
  49. data/lib/strongbolt/configuration.rb +111 -0
  50. data/lib/strongbolt/controllers/url_helpers.rb +37 -0
  51. data/lib/strongbolt/engine.rb +44 -0
  52. data/lib/strongbolt/errors.rb +38 -0
  53. data/lib/strongbolt/generators/migration.rb +35 -0
  54. data/lib/strongbolt/helpers.rb +18 -0
  55. data/lib/strongbolt/rails/routes.rb +20 -0
  56. data/lib/strongbolt/role.rb +46 -0
  57. data/lib/strongbolt/roles_user_group.rb +15 -0
  58. data/lib/strongbolt/rspec.rb +29 -0
  59. data/lib/strongbolt/rspec/user.rb +90 -0
  60. data/lib/strongbolt/tenantable.rb +304 -0
  61. data/lib/strongbolt/user_abilities.rb +292 -0
  62. data/lib/strongbolt/user_group.rb +24 -0
  63. data/lib/strongbolt/user_groups_user.rb +16 -0
  64. data/lib/strongbolt/users_tenant.rb +12 -0
  65. data/lib/strongbolt/version.rb +3 -0
  66. data/lib/tasks/strongbolt_tasks.rake +29 -0
  67. data/spec/controllers/strongbolt/capabilities_controller_spec.rb +254 -0
  68. data/spec/controllers/strongbolt/roles_controller_spec.rb +228 -0
  69. data/spec/controllers/strongbolt/user_groups_controller_spec.rb +216 -0
  70. data/spec/controllers/strongbolt/user_groups_users_controller_spec.rb +69 -0
  71. data/spec/controllers/without_authorization_controller_spec.rb +20 -0
  72. data/spec/dummy/.rspec +2 -0
  73. data/spec/dummy/README.rdoc +28 -0
  74. data/spec/dummy/Rakefile +6 -0
  75. data/spec/dummy/app/assets/images/.keep +0 -0
  76. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  77. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  78. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  79. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  80. data/spec/dummy/app/controllers/posts_controller.rb +18 -0
  81. data/spec/dummy/app/controllers/test_controller.rb +3 -0
  82. data/spec/dummy/app/controllers/without_authorization_controller.rb +5 -0
  83. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  84. data/spec/dummy/app/mailers/.keep +0 -0
  85. data/spec/dummy/app/models/.keep +0 -0
  86. data/spec/dummy/app/models/concerns/.keep +0 -0
  87. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  88. data/spec/dummy/bin/bundle +3 -0
  89. data/spec/dummy/bin/rails +4 -0
  90. data/spec/dummy/bin/rake +4 -0
  91. data/spec/dummy/config.ru +4 -0
  92. data/spec/dummy/config/application.rb +29 -0
  93. data/spec/dummy/config/boot.rb +5 -0
  94. data/spec/dummy/config/database.yml +25 -0
  95. data/spec/dummy/config/environment.rb +5 -0
  96. data/spec/dummy/config/environments/development.rb +37 -0
  97. data/spec/dummy/config/environments/production.rb +78 -0
  98. data/spec/dummy/config/environments/test.rb +39 -0
  99. data/spec/dummy/config/initializers/assets.rb +8 -0
  100. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  101. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  102. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  103. data/spec/dummy/config/initializers/inflections.rb +16 -0
  104. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  105. data/spec/dummy/config/initializers/session_store.rb +3 -0
  106. data/spec/dummy/config/initializers/strongbolt.rb +32 -0
  107. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  108. data/spec/dummy/config/locales/en.yml +23 -0
  109. data/spec/dummy/config/routes.rb +12 -0
  110. data/spec/dummy/config/secrets.yml +22 -0
  111. data/spec/dummy/db/development.sqlite3 +0 -0
  112. data/spec/dummy/db/migrate/20150630212236_create_strongbolt_tables.rb +54 -0
  113. data/spec/dummy/db/migrate/20150630212251_create_strongbolt_tables_indexes.rb +21 -0
  114. data/spec/dummy/db/schema.rb +84 -0
  115. data/spec/dummy/db/test.sqlite3 +0 -0
  116. data/spec/dummy/lib/assets/.keep +0 -0
  117. data/spec/dummy/public/404.html +67 -0
  118. data/spec/dummy/public/422.html +67 -0
  119. data/spec/dummy/public/500.html +66 -0
  120. data/spec/dummy/public/favicon.ico +0 -0
  121. data/spec/fabricators/capability_fabricator.rb +4 -0
  122. data/spec/fabricators/role_fabricator.rb +9 -0
  123. data/spec/fabricators/user_fabricator.rb +3 -0
  124. data/spec/fabricators/user_group_fabricator.rb +9 -0
  125. data/spec/fixtures/application.rb +28 -0
  126. data/spec/fixtures/controllers.rb +5 -0
  127. data/spec/spec_helper.rb +89 -0
  128. data/spec/strongbolt/bolted_controller_spec.rb +706 -0
  129. data/spec/strongbolt/bolted_spec.rb +136 -0
  130. data/spec/strongbolt/capability_spec.rb +251 -0
  131. data/spec/strongbolt/configuration_spec.rb +119 -0
  132. data/spec/strongbolt/controllers/url_helpers_spec.rb +34 -0
  133. data/spec/strongbolt/helpers_spec.rb +43 -0
  134. data/spec/strongbolt/role_spec.rb +90 -0
  135. data/spec/strongbolt/tenantable_spec.rb +281 -0
  136. data/spec/strongbolt/user_abilities_spec.rb +509 -0
  137. data/spec/strongbolt/user_group_spec.rb +37 -0
  138. data/spec/strongbolt/users_tenant_spec.rb +36 -0
  139. data/spec/strongbolt_spec.rb +274 -0
  140. data/spec/support/controller_macros.rb +11 -0
  141. data/spec/support/db_setup.rb +134 -0
  142. data/spec/support/helpers.rb +62 -0
  143. data/spec/support/transactional_specs.rb +17 -0
  144. data/strongbolt.gemspec +32 -0
  145. metadata +407 -0
@@ -0,0 +1,136 @@
1
+ require "spec_helper"
2
+
3
+ module Strongbolt
4
+
5
+ describe Bolted do
6
+
7
+ #
8
+ # Bolted?
9
+ #
10
+ describe "bolted?" do
11
+
12
+ context 'when grant is disabled' do
13
+ it "should return false" do
14
+ without_grant do
15
+ expect(Model.bolted?).to eq false
16
+ end
17
+ end
18
+ end # End Grant disabled
19
+
20
+ context "when no user but enabled" do
21
+ before do
22
+ expect(Grant::User).to receive(:current_user)
23
+ end
24
+
25
+ it "should return false" do
26
+ expect(Model.bolted?).to eq false
27
+ end
28
+ end
29
+
30
+ context "when using rails is on console" do
31
+ before do
32
+ rails = class_double 'Rails'#, :console => true
33
+ end
34
+
35
+ it "should return false" do
36
+ expect(Model.bolted?).to eq false
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ it "should let create a model" do
43
+ expect do
44
+ Model.create! name: "Cool"
45
+ end.not_to raise_error
46
+ end
47
+
48
+ end
49
+
50
+ #
51
+ # Owned?
52
+ #
53
+ describe 'owned?' do
54
+
55
+ context "when model is User" do
56
+ let(:user) { User.create! }
57
+
58
+ it "should be true" do
59
+ expect(User).to be_owned
60
+ end
61
+
62
+ it "should return the user id" do
63
+ expect(user.strongbolt_owner_id).to eq user.id
64
+ end
65
+
66
+ it "should have the right owner attribute" do
67
+ expect(User.owner_attribute).to eq :id
68
+ end
69
+ end
70
+
71
+ context 'when model is ownable' do
72
+
73
+ before do
74
+ define_model "OwnedModel" do
75
+ self.table_name = "models"
76
+
77
+ belongs_to :user
78
+ end
79
+ end
80
+
81
+ let(:model) { OwnedModel.create! user: User.create! }
82
+
83
+ it "should be true" do
84
+ expect(OwnedModel).to be_owned
85
+ end
86
+
87
+ it "should return the model user id" do
88
+ expect(model.strongbolt_owner_id).to eq model.user_id
89
+ end
90
+
91
+ it "should have the right owner attribute" do
92
+ expect(OwnedModel.owner_attribute).to eq :user_id
93
+ end
94
+
95
+ end
96
+
97
+ context 'when model isnt ownable' do
98
+
99
+ it "should be true" do
100
+ expect(UnownedModel).not_to be_owned
101
+ end
102
+
103
+ it "should raise error" do
104
+ expect do
105
+ UnownedModel.new.strongbolt_owner_id
106
+ end.to raise_error ModelNotOwned
107
+ end
108
+
109
+ end
110
+
111
+ end
112
+
113
+ #
114
+ # Name for authorization
115
+ #
116
+ describe 'name_for_authorization' do
117
+ it "should default to model name" do
118
+ expect(Model.name_for_authorization).to eq "Model"
119
+ end
120
+ end
121
+
122
+ #
123
+ # Authorize as
124
+ #
125
+ describe 'authorize_as' do
126
+
127
+ before { Model.authorize_as "ParentModel" }
128
+ after { Model.authorize_as nil }
129
+
130
+ it "should have changed name for authorization" do
131
+ expect(Model.name_for_authorization).to eq "ParentModel"
132
+ end
133
+
134
+ end
135
+
136
+ end
@@ -0,0 +1,251 @@
1
+ require "spec_helper"
2
+
3
+ module Strongbolt
4
+
5
+ describe Capability do
6
+
7
+ let(:capability) { Capability.new model: "User", action: "find" }
8
+
9
+ subject { capability }
10
+
11
+ #
12
+ # Associations
13
+ #
14
+ it { is_expected.to have_many(:capabilities_roles).class_name("Strongbolt::CapabilitiesRole")
15
+ .dependent :restrict_with_exception }
16
+ it { is_expected.to have_many(:roles).through :capabilities_roles }
17
+ it { is_expected.to have_many(:users).through :roles }
18
+
19
+
20
+ #
21
+ # VALIDATIONS
22
+ #
23
+
24
+ it { is_expected.to be_valid }
25
+
26
+ it { is_expected.to validate_presence_of :model }
27
+ it { is_expected.to validate_presence_of :action }
28
+
29
+ it { is_expected.to validate_uniqueness_of(:action).scoped_to :model, :require_ownership, :require_tenant_access }
30
+
31
+ it { is_expected.to validate_inclusion_of(:action).in_array %w{find create update destroy} }
32
+
33
+ it "should ensure the model exists" do
34
+ capability.model = "UserFake"
35
+ expect(capability).not_to be_valid
36
+ end
37
+
38
+ context "when there are roles linked to it" do
39
+
40
+ before do
41
+ capability.save
42
+ capability.roles << Role.create!(name: 'role')
43
+ end
44
+
45
+ it "cannot delete" do
46
+ expect do
47
+ capability.destroy
48
+ end.to raise_error ActiveRecord::DeleteRestrictionError
49
+ end
50
+
51
+ end
52
+
53
+
54
+ #
55
+ # Scopes and table
56
+ #
57
+ describe "scope and table" do
58
+ before(:all) do
59
+ define_model "OtherModel"
60
+
61
+ @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")
67
+ ]
68
+ end
69
+ after(:all) { Capability.all.delete_all }
70
+
71
+ #
72
+ # SCOPE ORDERED
73
+ #
74
+ describe "ordered" do
75
+ it "should have the scope" do
76
+ expect(Capability).to respond_to :ordered
77
+ end
78
+
79
+ describe "results" do
80
+
81
+ let(:results) { Capability.ordered }
82
+
83
+ subject { results }
84
+
85
+ it "should have 5 elements" do
86
+ expect(results.size).to eq 5
87
+ end
88
+
89
+ it { should == @capabilities }
90
+ end
91
+ end
92
+
93
+ #
94
+ # To Table
95
+ #
96
+ describe "to_table" do
97
+
98
+ it "should have the to_table" do
99
+ expect(Capability).to respond_to :to_table
100
+ end
101
+
102
+ describe "results" do
103
+ let(:results) { Capability.to_table }
104
+
105
+ subject { results }
106
+
107
+ it "should have 4" do
108
+ expect(results.size).to eq 4
109
+ end
110
+
111
+ it "should have each one as a hash with the right keys" do
112
+ 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
117
+ end
118
+ end
119
+ end
120
+
121
+ end # End to_table
122
+
123
+ #
124
+ # To Hash
125
+ #
126
+ describe "to_hash" do
127
+
128
+ it "should have the to_hash" do
129
+ expect(Capability).to respond_to :to_hash
130
+ end
131
+
132
+ describe "results" do
133
+ let(:results) { Capability.to_hash }
134
+
135
+ subject { results }
136
+
137
+ it "should have 4" do
138
+ expect(results.size).to eq 4
139
+ end
140
+
141
+ it "should have the correct keys" do
142
+ keys = [
143
+ {
144
+ model: "Model",
145
+ require_ownership: false,
146
+ require_tenant_access: true
147
+ },
148
+ {
149
+ model: "OtherModel",
150
+ require_ownership: false,
151
+ require_tenant_access: true
152
+ },
153
+ {
154
+ model: "OtherModel",
155
+ require_ownership: true,
156
+ require_tenant_access: true
157
+ },
158
+ {
159
+ model: "User",
160
+ require_ownership: false,
161
+ require_tenant_access: true
162
+ },
163
+ ]
164
+ results.each do |key, permission|
165
+ expect(keys).to include key
166
+ end
167
+ end
168
+
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|
172
+ expect(permission).to include attr
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ end # End to_hash
179
+
180
+ end # End Scope and Table
181
+
182
+
183
+
184
+ #
185
+ # Create capability from hash
186
+ #
187
+ describe "from_hash" do
188
+ let(:params) { {model: "User", require_ownership: true, require_tenant_access: false} }
189
+
190
+ let(:capabilities) { Capability.from_hash params }
191
+
192
+ subject { capabilities }
193
+
194
+ context "when list of actions" do
195
+ before { params[:actions] = [:find, :update] }
196
+
197
+ it "should have 2 element2" do
198
+ expect(subject.size).to eq 2
199
+ end
200
+
201
+ it "should have the right model" do
202
+ capabilities.each do |c|
203
+ expect(c.model).to eq "User"
204
+ end
205
+ end
206
+
207
+ it "should have the right require_ownership" do
208
+ capabilities.each { |c| expect(c.require_ownership).to eq true }
209
+ end
210
+
211
+ it "should have the right require_tenant_access" do
212
+ capabilities.each { |c| expect(c.require_tenant_access).to eq false }
213
+ end
214
+
215
+ it "should have the right actions" do
216
+ capabilities.each do |c|
217
+ expect(["find", "update"]).to include c.action.to_s
218
+ end
219
+ end
220
+ end #/list of actions
221
+
222
+ context "when list of actions" do
223
+ before { params[:actions] = "find" }
224
+
225
+ it "should have 1 element" do
226
+ expect(subject.size).to eq 1
227
+ end
228
+
229
+ it "should have the right action" do
230
+ expect(capabilities[0].action).to eq "find"
231
+ end
232
+ end
233
+
234
+ context "when :all" do
235
+ before { params[:actions] = "all" }
236
+
237
+ it "should have 4 elements" do
238
+ expect(subject.size).to eq 4
239
+ end
240
+
241
+ it "should have the right actions" do
242
+ capabilities.each do |c|
243
+ expect(Capability::Actions).to include c.action.to_s
244
+ end
245
+ end
246
+ end
247
+ end
248
+
249
+ end
250
+
251
+ end
@@ -0,0 +1,119 @@
1
+ require "spec_helper"
2
+
3
+ describe Strongbolt::Configuration do
4
+
5
+
6
+ #
7
+ # User class
8
+ #
9
+ describe "user class" do
10
+
11
+ it "should default to User" do
12
+ expect(Strongbolt::Configuration.user_class).to eq "User"
13
+ end
14
+
15
+ context "when setting it" do
16
+ before { Strongbolt::Configuration.user_class = "Account" }
17
+ after { Strongbolt::Configuration.user_class = "User" }
18
+
19
+ it "should give it" do
20
+ expect(Strongbolt::Configuration.user_class).to eq "Account"
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+
27
+
28
+ #
29
+ # Setting up tenants
30
+ #
31
+ describe 'tenants=' do
32
+
33
+ before do
34
+ define_model "Model" do
35
+ self.table_name = "models"
36
+ end
37
+
38
+ define_model "OtherModel" do
39
+ self.table_name = "models"
40
+ end
41
+
42
+ expect(Model).to receive(:send).with :tenant
43
+ expect(OtherModel).to receive(:send).with :tenant
44
+ end
45
+ after { Strongbolt::Configuration.tenants = [] }
46
+
47
+ it "should tenant the models" do
48
+ Strongbolt::Configuration.tenants = "Model", OtherModel, Model
49
+ expect(Strongbolt::Configuration.tenants).to eq [Model, OtherModel]
50
+ end
51
+
52
+ end
53
+
54
+ #
55
+ # Configuring Capability Models
56
+ #
57
+ describe "models=" do
58
+ before do
59
+ Strongbolt::Configuration.models = "OtherModel", "Model"
60
+ end
61
+ after do
62
+ Capability.models = nil
63
+ end
64
+
65
+ it "should set Capability::Models" do
66
+ expect(Capability.models).to eq ["Model", "OtherModel", "Strongbolt::Capability", "Strongbolt::Role", "Strongbolt::UserGroup", "Strongbolt::UsersTenant"]
67
+ end
68
+
69
+ context "when adding other models" do
70
+ before do
71
+ Strongbolt::Configuration.models = "Model", "LastModel"
72
+ end
73
+
74
+ it "should merge with current models" do
75
+ expect(Capability.models).to eq ["LastModel", "Model", "OtherModel", "Strongbolt::Capability", "Strongbolt::Role", "Strongbolt::UserGroup", "Strongbolt::UsersTenant"]
76
+ end
77
+ end
78
+
79
+ context "when adding 1 model" do
80
+ before do
81
+ Strongbolt::Configuration.models = "BottomModel"
82
+ end
83
+
84
+ it "should merge with current models" do
85
+ expect(Capability.models).to eq ["BottomModel", "Model", "OtherModel", "Strongbolt::Capability", "Strongbolt::Role", "Strongbolt::UserGroup", "Strongbolt::UsersTenant"]
86
+ end
87
+ end
88
+ end #/models=
89
+
90
+
91
+
92
+ #
93
+ # Setting default permissions
94
+ #
95
+ describe "default_capabilities=" do
96
+
97
+ before do
98
+ Strongbolt::Configuration.default_capabilities = [
99
+ {:model => "User", :actions => :all},
100
+ {:model => "Model", :actions => "find"}
101
+ ]
102
+ end
103
+ after do
104
+ Strongbolt::Configuration.default_capabilities = []
105
+ end
106
+
107
+ it "should return 5 Capabilities" do
108
+ expect(Strongbolt::Configuration.default_capabilities.size).to eq 5
109
+ end
110
+
111
+ it "should return Capability" do
112
+ Strongbolt::Configuration.default_capabilities.each do |c|
113
+ expect(c).to be_a Capability
114
+ end
115
+ end
116
+
117
+ end
118
+
119
+ end