maestrano-rails-test 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +273 -0
  4. data/Rakefile +38 -0
  5. data/app/controllers/maestrano/rails/metadata_controller.rb +8 -0
  6. data/app/controllers/maestrano/rails/saml_base_controller.rb +40 -0
  7. data/app/controllers/maestrano/rails/web_hook_controller.rb +16 -0
  8. data/lib/generators/active_record/maestrano_group_generator.rb +38 -0
  9. data/lib/generators/active_record/maestrano_user_generator.rb +38 -0
  10. data/lib/generators/active_record/templates/migration.rb +13 -0
  11. data/lib/generators/maestrano/USAGE +2 -0
  12. data/lib/generators/maestrano/group_generator.rb +11 -0
  13. data/lib/generators/maestrano/install_generator.rb +31 -0
  14. data/lib/generators/maestrano/orm_helpers.rb +75 -0
  15. data/lib/generators/maestrano/templates/group_users_controller.rb +26 -0
  16. data/lib/generators/maestrano/templates/groups_controller.rb +36 -0
  17. data/lib/generators/maestrano/templates/maestrano.rb +126 -0
  18. data/lib/generators/maestrano/templates/saml_controller.rb +52 -0
  19. data/lib/generators/maestrano/user_generator.rb +11 -0
  20. data/lib/generators/mongoid/maestrano_group_generator.rb +26 -0
  21. data/lib/generators/mongoid/maestrano_user_generator.rb +26 -0
  22. data/lib/maestrano-rails.rb +1 -0
  23. data/lib/maestrano/rails.rb +11 -0
  24. data/lib/maestrano/rails/controllers/maestrano_security.rb +32 -0
  25. data/lib/maestrano/rails/models/maestrano_auth_resource.rb +116 -0
  26. data/lib/maestrano/rails/routing/routes.rb +28 -0
  27. data/lib/maestrano/rails/version.rb +5 -0
  28. data/test/controllers/generic_controller_test.rb +56 -0
  29. data/test/controllers/group_users_controller_test.rb +23 -0
  30. data/test/controllers/groups_controller_test.rb +24 -0
  31. data/test/controllers/metadata_controller_test.rb +25 -0
  32. data/test/controllers/saml_controller_test.rb +123 -0
  33. data/test/dummy/db/development.sqlite3 +0 -0
  34. data/test/dummy/db/test.sqlite3 +0 -0
  35. data/test/dummy/log/development.log +44 -0
  36. data/test/dummy/log/test.log +6611 -0
  37. data/test/dummy_activerecord/README.rdoc +261 -0
  38. data/test/dummy_activerecord/Rakefile +7 -0
  39. data/test/dummy_activerecord/app/assets/javascripts/application.js +15 -0
  40. data/test/dummy_activerecord/app/assets/javascripts/pages.js +2 -0
  41. data/test/dummy_activerecord/app/assets/stylesheets/application.css +13 -0
  42. data/test/dummy_activerecord/app/assets/stylesheets/pages.css +4 -0
  43. data/test/dummy_activerecord/app/controllers/application_controller.rb +3 -0
  44. data/test/dummy_activerecord/app/controllers/maestrano/account/group_users_controller.rb +27 -0
  45. data/test/dummy_activerecord/app/controllers/maestrano/account/groups_controller.rb +37 -0
  46. data/test/dummy_activerecord/app/controllers/maestrano/auth/saml_controller.rb +14 -0
  47. data/test/dummy_activerecord/app/controllers/pages_controller.rb +4 -0
  48. data/test/dummy_activerecord/app/helpers/application_helper.rb +2 -0
  49. data/test/dummy_activerecord/app/helpers/pages_helper.rb +2 -0
  50. data/test/dummy_activerecord/app/models/admin.rb +5 -0
  51. data/test/dummy_activerecord/app/models/admin/monster.rb +2 -0
  52. data/test/dummy_activerecord/app/models/mno_crew.rb +7 -0
  53. data/test/dummy_activerecord/app/models/mno_monster.rb +9 -0
  54. data/test/dummy_activerecord/app/models/monster.rb +2 -0
  55. data/test/dummy_activerecord/app/views/layouts/application.html.erb +14 -0
  56. data/test/dummy_activerecord/app/views/pages/home.html.erb +2 -0
  57. data/test/dummy_activerecord/config.ru +4 -0
  58. data/test/dummy_activerecord/config/application.rb +56 -0
  59. data/test/dummy_activerecord/config/boot.rb +10 -0
  60. data/test/dummy_activerecord/config/database.yml +25 -0
  61. data/test/dummy_activerecord/config/environment.rb +5 -0
  62. data/test/dummy_activerecord/config/environments/development.rb +37 -0
  63. data/test/dummy_activerecord/config/environments/production.rb +67 -0
  64. data/test/dummy_activerecord/config/environments/test.rb +37 -0
  65. data/test/dummy_activerecord/config/initializers/backtrace_silencers.rb +7 -0
  66. data/test/dummy_activerecord/config/initializers/inflections.rb +15 -0
  67. data/test/dummy_activerecord/config/initializers/maestrano.rb +85 -0
  68. data/test/dummy_activerecord/config/initializers/mime_types.rb +5 -0
  69. data/test/dummy_activerecord/config/initializers/secret_token.rb +7 -0
  70. data/test/dummy_activerecord/config/initializers/session_store.rb +8 -0
  71. data/test/dummy_activerecord/config/initializers/wrap_parameters.rb +14 -0
  72. data/test/dummy_activerecord/config/locales/en.yml +5 -0
  73. data/test/dummy_activerecord/config/routes.rb +63 -0
  74. data/test/dummy_activerecord/db/development.sqlite3 +0 -0
  75. data/test/dummy_activerecord/db/migrate/20140526125222_create_monsters.rb +8 -0
  76. data/test/dummy_activerecord/db/migrate/20140526125242_create_admin_monsters.rb +8 -0
  77. data/test/dummy_activerecord/db/migrate/20140526144828_create_mno_monsters.rb +13 -0
  78. data/test/dummy_activerecord/db/migrate/20140526151139_create_mno_crews.rb +11 -0
  79. data/test/dummy_activerecord/db/schema.rb +44 -0
  80. data/test/dummy_activerecord/db/test.sqlite3 +0 -0
  81. data/test/dummy_activerecord/log/development.log +76 -0
  82. data/test/dummy_activerecord/log/test.log +326 -0
  83. data/test/dummy_activerecord/public/404.html +26 -0
  84. data/test/dummy_activerecord/public/422.html +26 -0
  85. data/test/dummy_activerecord/public/500.html +25 -0
  86. data/test/dummy_activerecord/public/favicon.ico +0 -0
  87. data/test/dummy_activerecord/script/rails +6 -0
  88. data/test/dummy_mongoid/README.rdoc +261 -0
  89. data/test/dummy_mongoid/Rakefile +7 -0
  90. data/test/dummy_mongoid/app/assets/javascripts/application.js +15 -0
  91. data/test/dummy_mongoid/app/assets/javascripts/pages.js +2 -0
  92. data/test/dummy_mongoid/app/assets/stylesheets/application.css +13 -0
  93. data/test/dummy_mongoid/app/assets/stylesheets/pages.css +4 -0
  94. data/test/dummy_mongoid/app/controllers/application_controller.rb +3 -0
  95. data/test/dummy_mongoid/app/controllers/maestrano/account/group_users_controller.rb +27 -0
  96. data/test/dummy_mongoid/app/controllers/maestrano/account/groups_controller.rb +37 -0
  97. data/test/dummy_mongoid/app/controllers/maestrano/auth/saml_controller.rb +14 -0
  98. data/test/dummy_mongoid/app/controllers/pages_controller.rb +4 -0
  99. data/test/dummy_mongoid/app/helpers/application_helper.rb +2 -0
  100. data/test/dummy_mongoid/app/helpers/pages_helper.rb +2 -0
  101. data/test/dummy_mongoid/app/models/mno_crew.rb +11 -0
  102. data/test/dummy_mongoid/app/models/mno_monster.rb +14 -0
  103. data/test/dummy_mongoid/app/models/monster.rb +3 -0
  104. data/test/dummy_mongoid/app/views/layouts/application.html.erb +14 -0
  105. data/test/dummy_mongoid/app/views/pages/home.html.erb +2 -0
  106. data/test/dummy_mongoid/config.ru +4 -0
  107. data/test/dummy_mongoid/config/application.rb +59 -0
  108. data/test/dummy_mongoid/config/boot.rb +10 -0
  109. data/test/dummy_mongoid/config/environment.rb +5 -0
  110. data/test/dummy_mongoid/config/environments/development.rb +37 -0
  111. data/test/dummy_mongoid/config/environments/production.rb +67 -0
  112. data/test/dummy_mongoid/config/environments/test.rb +37 -0
  113. data/test/dummy_mongoid/config/initializers/backtrace_silencers.rb +7 -0
  114. data/test/dummy_mongoid/config/initializers/inflections.rb +15 -0
  115. data/test/dummy_mongoid/config/initializers/maestrano.rb +85 -0
  116. data/test/dummy_mongoid/config/initializers/mime_types.rb +5 -0
  117. data/test/dummy_mongoid/config/initializers/secret_token.rb +7 -0
  118. data/test/dummy_mongoid/config/initializers/session_store.rb +8 -0
  119. data/test/dummy_mongoid/config/initializers/wrap_parameters.rb +14 -0
  120. data/test/dummy_mongoid/config/locales/en.yml +5 -0
  121. data/test/dummy_mongoid/config/mongoid.yml +80 -0
  122. data/test/dummy_mongoid/config/routes.rb +63 -0
  123. data/test/dummy_mongoid/db/migrate/20140526125222_create_monsters.rb +8 -0
  124. data/test/dummy_mongoid/db/migrate/20140526125242_create_admin_monsters.rb +8 -0
  125. data/test/dummy_mongoid/db/migrate/20140526144828_create_mno_monsters.rb +13 -0
  126. data/test/dummy_mongoid/db/migrate/20140526151139_create_mno_crews.rb +11 -0
  127. data/test/dummy_mongoid/db/schema.rb +44 -0
  128. data/test/dummy_mongoid/public/404.html +26 -0
  129. data/test/dummy_mongoid/public/422.html +26 -0
  130. data/test/dummy_mongoid/public/500.html +25 -0
  131. data/test/dummy_mongoid/public/favicon.ico +0 -0
  132. data/test/dummy_mongoid/script/rails +6 -0
  133. data/test/generators/group/active_record_generator_test.rb +79 -0
  134. data/test/generators/group/mongoid_generator_test.rb +76 -0
  135. data/test/generators/group_generator_test.rb +39 -0
  136. data/test/generators/install_generator_test.rb +45 -0
  137. data/test/generators/user/active_record_generator_test.rb +79 -0
  138. data/test/generators/user/mongoid_generator_test.rb +76 -0
  139. data/test/generators/user_generator_test.rb +39 -0
  140. data/test/maestrano-rails_test.rb +7 -0
  141. data/test/models/maestrano_group_via_test.rb +66 -0
  142. data/test/models/maestrano_user_via_test.rb +70 -0
  143. data/test/test_files/config/routes.rb +58 -0
  144. data/test/test_helper.rb +42 -0
  145. data/test/tmp/app/models/monster.rb +20 -0
  146. metadata +418 -0
