alberich 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. data/Gemfile +18 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +37 -0
  4. data/Rakefile +34 -0
  5. data/alberich.gemspec +34 -0
  6. data/app/assets/javascripts/alberich/application.js +15 -0
  7. data/app/assets/javascripts/alberich/permissions.js +2 -0
  8. data/app/assets/javascripts/alberich/privileges.js +2 -0
  9. data/app/assets/javascripts/alberich/roles.js +2 -0
  10. data/app/assets/stylesheets/alberich/application.css +13 -0
  11. data/app/assets/stylesheets/alberich/permissions.css +4 -0
  12. data/app/assets/stylesheets/alberich/privileges.css +4 -0
  13. data/app/assets/stylesheets/alberich/roles.css +4 -0
  14. data/app/assets/stylesheets/scaffold.css +56 -0
  15. data/app/controllers/alberich/application_controller.rb +4 -0
  16. data/app/controllers/alberich/application_controller_helper.rb +118 -0
  17. data/app/controllers/alberich/permissions_controller.rb +211 -0
  18. data/app/controllers/alberich/privileges_controller.rb +105 -0
  19. data/app/controllers/alberich/roles_controller.rb +97 -0
  20. data/app/helpers/alberich/application_helper.rb +4 -0
  21. data/app/helpers/alberich/permissions_helper.rb +4 -0
  22. data/app/helpers/alberich/privileges_helper.rb +4 -0
  23. data/app/helpers/alberich/roles_helper.rb +4 -0
  24. data/app/models/alberich/base_permission_object.rb +42 -0
  25. data/app/models/alberich/derived_permission.rb +25 -0
  26. data/app/models/alberich/entity.rb +27 -0
  27. data/app/models/alberich/entity_target_observer.rb +16 -0
  28. data/app/models/alberich/permission.rb +59 -0
  29. data/app/models/alberich/permission_session.rb +33 -0
  30. data/app/models/alberich/permissioned_object.rb +139 -0
  31. data/app/models/alberich/privilege.rb +29 -0
  32. data/app/models/alberich/role.rb +37 -0
  33. data/app/models/alberich/session_entity.rb +15 -0
  34. data/app/views/alberich/permissions/_form.html.haml +27 -0
  35. data/app/views/alberich/permissions/_list.html.haml +1 -0
  36. data/app/views/alberich/permissions/_objects.html.haml +38 -0
  37. data/app/views/alberich/permissions/_permissions.html.haml +45 -0
  38. data/app/views/alberich/permissions/index.html.haml +2 -0
  39. data/app/views/alberich/permissions/new.html.haml +5 -0
  40. data/app/views/alberich/permissions/show.html.haml +12 -0
  41. data/app/views/alberich/privileges/_form.html.haml +19 -0
  42. data/app/views/alberich/privileges/_list.html.haml +17 -0
  43. data/app/views/alberich/privileges/create.html.haml +2 -0
  44. data/app/views/alberich/privileges/destroy.html.haml +2 -0
  45. data/app/views/alberich/privileges/edit.html.haml +5 -0
  46. data/app/views/alberich/privileges/index.html.haml +5 -0
  47. data/app/views/alberich/privileges/new.html.haml +5 -0
  48. data/app/views/alberich/privileges/show.html.haml +12 -0
  49. data/app/views/alberich/privileges/update.html.haml +2 -0
  50. data/app/views/alberich/roles/_form.html.haml +24 -0
  51. data/app/views/alberich/roles/edit.html.haml +7 -0
  52. data/app/views/alberich/roles/index.html.haml +23 -0
  53. data/app/views/alberich/roles/new.html.haml +5 -0
  54. data/app/views/alberich/roles/show.html.haml +16 -0
  55. data/app/views/layouts/alberich/application.html.erb +14 -0
  56. data/config/initializers/haml.rb +1 -0
  57. data/config/routes.rb +17 -0
  58. data/db/migrate/20120925162242_create_alberich_roles.rb +12 -0
  59. data/db/migrate/20121022223626_create_alberich_privileges.rb +12 -0
  60. data/db/migrate/20121023051301_create_alberich_base_permission_objects.rb +9 -0
  61. data/db/migrate/20121023233648_create_alberich_permission_sessions.rb +11 -0
  62. data/db/migrate/20121027023136_create_alberich_entities.rb +34 -0
  63. data/db/migrate/20121204205213_create_alberich_session_entities.rb +12 -0
  64. data/db/migrate/20121205180518_create_alberich_permissions.rb +13 -0
  65. data/db/migrate/20130107043252_create_alberich_derived_permissions.rb +18 -0
  66. data/lib/alberich.rb +10 -0
  67. data/lib/alberich/#version.rb# +3 -0
  68. data/lib/alberich/engine.rb +10 -0
  69. data/lib/alberich/version.rb +3 -0
  70. data/lib/generators/alberich/install_generator.rb +15 -0
  71. data/lib/generators/alberich/templates/README +6 -0
  72. data/lib/generators/alberich/templates/alberich.rb +11 -0
  73. data/lib/tasks/alberich_tasks.rake +4 -0
  74. data/spec/controllers/alberich/permissions_controller_spec.rb +112 -0
  75. data/spec/controllers/alberich/privileges_controller_spec.rb +131 -0
  76. data/spec/controllers/alberich/roles_controller_spec.rb +130 -0
  77. data/spec/factories/alberich/permission.rb +51 -0
  78. data/spec/factories/alberich/permission_session.rb +7 -0
  79. data/spec/factories/alberich/privilege.rb +6 -0
  80. data/spec/factories/alberich/role.rb +103 -0
  81. data/spec/factories/child_resource.rb +14 -0
  82. data/spec/factories/child_resource.rb~ +7 -0
  83. data/spec/factories/global_resource.rb +11 -0
  84. data/spec/factories/global_resource.rb~ +25 -0
  85. data/spec/factories/parent_resource.rb +12 -0
  86. data/spec/factories/parent_resource.rb~ +7 -0
  87. data/spec/factories/standalone_resource.rb +7 -0
  88. data/spec/factories/standalone_resource.rb~ +11 -0
  89. data/spec/factories/user.rb +30 -0
  90. data/spec/factories/user_group.rb +8 -0
  91. data/spec/models/alberich/derived_permission_spec.rb +34 -0
  92. data/spec/models/alberich/entity_spec.rb +15 -0
  93. data/spec/models/alberich/permission_spec.rb +133 -0
  94. data/spec/models/alberich/privilege_spec.rb +39 -0
  95. data/spec/models/alberich/role_spec.rb +33 -0
  96. data/spec/models/alberich/session_entity_spec.rb +24 -0
  97. data/spec/spec_helper.rb +81 -0
  98. data/spec/support/routes.rb +41 -0
  99. data/test/dummy/README.rdoc +261 -0
  100. data/test/dummy/Rakefile +7 -0
  101. data/test/dummy/app/assets/javascripts/application.js +15 -0
  102. data/test/dummy/app/assets/javascripts/child_resources.js +2 -0
  103. data/test/dummy/app/assets/javascripts/global_resources.js +2 -0
  104. data/test/dummy/app/assets/javascripts/parent_resources.js +2 -0
  105. data/test/dummy/app/assets/javascripts/standalone_resources.js +2 -0
  106. data/test/dummy/app/assets/javascripts/user_groups.js +2 -0
  107. data/test/dummy/app/assets/javascripts/users.js +2 -0
  108. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  109. data/test/dummy/app/assets/stylesheets/child_resources.css +4 -0
  110. data/test/dummy/app/assets/stylesheets/global_resources.css +4 -0
  111. data/test/dummy/app/assets/stylesheets/parent_resources.css +4 -0
  112. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  113. data/test/dummy/app/assets/stylesheets/standalone_resources.css +4 -0
  114. data/test/dummy/app/assets/stylesheets/user_groups.css +4 -0
  115. data/test/dummy/app/assets/stylesheets/users.css +4 -0
  116. data/test/dummy/app/controllers/application_controller.rb +73 -0
  117. data/test/dummy/app/controllers/child_resources_controller.rb +99 -0
  118. data/test/dummy/app/controllers/child_resources_controller.rb~ +83 -0
  119. data/test/dummy/app/controllers/global_resources_controller.rb +95 -0
  120. data/test/dummy/app/controllers/global_resources_controller.rb~ +83 -0
  121. data/test/dummy/app/controllers/parent_resources_controller.rb +101 -0
  122. data/test/dummy/app/controllers/parent_resources_controller.rb~ +83 -0
  123. data/test/dummy/app/controllers/standalone_resources_controller.rb +101 -0
  124. data/test/dummy/app/controllers/standalone_resources_controller.rb~ +83 -0
  125. data/test/dummy/app/controllers/user_groups_controller.rb +131 -0
  126. data/test/dummy/app/controllers/user_sessions_controller.rb +38 -0
  127. data/test/dummy/app/controllers/users_controller.rb +87 -0
  128. data/test/dummy/app/helpers/application_helper.rb +2 -0
  129. data/test/dummy/app/helpers/child_resources_helper.rb +2 -0
  130. data/test/dummy/app/helpers/global_resources_helper.rb +2 -0
  131. data/test/dummy/app/helpers/parent_resources_helper.rb +2 -0
  132. data/test/dummy/app/helpers/standalone_resources_helper.rb +2 -0
  133. data/test/dummy/app/helpers/user_groups_helper.rb +2 -0
  134. data/test/dummy/app/helpers/users_helper.rb +2 -0
  135. data/test/dummy/app/models/child_resource.rb +25 -0
  136. data/test/dummy/app/models/child_resource.rb~ +4 -0
  137. data/test/dummy/app/models/global_resource.rb +3 -0
  138. data/test/dummy/app/models/parent_resource.rb +32 -0
  139. data/test/dummy/app/models/parent_resource.rb~ +3 -0
  140. data/test/dummy/app/models/standalone_resource.rb +22 -0
  141. data/test/dummy/app/models/standalone_resource.rb~ +3 -0
  142. data/test/dummy/app/models/user.rb +80 -0
  143. data/test/dummy/app/models/user_group.rb +12 -0
  144. data/test/dummy/app/views/child_resources/_form.html.erb +30 -0
  145. data/test/dummy/app/views/child_resources/_form.html.erb~ +29 -0
  146. data/test/dummy/app/views/child_resources/edit.html.erb +6 -0
  147. data/test/dummy/app/views/child_resources/index.html.erb +25 -0
  148. data/test/dummy/app/views/child_resources/index.html.erb~ +27 -0
  149. data/test/dummy/app/views/child_resources/new.html.erb +5 -0
  150. data/test/dummy/app/views/child_resources/show.html.erb +20 -0
  151. data/test/dummy/app/views/child_resources/show.html.erb~ +20 -0
  152. data/test/dummy/app/views/global_resources/_form.html.erb +25 -0
  153. data/test/dummy/app/views/global_resources/edit.html.erb +6 -0
  154. data/test/dummy/app/views/global_resources/index.html.erb +25 -0
  155. data/test/dummy/app/views/global_resources/new.html.erb +5 -0
  156. data/test/dummy/app/views/global_resources/show.html.erb +15 -0
  157. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  158. data/test/dummy/app/views/parent_resources/_form.html.erb +25 -0
  159. data/test/dummy/app/views/parent_resources/edit.html.erb +6 -0
  160. data/test/dummy/app/views/parent_resources/index.html.erb +25 -0
  161. data/test/dummy/app/views/parent_resources/index.html.erb~ +25 -0
  162. data/test/dummy/app/views/parent_resources/new.html.erb +5 -0
  163. data/test/dummy/app/views/parent_resources/show.html.erb +44 -0
  164. data/test/dummy/app/views/parent_resources/show.html.erb~ +15 -0
  165. data/test/dummy/app/views/standalone_resources/_form.html.erb +25 -0
  166. data/test/dummy/app/views/standalone_resources/edit.html.erb +6 -0
  167. data/test/dummy/app/views/standalone_resources/index.html.erb +25 -0
  168. data/test/dummy/app/views/standalone_resources/new.html.erb +5 -0
  169. data/test/dummy/app/views/standalone_resources/show.html.erb +15 -0
  170. data/test/dummy/app/views/user_groups/_form.html.haml +9 -0
  171. data/test/dummy/app/views/user_groups/add_members.html.haml +18 -0
  172. data/test/dummy/app/views/user_groups/edit.html.haml +13 -0
  173. data/test/dummy/app/views/user_groups/index.html.haml +20 -0
  174. data/test/dummy/app/views/user_groups/new.html.haml +11 -0
  175. data/test/dummy/app/views/user_groups/show.html.haml +42 -0
  176. data/test/dummy/app/views/user_sessions/new.html.haml +26 -0
  177. data/test/dummy/app/views/users/_form.html.haml +25 -0
  178. data/test/dummy/app/views/users/edit.html.haml +14 -0
  179. data/test/dummy/app/views/users/index.html.haml +26 -0
  180. data/test/dummy/app/views/users/new.html.haml +11 -0
  181. data/test/dummy/app/views/users/show.html.haml +56 -0
  182. data/test/dummy/config.ru +4 -0
  183. data/test/dummy/config/application.rb +59 -0
  184. data/test/dummy/config/boot.rb +10 -0
  185. data/test/dummy/config/database.yml +25 -0
  186. data/test/dummy/config/environment.rb +5 -0
  187. data/test/dummy/config/environments/development.rb +37 -0
  188. data/test/dummy/config/environments/production.rb +67 -0
  189. data/test/dummy/config/environments/test.rb +37 -0
  190. data/test/dummy/config/initializers/alberich.rb +13 -0
  191. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  192. data/test/dummy/config/initializers/inflections.rb +15 -0
  193. data/test/dummy/config/initializers/mime_types.rb +5 -0
  194. data/test/dummy/config/initializers/secret_token.rb +7 -0
  195. data/test/dummy/config/initializers/session_store.rb +8 -0
  196. data/test/dummy/config/initializers/warden.rb +79 -0
  197. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  198. data/test/dummy/config/locales/en.yml +5 -0
  199. data/test/dummy/config/routes.rb +33 -0
  200. data/test/dummy/db/migrate/20120801010101_create_users.rb +20 -0
  201. data/test/dummy/db/migrate/20121121054319_create_user_groups.rb +16 -0
  202. data/test/dummy/db/migrate/20130220160811_create_global_resources.rb +10 -0
  203. data/test/dummy/db/migrate/20130220175258_create_standalone_resources.rb +10 -0
  204. data/test/dummy/db/migrate/20130226145412_create_parent_resources.rb +10 -0
  205. data/test/dummy/db/migrate/20130226151256_create_child_resources.rb +12 -0
  206. data/test/dummy/db/migrate/20130226151256_create_child_resources.rb~ +12 -0
  207. data/test/dummy/db/schema.rb +151 -0
  208. data/test/dummy/db/seeds.rb +65 -0
  209. data/test/dummy/lib/password.rb +58 -0
  210. data/test/dummy/public/404.html +26 -0
  211. data/test/dummy/public/422.html +26 -0
  212. data/test/dummy/public/500.html +25 -0
  213. data/test/dummy/public/favicon.ico +0 -0
  214. data/test/dummy/script/rails +6 -0
  215. data/test/dummy/test/fixtures/child_resources.yml +11 -0
  216. data/test/dummy/test/fixtures/global_resources.yml +9 -0
  217. data/test/dummy/test/fixtures/parent_resources.yml +9 -0
  218. data/test/dummy/test/fixtures/standalone_resources.yml +9 -0
  219. data/test/dummy/test/fixtures/user_groups.yml +9 -0
  220. data/test/dummy/test/fixtures/users.yml +37 -0
  221. data/test/dummy/test/functional/child_resources_controller_test.rb +49 -0
  222. data/test/dummy/test/functional/global_resources_controller_test.rb +49 -0
  223. data/test/dummy/test/functional/parent_resources_controller_test.rb +49 -0
  224. data/test/dummy/test/functional/standalone_resources_controller_test.rb +49 -0
  225. data/test/dummy/test/functional/user_groups_controller_test.rb +49 -0
  226. data/test/dummy/test/functional/users_controller_test.rb +39 -0
  227. data/test/dummy/test/unit/child_resource_test.rb +7 -0
  228. data/test/dummy/test/unit/global_resource_test.rb +7 -0
  229. data/test/dummy/test/unit/helpers/child_resources_helper_test.rb +4 -0
  230. data/test/dummy/test/unit/helpers/global_resources_helper_test.rb +4 -0
  231. data/test/dummy/test/unit/helpers/parent_resources_helper_test.rb +4 -0
  232. data/test/dummy/test/unit/helpers/standalone_resources_helper_test.rb +4 -0
  233. data/test/dummy/test/unit/helpers/user_groups_helper_test.rb +4 -0
  234. data/test/dummy/test/unit/helpers/users_helper_test.rb +4 -0
  235. data/test/dummy/test/unit/parent_resource_test.rb +7 -0
  236. data/test/dummy/test/unit/standalone_resource_test.rb +7 -0
  237. data/test/dummy/test/unit/user_group_test.rb +7 -0
  238. data/test/dummy/test/unit/user_test.rb +7 -0
  239. data/test/integration/alberich/permission_test.rb +7 -0
  240. data/test/integration/alberich/privilege_test.rb +7 -0
  241. data/test/integration/alberich/role_test.rb +7 -0
  242. metadata +639 -0
