Guest User

Untitled

a guest
Feb 21st, 2018
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.27 KB | None | 0 0
  1. Index: test/unit/user_test.rb
  2. ===================================================================
  3. --- test/unit/user_test.rb (revision 0)
  4. +++ test/unit/user_test.rb (revision 0)
  5. @@ -0,0 +1,66 @@
  6. +require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
  7. +
  8. +class UserTest < Test::Unit::TestCase
  9. + fixtures :users
  10. +
  11. + def test_should_create_user
  12. + assert_difference User, :count do
  13. + user = create_user
  14. + assert !user.new_record?, "#{user.errors.full_messages.to_sentence}"
  15. + end
  16. + end
  17. +
  18. + def test_should_require_login
  19. + assert_no_difference User, :count do
  20. + u = create_user(:login => nil)
  21. + assert u.errors.on(:login)
  22. + end
  23. + end
  24. +
  25. + def test_should_require_password
  26. + assert_no_difference User, :count do
  27. + u = create_user(:password => nil)
  28. + assert u.errors.on(:password)
  29. + end
  30. + end
  31. +
  32. + def test_should_require_password_confirmation
  33. + assert_no_difference User, :count do
  34. + u = create_user(:password_confirmation => nil)
  35. + assert u.errors.on(:password_confirmation)
  36. + end
  37. + end
  38. +
  39. + def test_should_reset_password
  40. + users(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password')
  41. + assert_equal users(:quentin), User.authenticate('quentin', 'new password')
  42. + end
  43. +
  44. + def test_should_not_rehash_password
  45. + users(:quentin).update_attributes(:login => 'quentin2')
  46. + assert_equal users(:quentin), User.authenticate('quentin2', 'test')
  47. + end
  48. +
  49. + def test_should_authenticate_user
  50. + assert_equal users(:quentin), User.authenticate('quentin', 'test')
  51. + end
  52. +
  53. + def test_should_set_remember_token
  54. + users(:quentin).remember_me
  55. + assert_not_nil users(:quentin).remember_token
  56. + assert_not_nil users(:quentin).remember_token_expires_at
  57. + end
  58. +
  59. + def test_should_unset_remember_token
  60. + users(:quentin).remember_me
  61. + assert_not_nil users(:quentin).remember_token
  62. + users(:quentin).forget_me
  63. + assert_nil users(:quentin).remember_token
  64. + end
  65. +
  66. + protected
  67. + def create_user(options = {})
  68. + User.create({ :login => 'quire', :email => 'quire@example.com',
  69. + :password => 'quire', :password_confirmation => 'quire' }.merge(options))
  70. + end
  71. +end
  72. Index: test/unit/users_controller_test.rb
  73. ===================================================================
  74. --- test/unit/users_controller_test.rb (revision 0)
  75. +++ test/unit/users_controller_test.rb (revision 0)
  76. @@ -0,0 +1,52 @@
  77. +require File.dirname(__FILE__) + '/../test_helper'
  78. +require 'users_controller'
  79. +
  80. +# Re-raise errors caught by the controller.
  81. +class UsersController; def rescue_action(e) raise e end; end
  82. +
  83. +class UsersControllerTest < Test::Unit::TestCase
  84. + fixtures :users
  85. +
  86. + def setup
  87. + @controller = UsersController.new
  88. + @request = ActionController::TestRequest.new
  89. + @response = ActionController::TestResponse.new
  90. + end
  91. +
  92. + def test_should_allow_signup
  93. + assert_difference User, :count do
  94. + create_user
  95. + assert_response :redirect
  96. + end
  97. + end
  98. +
  99. + def test_should_require_login_on_signup
  100. + assert_no_difference User, :count do
  101. + create_user(:login => nil)
  102. + assert assigns(:user).errors.on(:login)
  103. + assert_response :success
  104. + end
  105. + end
  106. +
  107. + def test_should_require_password_on_signup
  108. + assert_no_difference User, :count do
  109. + create_user(:password => nil)
  110. + assert assigns(:user).errors.on(:password)
  111. + assert_response :success
  112. + end
  113. + end
  114. +
  115. + def test_should_require_password_confirmation_on_signup
  116. + assert_no_difference User, :count do
  117. + create_user(:password_confirmation => nil)
  118. + assert assigns(:user).errors.on(:password_confirmation)
  119. + assert_response :success
  120. + end
  121. + end
  122. +
  123. + protected
  124. + def create_user(options = {})
  125. + post :create, :user => { :login => 'quire', :email => 'quire@example.com',
  126. + :password => 'quire', :password_confirmation => 'quire' }.merge(options)
  127. + end
  128. +end
  129. Index: test/unit/member_test.rb
  130. ===================================================================
  131. --- test/unit/member_test.rb (revision 0)
  132. +++ test/unit/member_test.rb (revision 0)
  133. @@ -0,0 +1,52 @@
  134. +require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
  135. +
  136. +class MemberTest < Test::Unit::TestCase
  137. + fixtures :members
  138. +
  139. + def test_should_create_member
  140. + assert_difference Member, :count do
  141. + member = create_member
  142. + assert !member.new_record?, "#{member.errors.full_messages.to_sentence}"
  143. + end
  144. + end
  145. +
  146. + def test_should_require_login
  147. + assert_no_difference Member, :count do
  148. + u = create_member(:login => nil)
  149. + assert u.errors.on(:login)
  150. + end
  151. + end
  152. +
  153. + def test_should_require_password
  154. + assert_no_difference Member, :count do
  155. + u = create_member(:password => nil)
  156. + assert u.errors.on(:password)
  157. + end
  158. + end
  159. +
  160. + def test_should_require_password_confirmation
  161. + assert_no_difference Member, :count do
  162. + u = create_member(:password_confirmation => nil)
  163. + assert u.errors.on(:password_confirmation)
  164. + end
  165. + end
  166. +
  167. + def test_should_reset_password
  168. + members(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password')
  169. + assert_equal members(:quentin), Member.authenticate('quentin', 'new password')
  170. + end
  171. +
  172. + def test_should_not_rehash_password
  173. + members(:quentin).update_attributes(:login => 'quentin2')
  174. + assert_equal members(:quentin), Member.authenticate('quentin2', 'test')
  175. + end
  176. +
  177. + def test_should_authenticate_member
  178. + assert_equal members(:quentin), Member.authenticate('quentin', 'test')
  179. + end
  180. +
  181. + protected
  182. + def create_member(options = {})
  183. + Member.create({ :login => 'quire', :password => 'quire', :password_confirmation => 'quire' }.merge(options))
  184. + end
  185. +end
  186. Index: test/unit/sessions_controller_test.rb
  187. ===================================================================
  188. --- test/unit/sessions_controller_test.rb (revision 0)
  189. +++ test/unit/sessions_controller_test.rb (revision 0)
  190. @@ -0,0 +1,81 @@
  191. +require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
  192. +require 'sessions_controller'
  193. +
  194. +# Re-raise errors caught by the controller.
  195. +class SessionsController; def rescue_action(e) raise e end; end
  196. +
  197. +class SessionsControllerTest < Test::Unit::TestCase
  198. + fixtures :users
  199. +
  200. + def setup
  201. + @controller = SessionsController.new
  202. + @request = ActionController::TestRequest.new
  203. + @response = ActionController::TestResponse.new
  204. + end
  205. +
  206. + def test_should_login_and_redirect
  207. + post :create, :login => 'quentin', :password => 'test'
  208. + assert session[:user]
  209. + assert_response :redirect
  210. + end
  211. +
  212. + def test_should_fail_login_and_not_redirect
  213. + post :create, :login => 'quentin', :password => 'bad password'
  214. + assert_nil session[:user]
  215. + assert_response :success
  216. + end
  217. +
  218. + def test_should_logout
  219. + login_as :quentin
  220. + get :destroy
  221. + assert_nil session[:user]
  222. + assert_response :redirect
  223. + end
  224. +
  225. + def test_should_remember_me
  226. + post :create, :login => 'quentin', :password => 'test', :remember_me => "1"
  227. + assert_not_nil @response.cookies["auth_token"]
  228. + end
  229. +
  230. + def test_should_not_remember_me
  231. + post :create, :login => 'quentin', :password => 'test', :remember_me => "0"
  232. + assert_nil @response.cookies["auth_token"]
  233. + end
  234. +
  235. + def test_should_delete_token_on_logout
  236. + login_as :quentin
  237. + get :destroy
  238. + assert_equal @response.cookies["auth_token"], []
  239. + end
  240. +
  241. + def test_should_login_with_cookie
  242. + users(:quentin).remember_me
  243. + @request.cookies["auth_token"] = cookie_for(:quentin)
  244. + get :new
  245. + assert @controller.send(:logged_in?)
  246. + end
  247. +
  248. + def test_should_fail_cookie_login
  249. + users(:quentin).remember_me
  250. + users(:quentin).update_attribute :remember_token_expires_at, 5.minutes.ago.utc
  251. + @request.cookies["auth_token"] = cookie_for(:quentin)
  252. + get :new
  253. + assert !@controller.send(:logged_in?)
  254. + end
  255. +
  256. + def test_should_fail_cookie_login
  257. + users(:quentin).remember_me
  258. + @request.cookies["auth_token"] = auth_token('invalid_auth_token')
  259. + get :new
  260. + assert !@controller.send(:logged_in?)
  261. + end
  262. +
  263. + protected
  264. + def auth_token(token)
  265. + CGI::Cookie.new('name' => 'auth_token', 'value' => token)
  266. + end
  267. +
  268. + def cookie_for(user)
  269. + auth_token users(user).remember_token
  270. + end
  271. +end
  272. Index: test/unit/account_test.rb
  273. ===================================================================
  274. --- test/unit/account_test.rb (revision 0)
  275. +++ test/unit/account_test.rb (revision 0)
  276. @@ -0,0 +1,60 @@
  277. +require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
  278. +
  279. +class AccountTest < Test::Unit::TestCase
  280. + fixtures :accounts
  281. +
  282. + def test_should_create_account
  283. + assert_difference Account, :count do
  284. + account = create_account
  285. + assert !account.new_record?, "#{account.errors.full_messages.to_sentence}"
  286. + end
  287. + end
  288. +
  289. + def test_should_require_email
  290. + assert_no_difference Account, :count do
  291. + u = create_account(:email => nil)
  292. + assert u.errors.on(:email)
  293. + end
  294. + end
  295. +
  296. + def test_should_require_password
  297. + assert_no_difference Account, :count do
  298. + u = create_account(:password => nil)
  299. + assert u.errors.on(:password)
  300. + end
  301. + end
  302. +
  303. + def test_should_require_password_confirmation
  304. + assert_no_difference Account, :count do
  305. + u = create_account(:password_confirmation => nil)
  306. + assert u.errors.on(:password_confirmation)
  307. + end
  308. + end
  309. +
  310. + def test_should_reset_password
  311. + accounts(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password')
  312. + assert_equal accounts(:quentin), Account.authenticate('quentin@example.com', 'new password')
  313. + end
  314. +
  315. + def test_should_authenticate_account
  316. + assert_equal accounts(:quentin), Account.authenticate('quentin@example.com', 'test')
  317. + end
  318. +
  319. + def test_should_set_remember_token
  320. + accounts(:quentin).remember_me
  321. + assert_not_nil accounts(:quentin).remember_token
  322. + assert_not_nil accounts(:quentin).remember_token_expires_at
  323. + end
  324. +
  325. + def test_should_unset_remember_token
  326. + accounts(:quentin).remember_me
  327. + assert_not_nil accounts(:quentin).remember_token
  328. + accounts(:quentin).forget_me
  329. + assert_nil accounts(:quentin).remember_token
  330. + end
  331. +
  332. + protected
  333. + def create_account(options = {})
  334. + Account.create({ :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options))
  335. + end
  336. +end
  337. Index: test/test_helper.rb
  338. ===================================================================
  339. --- test/test_helper.rb (revision 0)
  340. +++ test/test_helper.rb (revision 0)
  341. @@ -0,0 +1,42 @@
  342. +$:.unshift(File.dirname(__FILE__) + '/../lib')
  343. +RAILS_ROOT = File.dirname(__FILE__) unless self.class.const_defined?("RAILS_ROOT")
  344. +
  345. +require 'rubygems'
  346. +require 'test/unit'
  347. +require 'action_controller'
  348. +require 'action_controller/test_process'
  349. +require 'active_record'
  350. +require 'active_record/fixtures'
  351. +require 'active_support/binding_of_caller'
  352. +require 'active_support/breakpoint'
  353. +require "#{File.dirname(__FILE__)}/../init"
  354. +
  355. +config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
  356. +ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
  357. +ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
  358. +
  359. +load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
  360. +
  361. +Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
  362. +$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
  363. +
  364. +ActionController::Routing::Routes.draw { |map| map.connect ':controller/:action/:id' }
  365. +
  366. +class Test::Unit::TestCase #:nodoc:
  367. + def create_fixtures(*table_names)
  368. + if block_given?
  369. + Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
  370. + else
  371. + Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
  372. + end
  373. + end
  374. +
  375. + # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
  376. + self.use_transactional_fixtures = true
  377. +
  378. + # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
  379. + self.use_instantiated_fixtures = false
  380. +
  381. + # Add more helper methods to be used by all tests here...
  382. + test_helper_authenticates :user
  383. +end
  384. Index: test/schema.rb
  385. ===================================================================
  386. --- test/schema.rb (revision 0)
  387. +++ test/schema.rb (revision 0)
  388. @@ -0,0 +1,30 @@
  389. +ActiveRecord::Schema.define(:version => 1) do
  390. +
  391. + create_table :users do |t|
  392. + t.column :login, :string
  393. + t.column :email, :string
  394. + t.column :crypted_password, :string, :limit => 40
  395. + t.column :salt, :string, :limit => 40
  396. + t.column :created_at, :datetime
  397. + t.column :updated_at, :datetime
  398. + t.column :remember_token, :string
  399. + t.column :remember_token_expires_at, :datetime
  400. + end
  401. +
  402. + create_table :accounts do |t|
  403. + t.column :email, :string
  404. + t.column :password, :string, :limit => 40
  405. + t.column :created_at, :datetime
  406. + t.column :updated_at, :datetime
  407. + t.column :remember_token, :string
  408. + t.column :remember_token_expires_at, :datetime
  409. + end
  410. +
  411. + create_table :members do |t|
  412. + t.column :login, :string
  413. + t.column :password_hash, :string, :limit => 40
  414. + t.column :created_at, :datetime
  415. + t.column :updated_at, :datetime
  416. + end
  417. +
  418. +end
  419. \ No newline at end of file
  420. Index: test/database.yml
  421. ===================================================================
  422. --- test/database.yml (revision 0)
  423. +++ test/database.yml (revision 0)
  424. @@ -0,0 +1,18 @@
  425. +sqlite:
  426. + :adapter: sqlite
  427. + :dbfile: plugin.sqlite.db
  428. +sqlite3:
  429. + :adapter: sqlite3
  430. + :dbfile: ":memory:"
  431. +postgresql:
  432. + :adapter: postgresql
  433. + :username: postgres
  434. + :password: postgres
  435. + :database: plugin_test
  436. + :min_messages: ERROR
  437. +mysql:
  438. + :adapter: mysql
  439. + :host: localhost
  440. + :username: rails
  441. + :password:
  442. + :database: plugin_test
  443. \ No newline at end of file
  444. Index: test/fixtures/accounts.yml
  445. ===================================================================
  446. --- test/fixtures/accounts.yml (revision 0)
  447. +++ test/fixtures/accounts.yml (revision 0)
  448. @@ -0,0 +1,10 @@
  449. +quentin:
  450. + id: 1
  451. + email: quentin@example.com
  452. + password: test
  453. + created_at: <%= 5.days.ago.to_s :db %>
  454. +aaron:
  455. + id: 2
  456. + email: aaron@example.com
  457. + password: test
  458. + created_at: <%= 1.days.ago.to_s :db %>
  459. \ No newline at end of file
  460. Index: test/fixtures/user.rb
  461. ===================================================================
  462. --- test/fixtures/user.rb (revision 0)
  463. +++ test/fixtures/user.rb (revision 0)
  464. @@ -0,0 +1,3 @@
  465. +class User < ActiveRecord::Base
  466. + acts_as_authenticated
  467. +end
  468. Index: test/fixtures/users.yml
  469. ===================================================================
  470. --- test/fixtures/users.yml (revision 0)
  471. +++ test/fixtures/users.yml (revision 0)
  472. @@ -0,0 +1,17 @@
  473. +quentin:
  474. + id: 1
  475. + login: quentin
  476. + email: quentin@example.com
  477. + salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
  478. + crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
  479. + #crypted_password: "ce2/iFrNtQ8=\n" # quentin, use only if you're using 2-way encryption
  480. + created_at: <%= 5.days.ago.to_s :db %>
  481. + # activated_at: <%%= 5.days.ago.to_s :db %> # only if you're activating new signups
  482. +aaron:
  483. + id: 2
  484. + login: aaron
  485. + email: aaron@example.com
  486. + salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
  487. + crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
  488. + # activation_code: aaronscode # only if you're activating new signups
  489. + created_at: <%= 1.days.ago.to_s :db %>
  490. \ No newline at end of file
  491. Index: test/fixtures/users_controller.rb
  492. ===================================================================
  493. --- test/fixtures/users_controller.rb (revision 0)
  494. +++ test/fixtures/users_controller.rb (revision 0)
  495. @@ -0,0 +1,18 @@
  496. +class UsersController < ActionController::Base
  497. + authenticates :user
  498. + before_filter :login_from_cookie
  499. +
  500. + def new
  501. + render :text => 'new'
  502. + end
  503. +
  504. + def create
  505. + @user = User.new(params[:user])
  506. + @user.save!
  507. + self.current_user = @user
  508. + redirect_back_or_default('/')
  509. + flash[:notice] = "Thanks for signing up!"
  510. + rescue ActiveRecord::RecordInvalid
  511. + render :text => 'new'
  512. + end
  513. +end
  514. Index: test/fixtures/member.rb
  515. ===================================================================
  516. --- test/fixtures/member.rb (revision 0)
  517. +++ test/fixtures/member.rb (revision 0)
  518. @@ -0,0 +1,3 @@
  519. +class Member < ActiveRecord::Base
  520. + acts_as_authenticated :password => :password_hash, :salt => "7e3041ebc2fc05a40c60028e2c4901a81035d3cd"
  521. +end
  522. Index: test/fixtures/members.yml
  523. ===================================================================
  524. --- test/fixtures/members.yml (revision 0)
  525. +++ test/fixtures/members.yml (revision 0)
  526. @@ -0,0 +1,10 @@
  527. +quentin:
  528. + id: 1
  529. + login: quentin
  530. + password_hash: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
  531. + created_at: <%= 5.days.ago.to_s :db %>
  532. +aaron:
  533. + id: 2
  534. + login: aaron
  535. + password_hash: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
  536. + created_at: <%= 1.days.ago.to_s :db %>
  537. \ No newline at end of file
  538. Index: test/fixtures/sessions_controller.rb
  539. ===================================================================
  540. --- test/fixtures/sessions_controller.rb (revision 0)
  541. +++ test/fixtures/sessions_controller.rb (revision 0)
  542. @@ -0,0 +1,25 @@
  543. +class SessionsController < ActionController::Base
  544. + authenticates :user
  545. + before_filter :login_from_cookie
  546. +
  547. + def new
  548. + render :text => 'new'
  549. + end
  550. +
  551. + def create
  552. + self.current_user = User.authenticate(params[:login], params[:password])
  553. + if logged_in?
  554. + store_login_cookie if params[:remember_me] == "1"
  555. + redirect_back_or_default('/')
  556. + flash[:notice] = "Logged in successfully"
  557. + else
  558. + render :text => 'new'
  559. + end
  560. + end
  561. +
  562. + def destroy
  563. + destroy_login_cookie
  564. + flash[:notice] = "You have been logged out."
  565. + redirect_back_or_default('/')
  566. + end
  567. +end
  568. \ No newline at end of file
  569. Index: test/fixtures/account.rb
  570. ===================================================================
  571. --- test/fixtures/account.rb (revision 0)
  572. +++ test/fixtures/account.rb (revision 0)
  573. @@ -0,0 +1,3 @@
  574. +class Account < ActiveRecord::Base
  575. + acts_as_authenticated :login => :email, :encrypt => false
  576. +end
  577. Index: Rakefile
  578. ===================================================================
  579. --- Rakefile (revision 0)
  580. +++ Rakefile (revision 0)
  581. @@ -0,0 +1,22 @@
  582. +require 'rake'
  583. +require 'rake/testtask'
  584. +require 'rake/rdoctask'
  585. +
  586. +desc 'Default: run unit tests.'
  587. +task :default => :test
  588. +
  589. +desc 'Test the acts_as_authenticated plugin.'
  590. +Rake::TestTask.new(:test) do |t|
  591. + t.libs << 'lib'
  592. + t.pattern = 'test/unit/**/*_test.rb'
  593. + t.verbose = true
  594. +end
  595. +
  596. +desc 'Generate documentation for the acts_as_authenticated plugin.'
  597. +Rake::RDocTask.new(:rdoc) do |rdoc|
  598. + rdoc.rdoc_dir = 'rdoc'
  599. + rdoc.title = 'ActsAsAuthenticated'
  600. + rdoc.options << '--line-numbers' << '--inline-source'
  601. + rdoc.rdoc_files.include('README')
  602. + rdoc.rdoc_files.include('lib/**/*.rb')
  603. +end
  604. Index: init.rb
  605. ===================================================================
  606. --- init.rb (revision 0)
  607. +++ init.rb (revision 0)
  608. @@ -0,0 +1,7 @@
  609. +require 'acts_as_authenticated'
  610. +require 'authenticated_system'
  611. +require 'authenticated_test_helper'
  612. +
  613. +ActiveRecord::Base.send :include, ActiveRecord::Acts::Authenticated
  614. +ActionController::Base.send :include, ActionController::AuthenticatedSystem
  615. +Test::Unit::TestCase.send :include, Test::Unit::AuthenticatedTestHelper
  616. Index: lib/acts_as_authenticated.rb
  617. ===================================================================
  618. --- lib/acts_as_authenticated.rb (revision 0)
  619. +++ lib/acts_as_authenticated.rb (revision 0)
  620. @@ -0,0 +1,96 @@
  621. +require 'digest/sha1'
  622. +module ActiveRecord
  623. + module Acts #:nodoc:
  624. + module Authenticated #:nodoc:
  625. + def self.included(base)
  626. + base.extend(ClassMethods)
  627. + end
  628. +
  629. + module ClassMethods
  630. + def acts_as_authenticated(options = {})
  631. + write_inheritable_attribute(:acts_as_authenticated_options, {
  632. + :login => :login,
  633. + :encrypt => true,
  634. + :password => (options[:encrypt] == false ? :password : :crypted_password),
  635. + :salt => (options[:encrypt] == false ? nil : :salt),
  636. + }.merge(options))
  637. +
  638. + class_inheritable_reader :acts_as_authenticated_options
  639. +
  640. + attr_accessor :password if acts_as_authenticated_options[:encrypt]
  641. +
  642. + validates_presence_of acts_as_authenticated_options[:login]
  643. +
  644. + validates_presence_of :password, :if => :password_required?
  645. + validates_presence_of :password_confirmation, :if => :password_required?
  646. + validates_length_of :password, :within => 4..40, :if => :password_required?, :allow_nil => :true
  647. + validates_confirmation_of :password, :if => :password_required?
  648. +
  649. + validates_uniqueness_of acts_as_authenticated_options[:login], :case_sensitive => false
  650. +
  651. + attr_protected acts_as_authenticated_options[:password], acts_as_authenticated_options[:salt] if acts_as_authenticated_options[:encrypt]
  652. + attr_protected :remember_token, :remember_token_expires_at
  653. +
  654. + before_save :encrypt_password if acts_as_authenticated_options[:encrypt]
  655. +
  656. + include ActiveRecord::Acts::Authenticated::InstanceMethods
  657. + extend ActiveRecord::Acts::Authenticated::SingletonMethods
  658. + end
  659. + end
  660. +
  661. + module SingletonMethods
  662. + def authenticate(login, password)
  663. + u = send("find_by_#{acts_as_authenticated_options[:login]}", login)
  664. + u && u.authenticated?(password) ? u : nil
  665. + end
  666. +
  667. + def encrypt(password, salt)
  668. + Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  669. + end
  670. + end
  671. +
  672. + module InstanceMethods
  673. + def encrypt(password)
  674. + return password unless acts_as_authenticated_options[:encrypt]
  675. + self.class.encrypt(password, (acts_as_authenticated_options[:salt].is_a?(String) ? acts_as_authenticated_options[:salt] : send(acts_as_authenticated_options[:salt])))
  676. + end
  677. +
  678. + def authenticated?(password)
  679. + send(acts_as_authenticated_options[:password]) == encrypt(password)
  680. + end
  681. +
  682. + def new_random_password
  683. + self.password = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{send(acts_as_authenticated_options[:login])}--")[0,8]
  684. + self.password_confirmation = self.password
  685. + end
  686. +
  687. + def remember_token?
  688. + remember_token_expires_at && Time.now.utc < remember_token_expires_at
  689. + end
  690. +
  691. + def remember_me
  692. + self.remember_token_expires_at = 2.weeks.from_now.utc
  693. + self.remember_token = encrypt("#{send(acts_as_authenticated_options[:login])}--#{remember_token_expires_at}")
  694. + save(false)
  695. + end
  696. +
  697. + def forget_me
  698. + self.remember_token_expires_at = nil
  699. + self.remember_token = nil
  700. + save(false)
  701. + end
  702. +
  703. + protected
  704. + def encrypt_password
  705. + return if password.blank?
  706. + self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{send(acts_as_authenticated_options[:login])}--") if new_record? && acts_as_authenticated_options[:salt].is_a?(Symbol)
  707. + self.send("#{acts_as_authenticated_options[:password]}=", encrypt(password))
  708. + end
  709. +
  710. + def password_required?
  711. + send(acts_as_authenticated_options[:password]).blank? || (acts_as_authenticated_options[:encrypt] ? !password.blank? : true)
  712. + end
  713. + end
  714. + end
  715. + end
  716. +end
  717. \ No newline at end of file
  718. Index: lib/authenticated_test_helper.rb
  719. ===================================================================
  720. --- lib/authenticated_test_helper.rb (revision 0)
  721. +++ lib/authenticated_test_helper.rb (revision 0)
  722. @@ -0,0 +1,117 @@
  723. +module Test
  724. + module Unit
  725. + module AuthenticatedTestHelper
  726. + def self.included(base)
  727. + base.extend(ClassMethods)
  728. + end
  729. +
  730. + module ClassMethods
  731. + def test_helper_authenticates(model_id, options = {})
  732. + singular_name = model_id.to_s
  733. + class_name = options[:class_name] || singular_name.camelize
  734. + plural_name = singular_name.pluralize
  735. +
  736. + module_eval <<-"end_eval"
  737. + def login_as(#{singular_name})
  738. + @request.session[:#{singular_name}] = #{singular_name} ? #{plural_name}(#{singular_name}).id : nil
  739. + end
  740. +
  741. + def content_type(type)
  742. + @request.env['Content-Type'] = type
  743. + end
  744. +
  745. + def accept(accept)
  746. + @request.env["HTTP_ACCEPT"] = accept
  747. + end
  748. +
  749. + def authorize_as(user)
  750. + if user
  751. + @request.env["HTTP_AUTHORIZATION"] = "Basic \#{Base64.encode64("\#{users(user).login}:test")}"
  752. + accept 'application/xml'
  753. + content_type 'application/xml'
  754. + else
  755. + @request.env["HTTP_AUTHORIZATION"] = nil
  756. + accept nil
  757. + content_type nil
  758. + end
  759. + end
  760. +
  761. + def assert_difference(object, method = nil, difference = 1)
  762. + initial_value = object.send(method)
  763. + yield
  764. + assert_equal initial_value + difference, object.send(method), "\#{object}#\#{method}"
  765. + end
  766. +
  767. + def assert_no_difference(object, method, &block)
  768. + assert_difference object, method, 0, &block
  769. + end
  770. +
  771. + def assert_requires_login(login = nil)
  772. + yield HttpLoginProxy.new(self, login)
  773. + end
  774. +
  775. + def assert_http_authentication_required(login = nil)
  776. + yield XmlLoginProxy.new(self, login)
  777. + end
  778. +
  779. + def reset!(*instance_vars)
  780. + instance_vars = [:controller, :request, :response] unless instance_vars.any?
  781. + instance_vars.collect! { |v| "@\#{v}".to_sym }
  782. + instance_vars.each do |var|
  783. + instance_variable_set(var, instance_variable_get(var).class.new)
  784. + end
  785. + end
  786. + end_eval
  787. + end
  788. + end
  789. + end
  790. +
  791. + class BaseLoginProxy
  792. + attr_reader :controller
  793. + attr_reader :options
  794. + def initialize(controller, login)
  795. + @controller = controller
  796. + @login = login
  797. + end
  798. +
  799. + private
  800. + def authenticated
  801. + raise NotImplementedError
  802. + end
  803. +
  804. + def check
  805. + raise NotImplementedError
  806. + end
  807. +
  808. + def method_missing(method, *args)
  809. + @controller.reset!
  810. + authenticate
  811. + @controller.send(method, *args)
  812. + check
  813. + end
  814. + end
  815. +
  816. + class HttpLoginProxy < BaseLoginProxy
  817. + protected
  818. + def authenticate
  819. + @controller.login_as @login if @login
  820. + end
  821. +
  822. + def check
  823. + @controller.assert_redirected_to :controller => 'sessions', :action => 'new'
  824. + end
  825. + end
  826. +
  827. + class XmlLoginProxy < BaseLoginProxy
  828. + protected
  829. + def authenticate
  830. + @controller.accept 'application/xml'
  831. + @controller.authorize_as @login if @login
  832. + end
  833. +
  834. + def check
  835. + @controller.assert_response 401
  836. + end
  837. + end
  838. + end
  839. +end
  840. \ No newline at end of file
  841. Index: lib/authenticated_system.rb
  842. ===================================================================
  843. --- lib/authenticated_system.rb (revision 0)
  844. +++ lib/authenticated_system.rb (revision 0)
  845. @@ -0,0 +1,104 @@
  846. +module ActionController
  847. + module AuthenticatedSystem #:nodoc:
  848. + def self.included(base)
  849. + base.extend(ClassMethods)
  850. + end
  851. +
  852. + module ClassMethods
  853. + def authenticates(model_id, options = {})
  854. + singular_name = model_id.to_s
  855. + class_name = options[:class_name] || singular_name.camelize
  856. + plural_name = singular_name.pluralize
  857. +
  858. + module_eval <<-"end_eval"
  859. + helper_method :current_#{singular_name}, :logged_in?
  860. +
  861. + protected
  862. + def logged_in?
  863. + (@current_#{singular_name} ||= session[:#{singular_name}] ? #{class_name}.find_by_id(session[:#{singular_name}]) : :false).is_a?(#{class_name})
  864. + end
  865. +
  866. + def current_#{singular_name}
  867. + @current_#{singular_name} if logged_in?
  868. + end
  869. +
  870. + def current_#{singular_name}=(new_#{singular_name})
  871. + session[:#{singular_name}] = (new_#{singular_name}.nil? || new_#{singular_name}.is_a?(Symbol)) ? nil : new_#{singular_name}.id
  872. + @current_#{singular_name} = new_#{singular_name}
  873. + end
  874. +
  875. + def authorized?
  876. + true
  877. + end
  878. +
  879. + def login_required
  880. + username, passwd = get_auth_data
  881. + self.current_#{singular_name} ||= #{class_name}.authenticate(username, passwd) || :false if username && passwd
  882. + logged_in? && authorized? ? true : access_denied
  883. + end
  884. +
  885. + def access_denied
  886. + respond_to do |accepts|
  887. + accepts.html do
  888. + store_location
  889. + redirect_to :controller => 'sessions', :action => 'new'
  890. + end
  891. + accepts.xml do
  892. + headers["Status"] = "Unauthorized"
  893. + headers["WWW-Authenticate"] = %(Basic realm="Web Password")
  894. + render :text => "Could't authenticate you", :status => '401 Unauthorized'
  895. + end
  896. + end
  897. + false
  898. + end
  899. +
  900. + def store_location
  901. + session[:return_to] = request.request_uri
  902. + end
  903. +
  904. + def redirect_back_or_default(default)
  905. + session[:return_to] ? redirect_to_url(session[:return_to]) : redirect_to(default)
  906. + session[:return_to] = nil
  907. + end
  908. +
  909. + def store_login_cookie
  910. + self.current_#{singular_name}.remember_me
  911. + cookies[:auth_token] = { :value => self.current_#{singular_name}.remember_token , :expires => self.current_#{singular_name}.remember_token_expires_at }
  912. + end
  913. +
  914. + def destroy_login_cookie
  915. + self.current_user.forget_me if logged_in?
  916. + cookies.delete :auth_token
  917. + reset_session
  918. + end
  919. +
  920. + def login_from_cookie
  921. + return unless cookies[:auth_token] && !logged_in?
  922. + user = #{class_name}.find_by_remember_token(cookies[:auth_token])
  923. + if user && user.remember_token?
  924. + user.remember_me
  925. + self.current_#{singular_name} = user
  926. + cookies[:auth_token] = { :value => self.current_#{singular_name}.remember_token , :expires => self.current_#{singular_name}.remember_token_expires_at }
  927. + flash[:notice] = "Logged in successfully"
  928. + end
  929. + end
  930. +
  931. + private
  932. + def get_auth_data
  933. + user, pass = nil, nil
  934. + if request.env.has_key? 'X-HTTP_AUTHORIZATION'
  935. + authdata = request.env['X-HTTP_AUTHORIZATION'].to_s.split
  936. + elsif request.env.has_key? 'HTTP_AUTHORIZATION'
  937. + authdata = request.env['HTTP_AUTHORIZATION'].to_s.split
  938. + end
  939. +
  940. + if authdata && authdata[0] == 'Basic'
  941. + user, pass = Base64.decode64(authdata[1]).split(':')[0..1]
  942. + end
  943. + return [user, pass]
  944. + end
  945. + end_eval
  946. + end
  947. + end
  948. + end
  949. +end
  950. \ No newline at end of file
Add Comment
Please, Sign In to add comment