2011-11-23 21:30:09 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2012-12-09 20:25:45 +00:00
|
|
|
shared_examples_for 'guest user trying to modify' do
|
|
|
|
it { should redirect_to(login_path) }
|
|
|
|
specify { flash[:notice].should =~ /login first/ }
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'non-owner user trying to modify' do
|
|
|
|
it { should redirect_to(asciicast_path(asciicast)) }
|
|
|
|
specify { flash[:alert].should =~ /can't/ }
|
|
|
|
end
|
|
|
|
|
2011-11-23 21:30:09 +00:00
|
|
|
describe AsciicastsController do
|
2012-12-01 19:38:22 +00:00
|
|
|
let(:user) { stub_model(User, :nickname => 'nick') }
|
2012-04-09 21:44:10 +00:00
|
|
|
let(:asciicast) { stub_model(Asciicast, :id => 666) }
|
2011-11-23 21:30:09 +00:00
|
|
|
|
2012-04-09 21:44:10 +00:00
|
|
|
subject { response }
|
|
|
|
|
|
|
|
describe '#index' do
|
2012-12-09 20:25:45 +00:00
|
|
|
let(:asciicasts) { [double('asciicast')] }
|
|
|
|
let(:page) { double('page').to_s }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Asciicast.should_receive(:newest_paginated).
|
|
|
|
with(page, an_instance_of(Fixnum)).and_return(asciicasts)
|
|
|
|
|
2013-05-27 19:23:46 +00:00
|
|
|
PaginatingDecorator.should_receive(:new).with(asciicasts).
|
2012-12-09 20:25:45 +00:00
|
|
|
and_return(asciicasts)
|
|
|
|
|
|
|
|
get :index, :page => page
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should be_success }
|
|
|
|
specify { assigns(:asciicasts).should == asciicasts }
|
|
|
|
specify { assigns(:category_name).should =~ /^All/ }
|
|
|
|
specify { assigns(:current_category).should == :all }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#popular' do
|
|
|
|
let(:asciicasts) { [double('asciicast')] }
|
|
|
|
let(:page) { double('page').to_s }
|
|
|
|
|
2012-04-09 21:44:10 +00:00
|
|
|
before do
|
2012-12-09 20:25:45 +00:00
|
|
|
Asciicast.should_receive(:popular_paginated).
|
|
|
|
with(page, an_instance_of(Fixnum)).and_return(asciicasts)
|
|
|
|
|
2013-05-27 19:23:46 +00:00
|
|
|
PaginatingDecorator.should_receive(:new).with(asciicasts).
|
2012-12-09 20:25:45 +00:00
|
|
|
and_return(asciicasts)
|
|
|
|
|
|
|
|
get :popular, :page => page
|
2012-04-09 21:44:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it { should be_success }
|
2012-12-09 20:25:45 +00:00
|
|
|
specify { assigns(:asciicasts).should == asciicasts }
|
|
|
|
specify { assigns(:category_name).should =~ /^Popular/ }
|
|
|
|
specify { assigns(:current_category).should == :popular }
|
2012-04-09 21:44:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#show' do
|
|
|
|
before do
|
|
|
|
Asciicast.should_receive(:find).and_return(asciicast)
|
2012-12-09 20:25:45 +00:00
|
|
|
|
|
|
|
asciicast.title = 'some tit'
|
2012-04-09 21:44:10 +00:00
|
|
|
end
|
|
|
|
|
2012-12-09 20:25:45 +00:00
|
|
|
context 'for html request' do
|
2013-06-13 20:26:27 +00:00
|
|
|
let(:view_counter) { mock('view_counter') }
|
|
|
|
|
2012-12-09 20:25:45 +00:00
|
|
|
before do
|
|
|
|
AsciicastDecorator.should_receive(:new).with(asciicast).
|
|
|
|
and_return(asciicast)
|
2013-06-13 20:26:27 +00:00
|
|
|
ViewCounter.should_receive(:new).with(asciicast, cookies).
|
|
|
|
and_return(view_counter)
|
|
|
|
view_counter.should_receive(:increment)
|
2012-12-09 20:25:45 +00:00
|
|
|
|
|
|
|
get :show, :id => asciicast.id, :format => :html
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should be_success }
|
|
|
|
specify { assigns(:asciicast).should == asciicast }
|
|
|
|
specify { assigns(:title).should == asciicast.title }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for json request' do
|
|
|
|
let(:asciicast) { FactoryGirl.build(:asciicast, :id => 666) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
AsciicastJSONDecorator.should_receive(:new).with(asciicast).
|
|
|
|
and_return(asciicast)
|
2013-06-13 20:26:27 +00:00
|
|
|
ViewCounter.should_not_receive(:new)
|
2012-12-09 20:25:45 +00:00
|
|
|
|
|
|
|
get :show, :id => asciicast.id, :format => :json
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should be_success }
|
|
|
|
end
|
2012-04-09 21:44:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#edit' do
|
2012-12-09 20:25:45 +00:00
|
|
|
let(:make_request) { get :edit, :id => asciicast.id }
|
|
|
|
|
2012-04-09 21:44:10 +00:00
|
|
|
before do
|
|
|
|
Asciicast.should_receive(:find).and_return(asciicast)
|
|
|
|
asciicast.user = user
|
|
|
|
end
|
|
|
|
|
2012-12-09 20:25:45 +00:00
|
|
|
context 'for owner user' do
|
|
|
|
before do
|
|
|
|
login_as user
|
|
|
|
make_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should be_success }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for guest user' do
|
|
|
|
before do
|
|
|
|
make_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like 'guest user trying to modify'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for other user' do
|
|
|
|
before do
|
|
|
|
login_as stub_model(User)
|
|
|
|
make_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like 'non-owner user trying to modify'
|
|
|
|
end
|
2012-04-09 21:44:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#update' do
|
2012-12-09 20:25:45 +00:00
|
|
|
let(:make_request) { put :update, :id => asciicast.id, :asciicast => { } }
|
|
|
|
|
2012-04-09 21:44:10 +00:00
|
|
|
before do
|
|
|
|
Asciicast.should_receive(:find).and_return(asciicast)
|
|
|
|
asciicast.user = user
|
|
|
|
end
|
|
|
|
|
2012-12-09 20:25:45 +00:00
|
|
|
context 'for owner user' do
|
|
|
|
before do
|
|
|
|
login_as user
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when update succeeds' do
|
|
|
|
before do
|
|
|
|
asciicast.should_receive(:update_attributes).and_return(true)
|
|
|
|
make_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should redirect_to(asciicast_path(asciicast)) }
|
|
|
|
specify { flash[:notice].should =~ /was updated/ }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when update fails' do
|
|
|
|
before do
|
|
|
|
asciicast.should_receive(:update_attributes).and_return(false)
|
|
|
|
make_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should render_template(:edit) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for guest user' do
|
|
|
|
before do
|
|
|
|
make_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like 'guest user trying to modify'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for other user' do
|
|
|
|
before do
|
|
|
|
login_as stub_model(User)
|
|
|
|
make_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like 'non-owner user trying to modify'
|
|
|
|
end
|
2012-04-09 21:44:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#destroy' do
|
2012-12-09 20:25:45 +00:00
|
|
|
let(:make_request) { delete :destroy, :id => asciicast.id }
|
|
|
|
|
2012-04-09 21:44:10 +00:00
|
|
|
before do
|
|
|
|
Asciicast.should_receive(:find).and_return(asciicast)
|
|
|
|
asciicast.user = user
|
|
|
|
end
|
|
|
|
|
2012-12-09 20:25:45 +00:00
|
|
|
context 'for owner user' do
|
|
|
|
before do
|
|
|
|
login_as user
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when destroy succeeds' do
|
|
|
|
before do
|
|
|
|
asciicast.should_receive(:destroy).and_return(true)
|
|
|
|
make_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should redirect_to(profile_path(user)) }
|
|
|
|
specify { flash[:notice].should =~ /was deleted/ }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when destroy fails' do
|
|
|
|
before do
|
|
|
|
asciicast.should_receive(:destroy).and_return(false)
|
|
|
|
make_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should redirect_to(asciicast_path(asciicast)) }
|
|
|
|
specify { flash[:alert].should =~ /again/ }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for guest user' do
|
2012-04-09 21:44:10 +00:00
|
|
|
before do
|
2012-12-09 20:25:45 +00:00
|
|
|
make_request
|
2012-04-09 21:44:10 +00:00
|
|
|
end
|
|
|
|
|
2012-12-09 20:25:45 +00:00
|
|
|
it_should_behave_like 'guest user trying to modify'
|
2012-04-09 21:44:10 +00:00
|
|
|
end
|
|
|
|
|
2012-12-09 20:25:45 +00:00
|
|
|
context 'for other user' do
|
2012-04-09 21:44:10 +00:00
|
|
|
before do
|
2012-12-09 20:25:45 +00:00
|
|
|
login_as stub_model(User)
|
|
|
|
make_request
|
2012-04-09 21:44:10 +00:00
|
|
|
end
|
|
|
|
|
2012-12-09 20:25:45 +00:00
|
|
|
it_should_behave_like 'non-owner user trying to modify'
|
2012-04-09 21:44:10 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-23 21:30:09 +00:00
|
|
|
end
|