simon_says 0.0.3

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 (101) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.gitpublish +3 -0
  4. data/.travis.yml +9 -0
  5. data/Gemfile +13 -0
  6. data/Guardfile +13 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +18 -0
  9. data/ROADMAP.md +9 -0
  10. data/Rakefile +24 -0
  11. data/SimonSays.png +0 -0
  12. data/lib/simon_says/authorizer.rb +157 -0
  13. data/lib/simon_says/roleable.rb +107 -0
  14. data/lib/simon_says/version.rb +3 -0
  15. data/lib/simon_says.rb +8 -0
  16. data/simon_says.gemspec +28 -0
  17. data/test/controllers/admin/reports_controller_test.rb +92 -0
  18. data/test/controllers/documents_controller_test.rb +87 -0
  19. data/test/models/admin_test.rb +7 -0
  20. data/test/models/membership_test.rb +7 -0
  21. data/test/rails_app/.gitignore +16 -0
  22. data/test/rails_app/README.rdoc +28 -0
  23. data/test/rails_app/Rakefile +6 -0
  24. data/test/rails_app/app/assets/images/.keep +0 -0
  25. data/test/rails_app/app/assets/javascripts/application.js +16 -0
  26. data/test/rails_app/app/assets/stylesheets/application.css +15 -0
  27. data/test/rails_app/app/controllers/admin/reports_controller.rb +40 -0
  28. data/test/rails_app/app/controllers/application_controller.rb +19 -0
  29. data/test/rails_app/app/controllers/concerns/.keep +0 -0
  30. data/test/rails_app/app/controllers/documents_controller.rb +48 -0
  31. data/test/rails_app/app/helpers/application_helper.rb +2 -0
  32. data/test/rails_app/app/mailers/.keep +0 -0
  33. data/test/rails_app/app/models/.keep +0 -0
  34. data/test/rails_app/app/models/admin/report.rb +2 -0
  35. data/test/rails_app/app/models/admin.rb +5 -0
  36. data/test/rails_app/app/models/concerns/.keep +0 -0
  37. data/test/rails_app/app/models/document.rb +4 -0
  38. data/test/rails_app/app/models/membership.rb +8 -0
  39. data/test/rails_app/app/models/user.rb +4 -0
  40. data/test/rails_app/app/views/layouts/application.html.erb +14 -0
  41. data/test/rails_app/bin/bundle +3 -0
  42. data/test/rails_app/bin/rails +8 -0
  43. data/test/rails_app/bin/rake +8 -0
  44. data/test/rails_app/bin/spring +18 -0
  45. data/test/rails_app/config/application.rb +29 -0
  46. data/test/rails_app/config/boot.rb +4 -0
  47. data/test/rails_app/config/database.yml +25 -0
  48. data/test/rails_app/config/environment.rb +5 -0
  49. data/test/rails_app/config/environments/development.rb +37 -0
  50. data/test/rails_app/config/environments/production.rb +78 -0
  51. data/test/rails_app/config/environments/test.rb +39 -0
  52. data/test/rails_app/config/initializers/assets.rb +8 -0
  53. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  54. data/test/rails_app/config/initializers/cookies_serializer.rb +3 -0
  55. data/test/rails_app/config/initializers/filter_parameter_logging.rb +4 -0
  56. data/test/rails_app/config/initializers/inflections.rb +16 -0
  57. data/test/rails_app/config/initializers/mime_types.rb +4 -0
  58. data/test/rails_app/config/initializers/session_store.rb +3 -0
  59. data/test/rails_app/config/initializers/wrap_parameters.rb +14 -0
  60. data/test/rails_app/config/locales/en.yml +23 -0
  61. data/test/rails_app/config/routes.rb +7 -0
  62. data/test/rails_app/config/secrets.yml +22 -0
  63. data/test/rails_app/config.ru +4 -0
  64. data/test/rails_app/db/migrate/20141016142638_create_admins.rb +9 -0
  65. data/test/rails_app/db/migrate/20141016183619_create_users.rb +8 -0
  66. data/test/rails_app/db/migrate/20141016183633_create_memberships.rb +12 -0
  67. data/test/rails_app/db/migrate/20141016183642_create_documents.rb +9 -0
  68. data/test/rails_app/db/migrate/20141017140833_create_admin_reports.rb +9 -0
  69. data/test/rails_app/db/schema.rb +47 -0
  70. data/test/rails_app/db/seeds.rb +7 -0
  71. data/test/rails_app/lib/assets/.keep +0 -0
  72. data/test/rails_app/lib/tasks/.keep +0 -0
  73. data/test/rails_app/log/.keep +0 -0
  74. data/test/rails_app/public/404.html +67 -0
  75. data/test/rails_app/public/422.html +67 -0
  76. data/test/rails_app/public/500.html +66 -0
  77. data/test/rails_app/public/favicon.ico +0 -0
  78. data/test/rails_app/public/robots.txt +5 -0
  79. data/test/rails_app/test/controllers/.keep +0 -0
  80. data/test/rails_app/test/fixtures/.keep +0 -0
  81. data/test/rails_app/test/fixtures/admin/reports.yml +2 -0
  82. data/test/rails_app/test/fixtures/admins.yml +11 -0
  83. data/test/rails_app/test/fixtures/documents.yml +8 -0
  84. data/test/rails_app/test/fixtures/memberships.yml +10 -0
  85. data/test/rails_app/test/fixtures/users.yml +2 -0
  86. data/test/rails_app/test/helpers/.keep +0 -0
  87. data/test/rails_app/test/integration/.keep +0 -0
  88. data/test/rails_app/test/mailers/.keep +0 -0
  89. data/test/rails_app/test/models/.keep +0 -0
  90. data/test/rails_app/test/models/admin/report_test.rb +7 -0
  91. data/test/rails_app/test/models/document_test.rb +7 -0
  92. data/test/rails_app/test/models/membership_test.rb +7 -0
  93. data/test/rails_app/test/models/user_test.rb +7 -0
  94. data/test/rails_app/test/test_helper.rb +10 -0
  95. data/test/rails_app/vendor/assets/javascripts/.keep +0 -0
  96. data/test/rails_app/vendor/assets/stylesheets/.keep +0 -0
  97. data/test/simon_says/authorizer_test.rb +143 -0
  98. data/test/simon_says/roleable_test.rb +200 -0
  99. data/test/simon_says_test.rb +7 -0
  100. data/test/test_helper.rb +48 -0
  101. metadata +312 -0
