idoit_spec.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. require 'rails_helper'
  2. RSpec.describe 'Idoit', type: :request do
  3. let!(:admin_user) do
  4. create(:admin_user, groups: Group.all)
  5. end
  6. let!(:agent_user) do
  7. create(:agent_user, groups: Group.all)
  8. end
  9. let!(:customer_user) do
  10. create(:customer_user)
  11. end
  12. let!(:token) do
  13. 'some_token'
  14. end
  15. let!(:endpoint) do
  16. 'https://idoit.example.com/i-doit/'
  17. end
  18. before do
  19. Setting.set('idoit_integration', true)
  20. Setting.set('idoit_config', {
  21. api_token: token,
  22. endpoint: endpoint,
  23. client_id: '',
  24. })
  25. end
  26. describe 'request handling' do
  27. it 'does unclear urls' do
  28. params = {
  29. api_token: token,
  30. endpoint: endpoint,
  31. client_id: '',
  32. }
  33. authenticated_as(agent_user)
  34. post '/api/v1/integration/idoit/verify', params: params, as: :json
  35. expect(response).to have_http_status(:unauthorized)
  36. expect(json_response).to be_a_kind_of(Hash)
  37. expect(json_response).not_to be_blank
  38. expect(json_response['error']).to eq('Not authorized (user)!')
  39. stub_request(:post, "#{endpoint}src/jsonrpc.php")
  40. .with(body: "{\"method\":\"cmdb.object_types\",\"params\":{\"apikey\":\"#{token}\"},\"version\":\"2.0\",\"id\":42}")
  41. .to_return(status: 200, body: read_message('object_types_response'), headers: {})
  42. params = {
  43. api_token: token,
  44. endpoint: endpoint,
  45. client_id: '',
  46. }
  47. authenticated_as(admin_user)
  48. post '/api/v1/integration/idoit/verify', params: params, as: :json
  49. expect(response).to have_http_status(:ok)
  50. expect(json_response).to be_a_kind_of(Hash)
  51. expect(json_response).not_to be_blank
  52. expect(json_response['result']).to eq('ok')
  53. expect(json_response['response']).to be_truthy
  54. expect(json_response['response']['jsonrpc']).to eq('2.0')
  55. expect(json_response['response']['result']).to be_truthy
  56. params = {
  57. api_token: token,
  58. endpoint: " #{endpoint}/",
  59. client_id: '',
  60. }
  61. post '/api/v1/integration/idoit/verify', params: params, as: :json
  62. expect(response).to have_http_status(:ok)
  63. expect(json_response).to be_a_kind_of(Hash)
  64. expect(json_response).not_to be_blank
  65. expect(json_response['result']).to eq('ok')
  66. expect(json_response['response']).to be_truthy
  67. expect(json_response['response']['jsonrpc']).to eq('2.0')
  68. expect(json_response['response']['result']).to be_truthy
  69. end
  70. it 'does list all object types' do
  71. stub_request(:post, "#{endpoint}src/jsonrpc.php")
  72. .with(body: "{\"method\":\"cmdb.object_types\",\"params\":{\"apikey\":\"#{token}\"},\"version\":\"2.0\",\"id\":42}")
  73. .to_return(status: 200, body: read_message('object_types_response'), headers: {})
  74. params = {
  75. method: 'cmdb.object_types',
  76. }
  77. authenticated_as(agent_user)
  78. post '/api/v1/integration/idoit', params: params, as: :json
  79. expect(response).to have_http_status(:ok)
  80. expect(json_response).to be_a_kind_of(Hash)
  81. expect(json_response).not_to be_blank
  82. expect(json_response['result']).to eq('ok')
  83. expect(json_response['response']).to be_truthy
  84. expect(json_response['response']['jsonrpc']).to eq('2.0')
  85. expect(json_response['response']['result']).to be_truthy
  86. expect(json_response['response']['result'][0]['id']).to eq('1')
  87. expect(json_response['response']['result'][0]['title']).to eq('System service')
  88. params = {
  89. method: 'cmdb.object_types',
  90. }
  91. authenticated_as(admin_user)
  92. post '/api/v1/integration/idoit', params: params, as: :json
  93. expect(response).to have_http_status(:ok)
  94. expect(json_response).to be_a_kind_of(Hash)
  95. expect(json_response).not_to be_blank
  96. expect(json_response['result']).to eq('ok')
  97. expect(json_response['response']).to be_truthy
  98. expect(json_response['response']['jsonrpc']).to eq('2.0')
  99. expect(json_response['response']['result']).to be_truthy
  100. expect(json_response['response']['result'][0]['id']).to eq('1')
  101. expect(json_response['response']['result'][0]['title']).to eq('System service')
  102. end
  103. it 'does query objects' do
  104. stub_request(:post, "#{endpoint}src/jsonrpc.php")
  105. .with(body: "{\"method\":\"cmdb.objects\",\"params\":{\"apikey\":\"#{token}\",\"filter\":{\"ids\":[\"33\"]}},\"version\":\"2.0\",\"id\":42}")
  106. .to_return(status: 200, body: read_message('object_types_filter_response'), headers: {})
  107. params = {
  108. method: 'cmdb.objects',
  109. filter: {
  110. ids: ['33']
  111. },
  112. }
  113. authenticated_as(agent_user)
  114. post '/api/v1/integration/idoit', params: params, as: :json
  115. expect(response).to have_http_status(:ok)
  116. expect(json_response).to be_a_kind_of(Hash)
  117. expect(json_response).not_to be_blank
  118. expect(json_response['result']).to eq('ok')
  119. expect(json_response['response']).to be_truthy
  120. expect(json_response['response']['jsonrpc']).to eq('2.0')
  121. expect(json_response['response']['result']).to be_truthy
  122. expect(json_response['response']['result'][0]['id']).to eq('26')
  123. expect(json_response['response']['result'][0]['title']).to eq('demo.example.com')
  124. expect(json_response['response']['result'][0]['type_title']).to eq('Virtual server')
  125. expect(json_response['response']['result'][0]['cmdb_status_title']).to eq('in operation')
  126. end
  127. def read_message(file)
  128. File.read(Rails.root.join('test', 'data', 'idoit', "#{file}.json"))
  129. end
  130. end
  131. end