maestrano-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. data/LICENSE +21 -0
  2. data/README.md +164 -0
  3. data/Rakefile +38 -0
  4. data/app/controllers/maestrano/rails/saml_base_controller.rb +39 -0
  5. data/lib/generators/active_record/maestrano_group_generator.rb +38 -0
  6. data/lib/generators/active_record/maestrano_user_generator.rb +38 -0
  7. data/lib/generators/active_record/templates/migration.rb +12 -0
  8. data/lib/generators/maestrano/USAGE +2 -0
  9. data/lib/generators/maestrano/group_generator.rb +11 -0
  10. data/lib/generators/maestrano/install_generator.rb +30 -0
  11. data/lib/generators/maestrano/orm_helpers.rb +75 -0
  12. data/lib/generators/maestrano/templates/maestrano.rb +84 -0
  13. data/lib/generators/maestrano/templates/saml_controller.rb +52 -0
  14. data/lib/generators/maestrano/user_generator.rb +11 -0
  15. data/lib/generators/mongoid/maestrano_group_generator.rb +26 -0
  16. data/lib/generators/mongoid/maestrano_user_generator.rb +26 -0
  17. data/lib/maestrano/rails/controllers/maestrano_security.rb +32 -0
  18. data/lib/maestrano/rails/models/maestrano_auth_resource.rb +96 -0
  19. data/lib/maestrano/rails/version.rb +5 -0
  20. data/lib/maestrano/rails.rb +10 -0
  21. data/lib/maestrano-rails.rb +1 -0
  22. data/test/controllers/generic_controller_test.rb +54 -0
  23. data/test/controllers/saml_controller_test.rb +117 -0
  24. data/test/dummy/README.rdoc +261 -0
  25. data/test/dummy/Rakefile +7 -0
  26. data/test/dummy/app/assets/javascripts/application.js +15 -0
  27. data/test/dummy/app/assets/javascripts/pages.js +2 -0
  28. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  29. data/test/dummy/app/assets/stylesheets/pages.css +4 -0
  30. data/test/dummy/app/controllers/application_controller.rb +3 -0
  31. data/test/dummy/app/controllers/maestrano/auth/saml_controller.rb +14 -0
  32. data/test/dummy/app/controllers/pages_controller.rb +4 -0
  33. data/test/dummy/app/helpers/application_helper.rb +2 -0
  34. data/test/dummy/app/helpers/pages_helper.rb +2 -0
  35. data/test/dummy/app/models/admin/monster.rb +2 -0
  36. data/test/dummy/app/models/admin.rb +5 -0
  37. data/test/dummy/app/models/mno_crew.rb +7 -0
  38. data/test/dummy/app/models/mno_monster.rb +9 -0
  39. data/test/dummy/app/models/monster.rb +2 -0
  40. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  41. data/test/dummy/app/views/pages/home.html.erb +2 -0
  42. data/test/dummy/config/application.rb +56 -0
  43. data/test/dummy/config/boot.rb +10 -0
  44. data/test/dummy/config/database.yml +25 -0
  45. data/test/dummy/config/environment.rb +5 -0
  46. data/test/dummy/config/environments/development.rb +37 -0
  47. data/test/dummy/config/environments/production.rb +67 -0
  48. data/test/dummy/config/environments/test.rb +37 -0
  49. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  50. data/test/dummy/config/initializers/inflections.rb +15 -0
  51. data/test/dummy/config/initializers/maestrano.rb +84 -0
  52. data/test/dummy/config/initializers/mime_types.rb +5 -0
  53. data/test/dummy/config/initializers/secret_token.rb +7 -0
  54. data/test/dummy/config/initializers/session_store.rb +8 -0
  55. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  56. data/test/dummy/config/locales/en.yml +5 -0
  57. data/test/dummy/config/routes.rb +70 -0
  58. data/test/dummy/config.ru +4 -0
  59. data/test/dummy/db/development.sqlite3 +0 -0
  60. data/test/dummy/db/migrate/20140526125222_create_monsters.rb +8 -0
  61. data/test/dummy/db/migrate/20140526125242_create_admin_monsters.rb +8 -0
  62. data/test/dummy/db/migrate/20140526144828_create_mno_monsters.rb +13 -0
  63. data/test/dummy/db/migrate/20140526151139_create_mno_crews.rb +11 -0
  64. data/test/dummy/db/schema.rb +44 -0
  65. data/test/dummy/db/test.sqlite3 +0 -0
  66. data/test/dummy/log/development.log +76 -0
  67. data/test/dummy/log/test.log +3732 -0
  68. data/test/dummy/public/404.html +26 -0
  69. data/test/dummy/public/422.html +26 -0
  70. data/test/dummy/public/500.html +25 -0
  71. data/test/dummy/public/favicon.ico +0 -0
  72. data/test/dummy/script/rails +6 -0
  73. data/test/dummy/test/fixtures/admin/monsters.yml +11 -0
  74. data/test/dummy/test/fixtures/mno_crews.yml +11 -0
  75. data/test/dummy/test/fixtures/mno_monsters.yml +15 -0
  76. data/test/dummy/test/fixtures/monsters.yml +11 -0
  77. data/test/dummy/test/functional/pages_controller_test.rb +9 -0
  78. data/test/dummy/test/unit/admin/monster_test.rb +7 -0
  79. data/test/dummy/test/unit/helpers/pages_helper_test.rb +4 -0
  80. data/test/dummy/test/unit/mno_crew_test.rb +7 -0
  81. data/test/dummy/test/unit/mno_monster_test.rb +7 -0
  82. data/test/dummy/test/unit/monster_test.rb +7 -0
  83. data/test/generators/group/active_record_generator_test.rb +79 -0
  84. data/test/generators/group/mongoid_generator_test.rb +76 -0
  85. data/test/generators/group_generator_test.rb +39 -0
  86. data/test/generators/install_generator_test.rb +36 -0
  87. data/test/generators/user/active_record_generator_test.rb +79 -0
  88. data/test/generators/user/mongoid_generator_test.rb +76 -0
  89. data/test/generators/user_generator_test.rb +39 -0
  90. data/test/maestrano-rails_test.rb +7 -0
  91. data/test/models/maestrano_group_via_test.rb +66 -0
  92. data/test/models/maestrano_user_via_test.rb +70 -0
  93. data/test/test_files/config/routes.rb +58 -0
  94. data/test/test_helper.rb +24 -0
  95. data/test/tmp/app/models/monster.rb +20 -0
  96. metadata +287 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,11 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
