Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
165 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module AuthenticationHelpers | ||
def require_authentication | ||
redirect '/login' unless logged_in? | ||
end | ||
|
||
def current_user | ||
warden_handler.user | ||
end | ||
|
||
def authenticate_user! | ||
warden_handler.authenticate! | ||
end | ||
|
||
def logout_user! | ||
warden_handler.logout | ||
end | ||
|
||
def logged_in? | ||
!warden_handler.nil? && warden_handler.authenticated? | ||
end | ||
|
||
def warden_handler | ||
request.env['warden'] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,21 @@ | ||
class Warden::Serializers::Session | ||
def serialize(user) | ||
p "----------------" | ||
p user | ||
p user.id | ||
user.nil? ? nil : user.id | ||
end | ||
|
||
def deserialize(id) | ||
p "==============#{id}" | ||
id.nil? ? nil : User.get(id) | ||
end | ||
end | ||
|
||
Warden::Strategies.add(:pickle) do | ||
Warden::Strategies.add(:bcrypt_password) do | ||
def valid? | ||
p params.inspect | ||
p params['login'] | ||
params["login"] || params["password"] | ||
params["login"] && params["password"] | ||
end | ||
|
||
def authenticate! | ||
p params.inspect | ||
p params['email'] | ||
u = User.first | ||
p u | ||
u = User.find_by_login_and_password(params["login"], params["password"]) | ||
|
||
u.nil? ? fail!("Could not log in") : success!(u) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ def before | |
|
||
def test_stuff_happens | ||
get '/', {}, { 'SERVER_NAME' => 'localhost' } | ||
p last_response | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), "test_bootstrap.rb")) | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', "wmnizr.rb")) | ||
require 'rack/test' | ||
|
||
::Wmnizr.use Rack::Session::Cookie | ||
|
||
::Wmnizr.use Warden::Manager do |manager| | ||
manager.default_strategies :bcrypt_password | ||
manager.failure_app = ::Wmnizr | ||
end | ||
|
||
class TestHosts < WmnizrTest | ||
include Rack::Test::Methods | ||
|
||
def app | ||
::Wmnizr | ||
end | ||
|
||
def before | ||
@user = User.create(:login => 'fanel', :password => 'pitulice') | ||
@post = Post.create | ||
end | ||
|
||
def test_redirected_to_login_when_requesting_admin_bits | ||
get '/admin' | ||
assert_equal 302, last_response.status | ||
|
||
get '/admin/posts' | ||
assert_equal 302, last_response.status | ||
|
||
post '/admin/posts' | ||
assert_equal 302, last_response.status | ||
|
||
get "/admin/posts/#{@post.id}" | ||
assert_equal 302, last_response.status | ||
|
||
get "/admin/posts/#{@post.id}/edit" | ||
assert_equal 302, last_response.status | ||
|
||
post "/admin/posts/#{@post.id}" | ||
assert_equal 302, last_response.status | ||
end | ||
|
||
def test_does_ok_when_logged_in | ||
# cheesy rack session id bug down here, do investigate :3 | ||
post '/login', :login => 'fanel', :password => 'pitulice' | ||
|
||
get '/admin' | ||
assert_equal 200, last_response.status | ||
|
||
get '/admin/posts' | ||
assert_equal 200, last_response.status | ||
|
||
post '/admin/posts', { :post => { :title => 'foo' }} | ||
assert_equal 302, last_response.status | ||
|
||
get "/admin/posts/#{@post.id}" | ||
assert_equal 200, last_response.status | ||
|
||
get "/admin/posts/#{@post.id}/edit" | ||
assert_equal 200, last_response.status | ||
|
||
post "/admin/posts/#{@post.id}", { :post => { :title => 'foo' }} | ||
assert_equal 302, last_response.status | ||
end | ||
|
||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), "test_bootstrap.rb")) | ||
|
||
|
||
class PostTest < WmnizrTest | ||
def test_setting_password_sets_crypted_password | ||
@user = User.new | ||
assert_equal nil, @user.crypted_password | ||
@user.password = "foobarbaz" | ||
assert_not_equal nil, @user.crypted_password | ||
end | ||
|
||
def test_can_find_user_by_login_and_password | ||
@user = User.create(:login => 'foo', :password => 'barbaz') | ||
|
||
assert_equal nil, User.find_by_login_and_password('foo', 'boo') | ||
assert_not_equal nil, User.find_by_login_and_password('foo', 'barbaz') | ||
end | ||
|
||
def test_validate_login_is_unique | ||
@user = User.create(:login => 'foo', :password => 'barbaz') | ||
@user = User.create(:login => 'foo', :password => 'barbaz') | ||
|
||
assert_equal 1, User.all.count | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters