PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_nifty_authentication_generator.rb

https://github.com/loucal/nifty-generators
Ruby | 274 lines | 232 code | 29 blank | 13 comment | 1 complexity | b0974967c951a69f6b03662b0f2dc0e7 MD5 | raw file
  1. require File.join(File.dirname(__FILE__), "test_helper.rb")
  2. class TestNiftyAuthenticationGenerator < Test::Unit::TestCase
  3. include NiftyGenerators::TestHelper
  4. # Some generator-related assertions:
  5. # assert_generated_file(name, &block) # block passed the file contents
  6. # assert_directory_exists(name)
  7. # assert_generated_class(name, &block)
  8. # assert_generated_module(name, &block)
  9. # assert_generated_test_for(name, &block)
  10. # The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
  11. # assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
  12. #
  13. # Other helper methods are:
  14. # app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
  15. # bare_setup - place this in setup method to create the APP_ROOT folder for each test
  16. # bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
  17. context "" do # empty context so we can use setup block
  18. setup do
  19. Rails.stubs(:version).returns("2.0.2")
  20. Dir.mkdir("#{RAILS_ROOT}/config") unless File.exists?("#{RAILS_ROOT}/config")
  21. File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
  22. f.puts "ActionController::Routing::Routes.draw do |map|\n\nend"
  23. end
  24. Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
  25. Dir.mkdir("#{RAILS_ROOT}/app/controllers") unless File.exists?("#{RAILS_ROOT}/app/controllers")
  26. File.open("#{RAILS_ROOT}/app/controllers/application.rb", 'w') do |f|
  27. f.puts "class Application < ActionController::Base\n\nend"
  28. end
  29. end
  30. teardown do
  31. FileUtils.rm_rf "#{RAILS_ROOT}/config"
  32. FileUtils.rm_rf "#{RAILS_ROOT}/app"
  33. end
  34. context "generator without arguments" do
  35. rails_generator :nifty_authentication
  36. should_generate_file 'app/models/user.rb'
  37. should_generate_file 'app/controllers/users_controller.rb'
  38. should_generate_file 'app/helpers/users_helper.rb'
  39. should_generate_file 'app/views/users/new.html.erb'
  40. should_generate_file 'app/controllers/sessions_controller.rb'
  41. should_generate_file 'app/helpers/sessions_helper.rb'
  42. should_generate_file 'app/views/sessions/new.html.erb'
  43. should_generate_file 'lib/authentication.rb'
  44. should_generate_file 'test/fixtures/users.yml'
  45. should_generate_file 'test/unit/user_test.rb'
  46. should_generate_file 'test/functional/users_controller_test.rb'
  47. should_generate_file 'test/functional/sessions_controller_test.rb'
  48. should "generate migration file" do
  49. assert !Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
  50. end
  51. should "generate routes" do
  52. assert_generated_file "config/routes.rb" do |body|
  53. assert_match "map.resources :sessions", body
  54. assert_match "map.resources :users", body
  55. assert_match "map.login 'login', :controller => 'sessions', :action => 'new'", body
  56. assert_match "map.logout 'logout', :controller => 'sessions', :action => 'destroy'", body
  57. assert_match "map.signup 'signup', :controller => 'users', :action => 'new'", body
  58. end
  59. end
  60. should "include Authentication" do
  61. application_controller_name = Rails.version >= '2.3.0' ? 'application_controller' : 'application'
  62. assert_generated_file "app/controllers/#{application_controller_name}.rb" do |body|
  63. assert_match "include Authentication", body
  64. end
  65. end
  66. end
  67. context "generator with user and session names" do
  68. rails_generator :nifty_authentication, "Account", "CurrentSession"
  69. should_generate_file 'app/models/account.rb'
  70. should_generate_file 'app/controllers/accounts_controller.rb'
  71. should_generate_file 'app/helpers/accounts_helper.rb'
  72. should_generate_file 'app/views/accounts/new.html.erb'
  73. should_generate_file 'app/controllers/current_sessions_controller.rb'
  74. should_generate_file 'app/helpers/current_sessions_helper.rb'
  75. should_generate_file 'app/views/current_sessions/new.html.erb'
  76. should_generate_file 'test/fixtures/accounts.yml'
  77. should_generate_file 'test/unit/account_test.rb'
  78. should_generate_file 'test/functional/accounts_controller_test.rb'
  79. should_generate_file 'test/functional/current_sessions_controller_test.rb'
  80. should "generate routes" do
  81. assert_generated_file "config/routes.rb" do |body|
  82. assert_match "map.resources :current_sessions", body
  83. assert_match "map.resources :accounts", body
  84. assert_match "map.login 'login', :controller => 'current_sessions', :action => 'new'", body
  85. assert_match "map.logout 'logout', :controller => 'current_sessions', :action => 'destroy'", body
  86. assert_match "map.signup 'signup', :controller => 'accounts', :action => 'new'", body
  87. end
  88. end
  89. end
  90. context "generator with shoulda option" do
  91. rails_generator :nifty_authentication, :test_framework => :shoulda
  92. should "have controller and model tests using shoulda syntax" do
  93. assert_generated_file "test/functional/users_controller_test.rb" do |body|
  94. assert_match " should ", body
  95. end
  96. assert_generated_file "test/functional/sessions_controller_test.rb" do |body|
  97. assert_match " should ", body
  98. end
  99. assert_generated_file "test/unit/user_test.rb" do |body|
  100. assert_match " should ", body
  101. end
  102. end
  103. end
  104. context "generator with rspec option" do
  105. rails_generator :nifty_authentication, :test_framework => :rspec
  106. should_generate_file 'spec/fixtures/users.yml'
  107. end
  108. context "with spec dir" do
  109. setup do
  110. Dir.mkdir("#{RAILS_ROOT}/spec") unless File.exists?("#{RAILS_ROOT}/spec")
  111. end
  112. teardown do
  113. FileUtils.rm_rf "#{RAILS_ROOT}/spec"
  114. end
  115. context "generator without arguments" do
  116. rails_generator :nifty_authentication
  117. should_generate_file 'spec/fixtures/users.yml'
  118. should_generate_file 'spec/models/user_spec.rb'
  119. should_generate_file 'spec/controllers/users_controller_spec.rb'
  120. should_generate_file 'spec/controllers/sessions_controller_spec.rb'
  121. end
  122. context "generator with user and session names" do
  123. rails_generator :nifty_authentication, "Account", "CurrentSessions"
  124. should_generate_file 'spec/fixtures/accounts.yml'
  125. should_generate_file 'spec/models/account_spec.rb'
  126. should_generate_file 'spec/controllers/accounts_controller_spec.rb'
  127. should_generate_file 'spec/controllers/current_sessions_controller_spec.rb'
  128. end
  129. context "generator with testunit option" do
  130. rails_generator :nifty_authentication, :test_framework => :testunit
  131. should_generate_file 'test/fixtures/users.yml'
  132. end
  133. end
  134. context "generator with haml option" do
  135. rails_generator :nifty_authentication, :haml => true
  136. should_generate_file "app/views/users/new.html.haml"
  137. should_generate_file "app/views/sessions/new.html.haml"
  138. end
  139. context "generator with authlogic option and custom account name" do
  140. rails_generator :nifty_authentication, "Account", :authlogic => true
  141. should_generate_file 'app/models/account.rb'
  142. should_generate_file 'app/controllers/accounts_controller.rb'
  143. should_generate_file 'app/helpers/accounts_helper.rb'
  144. should_generate_file 'app/views/accounts/new.html.erb'
  145. should_generate_file 'app/controllers/account_sessions_controller.rb'
  146. should_generate_file 'app/helpers/account_sessions_helper.rb'
  147. should_generate_file 'app/views/account_sessions/new.html.erb'
  148. should_generate_file 'test/fixtures/accounts.yml'
  149. should_generate_file 'test/unit/account_test.rb'
  150. should_generate_file 'test/functional/accounts_controller_test.rb'
  151. should_generate_file 'test/functional/account_sessions_controller_test.rb'
  152. should_generate_file 'lib/authentication.rb'
  153. should "only include acts_as_authentic in account model" do
  154. assert_generated_file "app/models/account.rb" do |body|
  155. assert_match "acts_as_authentic", body
  156. assert_no_match(/validates/, body)
  157. assert_no_match(/def/, body)
  158. end
  159. end
  160. should "should generate authentication module with current_account_session method" do
  161. assert_generated_file "lib/authentication.rb" do |body|
  162. assert_match "def current_account_session", body
  163. end
  164. end
  165. should "should generate AccountSession model" do
  166. assert_generated_file "app/models/account_session.rb" do |body|
  167. assert_match "class AccountSession < Authlogic::Session::Base", body
  168. end
  169. end
  170. should "should generate restful style actions in sessions controller" do
  171. assert_generated_file "app/controllers/account_sessions_controller.rb" do |body|
  172. assert_match "AccountSession.new", body
  173. end
  174. end
  175. should "should generate form for account session" do
  176. assert_generated_file "app/views/account_sessions/new.html.erb" do |body|
  177. assert_match "form_for @account_session", body
  178. end
  179. end
  180. should "should not include tests in account since authlogic is already tested" do
  181. assert_generated_file "test/unit/account_test.rb" do |body|
  182. assert_no_match(/def test/, body)
  183. end
  184. end
  185. should "should handle session controller tests differently" do
  186. assert_generated_file "test/functional/account_sessions_controller_test.rb" do |body|
  187. assert_match "AccountSession.find", body
  188. end
  189. end
  190. should "generate migration with authlogic columns" do
  191. file = Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").first
  192. assert file, "migration file doesn't exist"
  193. assert_match(/[0-9]+_create_accounts.rb$/, file)
  194. assert_generated_file "db/migrate/#{File.basename(file)}" do |body|
  195. assert_match "class CreateAccounts", body
  196. assert_match "t.string :username", body
  197. assert_match "t.string :email", body
  198. assert_match "t.string :crypted_password", body
  199. assert_match "t.string :password_salt", body
  200. assert_match "t.string :persistence_token", body
  201. assert_match "t.timestamps", body
  202. assert_no_match(/password_hash/, body)
  203. end
  204. end
  205. end
  206. context "generator with authlogic option and custom account and session name" do
  207. rails_generator :nifty_authentication, "Account", "UserSession", :authlogic => true
  208. should_generate_file 'app/controllers/user_sessions_controller.rb'
  209. should_generate_file 'app/helpers/user_sessions_helper.rb'
  210. should_generate_file 'app/views/user_sessions/new.html.erb'
  211. should_generate_file 'test/functional/user_sessions_controller_test.rb'
  212. should "should generate authentication module with current_user_session method" do
  213. assert_generated_file "lib/authentication.rb" do |body|
  214. assert_match "def current_user_session", body
  215. end
  216. end
  217. should "should generate UserSession model" do
  218. assert_generated_file "app/models/user_session.rb" do |body|
  219. assert_match "class UserSession < Authlogic::Session::Base", body
  220. end
  221. end
  222. should "should generate restful style actions in sessions controller" do
  223. assert_generated_file "app/controllers/user_sessions_controller.rb" do |body|
  224. assert_match "UserSession.new", body
  225. end
  226. end
  227. should "should generate form for user session" do
  228. assert_generated_file "app/views/user_sessions/new.html.erb" do |body|
  229. assert_match "form_for @user_session", body
  230. end
  231. end
  232. should "should handle session controller tests differently" do
  233. assert_generated_file "test/functional/user_sessions_controller_test.rb" do |body|
  234. assert_match "UserSession.find", body
  235. end
  236. end
  237. end
  238. end
  239. end