2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2018-07-09 06:51:58 +00:00
|
|
|
module ZammadSpecSupportRequest
|
|
|
|
|
|
|
|
# This ruby meta programming action creates the methods to perform:
|
|
|
|
# GET, POST, PATCH, PUT, DELETE and HEAD
|
|
|
|
# HTTP "requests".
|
|
|
|
# They overwrite the ones of `ActionDispatch::Integration::RequestHelpers`
|
|
|
|
# to add the headers set by #add_headers before
|
|
|
|
%i[get post patch put delete head].each do |method_id|
|
|
|
|
|
|
|
|
define_method(method_id) do |path, **args|
|
2018-07-10 05:32:24 +00:00
|
|
|
args = args.with_indifferent_access
|
|
|
|
args[:headers] = Hash(args[:headers]).merge!(Hash(@headers))
|
|
|
|
super(path, **args.symbolize_keys)
|
2018-07-09 06:51:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Adds one or more HTTP headers to all requests of the current example.
|
|
|
|
#
|
|
|
|
# @param [Hash{String => String}] headers Hash of HTTP headers
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# add_headers('Eg Some X-Header' => 'Some value')
|
|
|
|
|
|
|
|
# @example
|
|
|
|
# add_headers(
|
|
|
|
# 'Header 1' => 'Some value',
|
|
|
|
# 'Header 2' => 'Some value',
|
|
|
|
# ...
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# @return [Hash] The current headers Hash
|
|
|
|
def add_headers(headers)
|
|
|
|
@headers = Hash(@headers).merge(headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Parses the response.body as JSON.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# json_response
|
|
|
|
|
|
|
|
# @example
|
|
|
|
# json_response.is_a?(Array)
|
|
|
|
#
|
|
|
|
# @return [Array, Hash, ...] Parsed JSON structure as Ruby object
|
|
|
|
def json_response
|
|
|
|
JSON.parse(response.body)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Authenticates all requests of the current example as the given user.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# authenticated_as(some_admin_user)
|
|
|
|
#
|
2018-09-19 13:54:49 +00:00
|
|
|
# @example
|
|
|
|
# authenticated_as(some_admin_user, on_behalf_of: customer_user)
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# authenticated_as(some_admin_user, password: 'wrongpw')
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# authenticated_as(some_admin_user, password: 'wrongpw', token: create(:token, action: 'api', user_id: some_admin_user.id) )
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# authenticated_as(nil, login: 'not_existing', password: 'wrongpw' )
|
|
|
|
#
|
2018-07-09 06:51:58 +00:00
|
|
|
# @return nil
|
2020-03-30 08:47:19 +00:00
|
|
|
def authenticated_as(user, via: :api_client, **options)
|
2022-03-31 11:50:42 +00:00
|
|
|
password = options[:password] || user.try(:password_plain) || user.password.to_s
|
2020-03-30 08:47:19 +00:00
|
|
|
login = options[:login] || user.login
|
2018-09-19 13:54:49 +00:00
|
|
|
|
2020-03-30 08:47:19 +00:00
|
|
|
case via
|
|
|
|
when :api_client
|
2021-04-01 15:14:25 +00:00
|
|
|
# ensure that always the correct header value is set
|
|
|
|
# otherwise previous header configurations will be re-used
|
2021-08-10 12:00:34 +00:00
|
|
|
add_headers('From' => options[:from])
|
2018-09-19 13:54:49 +00:00
|
|
|
|
2021-04-01 15:14:25 +00:00
|
|
|
# if we want to authenticate by token
|
|
|
|
credentials = if options[:token].present?
|
|
|
|
"Token token=#{options[:token].name}"
|
|
|
|
else
|
|
|
|
ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
|
|
|
|
end
|
2018-09-19 13:54:49 +00:00
|
|
|
|
2021-04-01 15:14:25 +00:00
|
|
|
add_headers('Authorization' => credentials)
|
2020-03-30 08:47:19 +00:00
|
|
|
when :browser
|
2021-07-01 11:23:52 +00:00
|
|
|
post '/api/v1/signin', params: { username: login, password: password, fingerprint: Faker::Number.number(digits: 9) }
|
2020-03-30 08:47:19 +00:00
|
|
|
end
|
2018-07-09 06:51:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Provides a Hash of attributes for the given FactoryBot
|
|
|
|
# factory parameters which can be used as the params payload.
|
|
|
|
# Note that the attributes are "cleaned" so no created_by_id etc.
|
|
|
|
# is present.
|
|
|
|
#
|
|
|
|
# @see FactoryBot#attributes_for
|
|
|
|
#
|
|
|
|
# @example
|
2020-06-19 09:17:18 +00:00
|
|
|
# attributes_params_for(:admin, email: 'custom@example.com')
|
2018-07-09 06:51:58 +00:00
|
|
|
# # => {firstname: 'Nicole', email: 'custom@example.com', ...}
|
|
|
|
#
|
|
|
|
# @return [Hash{Symbol => <String, Array, Hash>}] request cleaned attributes
|
|
|
|
def attributes_params_for(*args)
|
|
|
|
filter_unused_params(attributes_for(*args))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Provides a Hash of attributes for the given Model instance which can
|
|
|
|
# be used as the params payload.
|
|
|
|
# Note that the attributes are "cleaned" so no created_by_id etc.
|
|
|
|
# is present.
|
|
|
|
#
|
|
|
|
# @param [Hash] instance An ActiveRecord instance
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# cleaned_params_for(some_admin_user)
|
|
|
|
# # => {firstname: 'Nicole', email: 'admin@example.com', ...}
|
|
|
|
#
|
|
|
|
# @return [Hash{Symbol => <String, Array, Hash>}] request cleaned attributes
|
|
|
|
def cleaned_params_for(instance)
|
|
|
|
filter_unused_params(instance.attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
# This is a self explaining internal method.
|
|
|
|
#
|
|
|
|
# @see ApplicationModel#filter_unused_params
|
|
|
|
def filter_unused_params(unfiltered)
|
|
|
|
# let's get private
|
|
|
|
ApplicationModel.send(:filter_unused_params, unfiltered)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.configure do |config|
|
|
|
|
config.include ZammadSpecSupportRequest, type: :request
|
2018-09-19 13:54:49 +00:00
|
|
|
|
|
|
|
config.before(:each, type: :request) do
|
|
|
|
Setting.set('system_init_done', true)
|
|
|
|
end
|
2019-04-18 13:01:21 +00:00
|
|
|
|
|
|
|
# This helper allows you to authenticate as a given user in request specs
|
|
|
|
# via the example metadata, rather than directly:
|
|
|
|
#
|
|
|
|
# it 'does something', authenticated_as: :user
|
|
|
|
#
|
|
|
|
# In order for this to work, you must define the user in a `let` block first:
|
|
|
|
#
|
2020-06-19 09:17:18 +00:00
|
|
|
# let(:user) { create(:customer) }
|
2019-04-18 13:01:21 +00:00
|
|
|
#
|
2020-06-18 11:51:25 +00:00
|
|
|
config.before(:each, :authenticated_as, type: :request) do |example|
|
|
|
|
user = authenticated_as_get_user example.metadata[:authenticated_as], return_type: :user
|
|
|
|
|
|
|
|
authenticated_as user if user
|
2019-04-18 13:01:21 +00:00
|
|
|
end
|
2018-07-09 06:51:58 +00:00
|
|
|
end
|