Sha256: 5afa2dec6c789384161651146a44bbab9edb1412e6307c2137d4ec724ac5f994

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 KB

Contents

#          Copyright (c) 2009 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.

require 'spec/helper'

$password = Digest::SHA1.hexdigest('pass')

class SpecHelperAuth < Ramaze::Controller
  map '/'
  helper :auth

  def index
    self.class.name
  end

  def session_inspect
    session.inspect
  end

  def secured
    "Secret content"
  end
  before(:secured){ login_required }
end

class SpecHelperAuthHash < SpecHelperAuth
  map '/hash'
  trait :auth_table => {
      'manveru' => $password
    }
end

class SpecHelperAuthMethod < SpecHelperAuth
  map '/method'
  trait :auth_table => :auth_table

  private

  def auth_table
    { 'manveru' => $password }
  end
end

class SpecHelperAuthLambda < SpecHelperAuth
  map '/lambda'
  trait :auth_table => lambda{
      { 'manveru' => $password }
    }
end

describe Ramaze::Helper::Auth do
  behaves_like :session, :multipart

  def procedure(prefix)
    session do |mock|
      got = mock.get("#{prefix}/secured")
      got.status.should == 302
      got['Location'].should =~ (/#{prefix}\/login$/)

      got = mock.get("#{prefix}/login")
      got.status.should == 200
      got.body.should =~ (/<form/)

      env = multipart('username' => 'manveru', 'password' => 'pass')
      got = mock.post("#{prefix}/login", env)
      got.status.should == 302
      got['Location'].should =~ (/#{prefix}\/secured/)

      got = mock.get("#{prefix}/secured")
      got.status.should == 200
      got.body.should == 'Secret content'

      got = mock.get("#{prefix}/logout")
      got.status.should == 302
      got['Location'].should =~ (/\/$/)

      got = mock.get("#{prefix}/secured")
      got.status.should == 302
      got['Location'].should =~ (/#{prefix}\/login$/)
    end
  end

  it 'authenticates by looking into a hash' do
    procedure('/hash')
  end

  it 'authenticates by looking into a lambda' do
    procedure('/lambda')
  end

  it 'authenticates by looking into a method' do
    procedure('/method')
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
Pistos-ramaze-2009.04.08 spec/ramaze/helper/auth.rb
manveru-ramaze-2009.04.01 spec/ramaze/helper/auth.rb
manveru-ramaze-2009.04.08 spec/ramaze/helper/auth.rb