request.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. module ZammadSpecSupportRequest
  2. # This ruby meta programming action creates the methods to perform:
  3. # GET, POST, PATCH, PUT, DELETE and HEAD
  4. # HTTP "requests".
  5. # They overwrite the ones of `ActionDispatch::Integration::RequestHelpers`
  6. # to add the headers set by #add_headers before
  7. %i[get post patch put delete head].each do |method_id|
  8. define_method(method_id) do |path, **args|
  9. headers = Hash(headers).merge(Hash(@headers))
  10. super(path, headers: headers, **args)
  11. end
  12. end
  13. # Adds one or more HTTP headers to all requests of the current example.
  14. #
  15. # @param [Hash{String => String}] headers Hash of HTTP headers
  16. #
  17. # @example
  18. # add_headers('Eg Some X-Header' => 'Some value')
  19. # @example
  20. # add_headers(
  21. # 'Header 1' => 'Some value',
  22. # 'Header 2' => 'Some value',
  23. # ...
  24. # )
  25. #
  26. # @return [Hash] The current headers Hash
  27. def add_headers(headers)
  28. @headers = Hash(@headers).merge(headers)
  29. end
  30. # Parses the response.body as JSON.
  31. #
  32. # @example
  33. # json_response
  34. # @example
  35. # json_response.is_a?(Array)
  36. #
  37. # @return [Array, Hash, ...] Parsed JSON structure as Ruby object
  38. def json_response
  39. JSON.parse(response.body)
  40. end
  41. # Authenticates all requests of the current example as the given user.
  42. #
  43. # @example
  44. # authenticated_as(some_admin_user)
  45. #
  46. # @return nil
  47. def authenticated_as(user)
  48. # mock authentication otherwise login won't
  49. # if user has no password (which is expensive to create)
  50. if user.password.nil?
  51. allow(User).to receive(:authenticate).with(user.login, '').and_return(user)
  52. end
  53. credentials = ActionController::HttpAuthentication::Basic.encode_credentials(user.login, user.password)
  54. add_headers('Authorization' => credentials)
  55. end
  56. # Provides a Hash of attributes for the given FactoryBot
  57. # factory parameters which can be used as the params payload.
  58. # Note that the attributes are "cleaned" so no created_by_id etc.
  59. # is present.
  60. #
  61. # @see FactoryBot#attributes_for
  62. #
  63. # @example
  64. # attributes_params_for(:admin_user, email: 'custom@example.com')
  65. # # => {firstname: 'Nicole', email: 'custom@example.com', ...}
  66. #
  67. # @return [Hash{Symbol => <String, Array, Hash>}] request cleaned attributes
  68. def attributes_params_for(*args)
  69. filter_unused_params(attributes_for(*args))
  70. end
  71. # Provides a Hash of attributes for the given Model instance which can
  72. # be used as the params payload.
  73. # Note that the attributes are "cleaned" so no created_by_id etc.
  74. # is present.
  75. #
  76. # @param [Hash] instance An ActiveRecord instance
  77. #
  78. # @example
  79. # cleaned_params_for(some_admin_user)
  80. # # => {firstname: 'Nicole', email: 'admin@example.com', ...}
  81. #
  82. # @return [Hash{Symbol => <String, Array, Hash>}] request cleaned attributes
  83. def cleaned_params_for(instance)
  84. filter_unused_params(instance.attributes)
  85. end
  86. # This is a self explaining internal method.
  87. #
  88. # @see ApplicationModel#filter_unused_params
  89. def filter_unused_params(unfiltered)
  90. # let's get private
  91. ApplicationModel.send(:filter_unused_params, unfiltered)
  92. end
  93. end
  94. RSpec.configure do |config|
  95. config.include ZammadSpecSupportRequest, type: :request
  96. end