Cover unauthenticated path in user_token controller spec

This commit is contained in:
Marcin Kulik 2012-12-09 21:24:57 +01:00
parent 11bab12afb
commit 4a9036a9d9

View File

@ -2,50 +2,70 @@ require 'spec_helper'
describe UserTokensController do
describe '#create' do
let(:user) { FactoryGirl.create(:user) }
let(:user_token) { FactoryGirl.build(:user_token, :user => nil) }
before do
login_as user
user.stub!(:add_user_token => user_token)
@controller.should_receive(:ensure_authenticated!)
context 'for guest user' do
before do
get :create, :user_token => user_token.token
end
it { should redirect_to(login_path) }
specify { flash[:notice].should =~ /login first/ }
end
context 'when given token is valid' do
context 'for authenticated user' do
let(:user) { FactoryGirl.create(:user) }
before do
user_token.stub!(:valid? => true)
login_as user
user.stub!(:add_user_token => user_token)
@controller.should_receive(:ensure_authenticated!)
end
it 'calls Asciicast.assign_user' do
Asciicast.should_receive(:assign_user).
with(user_token.token, user).
and_return(1)
context 'when given token is valid' do
let(:claimed) { 5 }
post :create, :user_token => user_token.token
before do
user_token.stub!(:valid? => true)
Asciicast.should_receive(:assign_user).
with(user_token.token, user).
and_return(claimed)
get :create, :user_token => user_token.token
end
it { should redirect_to(profile_path(user)) }
context 'when 0 asciicasts were claimed' do
let(:claimed) { 0 }
specify { flash[:notice].should =~ /Authenticated/ }
end
context 'when more than 0 asciicasts were claimed' do
let(:claimed) { 1 }
specify { flash[:notice].should =~ /Claimed #{claimed}/ }
end
end
it 'redirects to ~nickname' do
post :create, :user_token => user_token.token
context 'when given token is invalid' do
before do
user_token.stub!(:valid? => false)
end
response.should redirect_to(profile_path(user))
end
end
it "doesn't call Asciicast.assign_user" do
Asciicast.should_not_receive(:assign_user)
context 'when given token is invalid' do
before do
user_token.stub!(:valid? => false)
end
get :create, :user_token => user_token.token
end
it 'calls Asciicast.assign_user' do
Asciicast.should_not_receive(:assign_user)
it 'renders :error' do
get :create, :user_token => user_token.token
post :create, :user_token => user_token.token
end
it 'renders :error' do
post :create, :user_token => user_token.token
response.should render_template(:error)
response.should render_template(:error)
end
end
end
end