From c890aea68dc5c7f65e787aeed3a8f8b5c8857786 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Fri, 9 May 2014 14:44:01 +0200 Subject: [PATCH 01/27] 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 02/27] 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 03/27] 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 From 8aa68fb84a4359ab888a2b91e7f394359b82d2f9 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 15:55:17 +0200 Subject: [PATCH 04/27] Installs database cleaner - preparation for feature tests with capybara see: http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/ --- Gemfile | 1 + Gemfile.lock | 2 ++ app/models/user.rb | 12 +++++++----- spec/spec_helper.rb | 3 +-- spec/support/database_cleaner.rb | 22 ++++++++++++++++++++++ 5 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 spec/support/database_cleaner.rb diff --git a/Gemfile b/Gemfile index 2c8fd572..7847bfd9 100644 --- a/Gemfile +++ b/Gemfile @@ -76,6 +76,7 @@ group :development, :test do gem 'rspec', '>= 3.0.0.beta' gem 'rspec-rails', '>= 3.0.0.beta' gem 'capybara' + gem 'database_cleaner' end # FIXME: We should use http://weblog.rubyonrails.org/2012/3/21/strong-parameters/ diff --git a/Gemfile.lock b/Gemfile.lock index 58e6d2a8..1e787f6e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -81,6 +81,7 @@ GEM thor d3_rails (3.4.6) railties (>= 3.1.0) + database_cleaner (1.2.0) devise (3.2.4) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -305,6 +306,7 @@ DEPENDENCIES cocoon coveralls d3_rails + database_cleaner devise factory_girl_rails formtastic (~> 2.3.0.rc3) diff --git a/app/models/user.rb b/app/models/user.rb index d4a32cf7..b60a60af 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -14,7 +14,7 @@ class User < ActiveRecord::Base accepts_nested_attributes_for :person accepts_nested_attributes_for :roles - before_save :setup_role + before_create :setup_role before_create :create_person def role?(role) @@ -27,12 +27,14 @@ class User < ActiveRecord::Base end def setup_role - if self.id == 1 - self.role_ids = [3] + if User.count == 0 + admin = Role.where(name: 'Admin').first + self.role_ids = [admin.id] unless admin.nil? end - + if self.role_ids.empty? - self.role_ids = [1] + participant = Role.where(name: 'Participant').first + self.role_ids = [participant.id] unless participant.nil? end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 33a10083..4a3c03f8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,7 +6,6 @@ require File.expand_path('../../config/environment', __FILE__) if Rails.configuration.database_configuration['test']['database'] == ':memory:' load "#{Rails.root}/db/schema.rb" - load "#{Rails.root}/db/seeds.rb" end require 'rspec/rails' @@ -35,7 +34,7 @@ RSpec.configure do |config| # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. - config.use_transactional_fixtures = true + config.use_transactional_fixtures = false # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 00000000..1e0de70f --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,22 @@ +RSpec.configure do |config| + + config.before(:suite) do + DatabaseCleaner.clean_with(:truncation) + end + + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + + config.before(:each, :js => true) do + DatabaseCleaner.strategy = :truncation + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end +end From bbd9fd74d50ddb92082ff47a86be0d2784e992dd Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 15:57:38 +0200 Subject: [PATCH 05/27] Implements user model tests --- spec/models/user_spec.rb | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 spec/models/user_spec.rb diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 00000000..391d96a9 --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,70 @@ +#!/bin/env ruby +# encoding: utf-8 +require 'spec_helper' + +describe User do + + # It is necessary to build roles before user + let!(:organizer_role) { create(:organizer_role) } + let!(:participant_role) { create(:participant_role) } + let!(:admin_role) { create(:admin_role) } + let!(:admin) { create(:user) } + + it 'returns the correct role' do + participant = create(:user, email: 'participant@example.de') + expect(admin.roles.first).to eq(admin_role) + expect(participant.roles.first).to eq(participant_role) + end + + it 'returns the correct roles' do + roles = [organizer_role.id, participant_role.id, admin_role.id] + user_with_all_roles = create(:user, email: 'participant@example.de') + user_with_all_roles.role_ids = roles + user_with_all_roles.save + + expect(user_with_all_roles.roles.length).to eq(3) + expect(user_with_all_roles.roles[0]).to eq(participant_role) + expect(user_with_all_roles.roles[1]).to eq(organizer_role) + expect(user_with_all_roles.roles[2]).to eq(admin_role) + end + + describe '#role?' do + shared_examples '#role?' do |user, role, expected| + it "returns #{expected} for #{role}" do + user_obj = create(user, email: 'e@example.com') + expect(user_obj.role?(role)).to be expected + expect(user_obj.role?(role.downcase)).to be expected + expect(user_obj.role?(role.upcase)).to be expected + expect(user_obj.role?(role.downcase.capitalize)).to be expected + end + end + + context 'admin' do + it_behaves_like '#role?', :admin, 'orgAnizer', false + it_behaves_like '#role?', :admin, 'adMin', true + it_behaves_like '#role?', :admin, 'partiCipant', false + + it 'assigns first user admin role' do + expect(admin.role?('Admin')).to be true + expect(admin.role_ids).to match_array([admin_role.id]) + end + end + + context 'participant' do + it_behaves_like '#role?', :participant, 'orgAnizer', false + it_behaves_like '#role?', :participant, 'adMin', false + it_behaves_like '#role?', :participant, 'partiCipant', true + + it 'assigns second user participant role' do + participant = create(:user, email: 'participant@example.de') + expect(participant.role_ids).to match_array([participant_role.id]) + end + end + + context 'organizer' do + it_behaves_like '#role?', :organizer, 'orgAnizer', true + it_behaves_like '#role?', :organizer, 'adMin', false + it_behaves_like '#role?', :organizer, 'partiCipant', false + end + end +end From 1976276d4d8ff4b156c1ee5a391eb30fe5507883 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 15:58:02 +0200 Subject: [PATCH 06/27] Bugfix to pass user model tests --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index b60a60af..08be257c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,7 +19,7 @@ class User < ActiveRecord::Base def role?(role) Rails.logger.debug("Checking role in user") - return !!self.roles.find_by_name(role.to_s.camelize) + return !!self.roles.find_by_name(role.to_s.downcase.camelize) end def get_roles From f96ff6ed17f67fa1c95fca47a493dce39a4136ba Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 16:02:44 +0200 Subject: [PATCH 07/27] Fix Hound CI violations --- spec/support/database_cleaner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index 1e0de70f..284790b7 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -8,7 +8,7 @@ RSpec.configure do |config| DatabaseCleaner.strategy = :transaction end - config.before(:each, :js => true) do + config.before(:each, js: true) do DatabaseCleaner.strategy = :truncation end From 7e7dadac882c2a0466a4a3e6230fb6bd625eb30d Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 16:09:24 +0200 Subject: [PATCH 08/27] Fix Hound CI violations --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 08be257c..e62da4db 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,7 +19,7 @@ class User < ActiveRecord::Base def role?(role) Rails.logger.debug("Checking role in user") - return !!self.roles.find_by_name(role.to_s.downcase.camelize) + !!roles.find_by_name(role.to_s.downcase.camelize) end def get_roles From 0a5f08878f92d94cc70cea0420df1fe0f0195792 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 16:45:51 +0200 Subject: [PATCH 09/27] Switch back to sqlite test db http://stackoverflow.com/questions/19493153/rails-4-use-sqlite-memory-db-for-selenium-driven-tests --- config/database.yml.example | 2 +- spec/spec_helper.rb | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/config/database.yml.example b/config/database.yml.example index ba386c23..51a4dd45 100644 --- a/config/database.yml.example +++ b/config/database.yml.example @@ -14,7 +14,7 @@ development: # Do not set this db to the same as development or production. test: adapter: sqlite3 - database: ":memory:" + database: db/test.sqlite3 pool: 5 timeout: 5000 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4a3c03f8..da3d3021 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,10 +4,6 @@ Coveralls.wear!('rails') 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" -end - require 'rspec/rails' # Requires supporting ruby files with custom matchers and macros, etc, in From 12e6fd9cb58ea43ea4b79ec38b197b21b752d352 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 17:52:26 +0200 Subject: [PATCH 10/27] Bugfix to pass feature test - check if session[:return_to] is nil - adds unique html ids for dropdown login fields --- app/controllers/application_controller.rb | 3 ++- app/views/layouts/_navigation.html.haml | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 077f2073..12c0e8e0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,7 +9,8 @@ class ApplicationController < ActionController::Base end def after_sign_in_path_for(resource) - if not session[:return_to].start_with?(user_registration_path) + if session[:return_to] and + not session[:return_to].start_with?(user_registration_path) logger.debug "Returning to #{session[:return_to]}" session[:return_to] else diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 404ed23d..cedea71f 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -47,8 +47,8 @@ %span.caret .dropdown-menu{:style => "padding: 17px;"} = form_tag user_session_path do - = text_field_tag 'user[email]' - = password_field_tag 'user[password]' + = text_field_tag 'user[email]', nil, id: 'user_email_dd' + = password_field_tag 'user[password]', nil, id: 'user_password_dd' %label.checkbox = check_box_tag 'user[remember_me]' Remember me From d6c692962e87826469c4ae479919b152fbcc5f6a Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 17:53:31 +0200 Subject: [PATCH 11/27] Enables capybara-webkit --- Gemfile | 1 + Gemfile.lock | 4 ++++ spec/spec_helper.rb | 3 +++ 3 files changed, 8 insertions(+) diff --git a/Gemfile b/Gemfile index 7847bfd9..743510a2 100644 --- a/Gemfile +++ b/Gemfile @@ -77,6 +77,7 @@ group :development, :test do gem 'rspec-rails', '>= 3.0.0.beta' gem 'capybara' gem 'database_cleaner' + gem 'capybara-webkit' end # FIXME: We should use http://weblog.rubyonrails.org/2012/3/21/strong-parameters/ diff --git a/Gemfile.lock b/Gemfile.lock index 1e787f6e..ba6788b1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -55,6 +55,9 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) + capybara-webkit (1.1.0) + capybara (~> 2.0, >= 2.0.2) + json celluloid (0.15.2) timers (~> 1.1.0) celluloid-io (0.15.0) @@ -303,6 +306,7 @@ DEPENDENCIES bootstrap-sass cancan capybara + capybara-webkit cocoon coveralls d3_rails diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index da3d3021..06839d80 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -44,6 +44,9 @@ RSpec.configure do |config| # Enables devise sign_in function config.include Devise::TestHelpers, type: :controller + # Use capybara-webkit as default javascript driver + Capybara.javascript_driver = :webkit + # As we start from scratch in April 2014, let's forbid the old :should syntax config.expect_with :rspec do |c| c.syntax = :expect From e823fc02793a03b77a6b2aefd032b9f969a17224 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 17:54:56 +0200 Subject: [PATCH 12/27] Implements login macros --- spec/spec_helper.rb | 3 +++ spec/support/login_macros.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 spec/support/login_macros.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 06839d80..257c19d8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -47,6 +47,9 @@ RSpec.configure do |config| # Use capybara-webkit as default javascript driver Capybara.javascript_driver = :webkit + # Includes support/login_macros for feature tests + config.include LoginMacros, type: :feature + # As we start from scratch in April 2014, let's forbid the old :should syntax config.expect_with :rspec do |c| c.syntax = :expect diff --git a/spec/support/login_macros.rb b/spec/support/login_macros.rb new file mode 100644 index 00000000..3694a12b --- /dev/null +++ b/spec/support/login_macros.rb @@ -0,0 +1,12 @@ +module LoginMacros + + def sign_in(user) + visit new_user_session_path + + fill_in 'user_email', with: user.email + fill_in 'user_password', with: user.password + find(:xpath, "//div[@id='content']//input[@name='commit']").click + + expect(page.has_content?('Signed in successfully')).to be true + end +end From 110afe119200b028e6b41f3d9ec38f6f632e0dcf Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 17:55:15 +0200 Subject: [PATCH 13/27] Implements feature tests for conference add and update --- spec/features/conference_spec.rb | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 spec/features/conference_spec.rb diff --git a/spec/features/conference_spec.rb b/spec/features/conference_spec.rb new file mode 100644 index 00000000..25339575 --- /dev/null +++ b/spec/features/conference_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' + +feature Conference do + + shared_examples 'add and update conference' do |user| + scenario 'adds a new conference', feature: true, js: true do + expected_count = Conference.count + 1 + sign_in create(user) + + visit new_admin_conference_path + fill_in 'conference_title', with: 'Example Con' + fill_in 'conference_short_title', with: 'ExCon' + fill_in 'conference_social_tag', with: 'ExCon' + + page.execute_script("$('#conference-start-datepicker').val('21/12/2014')") + page.execute_script("$('#conference-end-datepicker').val('24/12/2014')") + + click_button 'Create Conference' + + expect(page.find('#flash_notice').text).to eq('Conference was successfully created.') + expect(Conference.count).to eq(expected_count) + end + + scenario 'update conference', feature: true, js: true do + conference = create(:conference) + expected_count = Conference.count + sign_in create(user) + + visit admin_conference_path(conference.short_title) + fill_in 'conference_title', with: 'New Con' + fill_in 'conference_short_title', with: 'NewCon' + fill_in 'conference_social_tag', with: 'NewCon' + + click_button 'Update Conference' + expect(page.find('#flash_notice').text).to eq('Conference was successfully updated.') + + conference.reload + expect(conference.title).to eq('New Con') + expect(conference.short_title).to eq('NewCon') + expect(conference.social_tag).to eq('NewCon') + expect(Conference.count).to eq(expected_count) + end + end + + describe 'admin' do + it_behaves_like 'add and update conference', :admin + end + + describe 'organizer' do + it_behaves_like 'add and update conference', :organizer + end + +end From c742e57534b6487fda1660b843a697b7e8e4009c Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 18:02:29 +0200 Subject: [PATCH 14/27] Fix Hound CI violations --- app/controllers/application_controller.rb | 4 ++-- spec/features/conference_spec.rb | 6 ++++-- spec/support/login_macros.rb | 1 - 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 12c0e8e0..c4e269b9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,8 +9,8 @@ class ApplicationController < ActionController::Base end def after_sign_in_path_for(resource) - if session[:return_to] and - not session[:return_to].start_with?(user_registration_path) + if session[:return_to] && + (not session[:return_to].start_with?(user_registration_path)) logger.debug "Returning to #{session[:return_to]}" session[:return_to] else diff --git a/spec/features/conference_spec.rb b/spec/features/conference_spec.rb index 25339575..058cd3d6 100644 --- a/spec/features/conference_spec.rb +++ b/spec/features/conference_spec.rb @@ -17,7 +17,8 @@ feature Conference do click_button 'Create Conference' - expect(page.find('#flash_notice').text).to eq('Conference was successfully created.') + expect(page.find('#flash_notice').text). + to eq('Conference was successfully created.') expect(Conference.count).to eq(expected_count) end @@ -32,7 +33,8 @@ feature Conference do fill_in 'conference_social_tag', with: 'NewCon' click_button 'Update Conference' - expect(page.find('#flash_notice').text).to eq('Conference was successfully updated.') + expect(page.find('#flash_notice').text). + to eq('Conference was successfully updated.') conference.reload expect(conference.title).to eq('New Con') diff --git a/spec/support/login_macros.rb b/spec/support/login_macros.rb index 3694a12b..ad6abe3c 100644 --- a/spec/support/login_macros.rb +++ b/spec/support/login_macros.rb @@ -1,5 +1,4 @@ module LoginMacros - def sign_in(user) visit new_user_session_path From 5423edd6b77cd8655b35ce61f94a91d7ea0b681c Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sat, 10 May 2014 18:47:16 +0200 Subject: [PATCH 15/27] Fix Travis CI build --- spec/features/conference_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/features/conference_spec.rb b/spec/features/conference_spec.rb index 058cd3d6..bd0dbce2 100644 --- a/spec/features/conference_spec.rb +++ b/spec/features/conference_spec.rb @@ -12,6 +12,8 @@ feature Conference do fill_in 'conference_short_title', with: 'ExCon' fill_in 'conference_social_tag', with: 'ExCon' + select('(GMT+01:00) Berlin', from: 'conference[timezone]') + page.execute_script("$('#conference-start-datepicker').val('21/12/2014')") page.execute_script("$('#conference-end-datepicker').val('24/12/2014')") From 9256ff9e035c1aa342611369f78c901c37239e3a Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sun, 11 May 2014 08:26:29 +0200 Subject: [PATCH 16/27] Deletes loading seeds for tests - we use factory girl and database cleaner instead --- spec/support/seeds.rb | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 spec/support/seeds.rb diff --git a/spec/support/seeds.rb b/spec/support/seeds.rb deleted file mode 100644 index 41e58998..00000000 --- a/spec/support/seeds.rb +++ /dev/null @@ -1,7 +0,0 @@ -RSpec.configure do |config| - - config.before(:suite) do - load "#{Rails.root}/db/seeds.rb" - end - -end From 4e6fcb0e7d463407132e61ec0d71f4ad82231dfd Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Sun, 11 May 2014 10:58:05 +0200 Subject: [PATCH 17/27] feature test for venue --- spec/features/conference_spec.rb | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/spec/features/conference_spec.rb b/spec/features/conference_spec.rb index bd0dbce2..f923f9be 100644 --- a/spec/features/conference_spec.rb +++ b/spec/features/conference_spec.rb @@ -46,12 +46,66 @@ feature Conference do end end + shared_examples 'venue' do |user| + scenario 'adds and updates venue' do + conference = create(:conference) + + sign_in create(user) + visit admin_conference_venue_info_path(conference_id: conference.short_title) + + expect(page.find("//*[@id='venue_submit_action']").text).to eq('Update Venue') + + fill_in 'venue_name', with: 'Example University' + fill_in 'venue_address', with: 'Example Street 42 \n 12345 Example City \n Germany' + fill_in 'venue_website', with: 'www.example.com' + fill_in 'venue_description', + with: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam' \ + 'nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,' \ + 'sed diam voluptua.' + + click_button 'Update Venue' + + expect(page.find('#flash_notice').text). + to eq('Venue was successfully updated.') + + venue = Conference.find(conference.id).venue + expect(venue.name).to eq('Example University') + expect(venue.address).to eq('Example Street 42 \n 12345 Example City \n Germany') + expect(venue.website).to eq('www.example.com') + expect(venue.description).to eq('Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam' \ + 'nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,' \ + 'sed diam voluptua.') + + fill_in 'venue_name', with: 'Example University new' + fill_in 'venue_address', with: 'Example Street 42 \n 12345 Example City \n Germany new' + fill_in 'venue_website', with: 'www.example.com new' + fill_in 'venue_description', + with: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam' \ + 'nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,' \ + 'sed diam voluptua. new' + + click_button 'Update Venue' + expect(page.find('#flash_notice').text). + to eq('Venue was successfully updated.') + + venue.reload + expect(venue.name).to eq('Example University new') + expect(venue.address).to eq('Example Street 42 \n 12345 Example City \n Germany new') + expect(venue.website).to eq('www.example.com new') + expect(venue.description).to eq('Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam' \ + 'nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,' \ + 'sed diam voluptua. new') + end + end + describe 'admin' do it_behaves_like 'add and update conference', :admin + it_behaves_like 'venue', :admin end describe 'organizer' do it_behaves_like 'add and update conference', :organizer + it_behaves_like 'venue', :organizer end end From cfe02baf02429e09fc961e7803e7cc8e6bba222d Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Mon, 12 May 2014 13:19:09 +0200 Subject: [PATCH 18/27] feature test for submitting a proposal --- spec/factories/event_type.rb | 10 +++++++ spec/features/proposal_spec.rb | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 spec/factories/event_type.rb create mode 100644 spec/features/proposal_spec.rb diff --git a/spec/factories/event_type.rb b/spec/factories/event_type.rb new file mode 100644 index 00000000..4b47202f --- /dev/null +++ b/spec/factories/event_type.rb @@ -0,0 +1,10 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :event_type do + title 'Example Event Type' + length 30 + minimum_abstract_length 0 + maximum_abstract_length 500 + end +end diff --git a/spec/features/proposal_spec.rb b/spec/features/proposal_spec.rb new file mode 100644 index 00000000..3f4a2050 --- /dev/null +++ b/spec/features/proposal_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +feature Event do + + shared_examples 'participant' do |user| + scenario 'submitts a new proposal and updates account', feature: true, js: true do + expected_count = Event.count + 1 + conference = create(:conference) + conference.call_for_papers = create(:call_for_papers) + conference.event_types = [create(:event_type)] + + sign_in create(user) + + visit conference_proposal_index_path(conference.short_title) + click_link 'New Proposal' + + fill_in 'event_title', with: 'Example Proposal' + fill_in 'event_subtitle', with: 'Example Proposal Subtitle' + + select('Example Event Type', from: 'event[event_type_id]') + + fill_in 'event_abstract', with: 'Lorem ipsum abstract' + fill_in 'event_description', with: 'Lorem ipsum description' + + select('YouTube', from: 'event[media_type]') + fill_in 'event_media_id', with: '123456' + + fill_in 'person_biography', with: 'Lorem ipsum biography' + fill_in 'person_public_name', with: 'Example User' + + click_button 'Submit Session' + expect(current_path).to eq(edit_user_registration_path) + + fill_in 'user_person_attributes_first_name', with: 'Example' + fill_in 'user_person_attributes_last_name', with: 'User' + + click_button 'Update' + + expect(page.find('#flash_notice').text). + to eq('You updated your account successfully.') + + expect(Event.count).to eq(expected_count) + end + end + + describe 'participant' do + it_behaves_like 'participant', :participant + it_behaves_like 'participant', :organizer + it_behaves_like 'participant', :admin + end +end From 9e831cb5141ddb24bd730adba2ffe32c4791aa75 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Mon, 12 May 2014 15:26:46 +0200 Subject: [PATCH 19/27] Merge with master --- Gemfile | 3 --- app/controllers/admin/conference_controller.rb | 8 -------- 2 files changed, 11 deletions(-) diff --git a/Gemfile b/Gemfile index 38c2fa96..346aee78 100644 --- a/Gemfile +++ b/Gemfile @@ -76,12 +76,9 @@ group :development, :test do gem 'rspec', '>= 3.0.0.beta' gem 'rspec-rails', '>= 3.0.0.beta' gem 'capybara' -<<<<<<< HEAD gem 'database_cleaner' gem 'capybara-webkit' -======= gem 'shoulda' ->>>>>>> 374efd311fb7f82d0e8257a62675a663904708d5 end # FIXME: We should use http://weblog.rubyonrails.org/2012/3/21/strong-parameters/ diff --git a/app/controllers/admin/conference_controller.rb b/app/controllers/admin/conference_controller.rb index 81820be6..bc52af9b 100644 --- a/app/controllers/admin/conference_controller.rb +++ b/app/controllers/admin/conference_controller.rb @@ -15,13 +15,6 @@ class Admin::ConferenceController < ApplicationController def create @conference = Conference.new(params[:conference]) -<<<<<<< HEAD - if @conference.save - redirect_to(admin_conference_path(id: @conference.short_title), - notice: 'Conference was successfully created.') - else - render action: 'new' -======= if @conference.valid? @conference.save redirect_to(admin_conference_path(id: @conference.short_title), @@ -29,7 +22,6 @@ class Admin::ConferenceController < ApplicationController else redirect_to(new_admin_conference_path, flash: { error: @conference.errors.full_messages.join('! ') }) ->>>>>>> 374efd311fb7f82d0e8257a62675a663904708d5 end end From eb3a0fe2b31d236b462ab50fb0c7f9b1f3912908 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Mon, 12 May 2014 15:32:46 +0200 Subject: [PATCH 20/27] Fix failing tests --- spec/controllers/conferences_controller_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/controllers/conferences_controller_spec.rb b/spec/controllers/conferences_controller_spec.rb index e53462aa..79590522 100644 --- a/spec/controllers/conferences_controller_spec.rb +++ b/spec/controllers/conferences_controller_spec.rb @@ -87,7 +87,7 @@ describe Admin::ConferenceController do it 're-renders the new template' do post :create, conference: attributes_for(:conference, short_title: nil) - expect(response).to render_template :new + expect(response).to redirect_to new_admin_conference_path end end @@ -102,7 +102,7 @@ describe Admin::ConferenceController do it 're-renders the new template' do post :create, conference: attributes_for(:conference) - expect(response).to render_template :new + expect(response).to redirect_to new_admin_conference_path end end end From 980eb9f6bc2ecffd66a0500a78e7c5406843d49c Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Mon, 12 May 2014 17:22:48 +0200 Subject: [PATCH 21/27] Changed not to exclamation mark --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c4e269b9..cf114293 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -10,7 +10,7 @@ class ApplicationController < ActionController::Base def after_sign_in_path_for(resource) if session[:return_to] && - (not session[:return_to].start_with?(user_registration_path)) + !session[:return_to].start_with?(user_registration_path) logger.debug "Returning to #{session[:return_to]}" session[:return_to] else From 3dfb73d61945e38f7b880fea3bbe78efbba6370a Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Mon, 12 May 2014 20:02:38 +0200 Subject: [PATCH 22/27] Removed shebang & encoding --- spec/controllers/conferences_controller_spec.rb | 2 -- spec/models/user_spec.rb | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/spec/controllers/conferences_controller_spec.rb b/spec/controllers/conferences_controller_spec.rb index 79590522..334f49d9 100644 --- a/spec/controllers/conferences_controller_spec.rb +++ b/spec/controllers/conferences_controller_spec.rb @@ -1,5 +1,3 @@ -#!/bin/env ruby -# encoding: utf-8 require 'spec_helper' describe Admin::ConferenceController do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 391d96a9..ffbcb1c4 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,10 +1,8 @@ -#!/bin/env ruby -# encoding: utf-8 require 'spec_helper' describe User do - # It is necessary to build roles before user + # It is necessary to use bang version of let to build roles before user let!(:organizer_role) { create(:organizer_role) } let!(:participant_role) { create(:participant_role) } let!(:admin_role) { create(:admin_role) } From e87e7e8fe609cb9c59261ad80a69115e7e3f7c1c Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Mon, 12 May 2014 20:45:44 +0200 Subject: [PATCH 23/27] Implements lazy let for conference controller spec --- .../conferences_controller_spec.rb | 110 ++++++++++-------- 1 file changed, 59 insertions(+), 51 deletions(-) diff --git a/spec/controllers/conferences_controller_spec.rb b/spec/controllers/conferences_controller_spec.rb index 334f49d9..57d6ad32 100644 --- a/spec/controllers/conferences_controller_spec.rb +++ b/spec/controllers/conferences_controller_spec.rb @@ -2,54 +2,58 @@ require 'spec_helper' describe Admin::ConferenceController do - shared_examples 'access as administration or organizer' do + let(:conference) { create(:conference) } + let(:admin) { create(:admin) } + let(:organizer) { create(:organizer) } + let(:participant) { create(:participant) } + 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: + 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) + expect(assigns(:conference)).to eq(conference) end - it 'changes @conference attributes' do - patch :update, id: @conference.short_title, conference: + it 'changes conference attributes' do + patch :update, id: conference.short_title, conference: attributes_for(:conference, title: 'Example Con', - short_title: 'ExCon') + short_title: 'ExCon') - @conference.reload - expect(@conference.title).to eq('Example Con') - expect(@conference.short_title).to eq('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: + 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) + conference.short_title) end end context 'invalid attributes' do it 'does not change conference attributes' do - patch :update, id: @conference.short_title, conference: + patch :update, id: conference.short_title, conference: attributes_for(:conference, title: 'Example Con', - short_title: nil) + short_title: nil) - @conference.reload - expect(@conference.title).to eq('The dog and pony show') - expect(@conference.short_title).to eq('dps14') + 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: + patch :update, id: conference.short_title, conference: attributes_for(:conference, title: 'Example Con', - short_title: nil) + short_title: nil) expect(response).to redirect_to admin_conference_path( - @conference.short_title) + conference.short_title) end end end @@ -91,6 +95,7 @@ describe Admin::ConferenceController do context 'with duplicate conference short title' do it 'does not save the conference to the database' do + conference expected = expect do post :create, conference: attributes_for(:conference) @@ -99,6 +104,7 @@ describe Admin::ConferenceController do end it 're-renders the new template' do + conference post :create, conference: attributes_for(:conference) expect(response).to redirect_to new_admin_conference_path end @@ -106,33 +112,43 @@ describe Admin::ConferenceController do 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 + 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 + 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]) + context 'with more than 0 conferences' 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 + conference + get :index + expect(response).to render_template :index + end end - it 'renders the index template' do - get :index - expect(response).to render_template :index + context 'no conferences' do + it 'redirect to new conference' do + get :index + expect(response).to redirect_to(redirect_to new_admin_conference_path) + end end end describe 'GET #new' do - it 'assigns a new conference to @conference' do + it 'assigns a new conference to conference' do get :new expect(assigns(:conference)).to be_a_new(Conference) end @@ -145,10 +161,9 @@ describe Admin::ConferenceController do end describe 'administrator access' do + before(:each) do - @conference = create(:conference) - @admin = create(:admin) - sign_in(@admin) + sign_in(admin) end it_behaves_like 'access as administration or organizer' @@ -156,10 +171,9 @@ describe Admin::ConferenceController do end describe 'organizer access' do + before(:each) do - @conference = create(:conference) - @organizer = create(:organizer) - sign_in(@organizer) + sign_in(organizer) end it_behaves_like 'access as administration or organizer' @@ -169,7 +183,7 @@ describe Admin::ConferenceController do 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 + get :show, id: conference.short_title expect(response).to redirect_to(send(success_path)) end end @@ -198,9 +212,9 @@ describe Admin::ConferenceController do describe 'PATCH #update' do it 'requires admin privileges' do - patch :update, id: @conference.short_title, - conference: attributes_for(:conference, - short_title: 'ExCon') + patch :update, id: conference.short_title, + conference: attributes_for(:conference, + short_title: 'ExCon') expect(response).to redirect_to(send(success_path)) end end @@ -208,9 +222,7 @@ describe Admin::ConferenceController do describe 'participant access' do before(:each) do - @conference = create(:conference) - @participant = create(:participant) - sign_in(@participant) + sign_in(participant) end it_behaves_like 'access as participant or guest', :root_path @@ -219,10 +231,6 @@ describe Admin::ConferenceController do describe 'guest access' do - before(:each) do - @conference = create(:conference) - end - it_behaves_like 'access as participant or guest', :new_user_session_path end From b94b5a4c756391e312bd84782b20f3122ed1b463 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Mon, 12 May 2014 21:15:33 +0200 Subject: [PATCH 24/27] Adds conference update errors to flash - fix tests --- app/controllers/admin/conference_controller.rb | 3 ++- spec/controllers/conferences_controller_spec.rb | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/conference_controller.rb b/app/controllers/admin/conference_controller.rb index bc52af9b..ea008d41 100644 --- a/app/controllers/admin/conference_controller.rb +++ b/app/controllers/admin/conference_controller.rb @@ -32,8 +32,9 @@ class Admin::ConferenceController < ApplicationController redirect_to(admin_conference_path(id: @conference.short_title), notice: 'Conference was successfully updated.') else + errors = @conference.errors.full_messages.join('! ') redirect_to(admin_conference_path(id: short_title), - notice: 'Conference update failed.') + flash: { error: "Conference update failed. #{errors}" }) end end diff --git a/spec/controllers/conferences_controller_spec.rb b/spec/controllers/conferences_controller_spec.rb index 57d6ad32..2b606ab1 100644 --- a/spec/controllers/conferences_controller_spec.rb +++ b/spec/controllers/conferences_controller_spec.rb @@ -43,6 +43,7 @@ describe Admin::ConferenceController do short_title: nil) conference.reload + expect(flash[:error]).to eq("Conference update failed. Short title can't be blank") expect(conference.title).to eq('The dog and pony show') expect(conference.short_title).to eq('dps14') end @@ -52,6 +53,7 @@ describe Admin::ConferenceController do attributes_for(:conference, title: 'Example Con', short_title: nil) + expect(flash[:error]).to eq("Conference update failed. Short title can't be blank") expect(response).to redirect_to admin_conference_path( conference.short_title) end From 84843ade483aa300c6990541ac2382a0eba84415 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Tue, 13 May 2014 10:35:23 +0200 Subject: [PATCH 25/27] refactoring setup_roles and updated tests --- app/models/user.rb | 21 +++++++------------ .../conferences_controller_spec.rb | 5 +++++ spec/features/conference_spec.rb | 5 +++++ spec/features/proposal_spec.rb | 5 +++++ spec/models/conference_spec.rb | 5 +++++ 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 296b4114..c322b710 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,15 +29,8 @@ class User < ActiveRecord::Base end def setup_role - if User.count == 0 - admin = Role.where(name: 'Admin').first - self.role_ids = [admin.id] unless admin.nil? - end - - if self.role_ids.empty? - participant = Role.where(name: 'Participant').first - self.role_ids = [participant.id] unless participant.nil? - end + self.roles << Role.find_by(name: 'Admin') if User.count == 0 + self.roles << Role.find_by(name: 'Participant') if self.roles.empty? end def popup_details @@ -56,9 +49,9 @@ class User < ActiveRecord::Base end private - def create_person - # TODO Search people for existing email address, add to their account - build_person(:email => self.email) if person.nil? - true - end + def create_person + # TODO Search people for existing email address, add to their account + build_person(:email => self.email) if person.nil? + true + end end diff --git a/spec/controllers/conferences_controller_spec.rb b/spec/controllers/conferences_controller_spec.rb index 2b606ab1..a310ad56 100644 --- a/spec/controllers/conferences_controller_spec.rb +++ b/spec/controllers/conferences_controller_spec.rb @@ -2,6 +2,11 @@ require 'spec_helper' describe Admin::ConferenceController do + # It is necessary to use bang version of let to build roles before user + let!(:organizer_role) { create(:organizer_role) } + let!(:participant_role) { create(:participant_role) } + let!(:admin_role) { create(:admin_role) } + let(:conference) { create(:conference) } let(:admin) { create(:admin) } let(:organizer) { create(:organizer) } diff --git a/spec/features/conference_spec.rb b/spec/features/conference_spec.rb index f923f9be..b5c6fd52 100644 --- a/spec/features/conference_spec.rb +++ b/spec/features/conference_spec.rb @@ -2,6 +2,11 @@ require 'spec_helper' feature Conference do + # It is necessary to use bang version of let to build roles before user + let!(:organizer_role) { create(:organizer_role) } + let!(:participant_role) { create(:participant_role) } + let!(:admin_role) { create(:admin_role) } + shared_examples 'add and update conference' do |user| scenario 'adds a new conference', feature: true, js: true do expected_count = Conference.count + 1 diff --git a/spec/features/proposal_spec.rb b/spec/features/proposal_spec.rb index 3f4a2050..0f318b41 100644 --- a/spec/features/proposal_spec.rb +++ b/spec/features/proposal_spec.rb @@ -2,6 +2,11 @@ require 'spec_helper' feature Event do + # It is necessary to use bang version of let to build roles before user + let!(:organizer_role) { create(:organizer_role) } + let!(:participant_role) { create(:participant_role) } + let!(:admin_role) { create(:admin_role) } + shared_examples 'participant' do |user| scenario 'submitts a new proposal and updates account', feature: true, js: true do expected_count = Event.count + 1 diff --git a/spec/models/conference_spec.rb b/spec/models/conference_spec.rb index e1d2093f..ecaac1be 100644 --- a/spec/models/conference_spec.rb +++ b/spec/models/conference_spec.rb @@ -52,6 +52,11 @@ describe Conference do describe '#user_registered?' do + # It is necessary to use bang version of let to build roles before user + let!(:organizer_role) { create(:organizer_role) } + let!(:participant_role) { create(:participant_role) } + let!(:admin_role) { create(:admin_role) } + let(:user) { create(:user) } context 'user not registered' do From 132bb8955692b4c37d53df507202d9a6ea101c29 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Tue, 13 May 2014 10:39:03 +0200 Subject: [PATCH 26/27] fix hound ci violations --- app/models/user.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index c322b710..8bac9c47 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,8 +29,8 @@ class User < ActiveRecord::Base end def setup_role - self.roles << Role.find_by(name: 'Admin') if User.count == 0 - self.roles << Role.find_by(name: 'Participant') if self.roles.empty? + roles << Role.find_by(name: 'Admin') if User.count == 0 + roles << Role.find_by(name: 'Participant') if self.roles.empty? end def popup_details @@ -51,7 +51,7 @@ class User < ActiveRecord::Base private def create_person # TODO Search people for existing email address, add to their account - build_person(:email => self.email) if person.nil? + build_person(email: email) if person.nil? true end end From 25ad5b74090290298c4ad01058cebe399984ed38 Mon Sep 17 00:00:00 2001 From: Chrisbr Date: Tue, 13 May 2014 11:12:53 +0200 Subject: [PATCH 27/27] fix hound ci violations --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 8bac9c47..6fd103d7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -30,7 +30,7 @@ class User < ActiveRecord::Base def setup_role roles << Role.find_by(name: 'Admin') if User.count == 0 - roles << Role.find_by(name: 'Participant') if self.roles.empty? + roles << Role.find_by(name: 'Participant') if roles.empty? end def popup_details