reports_controller_test.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. require 'test_helper'
  2. class ReportsControllerTest < ActionDispatch::IntegrationTest
  3. include SearchindexHelper
  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!(
  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!(
  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!(
  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. configure_elasticsearch do
  64. travel 1.minute
  65. rebuild_searchindex
  66. # execute background jobs
  67. Scheduler.worker(true)
  68. sleep 6
  69. end
  70. end
  71. test '01.01 report example - admin access' do
  72. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  73. 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)
  74. assert_response(200)
  75. assert(@response['Content-Disposition'])
  76. assert_equal('attachment; filename="tickets--all--Created.xls"', @response['Content-Disposition'])
  77. assert_equal('application/vnd.ms-excel', @response['Content-Type'])
  78. end
  79. end