From 7975ec2012a561ec46423bb46ac0449bfd0d653a Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Tue, 14 Jul 2015 01:22:50 -0400 Subject: [PATCH] Add some basic integration testing Rushed but it'll work for now --- app/controllers/user_sessions_controller.rb | 4 +- .../subcreddits/close_subcreddit_spec.rb | 39 +++++++++++++ .../subcreddits/list_subcreddits_spec.rb | 13 +++++ .../subcreddits/new_subcreddit_spec.rb | 47 ++++++++++++++++ spec/features/users/signin_spec.rb | 43 +++++++++++++++ spec/features/users/signout_spec.rb | 32 +++++++++++ spec/features/users/signup_spec.rb | 55 +++++++++++++++++++ spec/rails_helper.rb | 1 + spec/support/helpers.rb | 5 ++ spec/support/helpers/session_helpers.rb | 27 +++++++++ 10 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 spec/features/subcreddits/close_subcreddit_spec.rb create mode 100644 spec/features/subcreddits/list_subcreddits_spec.rb create mode 100644 spec/features/subcreddits/new_subcreddit_spec.rb create mode 100644 spec/features/users/signin_spec.rb create mode 100644 spec/features/users/signout_spec.rb create mode 100644 spec/features/users/signup_spec.rb create mode 100644 spec/support/helpers.rb create mode 100644 spec/support/helpers/session_helpers.rb diff --git a/app/controllers/user_sessions_controller.rb b/app/controllers/user_sessions_controller.rb index 3973f08..d3d7220 100644 --- a/app/controllers/user_sessions_controller.rb +++ b/app/controllers/user_sessions_controller.rb @@ -11,7 +11,7 @@ class UserSessionsController < ApplicationController if authenticate_user?(user) create_user_session(user) - redirect_to root_path, notice: 'You have been logged in!' + redirect_to root_path, notice: 'You have been signed in!' else redirect_to signin_path, alert: 'Username or password was incorrect!' end @@ -21,7 +21,7 @@ class UserSessionsController < ApplicationController cookies.permanent[:user_session] = nil current_session.destroy if current_session - redirect_to root_path, notice: 'You have been logged out!' + redirect_to root_path, notice: 'You have been signed out!' end private diff --git a/spec/features/subcreddits/close_subcreddit_spec.rb b/spec/features/subcreddits/close_subcreddit_spec.rb new file mode 100644 index 0000000..cc59678 --- /dev/null +++ b/spec/features/subcreddits/close_subcreddit_spec.rb @@ -0,0 +1,39 @@ +require 'rails_helper' + +describe 'Edit Subcreddit', type: :feature do + before(:each) { signout } + + context 'when logged in' do + context 'when board is open' do + let!(:subcreddit) { create(:subcreddit) } + before(:each) do + visit subcreddits_path + click_link 'Edit' + check :subcreddit_closed + click_button 'Update Subcreddit' + end + + it 'should be notified the subcreddit was updated' do + expect(page).to have_content('updated') + end + + it 'should close the board when closed is checked' do + expect(page).to have_content('closed') + end + end + + context 'when board is closed' do + let!(:subcreddit) { create(:subcreddit, closed_at: Time.now) } + before(:each) do + visit subcreddits_path + click_link 'Edit' + uncheck :subcreddit_closed + click_button 'Update Subcreddit' + end + + it 'should open the board when closed is checked' do + expect(page).to_not have_content('closed') + end + end + end +end diff --git a/spec/features/subcreddits/list_subcreddits_spec.rb b/spec/features/subcreddits/list_subcreddits_spec.rb new file mode 100644 index 0000000..c2f1ca5 --- /dev/null +++ b/spec/features/subcreddits/list_subcreddits_spec.rb @@ -0,0 +1,13 @@ +require 'rails_helper' + +describe 'New Subcreddit', type: :feature do + let!(:subcreddits) { 5.times.collect { create(:subcreddit) } } + + it 'should list all subcreddits' do + visit subcreddits_path + + subcreddits.each do |subcreddit| + expect(page).to have_link(subcreddit.name, subcreddit_path(subcreddit)) + end + end +end diff --git a/spec/features/subcreddits/new_subcreddit_spec.rb b/spec/features/subcreddits/new_subcreddit_spec.rb new file mode 100644 index 0000000..1082604 --- /dev/null +++ b/spec/features/subcreddits/new_subcreddit_spec.rb @@ -0,0 +1,47 @@ +require 'rails_helper' + +describe 'New Subcreddit', type: :feature do + before(:each) { signout } + + context 'when not signed in' do + it 'should not be able to create a new subcreddit' + end + + context 'when signed in' do + let(:user) { create(:user) } + let(:subcreddit) { build_stubbed(:subcreddit) } + before(:each) { signin(user: user) } + + context 'with valid data' do + before(:each) do + visit new_subcreddit_path + + fill_in :subcreddit_name, with: subcreddit.name + + click_button 'Create Subcreddit' + end + + it 'should notify that a new subcreddit was created' do + expect(page).to have_content('created') + end + + it 'should forward you to the new subcreddit' do + expect(page).to have_content(subcreddit.name) + end + end + + context 'with invalid data' do + before(:each) do + subcreddit.name = '' + + visit new_subcreddit_path + fill_in :subcreddit_name, with: subcreddit.name + click_button 'Create Subcreddit' + end + + it 'should display errors' do + expect(page).to have_content("can't be blank") + end + end + end +end diff --git a/spec/features/users/signin_spec.rb b/spec/features/users/signin_spec.rb new file mode 100644 index 0000000..bdcf2c1 --- /dev/null +++ b/spec/features/users/signin_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +describe 'Sign In', type: :feature do + before(:each) { signout } + + context 'when not signed in' do + let(:user) { create(:user) } + + it 'should display the sign in link' do + visit root_path + + expect(page).to have_link('Sign In') + end + + context 'with valid details' do + it 'should sign in successfully' do + signin(user: user) + + expect(page).to have_content('signed in') + end + end + + context 'with invalid details' do + it 'should return to the form and display errors' do + signin(username: user.username, password: 'garbage') + + expect(page).to have_content('incorrect') + end + end + end + + context 'when signed in' do + let(:user) { create(:user) } + + before(:each) { signin(user: user) } + + it 'should not display the sign in link' do + visit root_path + + expect(page).to_not have_link('Sign In') + end + end +end diff --git a/spec/features/users/signout_spec.rb b/spec/features/users/signout_spec.rb new file mode 100644 index 0000000..6c07de0 --- /dev/null +++ b/spec/features/users/signout_spec.rb @@ -0,0 +1,32 @@ +require 'rails_helper' + +describe 'Sign Out', type: :feature do + before(:each) { signout } + + context 'when not signed in' do + it 'should not display the sign out link' do + visit root_path + + expect(page).to_not have_link('Sign Out') + end + end + + context 'when signed in' do + let(:user) { create(:user) } + + before(:each) { signin(user: user) } + + it 'should display the sign out link' do + visit root_path + + expect(page).to have_link('Sign Out') + end + + it 'should sign a user out' do + visit root_path + click_link('Sign Out') + + expect(page).to have_content('signed out') + end + end +end diff --git a/spec/features/users/signup_spec.rb b/spec/features/users/signup_spec.rb new file mode 100644 index 0000000..2f5302f --- /dev/null +++ b/spec/features/users/signup_spec.rb @@ -0,0 +1,55 @@ +require 'rails_helper' + +describe 'Signup', type: :feature do + before(:each) { signout } + + context 'when not signed in' do + let(:user) { build_stubbed(:user) } + + it 'should display the create account link' do + visit root_path + + expect(page).to have_link('Create Account') + end + + context 'with valid details' do + it 'should create a new user' do + visit signup_path + + fill_in :user_username, with: user.username + fill_in :user_password, with: user.password + fill_in :user_email, with: user.email + + click_button 'Create User' + + expect(page).to have_content('created') + end + end + + context 'with invalid details' do + it 'should return to the form and display errors' do + visit signup_path + + fill_in :user_username, with: '' + fill_in :user_password, with: user.password + fill_in :user_email, with: user.email + + click_button 'Create User' + + expect(page).to have_content("can't be blank") + end + end + end + + context 'when signed in' do + let(:user) { create(:user) } + + before(:each) { signin(user: user) } + + it 'should not display the create account link' do + visit root_path + + expect(page).to_not have_link('Create Account') + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c0f2e41..27e54ae 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -16,6 +16,7 @@ require 'capybara/rspec' require 'support/capybara' require 'support/database_cleaner' require 'support/factory_girl' +require 'support/helpers' ActiveRecord::Migration.maintain_test_schema! diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb new file mode 100644 index 0000000..f999766 --- /dev/null +++ b/spec/support/helpers.rb @@ -0,0 +1,5 @@ +require_relative 'helpers/session_helpers' + +RSpec.configure do |config| + config.include SessionHelpers, type: :feature +end diff --git a/spec/support/helpers/session_helpers.rb b/spec/support/helpers/session_helpers.rb new file mode 100644 index 0000000..fd03c06 --- /dev/null +++ b/spec/support/helpers/session_helpers.rb @@ -0,0 +1,27 @@ +module SessionHelpers + def signin(**opts) + if opts[:user] + username = opts[:user].username + password = opts[:user].password + else + username = opts[:username] + password = opts[:password] + end + + visit root_path + + if page.has_link? 'Sign In' + click_link 'Sign In' + + fill_in :user_session_username, with: username + fill_in :user_session_password, with: password + + click_button 'Create User session' + end + end + + def signout + visit root_path + click_link 'Sign Out' if page.has_link? 'Sign Out' + end +end