Sha256: 615d9da39957dc78e2b52724ed0bce7cdde3d601b6577755253cb497f30c6eac

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'spec_helper'

class Controller
  include Authentication
  attr_accessor :session

  def initialize
    self.session = {}
  end

  def find_current_user success = true
    if session[:current_user_id]
      true
    else
      nil
    end
  end
end

class User
  def initialize(attributes)
    @attributes = attributes
  end
  def id
    @attributes[:id]
  end
end

describe Authentication do
  let(:controller) { Controller.new }
  let(:user)       { User.new(id: 300) }

  describe '#login!' do
    context 'with nil' do
      it "should raise Unauthenticated" do
        expect do
          controller.login! nil
        end.to raise_error Authentication::Unauthenticated
      end
    end
  end

  describe '#current_user' do
    context 'when logged in' do
      it "should return user" do
        controller.login! user
        controller.current_user.should == user
      end
    end
    context 'when not logged in' do
      it "should call #find_current_user and return its result" do
        controller.should_receive(:find_current_user).and_return "something"
        controller.current_user.should == "something"
      end
    end
  end

  describe '#current_user_id' do
    context 'when logged in' do
      it "should return user_id" do
        controller.login! user
        controller.current_user_id.should == user.id
      end
    end
    context 'when not logged in' do
      it "should return nil" do
        controller.current_user_id.should == nil
      end
    end
  end

  describe '#logged_in?' do
    context 'when logged in' do
      it "should return true" do
        controller.login! user
        controller.logged_in?.should == true
      end
    end
  end

  describe '#logout!' do
    context 'when logged in' do
      it "should set current_user to nil" do
        controller.login! user
        controller.logout!
        controller.current_user.should == nil
        controller.session[:current_user_id].should == nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authentication-0.0.1 spec/lib/authentication_spec.rb