Skip to content

Instantly share code, notes, and snippets.

@trystant
Created November 26, 2017 19:20
Show Gist options
  • Save trystant/aea2b729209b02bdb238526d971a4832 to your computer and use it in GitHub Desktop.
Save trystant/aea2b729209b02bdb238526d971a4832 to your computer and use it in GitHub Desktop.
Padrino mock login for specs
# #{Padrino.root}/spec/admin/controllers/resources_controller_spec.rb
require 'spec_helper'
describe 'admin/resources_controller' do
it 'displays the resources index for the admin' do
login_as('admin')
get "/admin/resources"
expect(last_response.body).to include("Resources | Client Admin")
end
end
# #{Padrino.root}/spec/spec_helper.rb
RACK_ENV = 'test' unless defined?(RACK_ENV)
require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
Dir[File.expand_path(File.dirname(__FILE__) + "/../app/helpers/**/*.rb")].each(&method(:require))
require 'rspec/core'
require 'rspec/padrino'
require 'active_support'
require 'active_support/core_ext/object/blank'
require 'rack/test'
RSpec.configure do |conf|
conf.include Rack::Test::Methods
conf.include RSpec::Padrino # add this
end
def app(app = nil, &blk)
@app ||= block_given? ? app.instance_eval(&blk) : app
@app ||= Padrino.application
end
# Authentication helper
def login_as(role = nil)
if (role)
Account.unrestrict_primary_key
acct = Account[:role => role] || Account.new(:name => 'Foo', :surname => role, :email => "#{role}@none.org", :role => role, :id => 1)
NewAdmin::Admin.any_instance.stub(:current_account).and_return(acct)
NewAdmin::Admin.any_instance.stub(:logged_in?).and_return(true)
NewAdmin::App.any_instance.stub(:current_account).and_return(acct)
NewAdmin::App.any_instance.stub(:logged_in?).and_return(true)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment