taskbars_controller_test.rb 3.6 KB

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