@@ -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_activerecord/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_activerecord/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,45 @@
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/account/groups_controller" do
23
+ run_generator
24
+ assert_file "app/controllers/maestrano/account/groups_controller.rb"
25
+ end
26
+
27
+ should "create the maestrano/account/group_users_controller" do
28
+ run_generator
29
+ assert_file "app/controllers/maestrano/account/group_users_controller.rb"
30
+ end
31
+
32
+ should "create the maestrano routes" do
33
+ run_generator
34
+ match = /maestrano_routes/
35
+ assert_file "config/routes.rb", match
36
+ end
37
+
38
+ def copy_routes
39
+ routes = File.expand_path("../../test_files/config/routes.rb", __FILE__)
40
+ destination = File.join(destination_root, "config")
41
+
42
+ FileUtils.mkdir_p(destination)
43
+ FileUtils.cp routes, destination
44
+ end
45
+ 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_activerecord/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_activerecord/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_activerecord/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
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+
3
+ class MaestranoGroupViaTest < ActiveSupport::TestCase
4
+ context "class methods" do
5
+ context "maestrano_*_via" do
6
+ should "have the right field definition" do
7
+ assert_equal "provider", MnoCrew.maestrano_options[:provider]
8
+ assert_equal "uid", MnoCrew.maestrano_options[:uid]
9
+ assert MnoCrew.maestrano_options[:mapping].is_a?(Proc)
10
+ end
11
+ end
12
+
13
+ context "find_or_create_for_maestrano class method" do
14
+ should "return the MnoCrew if it exists" do
15
+ m = MnoCrew.create(name: "SomeCrew",provider: 'maestrano', uid: 'cld-1')
16
+ assert_equal m, MnoCrew.find_or_create_for_maestrano({provider: 'maestrano', uid: 'cld-1'})
17
+ end
18
+
19
+ should "not return a MnoCrew from another provider if it exists" do
20
+ m = MnoCrew.create(name: "SomeOtherCrew", provider: 'someoneelse', uid: 'usr-1')
21
+ assert_not_equal m, MnoCrew.find_or_create_for_maestrano({provider: 'maestrano', uid: 'cld-1'})
22
+ end
23
+
24
+ should "create a monster using the mapping block" do
25
+ result_hash = {
26
+ provider: 'maestrano',
27
+ uid: 'usr-2',
28
+ info: {
29
+ company_name: "Monster Co",
30
+ }
31
+ }
32
+ m = MnoCrew.find_or_create_for_maestrano(result_hash)
33
+ assert_equal result_hash[:provider], m.provider
34
+ assert_equal result_hash[:uid], m.uid
35
+ assert_equal result_hash[:info][:company_name], m.name
36
+ end
37
+ end
38
+ end
39
+
40
+ context "instance methods" do
41
+ context "maestrano?" do
42
+ should "return true if provider is maestrano and uid not null" do
43
+ m = MnoCrew.new
44
+ m.provider = 'maestrano'
45
+ m.uid = 'cld-1'
46
+ assert m.maestrano?
47
+ end
48
+
49
+ should "return false if provider is something else" do
50
+ m = MnoCrew.new
51
+ m.provider = 'somethingelse'
52
+ m.uid = 'cld-1'
53
+ assert !m.maestrano?
54
+ end
55
+
56
+ should "return false if uid is blank" do
57
+ m = MnoCrew.new
58
+ m.provider = 'maestrano'
59
+ m.uid = ''
60
+ assert !m.maestrano?
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,70 @@
1
+ require 'test_helper'
2
+
3
+ class MaestranoUserViaTest < ActiveSupport::TestCase
4
+ context "class methods" do
5
+ context "maestrano_*_via" do
6
+ should "have the right field definition" do
7
+ assert_equal "provider", MnoMonster.maestrano_options[:provider]
8
+ assert_equal "uid", MnoMonster.maestrano_options[:uid]
9
+ assert MnoMonster.maestrano_options[:mapping].is_a?(Proc)
10
+ end
11
+ end
12
+
13
+ context "find_or_create_for_maestrano class method" do
14
+ should "return the MnoMonster if it exists" do
15
+ m = MnoMonster.create(first_name: "John", last_name: "Jack", email: "monster@co.com", provider: 'maestrano', uid: 'usr-1')
16
+ assert_equal m, MnoMonster.find_or_create_for_maestrano({provider: 'maestrano', uid: 'usr-1'})
17
+ end
18
+
19
+ should "not return a MnoMonster from another provider if it exists" do
20
+ m = MnoMonster.create(first_name: "John", last_name: "Jack", email: "monster@co.com", provider: 'someoneelse', uid: 'usr-1')
21
+ assert_not_equal m, MnoMonster.find_or_create_for_maestrano({provider: 'maestrano', uid: 'usr-1'})
22
+ end
23
+
24
+ should "create a monster using the mapping block" do
25
+ result_hash = {
26
+ provider: 'maestrano',
27
+ uid: 'usr-2',
28
+ info: {
29
+ email: "monster@monst.com",
30
+ first_name: "Mon",
31
+ last_name: "Ster"
32
+ }
33
+ }
34
+ m = MnoMonster.find_or_create_for_maestrano(result_hash)
35
+ assert_equal result_hash[:provider], m.provider
36
+ assert_equal result_hash[:uid], m.uid
37
+ assert_equal result_hash[:info][:email], m.email
38
+ assert_equal result_hash[:info][:first_name], m.first_name
39
+ assert_equal result_hash[:info][:last_name], m.last_name
40
+ end
41
+ end
42
+ end
43
+
44
+ context "instance methods" do
45
+ context "maestrano?" do
46
+ should "return true if provider is maestrano and uid not null" do
47
+ m = MnoMonster.new
48
+ m.provider = 'maestrano'
49
+ m.uid = 'usr-1'
50
+ assert m.maestrano?
51
+ end
52
+
53
+ should "return false if provider is something else" do
54
+ m = MnoMonster.new
55
+ m.provider = 'somethingelse'
56
+ m.uid = 'usr-1'
57
+ assert !m.maestrano?
58
+ end
59
+
60
+ should "return false if uid is blank" do
61
+ m = MnoMonster.new
62
+ m.provider = 'maestrano'
63
+ m.uid = ''
64
+ assert !m.maestrano?
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ end