authenticated_as.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module ZammadAuthenticatedAsHelper
  3. # parse authenticated_as params for request and system test helpers
  4. #
  5. # @param input [Any] any to parse, see below for options
  6. # @param return_type [Symbol] :credentials or :user
  7. def authenticated_as_get_user(input, return_type:)
  8. case input
  9. when Proc
  10. parse_meta instance_exec(&input), return_type: return_type
  11. when Symbol
  12. parse_meta instance_eval { send(input) }, return_type: return_type
  13. else
  14. parse_meta input, return_type: return_type
  15. end
  16. end
  17. private
  18. def parse_meta(input, return_type:)
  19. case return_type
  20. when :credentials
  21. parse_meta_credentials(input)
  22. when :user
  23. parse_meta_user_object(input)
  24. end
  25. end
  26. def parse_meta_user_object(input)
  27. case input
  28. when User
  29. input
  30. end
  31. end
  32. def parse_meta_credentials(input)
  33. case input
  34. when Hash
  35. input.slice(:username, :password)
  36. when User
  37. parse_meta_user(input)
  38. when true
  39. {
  40. username: 'admin@example.com',
  41. password: 'test',
  42. }
  43. end
  44. end
  45. def parse_meta_user(input)
  46. password = input.password_plain
  47. if password.blank?
  48. password = 'automagically set by your friendly capybara helper'
  49. input.update!(password: password)
  50. end
  51. {
  52. username: input.email,
  53. password: password,
  54. }
  55. end
  56. end
  57. RSpec.configure do |config|
  58. %i[request system graphql].each do |type|
  59. config.include ZammadAuthenticatedAsHelper, type: type
  60. end
  61. end