@@ -0,0 +1,9 @@
1
+ class CreateAdmins < ActiveRecord::Migration
2
+ def change
3
+ create_table :admins do |t|
4
+ t.integer :access_mask
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ class CreateMemberships < ActiveRecord::Migration
2
+ def change
3
+ create_table :memberships do |t|
4
+ t.references :user
5
+ t.references :document
6
+
7
+ t.integer :roles_mask, default: 0
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ class CreateDocuments < ActiveRecord::Migration
2
+ def change
3
+ create_table :documents do |t|
4
+ t.string :title
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class CreateAdminReports < ActiveRecord::Migration
2
+ def change
3
+ create_table :admin_reports do |t|
4
+ t.string :title
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20141017140833) do
15
+
16
+ create_table "admin_reports", force: true do |t|
17
+ t.string "title"
18
+ t.datetime "created_at"
19
+ t.datetime "updated_at"
20
+ end
21
+
22
+ create_table "admins", force: true do |t|
23
+ t.integer "access_mask"
24
+ t.datetime "created_at"
25
+ t.datetime "updated_at"
26
+ end
27
+
28
+ create_table "documents", force: true do |t|
29
+ t.string "title"
30
+ t.datetime "created_at"
31
+ t.datetime "updated_at"
32
+ end
33
+
34
+ create_table "memberships", force: true do |t|
35
+ t.integer "user_id"
36
+ t.integer "document_id"
37
+ t.integer "roles_mask", default: 0
38
+ t.datetime "created_at"
39
+ t.datetime "updated_at"
40
+ end
41
+
42
+ create_table "users", force: true do |t|
43
+ t.datetime "created_at"
44
+ t.datetime "updated_at"
45
+ end
46
+
47
+ end
@@ -0,0 +1,7 @@
1
+ # This file should contain all the record creation needed to seed the database with its default values.
2
+ # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
3
+ #
4
+ # Examples:
5
+ #
6
+ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
7
+ # Mayor.create(name: 'Emanuel', city: cities.first)
File without changes
File without changes
File without changes
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-agent: *
5
+ # Disallow: /
File without changes
File without changes
@@ -0,0 +1,2 @@
1
+ report_one:
2
+ title: Some Title
@@ -0,0 +1,11 @@
1
+ support:
2
+ access_mask: 1
3
+
4
+ content:
5
+ access_mask: 2
6
+
7
+ marketing:
8
+ access_mask: 4
9
+
10
+ all:
11
+ access_mask: <%= Admin::ACCESS.size.times.map { |n| n ** 2 }.sum %>
@@ -0,0 +1,8 @@
1
+ alpha:
2
+ title: Alpha
3
+
4
+ beta:
5
+ title: Beta
6
+
7
+ gamma:
8
+ title: Gamma
@@ -0,0 +1,10 @@
1
+ mb1:
2
+ user: bob
3
+ document: alpha
4
+ roles_mask: <%= Membership::ROLES.size.times.map { |n| 2 ** n }.sum %>
5
+
6
+ mb2:
7
+ user: bob
8
+ document: beta
9
+ roles_mask: 1
10
+
@@ -0,0 +1,2 @@
1
+ bob: {}
2
+ jim: {}
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class Admin::ReportTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class DocumentTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class MembershipTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class UserTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,10 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+ require File.expand_path('../../config/environment', __FILE__)
3
+ require 'rails/test_help'
4
+
5
+ class ActiveSupport::TestCase
6
+ # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
7
+ fixtures :all
8
+
9
+ # Add more helper methods to be used by all tests here...
10
+ end
File without changes
File without changes
@@ -0,0 +1,143 @@
1
+ require 'test_helper'
2
+
3
+ class AuthorizerTest < ActiveSupport::TestCase
4
+ setup do
5
+ @controller = Class.new(ApplicationController) do
6
+ # These would be defined by Devise or some authenication library
7
+ attr_accessor :current_user, :current_admin, :sites
8
+ attr_reader :params
9
+
10
+ def params=(params)
11
+ @params = params.with_indifferent_access
12
+ end
13
+
14
+ # shortcut to read instance variables
15
+ def [](ivar_name)
16
+ instance_variable_get :"@#{ivar_name}"
17
+ end
18
+
19
+ def authenticate_admin! # dummy method
20
+ end
21
+
22
+ def authenticate_user! # dummy method
23
+ end
24
+
25
+ include SimonSays::Authorizer
26
+ end.new
27
+
28
+ @controller.current_user = users(:bob)
29
+ @controller.params = { id: documents(:alpha).id }
30
+ end
31
+
32
+ test "find_resource" do
33
+ @controller.find_resource :document
34
+
35
+ assert_equal documents(:alpha), @controller[:document]
36
+ end
37
+
38
+ test "find_resource with class_name" do
39
+ @controller.find_resource :document, class_name: 'document'
40
+
41
+ assert_equal documents(:alpha), @controller[:document]
42
+ end
43
+
44
+ test "find_resource with default scope and through" do
45
+ @controller.class.default_authorization_scope = :current_user
46
+ @controller.current_user = users(:bob)
47
+
48
+ @controller.find_resource :document, through: :memberships
49
+
50
+ assert_equal documents(:alpha), @controller[:document]
51
+ end
52
+
53
+ test "find_resource with from" do
54
+ @controller.instance_variable_set :@user, users(:bob)
55
+
56
+ @controller.find_resource :document, from: :user
57
+
58
+ assert_equal documents(:alpha), @controller[:document]
59
+ end
60
+
61
+ test "find_resource with namespace" do
62
+ @controller.current_admin = admins(:support)
63
+ @controller.params = { id: admin_reports(:report_one).id }
64
+
65
+ @controller.find_resource :report, namespace: :admin
66
+
67
+ assert_equal admin_reports(:report_one), @controller[:report]
68
+ end
69
+
70
+ test "find_resource raises RecordNotFound" do
71
+ assert_raises ActiveRecord::RecordNotFound do
72
+ @controller.params = { id: -1 }
73
+ @controller.find_resource :document
74
+ end
75
+ end
76
+
77
+ test "find_resource raises RecordNotFound with default scope and through" do
78
+ @controller.class.default_authorization_scope = :current_user
79
+ @controller.current_user = users(:bob)
80
+
81
+ assert_raises ActiveRecord::RecordNotFound do
82
+ @controller.params = { id: -1 }
83
+ @controller.find_resource :document, through: :memberships
84
+ end
85
+ end
86
+
87
+ test "find_resource raises RecordNotFound with from" do
88
+ @controller.instance_variable_set :@user, users(:bob)
89
+
90
+ assert_raises ActiveRecord::RecordNotFound do
91
+ @controller.params = { id: -1 }
92
+ @controller.find_resource :document, from: :user
93
+ end
94
+ end
95
+
96
+ test "authorize with membership role" do
97
+ @controller.instance_variable_set :@membership, documents(:alpha).memberships.first
98
+
99
+ assert @controller.authorize(:fork, resource: :membership)
100
+ end
101
+
102
+ test "authorize with current_admin" do
103
+ @controller.current_admin = admins(:support)
104
+
105
+ assert @controller.authorize(:support, resource: :admin)
106
+ end
107
+
108
+ test "authorize with multiple roles" do
109
+ @controller.instance_variable_set :@membership, documents(:alpha).memberships.first
110
+
111
+ assert @controller.authorize([:update, :delete], resource: :membership)
112
+ end
113
+
114
+ test "authorize with through" do
115
+ @controller.instance_variable_set :@membership, documents(:alpha).memberships.first
116
+
117
+ assert @controller.authorize(:delete, through: :membership)
118
+ end
119
+
120
+ test "authorize invokes authentication_admin" do
121
+ @controller.current_admin = admins(:marketing)
122
+
123
+ @controller.expects(:authenticate_admin!).once
124
+ @controller.authorize(:marketing, resource: :admin)
125
+ end
126
+
127
+ test "authorization failure single role" do
128
+ assert_raises SimonSays::Authorizer::Denied do
129
+ @controller.instance_variable_set :@membership, documents(:beta).memberships.first
130
+
131
+ @controller.authorize(:delete, resource: :membership)
132
+ end
133
+ end
134
+
135
+ test "authorization failire multi roles" do
136
+ @controller.instance_variable_set :@membership, documents(:beta).memberships.first
137
+
138
+ assert_raises SimonSays::Authorizer::Denied do
139
+ @controller.authorize([:update, :delete], resource: :membership)
140
+ end
141
+ end
142
+ end
143
+