{"id":7588,"date":"2015-08-29T13:56:31","date_gmt":"2015-08-29T13:56:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/08\/29\/how-can-i-use-rspec-to-test-the-response-code-on-a-cancan-failed-authorization-open-source-projects-ryanb-cancan\/"},"modified":"2015-08-29T13:56:31","modified_gmt":"2015-08-29T13:56:31","slug":"how-can-i-use-rspec-to-test-the-response-code-on-a-cancan-failed-authorization-open-source-projects-ryanb-cancan","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/08\/29\/how-can-i-use-rspec-to-test-the-response-code-on-a-cancan-failed-authorization-open-source-projects-ryanb-cancan\/","title":{"rendered":"How can I use RSpec to test the response code on a CanCan failed authorization?-open source projects ryanb\/cancan"},"content":{"rendered":"<p>I&#8217;m working on a rails project in which I use CanCan to authorize my resources. When a user is not signed in and tries to submit a &#8220;talk&#8221; (via an ajax form submission), CanCan correctly raises a 401 with <code>{\"status\":\"error\",\"message\":\"You must be logged in to do that!\"}<\/code> as the response (I have verified this in the browser using firebug). However, in my tests get a 302 response code rather than a 401:<\/p>\n<pre><code>class TalksController &lt; ApplicationController\n  authorize_resource\n\n  def create\n    @talk = current_user.talks.build(params[:talk])\n\n    respond_to do |format|\n      if @talk.save\n        response = { :redirect =&gt; talk_path(@talk) }\n        format.html { redirect_to @talk, notice: 'Talk was successfully created.' }\n        format.json { render json: response, status: :created,  }\n      else\n        format.html { render action: \"new\" }\n        format.json { render json: @talk.errors, status: :unprocessable_entity }\n      end\n    end\n  end\nend\n<\/code><\/pre>\n<p>talks_controller_spec.rb:<\/p>\n<pre><code>describe TalksController do\n  describe \"POST create\" do\n    context \"when not signed in\" do\n      it \"should not assign talk\" do\n        post :create\n        assigns[:talk].should be_nil\n      end\n      it \"should respond with a 401\" do\n        post :create\n        response.response_code.should == 401\n      end\n    end\n  end\nend\n<\/code><\/pre>\n<p>The first example included here is successful (assigns[:talk] does not get assigned), but the second is not:<\/p>\n<pre><code>1) TalksController POST create when not signed in should respond with a 401\n     Failure\/Error: response.response_code.should == 401\n       expected: 401\n            got: 302 (using ==)\n     # .\/spec\/controllers\/talks_controller_spec.rb:53:in `block (4 levels) in '\n<\/code><\/pre>\n<p>I&#8217;m not sure exactly what&#8217;s going on. Is there a way I can test for the actual response code returned to the browser? Or a better way I can test the authorization?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m working on a rails project in which I use CanCan to authorize my resources. When a user is not signed in and tries to submit a &#8220;talk&#8221; (via an ajax form submission), CanCan correctly raises a 401 with {&#8220;status&#8221;:&#8221;error&#8221;,&#8221;message&#8221;:&#8221;You must be logged in to do that!&#8221;} as the response (I have verified this in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7588","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7588"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7588\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}