In my app.rb
file i have the following setup for current_user
class Application < Sinatra::Base
include Pundit
use JWTAuthorization
def current_user
env[:user]
end
delete '/users/:user_id' do
user = User.find(params[:user_id])
no_data! unless user
authorize user, :edit?
user.destroy
response = {status: 200, data:'success, user deleted.'}
json response
end
end
The problem is I need to setup the current_user
for my unit testing. Otherwise the test will throw an error inside authorize user, :edit?
or any other Pundit
authorization code because current_user
will return nill
.
How can I setup env[:user]
inside my spec?
Is there a way i can mock env[:user]
with a value using rspec or use rack_env={}
to setup?
This is what i tried so far in my spec file without success:
before(:each) do
@user = User.create(name: 'Oswaldinho',email: 'waldinho@com.com',role: 'user')
end
it 'deletes an existing user ' do
delete "/users/#{@user.id}", params={}, rack_env={user: @user}
expect(last_response).to be_ok
end