ruby-on-rails - Pundit 工作正常,但为未经授权的用户返回 200 而不是 401
问题描述
我有这样的规格:
it "should let a user destroy their own picture" do
expect do
delete :destroy, { id: p1.id }
puts "\n\nOK resp\n#{response.status}\n\n\n"
end.to change { Picture.count }.by(-1)
end
it "should not let a user delete another user's picture" do
sign_in(user2)
expect do
delete :destroy, { id: p1.id }
puts "\n\nNOT OK resp\n#{response.status}\n\n\n"
end.to change { Picture.count }.by(0)
end
第一个正确返回一个302
(我在销毁后重定向)。第二个正确通过,这意味着用户不能销毁,但它返回一个200
状态码,而不是401
它应该返回的状态码。
在我的ApplicationController
:
class ApplicationController < ActionController::Base
include Pundit
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
def user_not_authorized
render file: "public/401.html", status: :unauthorized
end
...
end
不管有没有这rescue from
条线,我仍然得到一个200
. 似乎 whilepundit
正确地捕获true
或false
在authorize(@obj)
方法中并停止执行,但是当它得到false
.
我怎样才能解决这个问题?
解决方案
推荐阅读
- python - BeautifulSoap 为具有特定类的 div 中的所有 img 获取多个元素
- android - Android后退按钮退出应用程序react-native
- javascript - 将工具提示添加到 Dynamics 365 中子网格中的列名称
- java - Spring Boot 如何选择外部化的弹簧属性文件
- curl - 使用 CURL 重定向后发送表单数据
- python - Python:将函数作为参数传递以初始化对象的方法。Pythonic 与否?
- android - Xamarin Android 使背景透明
- antlr4 - antlr4 等于/赋值歧义
- python - TensorFlow / Keras 在通道上堆叠图像
- java - Fortify High:VAPT Java 中的访问说明符操作问题