diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 717d8ca..6d159e7 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -33,7 +33,7 @@ class UsersController < ApplicationController end def edit_dagschotel - @user = User.find(params[:user_id]) + @user = User.find(params[:id]) authorize! :update_dagschotel, @user @dagschotel = @user.dagschotel @@ -42,7 +42,7 @@ class UsersController < ApplicationController end def update_dagschotel - user = User.find(params[:user_id]) + user = User.find(params[:id]) authorize! :update_dagschotel, user user.dagschotel = Product.find(params[:product_id]) diff --git a/config/routes.rb b/config/routes.rb index 762a86f..3b54d37 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,9 +20,11 @@ Rails.application.routes.draw do resources :users do resources :orders - get 'quickpay' => 'orders#quickpay' - get 'dagschotel/edit' => 'users#edit_dagschotel', as: 'edit_dagschotel' - get 'dagschotel/:product_id' => 'users#update_dagschotel', as: 'dagschotel' + member do + get 'quickpay' => 'orders#quickpay' + get 'dagschotel/edit' => 'users#edit_dagschotel', as: 'edit_dagschotel' + get 'dagschotel/:product_id' => 'users#update_dagschotel', as: 'dagschotel' + end end resources :products diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb new file mode 100644 index 0000000..8dcd250 --- /dev/null +++ b/spec/controllers/users_controller_spec.rb @@ -0,0 +1,84 @@ +require 'identicon' +require 'faker' + +describe UsersController, type: :controller do + before :each do + @user = create :user + sign_in @user + end + + describe 'GET show' do + before :each do + get :show, id: @user + end + + it 'should be successful' do + expect(response).to render_template(:show) + expect(response).to have_http_status(200) + end + + it 'should load the correct user' do + expect(assigns(:user)).to eq(@user) + end + end + + describe 'GET edit' do + before :each do + get :edit, id: @user + end + + it 'should render the form' do + expect(response).to render_template(:edit) + end + + it 'should load the correct user' do + expect(assigns(:user)).to eq(@user) + end + end + + 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 + it 'should update attributes' do + new_private = !(@user.private) + put :update, id: @user, user: { private: new_private } + expect(@user.reload.private).to be new_private + end + end + end + + describe 'GET index' do + before :each do + get :index + end + + it 'should load an array of all users' do + expect(assigns(:users)).to eq([@user]) + end + + it 'should render the correct template' do + expect(response).to render_template(:index) + expect(response).to have_http_status(200) + end + end + + describe 'GET edit_dagschotel' do + it 'should render the page' do + get :edit_dagschotel, id: @user + expect(response).to render_template(:edit_dagschotel) + expect(response).to have_http_status(200) + end + end + + describe 'GET update_dagschotel' do + it 'should update the dagschotel' do + product = create :product + get :update_dagschotel, id: @user, product_id: product + expect(@user.reload.dagschotel).to eq(product) + end + end +end