123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- module ZammadSpecSupportRequest
-
-
-
-
-
- %i[get post patch put delete head].each do |method_id|
- define_method(method_id) do |path, **args|
- args = args.with_indifferent_access
- args[:headers] = Hash(args[:headers]).merge!(Hash(@headers))
- super(path, **args.symbolize_keys)
- end
- end
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def add_headers(headers)
- @headers = Hash(@headers).merge(headers)
- end
-
-
-
-
-
-
-
-
- def json_response
- JSON.parse(response.body)
- end
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def authenticated_as(user, via: :api_client, **options)
- password = options[:password] || user.try(:password_plain) || user.password.to_s
- login = options[:login] || user.login
- case via
- when :api_client
-
-
- add_headers('From' => options[:from])
-
- credentials = if options[:token].present?
- "Token token=#{options[:token].token}"
- else
- ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
- end
- add_headers('Authorization' => credentials)
- when :browser
- post '/api/v1/signin', params: { username: login, password: password, fingerprint: Faker::Number.unique.number(digits: 9) }
- end
- end
-
-
-
-
-
-
-
-
-
-
-
-
- def attributes_params_for(*)
- filter_unused_params(attributes_for(*))
- end
-
-
-
-
-
-
-
-
-
-
-
-
- def cleaned_params_for(instance)
- filter_unused_params(instance.attributes)
- end
-
-
-
- def filter_unused_params(unfiltered)
-
- ApplicationModel.send(:filter_unused_params, unfiltered)
- end
-
- def stub_get(path)
- stub_request(:get, path)
- end
- def stub_post(path)
- stub_request(:post, path)
- end
- def stub_delete(path)
- stub_request(:delete, path)
- end
- def stub_put(path)
- stub_request(:put, path)
- end
- def a_get(path)
- a_request(:get, path)
- end
- def a_post(path)
- a_request(:post, path)
- end
- def a_delete(path)
- a_request(:delete, path)
- end
- def a_put(path)
- a_request(:put, path)
- end
- end
- RSpec.configure do |config|
- config.include ZammadSpecSupportRequest, type: :request
- config.before(:each, type: :request) do
- Setting.set('system_init_done', true)
- end
-
-
-
-
-
-
-
-
-
- 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
- end
- end
|