authenticated_as.rb 1.5 KB

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