<commit_before>class Ability
  include CanCan::Ability
  include PublishersHelper

  ROLES = %i(admin publisher)
  if Rails.application.secrets[:admin_ip_whitelist]
    ADMIN_IP_WHITELIST = Rails.application.secrets[:admin_ip_whitelist].split(",").map { |ip_cidr| IPAddr.new(ip_cidr) }.freeze
  else
    ADMIN_IP_WHITELIST = [].freeze
  end

  def initialize(publisher, ip)
    @publisher = publisher || Publisher.new
    @ip = ip

    alias_action :create,   :read, :update, :destroy, to: :crud
    alias_action :read,     :create,                  to: :cr
    alias_action :destroy,  :update,                  to: :modify

    ROLES.each { |role| send(role) if @publisher.role.to_sym == role }
  end

  private

  def base_role
    can :cr,              Publisher
    can :modify,          Publisher, id: @publisher.id
  end

  def publisher
    base_role
  end

  def admin
    raise AdminNotOnIPWhitelistError.new("Administrator must be IP whitelisted") unless admin_ip_whitelisted?
    if Rails.env.production?
      raise U2fDisabledError.new("U2F must be enabled for administrators") unless u2f_enabled?(@publisher)
    end
    can :manage, :all
    can :access, :all
  end

  def admin_ip_whitelisted?
    return true if ADMIN_IP_WHITELIST.blank? && (Rails.env.development? || Rails.env.test?)
    admin_ip_whitelisted = ADMIN_IP_WHITELIST.any? { |ip_addr| ip_addr.include?(@ip) }
    admin_ip_whitelisted
  end

  class U2fDisabledError < RuntimeError
  end

  class AdminNotOnIPWhitelistError < RuntimeError
  end

  class TwoFactorDisabledError < RuntimeError
  end
end<commit_msg>Run U2F checks in test env<commit_after>class Ability
  include CanCan::Ability
  include PublishersHelper

  ROLES = %i(admin publisher)
  if Rails.application.secrets[:admin_ip_whitelist]
    ADMIN_IP_WHITELIST = Rails.application.secrets[:admin_ip_whitelist].split(",").map { |ip_cidr| IPAddr.new(ip_cidr) }.freeze
  else
    ADMIN_IP_WHITELIST = [].freeze
  end

  def initialize(publisher, ip)
    @publisher = publisher || Publisher.new
    @ip = ip

    alias_action :create,   :read, :update, :destroy, to: :crud
    alias_action :read,     :create,                  to: :cr
    alias_action :destroy,  :update,                  to: :modify

    ROLES.each { |role| send(role) if @publisher.role.to_sym == role }
  end

  private

  def base_role
    can :cr,              Publisher
    can :modify,          Publisher, id: @publisher.id
  end

  def publisher
    base_role
  end

  def admin
    raise AdminNotOnIPWhitelistError.new("Administrator must be IP whitelisted") unless admin_ip_whitelisted?
    if Rails.env.production? || Rails.env.test?
      raise U2fDisabledError.new("U2F must be enabled for administrators") unless u2f_enabled?(@publisher)
    end
    can :manage, :all
    can :access, :all
  end

  def admin_ip_whitelisted?
    return true if ADMIN_IP_WHITELIST.blank? && (Rails.env.development? || Rails.env.test?)
    admin_ip_whitelisted = ADMIN_IP_WHITELIST.any? { |ip_addr| ip_addr.include?(@ip) }
    admin_ip_whitelisted
  end

  class U2fDisabledError < RuntimeError
  end

  class AdminNotOnIPWhitelistError < RuntimeError
  end
end
{'fraction_non_alphanumeric': 0.09294973126778375, 'fraction_numerical': 0.00284539993676889, 'mean_word_length': 3.2872628726287263, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5141884', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1067, 'n_words': 249}