diff --git a/app/controllers/asciicasts_controller.rb b/app/controllers/asciicasts_controller.rb index c003e1c..ff6bf35 100644 --- a/app/controllers/asciicasts_controller.rb +++ b/app/controllers/asciicasts_controller.rb @@ -53,9 +53,12 @@ class AsciicastsController < ApplicationController end def update - @asciicast.update_attributes(params[:asciicast]) - redirect_to asciicast_path(@asciicast), - :notice => 'Asciicast was updated.' + if @asciicast.update_attributes(params[:asciicast]) + redirect_to asciicast_path(@asciicast), + :notice => 'Asciicast was updated.' + else + render :edit + end end def destroy diff --git a/spec/controllers/asciicasts_controller_spec.rb b/spec/controllers/asciicasts_controller_spec.rb index 3882b02..7b83021 100644 --- a/spec/controllers/asciicasts_controller_spec.rb +++ b/spec/controllers/asciicasts_controller_spec.rb @@ -1,5 +1,15 @@ require 'spec_helper' +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 + describe AsciicastsController do let(:user) { stub_model(User, :nickname => 'nick') } let(:asciicast) { stub_model(Asciicast, :id => 666) } @@ -7,67 +17,214 @@ describe AsciicastsController do subject { response } describe '#index' do + 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) + + AsciicastDecorator.should_receive(:decorate).with(asciicasts). + 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 } + before do - get :index + Asciicast.should_receive(:popular_paginated). + with(page, an_instance_of(Fixnum)).and_return(asciicasts) + + AsciicastDecorator.should_receive(:decorate).with(asciicasts). + and_return(asciicasts) + + get :popular, :page => page end it { should be_success } + specify { assigns(:asciicasts).should == asciicasts } + specify { assigns(:category_name).should =~ /^Popular/ } + specify { assigns(:current_category).should == :popular } end describe '#show' do before do Asciicast.should_receive(:find).and_return(asciicast) - get :show, :id => asciicast.id + + asciicast.title = 'some tit' end - it { should be_success } + context 'for html request' do + before do + AsciicastDecorator.should_receive(:new).with(asciicast). + and_return(asciicast) + + 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) + + get :show, :id => asciicast.id, :format => :json + end + + it { should be_success } + end end describe '#edit' do + let(:make_request) { get :edit, :id => asciicast.id } + before do Asciicast.should_receive(:find).and_return(asciicast) asciicast.user = user - login_as user - get :edit, :id => asciicast.id end - it { should be_success } + 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 end describe '#update' do + let(:make_request) { put :update, :id => asciicast.id, :asciicast => { } } + before do Asciicast.should_receive(:find).and_return(asciicast) asciicast.user = user - login_as user - put :update, :id => asciicast.id, :asciicast => { } end - it { should redirect_to(asciicast_path(asciicast)) } + 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 end describe '#destroy' do + let(:make_request) { delete :destroy, :id => asciicast.id } + before do Asciicast.should_receive(:find).and_return(asciicast) asciicast.user = user - login_as user end - context 'when destroy succeeds' do + 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 before do - asciicast.should_receive(:destroy).and_return(true) - delete :destroy, :id => asciicast.id + make_request end - it { should redirect_to(profile_path(user)) } + it_should_behave_like 'guest user trying to modify' end - context 'when destroy fails' do + context 'for other user' do before do - asciicast.should_receive(:destroy).and_return(false) - delete :destroy, :id => asciicast.id + login_as stub_model(User) + make_request end - it { should redirect_to(asciicast_path(asciicast)) } + it_should_behave_like 'non-owner user trying to modify' end end end