From c890aea68dc5c7f65e787aeed3a8f8b5c8857786 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Fri, 9 May 2014 14:44:01 +0200 Subject: [PATCH 1/3] Conference Controller tests --- .../conferences_controller_spec.rb | 227 ++++++++++++++++++ spec/factories/role.rb | 16 ++ spec/factories/users.rb | 12 + spec/spec_helper.rb | 3 + 4 files changed, 258 insertions(+) create mode 100644 spec/controllers/conferences_controller_spec.rb create mode 100644 spec/factories/role.rb diff --git a/spec/controllers/conferences_controller_spec.rb b/spec/controllers/conferences_controller_spec.rb new file mode 100644 index 00000000..ae364027 --- /dev/null +++ b/spec/controllers/conferences_controller_spec.rb @@ -0,0 +1,227 @@ +#!/bin/env ruby +# encoding: utf-8 +require 'spec_helper' + +describe Admin::ConferenceController do + + shared_examples 'access as administration or organizer' do + + describe 'PATCH #update' do + context 'valid attributes' do + it 'locates the requested @conference' do + patch :update, id: @conference.short_title, conference: + attributes_for(:conference, title: 'Example Con') + + expect(assigns(:conference)).to eq(@conference) + end + + it 'changes @conference attributes' do + patch :update, id: @conference.short_title, conference: + attributes_for(:conference, title: 'Example Con', + short_title: 'ExCon') + + @conference.reload + expect(@conference.title).to eq('Example Con') + expect(@conference.short_title).to eq('ExCon') + end + + it 'redirects to the updated @conference' do + patch :update, id: @conference.short_title, conference: + attributes_for(:conference, title: 'Example Con') + + expect(response).to redirect_to admin_conference_path( + @conference.short_title) + end + end + + context 'invalid attributes' do + it 'does not change conference attributes' do + patch :update, id: @conference.short_title, conference: + attributes_for(:conference, title: 'Example Con', + short_title: nil) + + @conference.reload + expect(@conference.title).to eq('The dog and pony show') + expect(@conference.short_title).to eq('dps14') + end + + it 're-renders the #show template' do + patch :update, id: @conference.short_title, conference: + attributes_for(:conference, title: 'Example Con', + short_title: nil) + + expect(response).to redirect_to admin_conference_path( + @conference.short_title) + end + end + end + + describe 'POST #create' do + context 'with valid attributes' do + it 'saves the conference to the database' do + expect { + post :create, conference: + attributes_for(:conference, short_title: 'dps15') + }.to change(Conference, :count).by(1) + end + + it 'redirects to conference#show' do + post :create, conference: + attributes_for(:conference, short_title: 'dps15') + + expect(response).to redirect_to admin_conference_path( + assigns[:conference].short_title) + end + end + + context 'with invalid attributes' do + it 'does not save the conference to the database' do + expect { + post :create, conference: + attributes_for(:conference, short_title: nil) + }.to_not change(Conference, :count) + end + + it 're-renders the new template' do + post :create, conference: + attributes_for(:conference, short_title: nil) + expect(response).to render_template :new + end + end + + context 'with duplicate conference short title' do + it 'does not save the conference to the database' do + expect { + post :create, conference: attributes_for(:conference) + }.to_not change(Conference, :count) + end + + it 're-renders the new template' do + post :create, conference: attributes_for(:conference) + expect(response).to render_template :new + end + end + end + + describe 'GET #show' do + it 'assigns the requested conference to @conference' do + get :show, id: @conference.short_title + expect(assigns(:conference)).to eq @conference + end + + it 'renders the show template' do + get :show, id: @conference.short_title + expect(response).to render_template :show + end + end + + describe 'GET #index' do + it 'populates an array with conferences' do + con2 = create(:conference, short_title: 'dps15', + title: 'The dog and pony show 2015') + get :index + expect(assigns(:conferences)).to match_array([@conference, con2]) + end + + it 'renders the index template' do + get :index + expect(response).to render_template :index + end + end + + describe 'GET #new' do + it 'assigns a new conference to @conference' do + get :new + expect(assigns(:conference)).to be_a_new(Conference) + end + + it 'renders the :new template' do + get :new + expect(response).to render_template :new + end + end + end + + describe 'administrator access' do + before(:each) do + @conference = create(:conference) + @admin = create(:admin) + sign_in(@admin) + end + + it_behaves_like 'access as administration or organizer' + + end + + describe 'organizer access' do + before(:each) do + @conference = create(:conference) + @organizer = create(:organizer) + sign_in(@organizer) + end + + it_behaves_like 'access as administration or organizer' + + end + + shared_examples 'access as participant or guest' do |success_path| + describe 'GET #show' do + it 'requires admin privileges' do + get :show, id: @conference.short_title + expect(response).to redirect_to(send(success_path)) + end + end + + describe 'GET #index' do + it 'requires admin privileges' do + get :index + expect(response).to redirect_to(send(success_path)) + end + end + + describe 'GET #new' do + it 'requires admin privileges' do + get :new + expect(response).to redirect_to(send(success_path)) + end + end + + describe 'POST #create' do + it 'requires admin privileges' do + post :create, conference: attributes_for(:conference, + short_title: 'ExCon') + expect(response).to redirect_to(send(success_path)) + end + end + + describe 'PATCH #update' do + it 'requires admin privileges' do + patch :update, id: @conference.short_title, + conference: attributes_for(:conference, + short_title: 'ExCon') + expect(response).to redirect_to(send(success_path)) + end + end + end + + describe 'participant access' do + before(:each) do + @conference = create(:conference) + @participant = create(:participant) + sign_in(@participant) + end + + it_behaves_like 'access as participant or guest', :root_path + + end + + describe 'guest access' do + + before(:each) do + @conference = create(:conference) + end + + it_behaves_like 'access as participant or guest', :new_user_session_path + + end +end diff --git a/spec/factories/role.rb b/spec/factories/role.rb new file mode 100644 index 00000000..6d56375f --- /dev/null +++ b/spec/factories/role.rb @@ -0,0 +1,16 @@ +FactoryGirl.define do + factory :role do + + factory :admin_role do + name 'Admin' + end + + factory :organizer_role do + name 'Organizer' + end + + factory :participant_role do + name 'Participant' + end + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 0334d49e..5fe55ab8 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -6,5 +6,17 @@ FactoryGirl.define do password 'changeme' password_confirmation 'changeme' confirmed_at Time.now + + factory :participant do + after(:create) { |user| user.role_ids = create(:participant_role).id } + end + + factory :admin do + after(:create) { |user| user.role_ids = create(:admin_role).id } + end + + factory :organizer do + after(:create) { |user| user.role_ids = create(:organizer_role).id } + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0df96be7..ec20a169 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -49,5 +49,8 @@ RSpec.configure do |config| # As we start from scratch in April 2014, let's forbid the old :should syntax config.expect_with :rspec do |c| c.syntax = :expect + + # Enables devise sign_in function + config.include Devise::TestHelpers, type: :controller end end From 0661b86b1cfadba04de9e24b6836b883ae061baa Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Fri, 9 May 2014 14:44:18 +0200 Subject: [PATCH 2/3] Bugfix for Conference Controller update --- app/controllers/admin/conference_controller.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/conference_controller.rb b/app/controllers/admin/conference_controller.rb index 0027ff77..7e0dd3c8 100644 --- a/app/controllers/admin/conference_controller.rb +++ b/app/controllers/admin/conference_controller.rb @@ -24,9 +24,12 @@ class Admin::ConferenceController < ApplicationController def update @conference = Conference.find_by(short_title: params[:id]) - @conference.update_attributes(params[:conference]) - flash[:notice] = "Updated Conference" - redirect_to(admin_conference_path(:id => @conference.short_title), :notice => 'Conference was successfully updated.') + short_title = @conference.short_title + if @conference.update_attributes(params[:conference]) + redirect_to(admin_conference_path(id: @conference.short_title), notice: 'Conference was successfully updated.') + else + redirect_to(admin_conference_path(id: short_title), notice: 'Conference update failed.') + end end def show From 546977fc02ca490608fb50242b30b35238f54ccc Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 10:24:58 +0200 Subject: [PATCH 3/3] Fix Hound CI violations --- app/controllers/admin/conference_controller.rb | 13 ++++++++----- .../controllers/conferences_controller_spec.rb | 18 +++++++++++------- spec/spec_helper.rb | 14 +++++++------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/app/controllers/admin/conference_controller.rb b/app/controllers/admin/conference_controller.rb index 7e0dd3c8..70e56897 100644 --- a/app/controllers/admin/conference_controller.rb +++ b/app/controllers/admin/conference_controller.rb @@ -16,9 +16,10 @@ class Admin::ConferenceController < ApplicationController def create @conference = Conference.new(params[:conference]) if @conference.save - redirect_to(admin_conference_path(:id => @conference.short_title), :notice => 'Conference was successfully created.') + redirect_to(admin_conference_path(id: @conference.short_title), + notice: 'Conference was successfully created.') else - render :action => "new" + render action: 'new' end end @@ -26,9 +27,11 @@ class Admin::ConferenceController < ApplicationController @conference = Conference.find_by(short_title: params[:id]) short_title = @conference.short_title if @conference.update_attributes(params[:conference]) - redirect_to(admin_conference_path(id: @conference.short_title), notice: 'Conference was successfully updated.') + redirect_to(admin_conference_path(id: @conference.short_title), + notice: 'Conference was successfully updated.') else - redirect_to(admin_conference_path(id: short_title), notice: 'Conference update failed.') + redirect_to(admin_conference_path(id: short_title), + notice: 'Conference update failed.') end end @@ -37,7 +40,7 @@ class Admin::ConferenceController < ApplicationController @conference = Conference.find_by(short_title: params[:id]) respond_to do |format| format.html - format.json { render :json => @conference.to_json } + format.json { render json: @conference.to_json } end end end diff --git a/spec/controllers/conferences_controller_spec.rb b/spec/controllers/conferences_controller_spec.rb index ae364027..e53462aa 100644 --- a/spec/controllers/conferences_controller_spec.rb +++ b/spec/controllers/conferences_controller_spec.rb @@ -59,10 +59,11 @@ describe Admin::ConferenceController do describe 'POST #create' do context 'with valid attributes' do it 'saves the conference to the database' do - expect { + expected = expect do post :create, conference: attributes_for(:conference, short_title: 'dps15') - }.to change(Conference, :count).by(1) + end + expected.to change { Conference.count }.by 1 end it 'redirects to conference#show' do @@ -76,10 +77,11 @@ describe Admin::ConferenceController do context 'with invalid attributes' do it 'does not save the conference to the database' do - expect { + expected = expect do post :create, conference: attributes_for(:conference, short_title: nil) - }.to_not change(Conference, :count) + end + expected.to_not change { Conference.count } end it 're-renders the new template' do @@ -91,9 +93,11 @@ describe Admin::ConferenceController do context 'with duplicate conference short title' do it 'does not save the conference to the database' do - expect { - post :create, conference: attributes_for(:conference) - }.to_not change(Conference, :count) + expected = expect do + post :create, conference: + attributes_for(:conference) + end + expected.to_not change { Conference.count } end it 're-renders the new template' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ec20a169..33a10083 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,8 @@ require 'coveralls' Coveralls.wear!('rails') # This file is copied to spec/ when you run 'rails generate rspec:install' -ENV["RAILS_ENV"] ||= 'test' -require File.expand_path("../../config/environment", __FILE__) +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) if Rails.configuration.database_configuration['test']['database'] == ':memory:' load "#{Rails.root}/db/schema.rb" @@ -18,7 +18,7 @@ require 'rspec/rails' # run twice. It is recommended that you do not name files matching this glob to # end with _spec.rb. You can configure this pattern with with the --pattern # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. -Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } +Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } RSpec.configure do |config| # ## Mock Framework @@ -41,16 +41,16 @@ RSpec.configure do |config| # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 - config.order = "random" + config.order = 'random' # Include factory_girls syntax config.include FactoryGirl::Syntax::Methods + # Enables devise sign_in function + config.include Devise::TestHelpers, type: :controller + # As we start from scratch in April 2014, let's forbid the old :should syntax config.expect_with :rspec do |c| c.syntax = :expect - - # Enables devise sign_in function - config.include Devise::TestHelpers, type: :controller end end