<commit_before># AccessMatchersForController
#
# For testing authorize_xxx in controller. 
module AccessMatchersForController
  extend RSpec::Matchers::DSL
  include Warden::Test::Helpers

  EXPECTED_STATUS_CODE_ALLOWED = [200, 201, 302].freeze
  EXPECTED_STATUS_CODE_DENIED = [401, 404].freeze

  def emulate_user(role, membership = nil)
    case role
    when :admin
      user = create(:admin)
      sign_in(user)
    when :user
      user = create(:user)
      sign_in(user)
    when :external
      user = create(:user, external: true)
      sign_in(user)
    when :visitor
      user = nil
    when User
      user = role
      sign_in(user)
    when *Gitlab::Access.sym_options_with_owner.keys # owner, master, developer, reporter, guest
      user = cerate_user_by_membership(role, membership)
      sign_in(user)
    else
      raise ArgumentError, "cannot emulate user #{role}"
    end

    user
  end

  def cerate_user_by_membership(role, membership = nil)
    raise ArgumentError, "cannot emulate #{role} without membership parent" unless membership

    if role == :owner && membership.owner
      user = membership.owner
    else
      user = create(:user)
      membership.public_send(:"add_#{role}", user)
    end
    user
  end

  def description_for(role, type, expected, result)
    "be #{type} for #{role}." \
    " Expected: #{expected.join(',')} Got: #{result}"
  end

  matcher :be_allowed_for do |role|
    match do |action|
      user = emulate_user(role, @membership)
      action.call(user)

      EXPECTED_STATUS_CODE_ALLOWED.include?(response.status)
    end

    chain :of do |membership|
      @membership = membership
    end<commit_msg>Improve description_for<commit_after># AccessMatchersForController
#
# For testing authorize_xxx in controller. 
module AccessMatchersForController
  extend RSpec::Matchers::DSL
  include Warden::Test::Helpers

  EXPECTED_STATUS_CODE_ALLOWED = [200, 201, 302].freeze
  EXPECTED_STATUS_CODE_DENIED = [401, 404].freeze

  def emulate_user(role, membership = nil)
    case role
    when :admin
      user = create(:admin)
      sign_in(user)
    when :user
      user = create(:user)
      sign_in(user)
    when :external
      user = create(:user, external: true)
      sign_in(user)
    when :visitor
      user = nil
    when User
      user = role
      sign_in(user)
    when *Gitlab::Access.sym_options_with_owner.keys # owner, master, developer, reporter, guest
      user = cerate_user_by_membership(role, membership)
      sign_in(user)
    else
      raise ArgumentError, "cannot emulate user #{role}"
    end

    user
  end

  def cerate_user_by_membership(role, membership = nil)
    raise ArgumentError, "cannot emulate #{role} without membership parent" unless membership

    if role == :owner && membership.owner
      user = membership.owner
    else
      user = create(:user)
      membership.public_send(:"add_#{role}", user)
    end
    user
  end

  def description_for(role, type, expected, result)
    "be #{type} for #{role}. Expected: #{expected.join(',')} Got: #{result}"
  end

  matcher :be_allowed_for do |role|
    match do |action|
      user = emulate_user(role, @membership)
      action.call(user)

      EXPECTED_STATUS_CODE_ALLOWED.include?(response.status)
    end

    chain :of do |membership|
      @membership = membership
    end
{'fraction_non_alphanumeric': 0.09847351092487279, 'fraction_numerical': 0.008979347500748278, 'mean_word_length': 3.2143757881462798, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6739886', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1043, 'n_words': 274}