Skip to content

Commit

Permalink
Update specs and dependencies for RSpec 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
futhr committed Aug 11, 2014
1 parent defc9df commit a2aba48
Show file tree
Hide file tree
Showing 35 changed files with 194 additions and 216 deletions.
4 changes: 3 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
--color
--color
-r spec_helper
-f documentation
8 changes: 3 additions & 5 deletions spec/controllers/spree/admin/orders_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
require 'spec_helper'

module Spree
module Admin
describe OrdersController do
RSpec.describe OrdersController, type: :controller do
stub_authorization!

context '#authorize_admin' do
it 'grant access to users with an admin role' do
it 'grants access to users with an admin role' do
spree_get :new
expect(response).to redirect_to spree.edit_admin_order_path Order.last
expect(response).to redirect_to spree.edit_admin_order_path(Order.last)
end
end
end
Expand Down
73 changes: 33 additions & 40 deletions spec/controllers/spree/checkout_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
require 'spec_helper'
RSpec.describe Spree::CheckoutController, type: :controller do

describe Spree::CheckoutController do
let(:order) { create(:order_with_totals, email: nil, user: nil) }
let(:user) { mock_model Spree::User, last_incomplete_spree_order: nil, has_spree_role?: true, spree_api_key: 'fake' }
let(:user) { build(:user, spree_api_key: 'fake') }
let(:token) { 'some_token' }

before do
controller.stub current_order: order
order.stub confirmation_required?: true
allow(controller).to receive(:current_order) { order }
allow(order).to receive(:confirmation_required?) { true }
end

context '#edit' do
context 'when registration step enabled' do
before do
controller.stub :check_authorization
allow(controller).to receive(:check_authorization)
Spree::Auth::Config.set(registration_step: true)
end

context 'when authenticated as registered user' do
before { controller.stub spree_current_user: user }

it 'proceed to the first checkout step' do
user.should_receive(:ship_address)
before { allow(controller).to receive(:spree_current_user) { user } }

it 'proceeds to the first checkout step' do
spree_get :edit, { state: 'address' }
expect(response).to render_template :edit
end
end

context 'when authenticated as guest' do
it 'redirect to registration step' do
it 'redirects to registration step' do
spree_get :edit, { state: 'address' }
expect(response).to redirect_to spree.checkout_registration_path
end
Expand All @@ -39,22 +36,20 @@
context 'when registration step disabled' do
before do
Spree::Auth::Config.set(registration_step: false)
controller.stub :check_authorization
allow(controller).to receive(:check_authorization)
end

context 'when authenticated as registered' do
before { controller.stub spree_current_user: user }

it 'proceed to the first checkout step' do
user.should_receive(:ship_address)
before { allow(controller).to receive(:spree_current_user) { user } }

it 'proceeds to the first checkout step' do
spree_get :edit, { state: 'address' }
expect(response).to render_template :edit
end
end

context 'when authenticated as guest' do
it 'proceed to the first checkout step' do
it 'proceeds to the first checkout step' do
spree_get :edit, { state: 'address' }
expect(response).to render_template :edit
end
Expand All @@ -69,15 +64,13 @@
order.update_column(:state, 'confirm')

# So that the order can transition to complete successfully
order.stub payment_required?: false
allow(order).to receive(:payment_required?) { false }
end

context 'with a token' do
before do
order.stub guest_token: 'ABC'
end
before { allow(order).to receive(:guest_token) { 'ABC' } }

it 'redirect to the tokenized order view' do
it 'redirects to the tokenized order view' do
request.cookie_jar.signed[:guest_token] = 'ABC'
spree_post :update, { state: 'confirm' }
expect(response).to redirect_to spree.token_order_path(order, 'ABC')
Expand All @@ -87,13 +80,13 @@

context 'with a registered user' do
before do
controller.stub spree_current_user: user
order.stub user: user
order.stub guest_token: nil
allow(controller).to receive(:spree_current_user) { user }
allow(order).to receive(:user) { user }
allow(order).to receive(:guest_token) { nil }
end

it 'redirect to the standard order view' do
spree_post :update, { :state => 'confirm' }
it 'redirects to the standard order view' do
spree_post :update, { state: 'confirm' }
expect(response).to redirect_to spree.order_path(order)
end
end
Expand All @@ -102,20 +95,20 @@

