ruby-on-rails - Pundit::NotDefinedError:无法找到策略 `UserPolicy`

标签 ruby-on-rails ruby ruby-on-rails-5 pundit

我一定做错了什么,或者我需要眼镜。我在这里关注本教程:

http://vaidehijoshi.github.io/blog/2015/09/29/using-pundit-the-cool-kid-of-authorization/

我已按照说明在 app/policies 文件夹中创建了 application_policy.rb 文件和 user_policy.rb 文件。

Pundit 似乎没有在 IntegrationTests 中检测到我的 UserPolicy 文件。

我的设置

  • Ruby on Rails 5 RC1
  • ruby 2.2.4p230
  • 敲击 gem 进行 JWT 身份验证
  • 专家1.1.0

用户策略.rb

class UserPolicy < ApplicationPolicy    
  def update?
    user == resource
  end
end

在我的 UserController 中,我定义了 REST API 操作。目前,我只是测试“更新”操作:

用户 Controller

def update
  user = User.find_by({id: params[:id]})

  authorize user

  render json: { error: "Failed to find" }, status: :not_found and return unless user

  if user.update!(user_params)
    render json: user
  else
    render json: { error: "Not found" }, status: :not_found
  end
end

users_controller_test.rb

test "update other user's data should raise NotAuthorized exception" do
  @user_two = users(:two)

  put user_path(@user_two.id), params: { first_name: "Jim" }, headers: @authorization_header
  assert_response :success # forcing fail test for now to test plumbing
end

我收到以下错误:

.E

Error:
UsersControllerTest#test_update_other_user's_data_should_raise_NotAuthorized_exception:
Pundit::NotDefinedError: unable to find policy `UserPolicy` for `#<User id: 298486374, first_name: "MyString", last_name: "MyString", email: "MyString", password_digest: "MyString", created_at: "2016-05-27 13:30:07", updated_at: "2016-05-27 13:30:07", role_id: nil>`
    app/controllers/users_controller.rb:42:in `update'
    test/controllers/users_controller_test.rb:60:in `block in <class:UsersControllerTest>'

你知道我可能做错了什么吗?

编辑

如果有任何帮助,我的 UserControllerTest 文件如下所示:

require 'test_helper'

class UsersControllerTest < ActionDispatch::IntegrationTest
  def authenticate
    token = Knock::AuthToken.new(payload: { sub: users(:one).id }).token
    @authorization_header = { HTTP_AUTHORIZATION: "Bearer #{token}" }
  end

  setup do
    # load "#{Rails.root}/db/seeds.rb"
    Rails.application.load_seed
    authenticate
    @user = users(:one)
  end

  test "logged in user should return ok" do
    get users_path, headers: @authorization_header
    assert_response :ok
  end

  test "not logged in user should return unauthorized" do
    get users_path
    assert_response :unauthorized
  end

  test "user json should not contain password_digest" do
    get user_path(@user.id), headers: @authorization_header
    assert_response :ok
    json = JSON.parse(response.body)
    assert json.key?("password_digest") == false
  end

  test "create user without authorization header should return created" do
    user = {
      first_name: "Bob",
      last_name: "Brown",
      email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2808d80a2858f838b8ecc818d8f" rel="noreferrer noopener nofollow">[email protected]</a>",
      password: "abc",
      password_confirmation: "abc"
    }

    post users_path, params: user
    assert_response :created
    json = JSON.parse(response.body)
    assert !json.empty?
  end

  test "update user should return ok" do
    put user_path(@user.id), params: { first_name: "Bob"}, headers: @authorization_header
    assert_response :ok

    updated_user = User.find_by({id: @user.id})

    assert_equal "Bob", updated_user.first_name
  end

  test "update other user's data should raise NotAuthorized exception" do
    @user_two = users(:two)

    put user_path(@user_two.id), params: { first_name: "Jim" }, headers: @authorization_header
    assert_response :success
  end

  test "delete user shoudl return ok" do
    assert_difference "User.count", -1 do
      delete user_path(@user.id), headers: @authorization_header
    end
  end
end

在一小时前添加 Pundit 内容之前,我的所有测试都已通过。

最佳答案

为了解决这个问题,我跑了:

bin/spring stop
bin/spring binstub --remove --all
bundle update spring
bundle exec spring binstub --all

关于ruby-on-rails - Pundit::NotDefinedError:无法找到策略 `UserPolicy`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37485049/

相关文章:

Ruby - 寻找子数组之间的最大差异

ruby-on-rails - 自定义 Ruby 方法不适用于 nil 对象

ruby-on-rails - 当文件存在时,如何更改 simple_form 中文件字段的名称?

ruby - Rails 3 查询 : Find all Posts with the same Topic

ruby-on-rails - 找不到 libxml2 的包配置

ruby-on-rails - Rails 5.1 + Webpack .... CSS 中的图像?

ruby-on-rails - Rails 将大型表单分解为多个步骤的方法?

ruby-on-rails - 如何通过多个模型建立关联

ruby-on-rails - 如何建立/维护 2 个用户之间的亲和性分数?

ruby-on-rails - Ruby on Rails 4 每当 cron 作业不工作时