tap/spec/controllers/users_controller_spec.rb

157 lines
4.1 KiB
Ruby
Raw Normal View History

2015-10-29 11:50:16 +01:00
# quickpay_user GET /users/:id/quickpay(.:format) users#quickpay
# edit_dagschotel_user GET /users/:id/dagschotel/edit(.:format) users#edit_dagschotel
# edit_user GET /users/:id/edit(.:format) users#edit
# user GET /users/:id(.:format) users#show
# PATCH /users/:id(.:format) users#update
# PUT /users/:id(.:format) users#update
2015-09-22 20:06:50 +02:00
#
2015-09-07 21:15:16 +02:00
describe UsersController, type: :controller do
before :each do
@user = create :user
sign_in @user
end
2015-09-22 20:06:50 +02:00
##########
# SHOW #
##########
2015-09-07 21:15:16 +02:00
describe 'GET show' do
before :each do
get :show, id: @user
end
it 'should be successful' do
expect(response).to have_http_status(200)
end
2015-10-29 11:50:16 +01:00
it 'should render the user' do
expect(response).to render_template(:show)
end
2015-09-07 21:15:16 +02:00
it 'should load the correct user' do
expect(assigns(:user)).to eq(@user)
end
end
2015-09-22 20:06:50 +02:00
############
# UPDATE #
############
2015-09-07 21:15:16 +02:00
describe 'PUT update' do
it 'should load the correct user' do
put :update, id: @user, user: attributes_for(:user)
expect(assigns(:user)).to eq(@user)
end
context 'successful' do
2016-02-05 20:51:19 +01:00
it 'should update privacy' do
2015-09-07 21:15:16 +02:00
new_private = !(@user.private)
put :update, id: @user, user: { private: new_private }
expect(@user.reload.private).to be new_private
2016-02-05 20:51:19 +01:00
expect(flash[:success]).to be_present
2015-09-07 21:15:16 +02:00
end
2015-09-22 20:06:50 +02:00
it 'should update dagschotel' do
product = create :product
put :update, id: @user, user: { dagschotel_id: product.id }
expect(@user.reload.dagschotel).to eq(product)
end
2016-02-05 20:51:19 +01:00
it 'should accept real images' do
file = fixture_file_upload('files/real-image.png', 'image/png')
put :update, id: @user, user: { avatar: file }
expect(flash[:success]).to be_present
end
end
context 'danger zone' do
it 'should warn for NOPs' do
put :update, id: @user, user: {}
expect(flash[:notice]).to be_present
end
it 'should not accept unreal images' do
file = fixture_file_upload('files/unreal-image.svg', 'image/svg+xml')
put :update, id: @user, user: { avatar: file }
expect(flash[:error]).to be_present
end
2015-09-07 21:15:16 +02:00
end
end
2015-09-22 20:06:50 +02:00
#####################
# EDIT_DAGSCHOTEL #
#####################
2015-09-07 21:15:16 +02:00
describe 'GET edit_dagschotel' do
2015-10-29 11:50:16 +01:00
it 'should be successful' do
expect(response).to have_http_status(200)
end
2015-09-07 21:15:16 +02:00
it 'should render the page' do
get :edit_dagschotel, id: @user
expect(response).to render_template(:edit_dagschotel)
2015-10-29 11:50:16 +01:00
end
end
##############
# QUICKPAY #
##############
describe 'GET quickpay' do
describe 'successful' do
before :each do
@dagschotel = create :product, stock: 20
@user.update_attribute(:dagschotel, @dagschotel)
end
it 'should be successful' do
expect(response).to have_http_status(200)
end
it 'should make an order' do
expect{
get :quickpay, id: @user
}.to change{ @user.reload.orders_count }.by(1)
end
describe 'order' do
before :each do
get :quickpay, id: @user
@order = @user.orders.last
end
it 'should contain 1 orderitem' do
expect(@order.order_items.size).to eq 1
end
it 'should have an orderitem with count 1' do
expect(@order.order_items.first.count).to eq 1
end
it 'should contain an orderitem for @dagschotel' do
expect(@order.order_items.first.product).to eq @dagschotel
end
end
end
describe 'failed' do
before :each do
@dagschotel = create :product, stock: 0
@user.update_attribute(:dagschotel, @dagschotel)
end
it 'should fail' do
2015-10-29 11:54:02 +01:00
xhr :get, :quickpay, id: @user
expect(response).to have_http_status(422)
2015-10-29 11:50:16 +01:00
end
it 'should not make an order' do
expect{
get :quickpay, id: @user
}.to_not change{ @user.reload.orders_count }
end
2015-09-07 21:15:16 +02:00
end
end
end