context '#registration' do
it 'does not check registration' do
controller.stub :check_authorization
controller.should_not_receive :check_registration
allow(controller).to receive(:check_authorization)
expect(controller).not_to receive(:check_registration)
spree_get :registration
end

it 'check if the user is authorized for :edit' do
controller.should_receive(:authorize!).with(:edit, order, token)
it 'checks if the user is authorized for :edit' do
expect(controller).to receive(:authorize!).with(:edit, order, token)
request.cookie_jar.signed[:guest_token] = token
spree_get :registration, {}
end
end

context '#update_registration' do
let(:user) { user = mock_model Spree::User }
let(:user) { build(:user) }

it 'does not check registration' do
controller.stub :check_authorization
Expand All @@ -124,24 +117,24 @@
spree_put :update_registration, { order: { } }
end

it 'render the registration view if unable to save' do
controller.stub :check_authorization
it 'renders the registration view if unable to save' do
allow(controller).to receive(:check_authorization)
spree_put :update_registration, { order: { email: 'invalid' } }
expect(flash[:registration_error]).to eq I18n.t(:email_is_invalid, scope: [:errors, :messages])
expect(response).to render_template :registration
end

it 'redirect to the checkout_path after saving' do
order.stub update_attributes: true
controller.stub :check_authorization
it 'redirects to the checkout_path after saving' do
allow(order).to receive(:update_attributes) { true }
allow(controller).to receive(:check_authorization)
spree_put :update_registration, { order: { email: '[email protected]' } }
expect(response).to redirect_to spree.checkout_path
end

it 'check if the user is authorized for :edit' do
it 'checks if the user is authorized for :edit' do
request.cookie_jar.signed[:guest_token] = token
order.stub update_attributes: true
controller.should_receive(:authorize!).with(:edit, order, token)
allow(order).to receive(:update_attributes) { true }
expect(controller).to receive(:authorize!).with(:edit, order, token)
spree_put :update_registration, { order: { email: '[email protected]' } }
end
end
Expand Down
17 changes: 8 additions & 9 deletions spec/controllers/spree/products_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
require 'spec_helper'
RSpec.describe Spree::ProductsController, type: :controller do

describe Spree::ProductsController do
let!(:product) { create(:product, available_on: 1.year.from_now) }
let!(:user) { mock_model(Spree::User, spree_api_key: 'fake', last_incomplete_spree_order: nil) }
let!(:user) { build(:user, spree_api_key: 'fake') }

it 'allows admins to view non-active products' do
controller.stub :before_save_new_order
controller.stub spree_current_user: user
user.stub has_spree_role?: true
allow(controller).to receive(:before_save_new_order)
allow(controller).to receive(:spree_current_user) { user }
allow(user).to receive(:has_spree_role?) { true }
spree_get :show, id: product.to_param
expect(response.status).to eq(200)
end

it 'cannot view non-active products' do
controller.stub :before_save_new_order
controller.stub spree_current_user: user
user.stub has_spree_role?: false
allow(controller).to receive(:before_save_new_order)
allow(controller).to receive(:spree_current_user) { user }
allow(user).to receive(:has_spree_role?) { false }
spree_get :show, id: product.to_param
expect(response.status).to eq(404)
end
Expand Down
11 changes: 4 additions & 7 deletions spec/controllers/spree/user_passwords_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
require 'spec_helper'
RSpec.describe Spree::UserPasswordsController, type: :controller do

describe Spree::UserPasswordsController do
let(:token) { 'some_token' }

before do
@request.env["devise.mapping"] = Devise.mappings[:spree_user]
end


before { @request.env['devise.mapping'] = Devise.mappings[:spree_user] }

context '#update' do
context 'when updating password with blank password' do
it 'shows error flash message, sets spree_user with token and re-displays password edit form' do
Expand Down
16 changes: 8 additions & 8 deletions spec/controllers/spree/user_registrations_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'spec_helper'
describe Spree::UserRegistrationsController do
before do
@request.env["devise.mapping"] = Devise.mappings[:spree_user]
end
RSpec.describe Spree::UserRegistrationsController, type: :controller do

before { @request.env['devise.mapping'] = Devise.mappings[:spree_user] }

context '#create' do
before { controller.stub(:after_sign_up_path_for).and_return(spree.root_path(thing: 7)) }
it 'should redirect to after_sign_up_path_for' do
spree_post :create, { :spree_user => { :email => '[email protected]', :password => 'foobar123', :password_confirmation => 'foobar123' } }
before { allow(controller).to receive(:after_sign_up_path_for).and_return(spree.root_path(thing: 7)) }

