asciinema.org/spec/strategies/auth_cookie_strategy_spec.rb

69 lines
1.3 KiB
Ruby
Raw Normal View History

2014-08-30 17:38:47 +00:00
require 'rails_helper'
2014-02-20 22:00:44 +00:00
require 'rack/mock'
describe AuthCookieStrategy do
let(:strategy) { described_class.new(env) }
let(:env) { Rack::MockRequest.env_for('', 'HTTP_COOKIE' => cookies) }
describe '#valid?' do
subject { strategy.valid? }
context "when auth_token is present in cookies" do
let(:cookies) { 'auth_token=abc' }
it { should be(true) }
end
context "when auth_token isn't present in cookies" do
let(:cookies) { '' }
it { should be(false) }
end
end
describe '#authenticate!' do
subject { strategy.authenticate! }
let(:cookies) { "auth_token=#{auth_token}" }
2014-02-20 22:14:36 +00:00
let(:auth_token) { "yadayadayada" }
2014-02-20 22:00:44 +00:00
2014-02-20 22:14:36 +00:00
before do
allow(User).to receive(:for_auth_token).with(auth_token) { user }
end
context "when user exists for given token" do
let(:user) { User.new }
2014-02-20 22:00:44 +00:00
it "halts the chain" do
subject
expect(strategy).to be_halted
end
it "sets the proper user" do
subject
expect(strategy.user).to eq(user)
end
end
2014-02-20 22:14:36 +00:00
context "when user doesn't exist for given token" do
let(:user) { nil }
2014-02-20 22:00:44 +00:00
it "doesn't halt the chain" do
subject
expect(strategy).to_not be_halted
end
it "doesn't set user" do
subject
expect(strategy.user).to be(nil)
end
end
end
end