Sha256: de6bc8674f8ff0353767d66c39bbad919118c802f4770fef463b59f7e2a6770e

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

require 'spec_helper'
require 'support/controllers/controller_helpers'

# Matcher that asserts user was denied access.
RSpec::Matchers.define :deny_access do
  match do |controller|
    redirects_to_sign_in?(controller) && sets_flash?(controller)
  end

  def redirects_to_sign_in?(controller)
    expect(controller).to redirect_to(controller.sign_in_url)
  end

  def sets_flash?(controller)
    controller.flash[:notice].match(/sign in to continue/)
  end
end

# A dummy 'secured' controller to test
class SecuredAppsController < ActionController::Base
  include Authenticate::Controller
  before_action :require_authentication, only: :show

  def new
    head :ok
  end

  def show
    head :ok
  end
end

describe SecuredAppsController, type: :controller do
  before do
    Rails.application.routes.draw do
      resource :secured_app, only: [:new, :show]
      get '/sign_in' => 'authenticate/sessions#new', as: 'sign_in'
    end
  end

  after do
    Rails.application.reload_routes!
  end

  context 'with authenticated user' do
    before { sign_in }

    it 'allows access to new' do
      do_get :new
      expect(subject).to_not deny_access
    end

    it 'allows access to show' do
      do_get :show
      expect(subject).to_not deny_access
    end
  end

  context 'with an unauthenticated visitor' do
    it 'allows access to new' do
      do_get :new
      expect(subject).to_not deny_access
    end

    it 'denies access to show' do
      do_get :show
      expect(subject).to deny_access
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
authenticate-0.6.1 spec/controllers/secured_controller_spec.rb
authenticate-0.6.0 spec/controllers/secured_controller_spec.rb