@@ -0,0 +1,12 @@
1
+ class CreateAlberichPrivileges < ActiveRecord::Migration
2
+ def change
3
+ create_table :alberich_privileges do |t|
4
+ t.integer :role_id, :null => false
5
+ t.string :target_type, :null => false
6
+ t.string :action, :null => false
7
+
8
+ t.integer :lock_version, :default => 0
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ class CreateAlberichBasePermissionObjects < ActiveRecord::Migration
2
+ def change
3
+ create_table :alberich_base_permission_objects do |t|
4
+ t.string :name, :null => false
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ class CreateAlberichPermissionSessions < ActiveRecord::Migration
2
+ def change
3
+ create_table :alberich_permission_sessions do |t|
4
+ t.integer :user_id, :null => false
5
+ t.string :session_id, :null => false
6
+ t.integer :lock_version, :default => 0
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,34 @@
1
+ class CreateAlberichEntities < ActiveRecord::Migration
2
+ class Alberich::Entity < ActiveRecord::Base; end
3
+
4
+ def up
5
+ create_table :alberich_entities do |t|
6
+ t.string :name
7
+ t.references :entity_target, :polymorphic => true, :null => false
8
+
9
+ t.integer :lock_version, :default => 0
10
+ t.timestamps
11
+ end
12
+ if Alberich.user_class.constantize.table_exists?
13
+ Alberich.user_class.constantize.all.each do |u|
14
+ unless u.entity
15
+ entity = Entity.new(:entity_target => u)
16
+ entity.name = u.to_s
17
+ entity.save!
18
+ end
19
+ end
20
+ end
21
+ if Alberich.user_group_class.constantize.table_exists?
22
+ Alberich.user_group_class.constantize.all.each do |ug|
23
+ unless ug.entity
24
+ entity = Entity.new(:entity_target => ug)
25
+ entity.name = ug.to_s
26
+ entity.save!
27
+ end
28
+ end
29
+ end
30
+ end
31
+ def down
32
+ drop_table :alberich_entities
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ class CreateAlberichSessionEntities < ActiveRecord::Migration
2
+ def change
3
+ create_table :alberich_session_entities do |t|
4
+ t.integer :user_id, :null => false
5
+ t.integer :entity_id, :null => false
6
+ t.integer :permission_session_id, :null => false
7
+ t.integer :lock_version, :default => 0
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ class CreateAlberichPermissions < ActiveRecord::Migration
2
+ def change
3
+ create_table :alberich_permissions do |t|
4
+ t.integer :role_id, :null => false
5
+ t.integer :entity_id, :null => false
6
+ t.integer :permission_object_id
7
+ t.string :permission_object_type
8
+ t.integer :lock_version, :default => 0
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ class CreateAlberichDerivedPermissions < ActiveRecord::Migration
2
+ def change
3
+ create_table :alberich_derived_permissions do |t|
4
+ t.integer :permission_id, :null => false
5
+ t.integer :role_id, :null => false
6
+ t.integer :entity_id, :null => false
7
+ t.integer :permission_object_id
8
+ t.string :permission_object_type
9
+ t.integer :lock_version, :default => 0
10
+
11
+ t.timestamps
12
+ end
13
+ add_index :alberich_derived_permissions, :permission_id
14
+ add_index :alberich_derived_permissions,
15
+ [:permission_object_id, :permission_object_type],
16
+ :name => 'index_alberich_derived_permissions_on_perm_obj'
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ require "alberich/engine"
2
+
3
+ module Alberich
4
+ mattr_accessor :user_class
5
+ mattr_accessor :groups_for_user_method
6
+ mattr_accessor :user_group_class
7
+ mattr_accessor :permissioned_object_classes
8
+ mattr_accessor :additional_privilege_scopes
9
+ mattr_accessor :require_user_method
10
+ end
@@ -0,0 +1,3 @@
1
+ module Alberich
2
+ VERSION = "0.2a.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ module Alberich
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Alberich
4
+ config.generators do |g|
5
+ g.test_framework :rspec, :view_specs => false
6
+ g.template_engine :haml
7
+ end
8
+ config.active_record.observers = "alberich::Entity_target_observer"
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Alberich
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/generators'
2
+
3
+ module Alberich
4
+ class InstallGenerator < Rails::Generators::Base
5
+ desc "Copy Alberich default files"
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def copy_config
9
+ copy_file "alberich.rb", "config/initializers/alberich.rb"
10
+ end
11
+ def show_readme
12
+ readme 'README' if behavior == :invoke
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ See the newly installed alberich.rb in your initializers directory for
2
+ default settings.
3
+
4
+ As an example. you can choose different classes for Alberich to hook
5
+ into for user and user group, just specify those
6
+ classes here.
@@ -0,0 +1,11 @@
1
+ Alberich.user_class = "User"
2
+ Alberich.groups_for_user_method = "all_groups"
3
+ Alberich.user_group_class = "UserGroup"
4
+ Alberich.permissioned_object_classes = []
5
+ Alberich.additional_privilege_scopes = []
6
+
7
+ Alberich.require_user_method = "require_user"
8
+
9
+ ActiveSupport.on_load(:action_controller) do
10
+ ActionController::Base.send(:include, Alberich::ApplicationControllerHelper)
11
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :alberich do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ module Alberich
4
+ describe PermissionsController do
5
+
6
+ before(:each) do
7
+ @admin_permission = FactoryGirl.create(:admin_permission)
8
+ @admin = @admin_permission.user
9
+ mock_warden(@admin)
10
+
11
+ @permission = FactoryGirl.create(:global_permission)
12
+ end
13
+
14
+ describe "GET index" do
15
+ it "assigns all permissions as @permissions" do
16
+ get :index, {}
17
+ assigns(:permissions).should include(@permission)
18
+ end
19
+ end
20
+
21
+ describe "POST create" do
22
+ describe "with valid params" do
23
+ it "creates a new Permission" do
24
+ expect {
25
+ post :create, {:entity_role_selected =>
26
+ [[Entity.for_target(FactoryGirl.create(:user)).id,
27
+ FactoryGirl.create(:role).id].join(",")]}
28
+ }.to change(Permission, :count).by(1)
29
+ end
30
+
31
+ it "redirects to index for global permission" do
32
+ post :create, {:entity_role_selected =>
33
+ [[Entity.for_target(FactoryGirl.create(:user)).id,
34
+ FactoryGirl.create(:role).id].join(",")]}
35
+ response.should redirect_to(permissions_path(:return_from_permission_change => true))
36
+ end
37
+ end
38
+
39
+ describe "with invalid params" do
40
+ it "shows proper error message" do
41
+ # Trigger the behavior that occurs when invalid params are submitted
42
+ Permission.any_instance.stub(:save).and_return(false)
43
+ post :create, {:entity_role_selected => []}
44
+ flash[:error].should eq("No users or groups selected")
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "PUT update" do
50
+ describe "with valid params" do
51
+ it "updates the requested permission" do
52
+ # Assuming there are no other permissions in the database, this
53
+ # specifies that the Permission created on the previous line
54
+ # receives the :update_attributes message with whatever params are
55
+ # submitted in the request.
56
+ old_role = @permission.role
57
+ new_role = FactoryGirl.create(:role)
58
+ @permission.role.should eq(old_role)
59
+ put :multi_update, {:permission_role_selected =>
60
+ [[@permission.id,
61
+ new_role.id].join(",")]}
62
+ @permission.reload
63
+ @permission.role.should eq(new_role)
64
+ end
65
+
66
+ it "redirects to the index for global permissions" do
67
+ put :multi_update, {:permission_role_selected =>
68
+ [[@permission.id,
69
+ FactoryGirl.create(:role).id].join(",")]}
70
+ response.should redirect_to(permissions_path(:return_from_permission_change => true))
71
+ end
72
+
73
+ end
74
+
75
+ describe "with invalid params" do
76
+ it "assigns the permission as @permission" do
77
+ # Trigger the behavior that occurs when invalid params are submitted
78
+ permission2 = FactoryGirl.create(:global_permission, :entity => @permission.entity)
79
+ permission2.entity.should eq(@permission.entity)
80
+ permission2.permission_object.should eq(@permission.permission_object)
81
+ Permission.any_instance.stub(:save).and_return(false)
82
+ put :multi_update, {:permission_role_selected =>
83
+ ["#{@permission.id},#{permission2.role.id}"]}
84
+ flash[:error].should include("Could not add")
85
+ end
86
+
87
+ it "returns to index for global permission" do
88
+ permission2 = FactoryGirl.create(:global_permission, :entity => @permission.entity)
89
+ permission2.entity.should eq(@permission.entity)
90
+ permission2.permission_object.should eq(@permission.permission_object)
91
+ Permission.any_instance.stub(:save).and_return(false)
92
+ put :multi_update, {:permission_role_selected =>
93
+ ["#{@permission.id},#{permission2.role.id}"]}
94
+ response.should redirect_to(permissions_path(:return_from_permission_change => true))
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "DELETE destroy" do
100
+ it "destroys the requested permission" do
101
+ expect {
102
+ delete :multi_destroy, {:permission_selected => [@permission.to_param]}
103
+ }.to change(Permission, :count).by(-1)
104
+ end
105
+
106
+ it "redirects to the permissions list" do
107
+ delete :multi_destroy, {:permission_selected => [@permission.to_param]}
108
+ response.should redirect_to(permissions_path(:return_from_permission_change => true))
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ module Alberich
4
+ describe PrivilegesController do
5
+
6
+ before(:each) do
7
+ @role = FactoryGirl.create(:role)
8
+ @privilege = FactoryGirl.create(:privilege, :role_id => @role.id)
9
+ @admin_permission = FactoryGirl.create(:admin_permission)
10
+ @admin = @admin_permission.user
11
+ mock_warden(@admin)
12
+ end
13
+
14
+ describe "GET index" do
15
+ it "assigns all privileges as @privileges" do
16
+ get :index, {}
17
+ assigns(:privileges).should include(@privilege)
18
+ end
19
+ end
20
+
21
+ describe "GET show" do
22
+ it "assigns the requested privilege as @privilege" do
23
+ get :show, {:id => @privilege.to_param}
24
+ assigns(:privilege).should eq(@privilege)
25
+ end
26
+ end
27
+
28
+ describe "GET new" do
29
+ it "assigns a new privilege as @privilege" do
30
+ get :new, {}
31
+ assigns(:privilege).should be_a_new(Privilege)
32
+ end
33
+ end
34
+
35
+ describe "GET edit" do
36
+ it "assigns the requested privilege as @privilege" do
37
+ get :edit, {:id => @privilege.to_param}
38
+ assigns(:privilege).should eq(@privilege)
39
+ end
40
+ end
41
+
42
+ describe "POST create" do
43
+ describe "with valid params" do
44
+ it "creates a new Privilege" do
45
+ expect {
46
+ post :create, {:privilege => FactoryGirl.attributes_for(:privilege, :role_id => @role.id, :action => "modify").symbolize_keys}
47
+ }.to change(Privilege, :count).by(1)
48
+ end
49
+
50
+ it "assigns a newly created privilege as @privilege" do
51
+ post :create, {:privilege => FactoryGirl.attributes_for(:privilege, :role_id => @role.id, :action => "modify").symbolize_keys}
52
+ assigns(:privilege).should be_a(Privilege)
53
+ assigns(:privilege).should be_persisted
54
+ end
55
+
56
+ it "redirects to the created privilege" do
57
+ post :create, {:privilege => FactoryGirl.attributes_for(:privilege, :role_id => @role.id, :action => "modify").symbolize_keys}
58
+ response.should redirect_to(@role)
59
+ end
60
+ end
61
+
62
+ describe "with invalid params" do
63
+ it "assigns a newly created but unsaved privilege as @privilege" do
64
+ # Trigger the behavior that occurs when invalid params are submitted
65
+ Privilege.any_instance.stub(:save).and_return(false)
66
+ post :create, {:privilege => {}}
67
+ assigns(:privilege).should be_a_new(Privilege)
68
+ end
69
+
70
+ it "re-renders the 'new' template" do
71
+ # Trigger the behavior that occurs when invalid params are submitted
72
+ Privilege.any_instance.stub(:save).and_return(false)
73
+ post :create, {:privilege => {}}
74
+ response.should render_template("new")
75
+ end
76
+ end
77
+ end
78
+
79
+ describe "PUT update" do
80
+ describe "with valid params" do
81
+ it "updates the requested privilege" do
82
+ # Assuming there are no other privileges in the database, this
83
+ # specifies that the Privilege created on the previous line
84
+ # receives the :update_attributes message with whatever params are
85
+ # submitted in the request.
86
+ Privilege.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
87
+ put :update, {:id => @privilege.to_param, :privilege => {'these' => 'params'}}
88
+ end
89
+
90
+ it "assigns the requested privilege as @privilege" do
91
+ put :update, {:id => @privilege.to_param, :privilege => FactoryGirl.attributes_for(:privilege, :role_id => @role.id, :action => "modify").symbolize_keys}
92
+ assigns(:privilege).should eq(@privilege)
93
+ end
94
+
95
+ it "redirects to the privilege" do
96
+ put :update, {:id => @privilege.to_param, :privilege => FactoryGirl.attributes_for(:privilege, :role_id => @role.id, :action => "modify").symbolize_keys}
97
+ response.should redirect_to(@role)
98
+ end
99
+ end
100
+
101
+ describe "with invalid params" do
102
+ it "assigns the privilege as @privilege" do
103
+ # Trigger the behavior that occurs when invalid params are submitted
104
+ Privilege.any_instance.stub(:save).and_return(false)
105
+ put :update, {:id => @privilege.to_param, :privilege => {}}
106
+ assigns(:privilege).should eq(@privilege)
107
+ end
108
+
109
+ it "re-renders the 'edit' template" do
110
+ # Trigger the behavior that occurs when invalid params are submitted
111
+ Privilege.any_instance.stub(:save).and_return(false)
112
+ put :update, {:id => @privilege.to_param, :privilege => {}}
113
+ response.should render_template("edit")
114
+ end
115
+ end
116
+ end
117
+
118
+ describe "DELETE destroy" do
119
+ it "destroys the requested privilege" do
120
+ expect {
121
+ delete :destroy, {:id => @privilege.to_param}
122
+ }.to change(Privilege, :count).by(-1)
123
+ end
124
+
125
+ it "redirects to the privileges list" do
126
+ delete :destroy, {:id => @privilege.to_param}
127
+ response.should redirect_to(@role)
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ module Alberich
4
+ describe RolesController do
5
+
6
+ before(:each) do
7
+ @role = FactoryGirl.create(:role)
8
+ @admin_permission = FactoryGirl.create(:admin_permission)
9
+ @admin = @admin_permission.user
10
+ mock_warden(@admin)
11
+ end
12
+
13
+ describe "GET index" do
14
+ it "assigns all roles as @roles" do
15
+ get :index, {}
16
+ assigns(:roles).should include(@role)
17
+ end
18
+ end
19
+
20
+ describe "GET show" do
21
+ it "assigns the requested role as @role" do
22
+ get :show, {:id => @role.to_param}
23
+ assigns(:role).should eq(@role)
24
+ end
25
+ end
26
+
27
+ describe "GET new" do
28
+ it "assigns a new role as @role" do
29
+ get :new, {}
30
+ assigns(:role).should be_a_new(Role)
31
+ end
32
+ end
33
+
34
+ describe "GET edit" do
35
+ it "assigns the requested role as @role" do
36
+ get :edit, {:id => @role.to_param}
37
+ assigns(:role).should eq(@role)
38
+ end
39
+ end
40
+
41
+ describe "POST create" do
42
+ describe "with valid params" do
43
+ it "creates a new Role" do
44
+ expect {
45
+ post :create, {:role => FactoryGirl.attributes_for(:role).symbolize_keys}
46
+ }.to change(Role, :count).by(1)
47
+ end
48
+
49
+ it "assigns a newly created role as @role" do
50
+ post :create, {:role => FactoryGirl.attributes_for(:role).symbolize_keys}
51
+ assigns(:role).should be_a(Role)
52
+ assigns(:role).should be_persisted
53
+ end
54
+
55
+ it "redirects to the created role" do
56
+ post :create, {:role => FactoryGirl.attributes_for(:role).symbolize_keys}
57
+ response.should redirect_to(Role.last)
58
+ end
59
+ end
60
+
61
+ describe "with invalid params" do
62
+ it "assigns a newly created but unsaved role as @role" do
63
+ # Trigger the behavior that occurs when invalid params are submitted
64
+ Role.any_instance.stub(:save).and_return(false)
65
+ post :create, {:role => {}}
66
+ assigns(:role).should be_a_new(Role)
67
+ end
68
+
69
+ it "re-renders the 'new' template" do
70
+ # Trigger the behavior that occurs when invalid params are submitted
71
+ Role.any_instance.stub(:save).and_return(false)
72
+ post :create, {:role => {}}
73
+ response.should render_template("new")
74
+ end
75
+ end
76
+ end
77
+
78
+ describe "PUT update" do
79
+ describe "with valid params" do
80
+ it "updates the requested role" do
81
+ # Assuming there are no other roles in the database, this
82
+ # specifies that the Role created on the previous line
83
+ # receives the :update_attributes message with whatever params are
84
+ # submitted in the request.
85
+ Role.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
86
+ put :update, {:id => @role.to_param, :role => {'these' => 'params'}}
87
+ end
88
+
89
+ it "assigns the requested role as @role" do
90
+ put :update, {:id => @role.to_param, :role => FactoryGirl.attributes_for(:role).symbolize_keys}
91
+ assigns(:role).should eq(@role)
92
+ end
93
+
94
+ it "redirects to the role" do
95
+ put :update, {:id => @role.to_param, :role => FactoryGirl.attributes_for(:role).symbolize_keys}
96
+ response.should redirect_to(@role)
97
+ end
98
+ end
99
+
100
+ describe "with invalid params" do
101
+ it "assigns the role as @role" do
102
+ # Trigger the behavior that occurs when invalid params are submitted
103
+ Role.any_instance.stub(:save).and_return(false)
104
+ put :update, {:id => @role.to_param, :role => {}}
105
+ assigns(:role).should eq(@role)
106
+ end
107
+
108
+ it "re-renders the 'edit' template" do
109
+ # Trigger the behavior that occurs when invalid params are submitted
110
+ Role.any_instance.stub(:save).and_return(false)
111
+ put :update, {:id => @role.to_param, :role => {}}
112
+ response.should render_template("edit")
113
+ end
114
+ end
115
+ end
116
+
117
+ describe "DELETE destroy" do
118
+ it "destroys the requested role" do
119
+ expect {
120
+ delete :destroy, {:id => @role.to_param}
121
+ }.to change(Role, :count).by(-1)
122
+ end
123
+
124
+ it "redirects to the roles list" do
125
+ delete :destroy, {:id => @role.to_param}
126
+ response.should redirect_to(roles_path)
127
+ end
128
+ end
129
+ end
130
+ end