taskbars_controller_test.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. require 'test_helper'
  2. class TaskbarsControllerTest < ActionDispatch::IntegrationTest
  3. setup do
  4. # set accept header
  5. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  6. UserInfo.current_user_id = 1
  7. # create agent
  8. roles = Role.where(name: 'Agent')
  9. groups = Group.all
  10. @agent = User.create_or_update(
  11. login: 'taskbar-agent@example.com',
  12. firstname: 'Taskbar',
  13. lastname: 'Agent',
  14. email: 'taskbar-agent@example.com',
  15. password: 'agentpw',
  16. active: true,
  17. roles: roles,
  18. groups: groups,
  19. )
  20. # create customer without org
  21. roles = Role.where(name: 'Customer')
  22. @customer_without_org = User.create_or_update(
  23. login: 'taskbar-customer1@example.com',
  24. firstname: 'Taskbar',
  25. lastname: 'Customer1',
  26. email: 'taskbar-customer1@example.com',
  27. password: 'customer1pw',
  28. active: true,
  29. roles: roles,
  30. )
  31. end
  32. test 'task ownership' do
  33. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('taskbar-agent@example.com', 'agentpw')
  34. params = {
  35. user_id: @customer_without_org.id,
  36. client_id: '123',
  37. key: 'Ticket-5',
  38. callback: 'TicketZoom',
  39. state: {
  40. ticket: {
  41. owner_id: @agent.id,
  42. },
  43. article: {},
  44. },
  45. params: {
  46. ticket_id: 5,
  47. shown: true,
  48. },
  49. prio: 3,
  50. notify: false,
  51. active: false,
  52. }
  53. post '/api/v1/taskbar', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  54. assert_response(201)
  55. result = JSON.parse(@response.body)
  56. assert_equal(Hash, result.class)
  57. assert_equal('123', result['client_id'])
  58. assert_equal(@agent.id, result['user_id'])
  59. assert_equal(5, result['params']['ticket_id'])
  60. assert_equal(true, result['params']['shown'])
  61. taskbar_id = result['id']
  62. params[:user_id] = @customer_without_org.id
  63. params[:params] = {
  64. ticket_id: 5,
  65. shown: false,
  66. }
  67. put "/api/v1/taskbar/#{taskbar_id}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  68. assert_response(200)
  69. result = JSON.parse(@response.body)
  70. assert_equal(Hash, result.class)
  71. assert_equal('123', result['client_id'])
  72. assert_equal(@agent.id, result['user_id'])
  73. assert_equal(5, result['params']['ticket_id'])
  74. assert_equal(false, result['params']['shown'])
  75. # try to access with other user
  76. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('taskbar-customer1@example.com', 'customer1pw')
  77. params = {
  78. active: true,
  79. }
  80. put "/api/v1/taskbar/#{taskbar_id}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  81. assert_response(422)
  82. result = JSON.parse(@response.body)
  83. assert_equal(Hash, result.class)
  84. assert_equal('Not allowed to access this task.', result['error'])
  85. delete "/api/v1/taskbar/#{taskbar_id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  86. assert_response(422)
  87. result = JSON.parse(@response.body)
  88. assert_equal(Hash, result.class)
  89. assert_equal('Not allowed to access this task.', result['error'])
  90. # delete with correct user
  91. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('taskbar-agent@example.com', 'agentpw')
  92. delete "/api/v1/taskbar/#{taskbar_id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  93. assert_response(200)
  94. result = JSON.parse(@response.body)
  95. assert_equal(Hash, result.class)
  96. assert(result.blank?)
  97. end
  98. end