Add some basic integration testing

Rushed but it'll work for now
This commit is contained in:
Andrew Tomaka 2015-07-14 01:22:50 -04:00
parent 2cac3d9f1c
commit 7975ec2012
10 changed files with 264 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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!

5
spec/support/helpers.rb Normal file
View file

@ -0,0 +1,5 @@
require_relative 'helpers/session_helpers'
RSpec.configure do |config|
config.include SessionHelpers, type: :feature
end

View file

@ -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