1
0
Fork 0
mirror of https://github.com/varvet/pundit.git synced 2022-11-09 12:30:11 -05:00
varvet--pundit/spec/spec_helper.rb

272 lines
4.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "pundit"
require "pundit/rspec"
require "rack"
require "rack/test"
require "pry"
require "active_support"
require "active_support/core_ext"
require "active_model/naming"
require "action_controller/metal/strong_parameters"
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
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
def update?
post.user == user
end
2016-01-14 09:15:30 -05:00
def destroy?
false
end
2016-01-14 09:15:30 -05:00
def show?
true
end
2016-01-14 09:15:30 -05:00
def permitted_attributes
if post.user == user
%i[title votes]
else
[:votes]
end
end
def permitted_attributes_for_revise
[:body]
end
end
2016-01-14 09:15:30 -05:00
class Post < Struct.new(:user)
def self.published
:published
end
2016-01-14 09:15:30 -05:00
def self.read
:read
end
2016-01-14 09:15:30 -05:00
def to_s
"Post"
end
def inspect
"#<Post>"
end
end
module Customer
class Post < Post
def model_name
2016-01-14 09:15:30 -05:00
OpenStruct.new(param_key: "customer_post")
end
2016-01-14 09:15:30 -05:00
def self.policy_class
PostPolicy
end
def policy_class
self.class.policy_class
end
end
end
2016-01-14 09:15:30 -05: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
CommentScope.new(scope)
2016-01-14 09:15:30 -05:00
end
end
end
class PublicationPolicy < Struct.new(:user, :publication)
class Scope < Struct.new(:user, :scope)
def resolve
scope.published
end
end
def create?
true
end
end
2016-01-14 09:15:30 -05:00
class Comment
extend ActiveModel::Naming
end
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
end
class Article; end
class BlogPolicy < Struct.new(:user, :blog); end
2016-01-14 09:15:30 -05:00
class Blog; end
2016-01-14 09:15:30 -05: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
class ArticleTag
def self.policy_class
2016-01-14 09:15:30 -05:00
ArticleTagOtherNamePolicy
end
end
class CriteriaPolicy < Struct.new(:user, :criteria); end
module Project
class CommentPolicy < Struct.new(:user, :comment)
class Scope < Struct.new(:user, :scope)
def resolve
scope
end
end
end
class CriteriaPolicy < Struct.new(:user, :criteria); end
class PostPolicy < Struct.new(:user, :post)
class Scope < Struct.new(:user, :scope)
def resolve
scope.read
end
end
end
end
class DenierPolicy < Struct.new(:user, :record)
def update?
false
end
end
class Controller
include Pundit
# Mark protected methods public so they may be called in test
# rubocop:disable Layout/AccessModifierIndentation, Style/AccessModifierDeclarations
public(*Pundit.protected_instance_methods)
# rubocop:enable Layout/AccessModifierIndentation, Style/AccessModifierDeclarations
attr_reader :current_user, :action_name, :params
def initialize(current_user, action_name, params)
@current_user = current_user
@action_name = action_name
@params = params
end
end
class NilClassPolicy < Struct.new(:user, :record)
class Scope
def initialize(*)
raise Pundit::NotDefinedError, "Cannot scope NilClass"
end
end
def show?
false
end
def destroy?
false
end
end
class Wiki; end
class WikiPolicy
class Scope
# deliberate typo method
def initalize; end
end
end
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
class PostFourFiveSix < Struct.new(:user); end
2016-01-14 09:15:30 -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
class CriteriaFourFiveSixPolicy < Struct.new(:user, :criteria); end
2016-01-14 09:15:30 -05:00
class PostFourFiveSixPolicy < Struct.new(:user, :post); end
2016-01-14 09:15:30 -05:00
class TagFourFiveSix < Struct.new(:user); end
2016-01-14 09:15:30 -05:00
class TagFourFiveSixPolicy < Struct.new(:user, :tag); end
2016-01-14 09:15:30 -05:00
class AvatarFourFiveSix; extend ActiveModel::Naming; end
2016-01-14 09:15:30 -05:00
class AvatarFourFiveSixPolicy < Struct.new(:user, :avatar); end
end