+
3
+ # This model initially had no columns defined. If you add columns to the
4
+ # model remove the '{}' from the fixture names and add the columns immediately
5
+ # below each fixture, per the syntax in the comments below
6
+ #
7
+ one: {}
8
+ # column: value
9
+ #
10
+ two: {}
11
+ # column: value
@@ -0,0 +1,11 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
+
3
+ one:
4
+ name: MyString
5
+ provider: MyString
6
+ uid: MyString
7
+
8
+ two:
9
+ name: MyString
10
+ provider: MyString
11
+ uid: MyString
@@ -0,0 +1,15 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
+
3
+ one:
4
+ first_name: MyString
5
+ last_name: MyString
6
+ email: MyString
7
+ provider: MyString
8
+ uid: MyString
9
+
10
+ two:
11
+ first_name: MyString
12
+ last_name: MyString
13
+ email: MyString
14
+ provider: MyString
15
+ uid: MyString
@@ -0,0 +1,11 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
+
3
+ # This model initially had no columns defined. If you add columns to the
4
+ # model remove the '{}' from the fixture names and add the columns immediately
5
+ # below each fixture, per the syntax in the comments below
6
+ #
7
+ one: {}
8
+ # column: value
9
+ #
10
+ two: {}
11
+ # column: value
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class PagesControllerTest < ActionController::TestCase
4
+ test "should get home" do
5
+ get :home
6
+ assert_response :success
7
+ end
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class Admin::MonsterTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class PagesHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class MnoCrewTest < 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 MnoMonsterTest < 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 MonsterTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,79 @@
1
+ require "test_helper"
2
+
3
+ if TEST_ORM == :active_record
4
+ require "generators/active_record/maestrano_group_generator"
5
+
6
+ class ActiveRecordGroupGeneratorTest < Rails::Generators::TestCase
7
+ tests ActiveRecord::Generators::MaestranoGroupGenerator
8
+ destination File.expand_path("../../../tmp", __FILE__)
9
+ setup :prepare_destination
10
+
11
+
12
+ context "normal model" do
13
+ setup { copy_model('monster') }
14
+
15
+ should "create the right migration" do
16
+ run_generator %w(monster)
17
+ assert_migration "db/migrate/add_maestrano_to_monsters.rb", /def self.up/
18
+ end
19
+
20
+ should "edit the model content" do
21
+ run_generator %w(monster)
22
+ assert_file "app/models/monster.rb", /maestrano_group_via/
23
+ end
24
+
25
+ should "not include parameters in rails 4" do
26
+ ActiveRecord::Generators::MaestranoGroupGenerator.any_instance.stubs(:rails_3?).returns(false)
27
+ run_generator %w(monster)
28
+ assert_file "app/models/monster.rb" do |content|
29
+ assert_no_match /attr_protected :provider/, content
30
+ end
31
+ end
32
+
33
+ should "not include parameters if rails 3 and strong_parameter present" do
34
+ ActiveRecord::Generators::MaestranoGroupGenerator.any_instance.stubs(:rails_3?).returns(true)
35
+ ActiveRecord::Generators::MaestranoGroupGenerator.any_instance.stubs(:strong_parameters_enabled?).returns(true)
36
+ run_generator %w(monster)
37
+ assert_file "app/models/monster.rb" do |content|
38
+ assert_no_match /attr_protected :provider/, content
39
+ end
40
+ end
41
+
42
+ should "include parameters if rails 3 and strong_parameter not present" do
43
+ ActiveRecord::Generators::MaestranoGroupGenerator.any_instance.stubs(:rails_3?).returns(true)
44
+ ActiveRecord::Generators::MaestranoGroupGenerator.any_instance.stubs(:strong_parameters_enabled?).returns(false)
45
+ run_generator %w(monster)
46
+ assert_file "app/models/monster.rb" do |content|
47
+ assert_match /attr_protected :provider/, content
48
+ end
49
+ end
50
+ end
51
+
52
+ context "namespaced model" do
53
+ setup { copy_model('admin/monster') }
54
+
55
+ should "create the right migration" do
56
+ run_generator %w(admin/monster)
57
+ assert_migration "db/migrate/add_maestrano_to_admin_monsters.rb", /def self.up/
58
+ end
59
+
60
+ should "edit the model content" do
61
+ run_generator %w(admin/monster)
62
+ assert_file "app/models/admin/monster.rb", /maestrano_group_via/
63
+ end
64
+ end
65
+
66
+ # Copy model like: 'monster' or 'admin/monster'
67
+ def copy_model(relative_model_path)
68
+ model_path = File.expand_path("../../../dummy/app/models/#{relative_model_path}.rb", __FILE__)
69
+ if (path_ary = relative_model_path.split('/')) && path_ary.size > 1
70
+ destination = File.join(destination_root, "app", "models",*path_ary[0..-2])
71
+ else
72
+ destination = File.join(destination_root, "app", "models")
73
+ end
74
+
75
+ FileUtils.mkdir_p(destination)
76
+ FileUtils.cp model_path, destination
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,76 @@
1
+ require "test_helper"
2
+
3
+ if TEST_ORM == :mongoid
4
+ require "generators/mongoid/maestrano_group_generator"
5
+
6
+ class MongoidGroupGeneratorTest < Rails::Generators::TestCase
7
+ tests Mongoid::Generators::MaestranoGroupGenerator
8
+ destination File.expand_path("../../../tmp", __FILE__)
9
+ setup :prepare_destination
10
+
11
+ context "normal model" do
12
+ setup { copy_model('monster') }
13
+
14
+ should "edit the model content" do
15
+ run_generator %w(monster)
16
+ assert_file "app/models/monster.rb", /maestrano_group_via/
17
+ end
18
+
19
+ should "not include parameters in rails 4" do
20
+ Mongoid::Generators::MaestranoGroupGenerator.any_instance.stubs(:rails_3?).returns(false)
21
+ run_generator %w(monster)
22
+ assert_file "app/models/monster.rb" do |content|
23
+ assert_no_match /attr_protected :provider/, content
24
+ end
25
+ end
26
+
27
+ should "not include parameters if rails 3 and strong_parameter present" do
28
+ Mongoid::Generators::MaestranoGroupGenerator.any_instance.stubs(:rails_3?).returns(true)
29
+ Mongoid::Generators::MaestranoGroupGenerator.any_instance.stubs(:strong_parameters_enabled?).returns(true)
30
+ run_generator %w(monster)
31
+ assert_file "app/models/monster.rb" do |content|
32
+ assert_no_match /attr_protected :provider/, content
33
+ end
34
+ end
35
+
36
+ should "include parameters if rails 3 and strong_parameter not present" do
37
+ Mongoid::Generators::MaestranoGroupGenerator.any_instance.stubs(:rails_3?).returns(true)
38
+ Mongoid::Generators::MaestranoGroupGenerator.any_instance.stubs(:strong_parameters_enabled?).returns(false)
39
+ run_generator %w(monster)
40
+ assert_file "app/models/monster.rb" do |content|
41
+ assert_match /attr_protected :provider/, content
42
+ end
43
+ end
44
+ end
45
+
46
+ context "namespaced model" do
47
+ setup { copy_model('admin/monster') }
48
+
49
+ should "edit the model content" do
50
+ run_generator %w(admin/monster)
51
+ assert_file "app/models/admin/monster.rb", /maestrano_group_via/
52
+ end
53
+ end
54
+
55
+ # Copy model like: 'monster' or 'admin/monster'
56
+ def copy_model(relative_model_path)
57
+ model_path = File.expand_path("../../../dummy/app/models/#{relative_model_path}.rb", __FILE__)
58
+ if (path_ary = relative_model_path.split('/')) && path_ary.size > 1
59
+ destination = File.join(destination_root, "app", "models",*path_ary[0..-2])
60
+ else
61
+ destination = File.join(destination_root, "app", "models")
62
+ end
63
+
64
+ FileUtils.mkdir_p(destination)
65
+ FileUtils.cp model_path, destination
66
+
67
+ # Replace class name
68
+ class_name = path_ary.map(&:capitalize).join("::")
69
+ full_path = File.join(destination,"#{path_ary[-1]}.rb")
70
+ text = File.read(full_path)
71
+ replace = text.gsub("class #{class_name} < ActiveRecord::Base", "class #{class_name}\ninclude Mongoid::Document\n")
72
+ File.open(full_path, "w") {|file| file.puts replace}
73
+ end
74
+ end
75
+ end
76
+
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+
4
+ class GroupGeneratorTest < Rails::Generators::TestCase
5
+ tests Maestrano::Generators::GroupGenerator
6
+ destination File.expand_path("../../tmp", __FILE__)
7
+ setup :prepare_destination
8
+
9
+ should "call the active_record migration generator successfully" do
10
+ run_generator %w(monster --orm=active_record)
11
+ assert_migration "db/migrate/add_maestrano_to_monsters.rb", /def self.up/
12
+ end
13
+
14
+ should "call the mongoid migration generator successfully" do
15
+ copy_model('monster')
16
+ run_generator %w(monster --orm=mongoid)
17
+ assert_file "app/models/monster.rb", /maestrano_group_via/
18
+ end
19
+
20
+ # Copy model like: 'monster' or 'admin/monster'
21
+ def copy_model(relative_model_path)
22
+ model_path = File.expand_path("../../dummy/app/models/#{relative_model_path}.rb", __FILE__)
23
+ if (path_ary = relative_model_path.split('/')) && path_ary.size > 1
24
+ destination = File.join(destination_root, "app", "models",*path_ary[0..-2])
25
+ else
26
+ destination = File.join(destination_root, "app", "models")
27
+ end
28
+
29
+ FileUtils.mkdir_p(destination)
30
+ FileUtils.cp model_path, destination
31
+
32
+ # Replace class name
33
+ class_name = path_ary.map(&:capitalize).join("::")
34
+ full_path = File.join(destination,"#{path_ary[-1]}.rb")
35
+ text = File.read(full_path)
36
+ replace = text.gsub("class #{class_name} < ActiveRecord::Base", "class #{class_name}\ninclude Mongoid::Document\n")
37
+ File.open(full_path, "w") {|file| file.puts replace}
38
+ end
39
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ class InstallGeneratorTest < Rails::Generators::TestCase
4
+ tests Maestrano::Generators::InstallGenerator
5
+ destination File.expand_path("../../tmp", __FILE__)
6
+
7
+ setup do
8
+ prepare_destination
9
+ copy_routes
10
+ end
11
+
12
+ should "create the maestrano initializer" do
13
+ run_generator
14
+ assert_file "config/initializers/maestrano.rb"
15
+ end
16
+
17
+ should "create the maestrano/auth/saml controller" do
18
+ run_generator
19
+ assert_file "app/controllers/maestrano/auth/saml_controller.rb"
20
+ end
21
+
22
+ should "create the maestrano routes" do
23
+
24
+ run_generator
25
+ match = /namespace\s+:maestrano\s+do\n\s*namespace\s+:auth\s+do\n\s*resources\s+:saml,\s+only:\[\]\s+do\n\s*get 'init',\son:\s:collection\n\s*post\s'consume',\s+on:\s+:collection(\n\s*end){3}/
26
+ assert_file "config/routes.rb", match
27
+ end
28
+
29
+ def copy_routes
30
+ routes = File.expand_path("../../test_files/config/routes.rb", __FILE__)
31
+ destination = File.join(destination_root, "config")
32
+
33
+ FileUtils.mkdir_p(destination)
34
+ FileUtils.cp routes, destination
35
+ end
36
+ end
@@ -0,0 +1,79 @@
1
+ require "test_helper"
2
+
3
+ if TEST_ORM == :active_record
4
+ require "generators/active_record/maestrano_user_generator"
5
+
6
+ class ActiveRecordUserGeneratorTest < Rails::Generators::TestCase
7
+ tests ActiveRecord::Generators::MaestranoUserGenerator
8
+ destination File.expand_path("../../../tmp", __FILE__)
9
+ setup :prepare_destination
10
+
11
+
12
+ context "normal model" do
13
+ setup { copy_model('monster') }
14
+
15
+ should "create the right migration" do
16
+ run_generator %w(monster)
17
+ assert_migration "db/migrate/add_maestrano_to_monsters.rb", /def self.up/
18
+ end
19
+
20
+ should "edit the model content" do
21
+ run_generator %w(monster)
22
+ assert_file "app/models/monster.rb", /maestrano_user_via/
23
+ end
24
+
25
+ should "not include parameters in rails 4" do
26
+ ActiveRecord::Generators::MaestranoUserGenerator.any_instance.stubs(:rails_3?).returns(false)
27
+ run_generator %w(monster)
28
+ assert_file "app/models/monster.rb" do |content|
29
+ assert_no_match /attr_protected :provider/, content
30
+ end
31
+ end
32
+
33
+ should "not include parameters if rails 3 and strong_parameter present" do
34
+ ActiveRecord::Generators::MaestranoUserGenerator.any_instance.stubs(:rails_3?).returns(true)
35
+ ActiveRecord::Generators::MaestranoUserGenerator.any_instance.stubs(:strong_parameters_enabled?).returns(true)
36
+ run_generator %w(monster)
37
+ assert_file "app/models/monster.rb" do |content|
38
+ assert_no_match /attr_protected :provider/, content
39
+ end
40
+ end
41
+
42
+ should "include parameters if rails 3 and strong_parameter not present" do
43
+ ActiveRecord::Generators::MaestranoUserGenerator.any_instance.stubs(:rails_3?).returns(true)
44
+ ActiveRecord::Generators::MaestranoUserGenerator.any_instance.stubs(:strong_parameters_enabled?).returns(false)
45
+ run_generator %w(monster)
46
+ assert_file "app/models/monster.rb" do |content|
47
+ assert_match /attr_protected :provider/, content
48
+ end
49
+ end
50
+ end
51
+
52
+ context "namespaced model" do
53
+ setup { copy_model('admin/monster') }
54
+
55
+ should "create the right migration" do
56
+ run_generator %w(admin/monster)
57
+ assert_migration "db/migrate/add_maestrano_to_admin_monsters.rb", /def self.up/
58
+ end
59
+
60
+ should "edit the model content" do
61
+ run_generator %w(admin/monster)
62
+ assert_file "app/models/admin/monster.rb", /maestrano_user_via/
63
+ end
64
+ end
65
+
66
+ # Copy model like: 'monster' or 'admin/monster'
67
+ def copy_model(relative_model_path)
68
+ model_path = File.expand_path("../../../dummy/app/models/#{relative_model_path}.rb", __FILE__)
69
+ if (path_ary = relative_model_path.split('/')) && path_ary.size > 1
70
+ destination = File.join(destination_root, "app", "models",*path_ary[0..-2])
71
+ else
72
+ destination = File.join(destination_root, "app", "models")
73
+ end
74
+
75
+ FileUtils.mkdir_p(destination)
76
+ FileUtils.cp model_path, destination
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,76 @@
1
+ require "test_helper"
2
+
3
+ if TEST_ORM == :mongoid
4
+ require "generators/mongoid/maestrano_user_generator"
5
+
6
+ class MongoidUserGeneratorTest < Rails::Generators::TestCase
7
+ tests Mongoid::Generators::MaestranoUserGenerator
8
+ destination File.expand_path("../../../tmp", __FILE__)
9
+ setup :prepare_destination
10
+
11
+ context "normal model" do
12
+ setup { copy_model('monster') }
13
+
14
+ should "edit the model content" do
15
+ run_generator %w(monster)
16
+ assert_file "app/models/monster.rb", /maestrano_user_via/
17
+ end
18
+
19
+ should "not include parameters in rails 4" do
20
+ Mongoid::Generators::MaestranoUserGenerator.any_instance.stubs(:rails_3?).returns(false)
21
+ run_generator %w(monster)
22
+ assert_file "app/models/monster.rb" do |content|
23
+ assert_no_match /attr_protected :provider/, content
24
+ end
25
+ end
26
+
27
+ should "not include parameters if rails 3 and strong_parameter present" do
28
+ Mongoid::Generators::MaestranoUserGenerator.any_instance.stubs(:rails_3?).returns(true)
29
+ Mongoid::Generators::MaestranoUserGenerator.any_instance.stubs(:strong_parameters_enabled?).returns(true)
30
+ run_generator %w(monster)
31
+ assert_file "app/models/monster.rb" do |content|
32
+ assert_no_match /attr_protected :provider/, content
33
+ end
34
+ end
35
+
36
+ should "include parameters if rails 3 and strong_parameter not present" do
37
+ Mongoid::Generators::MaestranoUserGenerator.any_instance.stubs(:rails_3?).returns(true)
38
+ Mongoid::Generators::MaestranoUserGenerator.any_instance.stubs(:strong_parameters_enabled?).returns(false)
39
+ run_generator %w(monster)
40
+ assert_file "app/models/monster.rb" do |content|
41
+ assert_match /attr_protected :provider/, content
42
+ end
43
+ end
44
+ end
45
+
46
+ context "namespaced model" do
47
+ setup { copy_model('admin/monster') }
48
+
49
+ should "edit the model content" do
50
+ run_generator %w(admin/monster)
51
+ assert_file "app/models/admin/monster.rb", /maestrano_user_via/
52
+ end
53
+ end
54
+
55
+ # Copy model like: 'monster' or 'admin/monster'
56
+ def copy_model(relative_model_path)
57
+ model_path = File.expand_path("../../../dummy/app/models/#{relative_model_path}.rb", __FILE__)
58
+ if (path_ary = relative_model_path.split('/')) && path_ary.size > 1
59
+ destination = File.join(destination_root, "app", "models",*path_ary[0..-2])
60
+ else
61
+ destination = File.join(destination_root, "app", "models")
62
+ end
63
+
64
+ FileUtils.mkdir_p(destination)
65
+ FileUtils.cp model_path, destination
66
+
67
+ # Replace class name
68
+ class_name = path_ary.map(&:capitalize).join("::")
69
+ full_path = File.join(destination,"#{path_ary[-1]}.rb")
70
+ text = File.read(full_path)
71
+ replace = text.gsub("class #{class_name} < ActiveRecord::Base", "class #{class_name}\ninclude Mongoid::Document\n")
72
+ File.open(full_path, "w") {|file| file.puts replace}
73
+ end
74
+ end
75
+ end
76
+
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+
4
+ class UserGeneratorTest < Rails::Generators::TestCase
5
+ tests Maestrano::Generators::UserGenerator
6
+ destination File.expand_path("../../tmp", __FILE__)
7
+ setup :prepare_destination
8
+
9
+ should "call the active_record migration generator successfully" do
10
+ run_generator %w(monster --orm=active_record)
11
+ assert_migration "db/migrate/add_maestrano_to_monsters.rb", /def self.up/
12
+ end
13
+
14
+ should "call the mongoid migration generator successfully" do
15
+ copy_model('monster')
16
+ run_generator %w(monster --orm=mongoid)
17
+ assert_file "app/models/monster.rb", /maestrano_user_via/
18
+ end
19
+
20
+ # Copy model like: 'monster' or 'admin/monster'
21
+ def copy_model(relative_model_path)
22
+ model_path = File.expand_path("../../dummy/app/models/#{relative_model_path}.rb", __FILE__)
23
+ if (path_ary = relative_model_path.split('/')) && path_ary.size > 1
24
+ destination = File.join(destination_root, "app", "models",*path_ary[0..-2])
25
+ else
26
+ destination = File.join(destination_root, "app", "models")
27
+ end
28
+
29
+ FileUtils.mkdir_p(destination)
30
+ FileUtils.cp model_path, destination
31
+
32
+ # Replace class name
33
+ class_name = path_ary.map(&:capitalize).join("::")
34
+ full_path = File.join(destination,"#{path_ary[-1]}.rb")
35
+ text = File.read(full_path)
36
+ replace = text.gsub("class #{class_name} < ActiveRecord::Base", "class #{class_name}\ninclude Mongoid::Document\n")
37
+ File.open(full_path, "w") {|file| file.puts replace}
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class MaestranoRailsTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, Maestrano::Rails
6
+ end
7
+ end