it 'redirects to after_sign_up_path_for' do
spree_post :create, { spree_user: { email: '[email protected]', password: 'foobar123', password_confirmation: 'foobar123' } }
expect(response).to redirect_to spree.root_path(thing: 7)
end
end
Expand Down
12 changes: 4 additions & 8 deletions spec/controllers/spree/user_sessions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
require 'spec_helper'
RSpec.describe Spree::UserSessionsController, type: :controller do

describe Spree::UserSessionsController do
let(:user) { create(:user) }
before do
@request.env["devise.mapping"] = Devise.mappings[:spree_user]
end

context "#create" do
before { @request.env['devise.mapping'] = Devise.mappings[:spree_user] }

context "#create" do
context "using correct login information" do
it 'properly assigns orders user from guest_token' do
order1 = create(:order, guest_token: 'ABC', user_id: nil, created_by_id: nil)
Expand All @@ -23,7 +20,6 @@
context "and html format is used" do
it "redirects to default after signing in" do
spree_post :create, spree_user: { email: user.email, password: 'secret' }

expect(response).to redirect_to spree.root_path
end
end
Expand All @@ -44,7 +40,7 @@
it "renders new template again with errors" do
spree_post :create, spree_user: { email: user.email, password: 'wrong' }
expect(response).to render_template('new')
expect(flash[:error]).to eq I18n.t('devise.failure.invalid')
expect(flash[:error]).to eq I18n.t(:'devise.failure.invalid')
end
end

Expand Down
19 changes: 8 additions & 11 deletions spec/controllers/spree/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
require 'spec_helper'
RSpec.describe Spree::UsersController, type: :controller do

describe Spree::UsersController do
let(:admin_user) { create(:user) }
let(:user) { create(:user) }
let(:role) { create(:role) }

before do
controller.stub spree_current_user: user
end
before { allow(controller).to receive(:spree_current_user) { user } }

context '#load_object' do
it 'should redirect to signup path if user is not found' do
controller.stub(:spree_current_user => nil)
spree_put :update, { :user => { :email => '[email protected]' } }
response.should redirect_to('/login')
it 'redirects to signup path if user is not found' do
allow(controller).to receive(:spree_current_user) { nil }
spree_put :update, { user: { email: '[email protected]' } }
expect(response).to redirect_to('/login')
end
end

context '#create' do
it 'create a new user' do
it 'creates a new user' do
spree_post :create, { user: { email: '[email protected]', password: 'foobar123', password_confirmation: 'foobar123' } }
expect(assigns[:user].new_record?).to be false
end
end

context '#update' do
context 'when updating own account' do
it 'perform update' do
it 'performs update' do
spree_put :update, { user: { email: '[email protected]' } }
expect(assigns[:user].email).to eq '[email protected]'
expect(response).to redirect_to spree.account_url(only_path: true)
Expand Down
6 changes: 2 additions & 4 deletions spec/features/account_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'spec_helper'

feature 'Accounts' do
RSpec.feature 'Accounts', type: :feature do

context 'editing' do
scenario 'can edit an admin user' do
Expand All @@ -21,7 +19,7 @@

fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'password'
fill_in 'Password Confirmation', :with => 'password'
fill_in 'Password Confirmation', with: 'password'
click_button 'Create'

click_link 'My Account'
Expand Down
12 changes: 5 additions & 7 deletions spec/features/admin/orders_spec.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
require 'spec_helper'
RSpec.feature 'Admin orders', type: :feature do

feature 'Admin orders' do
background do
sign_in_as! create(:admin_user)
end

# regression #203
# Regression #203
scenario 'can lists orders' do
expect { visit spree.admin_orders_path }.not_to raise_error
end

# regression #203
# Regression #203
scenario 'can new orders' do
expect { visit spree.new_admin_order_path }.not_to raise_error
end

# regression #203
# Regression #203
scenario 'can not edit orders' do
expect { visit spree.edit_admin_order_path('nodata') }.to raise_error(ActiveRecord::RecordNotFound)
end

# regression #203
# Regression #203
scenario 'can edit orders' do
create(:order, number: 'R123')
visit spree.edit_admin_order_path('R123')
expect(page).not_to have_text 'Authorization Failure'
end

end
Loading

0 comments on commit a2aba48

Please sign in to comment.