2019-08-12 07:34:03 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-10 09:27:32 -04:00
|
|
|
require "pundit"
|
|
|
|
require "pundit/rspec"
|
2014-07-18 10:20:48 -04:00
|
|
|
|
2015-03-27 05:14:09 -04:00
|
|
|
require "rack"
|
|
|
|
require "rack/test"
|
2014-04-23 22:58:38 -04:00
|
|
|
require "pry"
|
2015-03-10 09:27:32 -04:00
|
|
|
require "active_support"
|
2014-04-23 22:58:38 -04:00
|
|
|
require "active_support/core_ext"
|
|
|
|
require "active_model/naming"
|
2015-03-27 05:14:09 -04:00
|
|
|
require "action_controller/metal/strong_parameters"
|
2014-04-23 22:58:38 -04:00
|
|
|
|
2014-07-18 10:20:48 -04:00
|
|
|
I18n.enforce_available_locales = false
|
|
|
|
|
|
|
|
module PunditSpecHelper
|
|
|
|
extend RSpec::Matchers::DSL
|
|
|
|
|
|
|
|
matcher :be_truthy do
|
|
|
|
match do |actual|
|
|
|
|
actual
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.configure do |config|
|
|
|
|
config.include PunditSpecHelper
|
|
|
|
end
|
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class PostPolicy < Struct.new(:user, :post)
|
2016-01-14 09:15:30 -05:00
|
|
|
class Scope < Struct.new(:user, :scope)
|
|
|
|
def resolve
|
|
|
|
scope.published
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
def update?
|
|
|
|
post.user == user
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
def destroy?
|
|
|
|
false
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
def show?
|
|
|
|
true
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-03-27 05:14:09 -04:00
|
|
|
def permitted_attributes
|
|
|
|
if post.user == user
|
2017-12-05 04:46:03 -05:00
|
|
|
%i[title votes]
|
2015-03-27 05:14:09 -04:00
|
|
|
else
|
|
|
|
[:votes]
|
|
|
|
end
|
|
|
|
end
|
2016-01-14 08:43:51 -05:00
|
|
|
|
|
|
|
def permitted_attributes_for_revise
|
|
|
|
[:body]
|
|
|
|
end
|
2014-04-23 22:58:38 -04:00
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class Post < Struct.new(:user)
|
|
|
|
def self.published
|
|
|
|
:published
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2018-06-17 03:26:10 -04:00
|
|
|
def self.read
|
|
|
|
:read
|
|
|
|
end
|
|
|
|
|
2016-01-14 09:15:30 -05:00
|
|
|
def to_s
|
|
|
|
"Post"
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<Post>"
|
|
|
|
end
|
2014-04-23 22:58:38 -04:00
|
|
|
end
|
|
|
|
|
2015-07-13 09:56:55 -04:00
|
|
|
module Customer
|
|
|
|
class Post < Post
|
2015-07-15 05:58:54 -04:00
|
|
|
def model_name
|
2016-01-14 09:15:30 -05:00
|
|
|
OpenStruct.new(param_key: "customer_post")
|
2015-07-13 09:56:55 -04:00
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2017-07-22 12:03:31 -04:00
|
|
|
def self.policy_class
|
2015-07-13 09:56:55 -04:00
|
|
|
PostPolicy
|
|
|
|
end
|
2017-07-22 12:03:31 -04:00
|
|
|
|
|
|
|
def policy_class
|
|
|
|
self.class.policy_class
|
|
|
|
end
|
2015-07-13 09:56:55 -04:00
|
|
|
end
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2017-10-11 18:18:39 -04:00
|
|
|
class CommentScope
|
|
|
|
attr_reader :original_object
|
|
|
|
def initialize(original_object)
|
|
|
|
@original_object = original_object
|
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
original_object == other.original_object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-14 09:15:30 -05:00
|
|
|
class CommentPolicy < Struct.new(:user, :comment)
|
|
|
|
class Scope < Struct.new(:user, :scope)
|
|
|
|
def resolve
|
2017-10-11 18:18:39 -04:00
|
|
|
CommentScope.new(scope)
|
2016-01-14 09:15:30 -05:00
|
|
|
end
|
2014-04-23 22:58:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-19 05:54:01 -05:00
|
|
|
class PublicationPolicy < Struct.new(:user, :publication)
|
|
|
|
class Scope < Struct.new(:user, :scope)
|
|
|
|
def resolve
|
2016-12-19 10:03:40 -05:00
|
|
|
scope.published
|
2016-12-19 05:54:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-14 09:15:30 -05:00
|
|
|
class Comment
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
end
|
|
|
|
|
2015-03-27 13:09:26 -04:00
|
|
|
class CommentsRelation
|
2016-01-14 09:15:30 -05:00
|
|
|
def initialize(empty = false)
|
|
|
|
@empty = empty
|
|
|
|
end
|
|
|
|
|
|
|
|
def blank?
|
|
|
|
@empty
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_name
|
|
|
|
Comment.model_name
|
|
|
|
end
|
2015-03-27 13:09:26 -04:00
|
|
|
end
|
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class Article; end
|
|
|
|
|
|
|
|
class BlogPolicy < Struct.new(:user, :blog); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class Blog; end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class ArtificialBlog < Blog
|
|
|
|
def self.policy_class
|
|
|
|
BlogPolicy
|
|
|
|
end
|
|
|
|
end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
|
|
|
class ArticleTagOtherNamePolicy < Struct.new(:user, :tag)
|
|
|
|
def show?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-23 22:58:38 -04:00
|
|
|
class ArticleTag
|
|
|
|
def self.policy_class
|
2016-01-14 09:15:30 -05:00
|
|
|
ArticleTagOtherNamePolicy
|
2014-04-23 22:58:38 -04:00
|
|
|
end
|
|
|
|
end
|
2014-05-21 22:09:17 -04:00
|
|
|
|
2015-05-08 20:43:48 -04:00
|
|
|
class CriteriaPolicy < Struct.new(:user, :criteria); end
|
2014-07-13 06:34:09 -04:00
|
|
|
|
2014-10-11 09:01:45 -04:00
|
|
|
module Project
|
2018-06-17 03:26:10 -04:00
|
|
|
class CommentPolicy < Struct.new(:user, :comment)
|
|
|
|
class Scope < Struct.new(:user, :scope)
|
|
|
|
def resolve
|
|
|
|
scope
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-08 20:43:48 -04:00
|
|
|
class CriteriaPolicy < Struct.new(:user, :criteria); end
|
2018-06-17 03:26:10 -04:00
|
|
|
|
|
|
|
class PostPolicy < Struct.new(:user, :post)
|
|
|
|
class Scope < Struct.new(:user, :scope)
|
|
|
|
def resolve
|
|
|
|
scope.read
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-11 09:01:45 -04:00
|
|
|
end
|
|
|
|
|
2015-03-26 05:32:20 -04:00
|
|
|
class DenierPolicy < Struct.new(:user, :record)
|
|
|
|
def update?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-21 22:09:17 -04:00
|
|
|
class Controller
|
|
|
|
include Pundit
|
2016-02-02 14:31:43 -05:00
|
|
|
# Mark protected methods public so they may be called in test
|
2018-06-06 14:45:30 -04:00
|
|
|
# rubocop:disable Layout/AccessModifierIndentation, Style/AccessModifierDeclarations
|
2016-02-02 14:31:43 -05:00
|
|
|
public(*Pundit.protected_instance_methods)
|
2018-06-06 14:45:30 -04:00
|
|
|
# rubocop:enable Layout/AccessModifierIndentation, Style/AccessModifierDeclarations
|
2014-05-21 22:09:17 -04:00
|
|
|
|
2016-09-27 11:29:43 -04:00
|
|
|
attr_reader :current_user, :action_name, :params
|
2014-05-21 22:09:17 -04:00
|
|
|
|
2016-09-27 11:29:43 -04:00
|
|
|
def initialize(current_user, action_name, params)
|
2014-05-21 22:09:17 -04:00
|
|
|
@current_user = current_user
|
2016-09-27 11:29:43 -04:00
|
|
|
@action_name = action_name
|
2014-05-21 22:09:17 -04:00
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
end
|
2015-03-26 05:25:21 -04:00
|
|
|
|
2018-06-07 04:57:51 -04:00
|
|
|
class NilClassPolicy < Struct.new(:user, :record)
|
2015-03-26 05:25:21 -04:00
|
|
|
class Scope
|
|
|
|
def initialize(*)
|
2018-06-07 04:57:51 -04:00
|
|
|
raise Pundit::NotDefinedError, "Cannot scope NilClass"
|
2015-03-26 05:25:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-07 04:57:51 -04:00
|
|
|
def show?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy?
|
|
|
|
false
|
2015-03-26 05:25:21 -04:00
|
|
|
end
|
|
|
|
end
|
2015-11-20 22:02:56 -05:00
|
|
|
|
2017-02-18 11:45:00 -05:00
|
|
|
class Wiki; end
|
|
|
|
class WikiPolicy
|
|
|
|
class Scope
|
|
|
|
# deliberate typo method
|
|
|
|
def initalize; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-20 07:18:41 -04:00
|
|
|
class Thread
|
|
|
|
def self.all; end
|
|
|
|
end
|
|
|
|
class ThreadPolicy < Struct.new(:user, :thread)
|
|
|
|
class Scope < Struct.new(:user, :scope)
|
|
|
|
def resolve
|
|
|
|
# deliberate wrong useage of the method
|
|
|
|
scope.all(:unvalid, :parameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class PostFourFiveSix < Struct.new(:user); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class CommentFourFiveSix; extend ActiveModel::Naming; end
|
|
|
|
|
|
|
|
module ProjectOneTwoThree
|
|
|
|
class CommentFourFiveSixPolicy < Struct.new(:user, :post); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class CriteriaFourFiveSixPolicy < Struct.new(:user, :criteria); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class PostFourFiveSixPolicy < Struct.new(:user, :post); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class TagFourFiveSix < Struct.new(:user); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class TagFourFiveSixPolicy < Struct.new(:user, :tag); end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class AvatarFourFiveSix; extend ActiveModel::Naming; end
|
2016-01-14 09:15:30 -05:00
|
|
|
|
2015-11-20 22:02:56 -05:00
|
|
|
class AvatarFourFiveSixPolicy < Struct.new(:user, :avatar); end
|
|
|
|
end
|