reports_controller_test.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. require 'test_helper'
  2. require 'rake'
  3. class ReportsControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. @year = DateTime.now.utc.year
  8. @month = DateTime.now.utc.month
  9. @week = DateTime.now.utc.strftime('%U').to_i
  10. @day = DateTime.now.utc.day
  11. roles = Role.where(name: 'Admin')
  12. groups = Group.all
  13. UserInfo.current_user_id = 1
  14. @admin = User.create_or_update(
  15. login: 'rest-admin',
  16. firstname: 'Rest',
  17. lastname: 'Agent',
  18. email: 'rest-admin@example.com',
  19. password: 'adminpw',
  20. active: true,
  21. roles: roles,
  22. groups: groups,
  23. updated_by_id: 1,
  24. created_by_id: 1
  25. )
  26. roles = Role.where(name: 'Customer')
  27. @customer_without_org = User.create_or_update(
  28. login: 'rest-customer1@example.com',
  29. firstname: 'Rest',
  30. lastname: 'Customer1',
  31. email: 'rest-customer1@example.com',
  32. password: 'customer1pw',
  33. active: true,
  34. roles: roles,
  35. updated_by_id: 1,
  36. created_by_id: 1
  37. )
  38. @group1 = Group.create_or_update(
  39. name: "GroupWithoutPermission-#{rand(9_999_999_999)}",
  40. active: true,
  41. updated_by_id: 1,
  42. created_by_id: 1,
  43. )
  44. @ticket1 = Ticket.create!(
  45. title: 'ticket for report',
  46. group_id: @group1.id,
  47. customer_id: @customer_without_org.id,
  48. state: Ticket::State.lookup(name: 'open'),
  49. priority: Ticket::Priority.lookup(name: '2 normal'),
  50. updated_by_id: 1,
  51. created_by_id: 1,
  52. )
  53. Ticket::Article.create!(
  54. type: Ticket::Article::Type.lookup(name: 'note'),
  55. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  56. from: 'sender',
  57. subject: 'subject',
  58. body: 'some body',
  59. ticket_id: @ticket1.id,
  60. updated_by_id: 1,
  61. created_by_id: 1,
  62. )
  63. if ENV['ES_URL'].present?
  64. #fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
  65. Setting.set('es_url', ENV['ES_URL'])
  66. # Setting.set('es_url', 'http://127.0.0.1:9200')
  67. # Setting.set('es_index', 'estest.local_zammad')
  68. # Setting.set('es_user', 'elasticsearch')
  69. # Setting.set('es_password', 'zammad')
  70. if ENV['ES_INDEX_RAND'].present?
  71. ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
  72. end
  73. if ENV['ES_INDEX'].blank?
  74. raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
  75. end
  76. Setting.set('es_index', ENV['ES_INDEX'])
  77. travel 1.minute
  78. # drop/create indexes
  79. Rake::Task.clear
  80. Zammad::Application.load_tasks
  81. #Rake::Task["searchindex:drop"].execute
  82. #Rake::Task["searchindex:create"].execute
  83. Rake::Task['searchindex:rebuild'].execute
  84. # execute background jobs
  85. Scheduler.worker(true)
  86. sleep 6
  87. end
  88. end
  89. teardown do
  90. if ENV['ES_URL'].present?
  91. Rake::Task['searchindex:drop'].execute
  92. end
  93. end
  94. test '01.01 report example - admin access' do
  95. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  96. get "/api/v1/reports/sets?sheet=true;metric=count;year=#{@year};month=#{@month};week=#{@week};day=#{@day};timeRange=year;profile_id=1;downloadBackendSelected=count::created", params: {}, headers: @headers.merge('Authorization' => credentials)
  97. assert_response(200)
  98. assert(@response['Content-Disposition'])
  99. assert_equal('attachment; filename="tickets--all--Created.xls"', @response['Content-Disposition'])
  100. assert_equal('application/vnd.ms-excel', @response['Content-Type'])
  101. end
  102. end