Pundit causes 3022

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Pundit causes 3022

Ralph Shnelvar
This is a continuation of
Rspec. Is 302 a success? Why 302?
https://groups.google.com/forum/#!topic/rubyonrails-talk/Merk1uUyhyc


Setting up the relevant code

In config/routes.rb I have
Rails.application.routes.draw do
  resources
:sql_statements
  resources
:articles
 
  devise_for
:users
  root
'static_pages#root'
  resources
:users

 
# root to: "articles#index"
 
# byebug
end

In spec/controllers/sql_statements_controller_spec.rb I have
  describe "GET #new" do
    it
"returns a success response" do
     
get :new, params: {}, session: valid_session
      puts
; p_here2(response.status)
      byebug
      expect
(response).to be_success
   
end
 
end

In app/controllers/sql_statements_controller.rb I have
  # GET /sql_statements/new
 
def new
   
@sql_statement = SqlStatement.new
    authorize
@sql_statement # I think this causes a 302
 
end

In app/policies/sql_statement_policy.rb
# See https://code.tutsplus.com/tutorials/authorization-with-pundit--cms-28202

#app/policies/sql_statement_policy.rb
 
class SqlStatementPolicy < ApplicationPolicy
 
def index?
   
true
 
end
 
 
def create?
    user
.present?
 
end
 
 
def update?
   
return true if user.present? && user == sql_statement.user
 
end
 
 
def destroy?
   
return true if user.present? && user == sql_statement.user
 
end
 
 
private
 
   
def sql_statement
      record
   
end
end

p_here2 is debugging code I find extremely useful
def p_here2(*args)
 
# byebug
  caller_text
= caller[0]
  puts caller_text
 
  args
.each_with_index do |value, index|
    printf
("%d: ", index)
    pp value
 
end


Analysis
Let's focus on app/controllers/sql_statements_controller.rb.  The line to focus on is
    authorize @sql_statement # I think this causes a 302

With the line above in, I see this when I run rspec
/home/real-estate-data-mining/spec/controllers/sql_statements_controller_spec.rb:63:in `block (3 levels) in <top (required)>'
0: 302

[60, 69] in /home/real-estate-data-mining/spec/controllers/sql_statements_controller_spec.rb
   60:   describe "GET #new" do
   61:     it "returns a success response" do
   62:       get :new, params: {}, session: valid_session
   63:       puts; p_here2(response.status)
   64:       byebug
=> 65:       expect(response).to be_success
   66:     end
   67:   end
   68:
   69:   describe "GET #edit" do
(byebug)

If I comment out
    # authorize @sql_statement # I think this causes a 302
I see this:
/home/real-estate-data-mining/spec/controllers/sql_statements_controller_spec.rb:63:in `block (3 levels) in <top (required)>'
0: 200

[60, 69] in /home/real-estate-data-mining/spec/controllers/sql_statements_controller_spec.rb
   60:   describe "GET #new" do
   61:     it "returns a success response" do
   62:       get :new, params: {}, session: valid_session
   63:       puts; p_here2(response.status)
   64:       byebug
=> 65:       expect(response).to be_success
   66:     end
   67:   end
   68:
   69:   describe "GET #edit" do
(byebug)

So, I think it is fairly clear Pundit is causing the 302.

Questions

I am a novice in both Pundit and Rspec

Question 1: Why is Pundit causing this "problem"?

Question 2: What is the preferred way to have Rspec go green on the test in spec/controllers/sql_statements_controller_spec.rb ?





--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/561435a7-caa2-4056-89d6-6b2015e94d68%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...