time_accounting_controller_test.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. require 'test_helper'
  2. require 'rake'
  3. class TimeAccountingControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. roles = Role.where(name: 'Admin')
  8. groups = Group.all
  9. UserInfo.current_user_id = 1
  10. @year = DateTime.now.utc.year
  11. @month = DateTime.now.utc.month
  12. @admin = User.create!(
  13. login: 'rest-admin',
  14. firstname: 'Rest',
  15. lastname: 'Agent',
  16. email: 'rest-admin@example.com',
  17. password: 'adminpw',
  18. active: true,
  19. roles: roles,
  20. groups: groups,
  21. updated_by_id: 1,
  22. created_by_id: 1
  23. )
  24. roles = Role.where(name: 'Customer')
  25. @customer_without_org = User.create!(
  26. login: 'rest-customer1@example.com',
  27. firstname: 'Rest',
  28. lastname: 'Customer1',
  29. email: 'rest-customer1@example.com',
  30. password: 'customer1pw',
  31. active: true,
  32. roles: roles,
  33. updated_by_id: 1,
  34. created_by_id: 1
  35. )
  36. end
  37. test '01.01 time account report' do
  38. group = 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. ticket = Ticket.create!(
  45. title: 'ticket for report',
  46. group_id: group.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. article = 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: ticket.id,
  60. updated_by_id: 1,
  61. created_by_id: 1,
  62. )
  63. Ticket::TimeAccounting.create!(
  64. ticket_id: ticket.id,
  65. ticket_article_id: article.id,
  66. time_unit: 200,
  67. )
  68. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  69. get "/api/v1/time_accounting/log/by_ticket/#{@year}/#{@month}?download=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  70. assert_response(200)
  71. assert(@response['Content-Disposition'])
  72. assert_equal("attachment; filename=\"by_ticket-#{@year}-#{@month}.xls\"", @response['Content-Disposition'])
  73. assert_equal('application/vnd.ms-excel', @response['Content-Type'])
  74. end
  75. end