2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2018-12-05 10:43:53 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Idoit', type: :request do
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
let!(:admin) do
|
|
|
|
create(:admin, groups: Group.all)
|
2018-12-05 10:43:53 +00:00
|
|
|
end
|
2020-06-19 09:17:18 +00:00
|
|
|
let!(:agent) do
|
|
|
|
create(:agent, groups: Group.all)
|
2018-12-05 10:43:53 +00:00
|
|
|
end
|
2020-06-19 09:17:18 +00:00
|
|
|
let!(:customer) do
|
|
|
|
create(:customer)
|
2018-12-05 10:43:53 +00:00
|
|
|
end
|
|
|
|
let!(:token) do
|
|
|
|
'some_token'
|
|
|
|
end
|
|
|
|
let!(:endpoint) do
|
|
|
|
'https://idoit.example.com/i-doit/'
|
|
|
|
end
|
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
before do
|
2018-12-05 10:43:53 +00:00
|
|
|
Setting.set('idoit_integration', true)
|
|
|
|
Setting.set('idoit_config', {
|
|
|
|
api_token: token,
|
2018-12-19 17:31:51 +00:00
|
|
|
endpoint: endpoint,
|
2018-12-05 10:43:53 +00:00
|
|
|
client_id: '',
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'request handling' do
|
|
|
|
|
|
|
|
it 'does unclear urls' do
|
|
|
|
|
|
|
|
params = {
|
|
|
|
api_token: token,
|
2018-12-19 17:31:51 +00:00
|
|
|
endpoint: endpoint,
|
2018-12-05 10:43:53 +00:00
|
|
|
client_id: '',
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(agent)
|
2018-12-05 10:43:53 +00:00
|
|
|
post '/api/v1/integration/idoit/verify', params: params, as: :json
|
2021-02-04 08:28:41 +00:00
|
|
|
expect(response).to have_http_status(:forbidden)
|
2018-12-05 10:43:53 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(json_response).not_to be_blank
|
2018-12-05 10:43:53 +00:00
|
|
|
expect(json_response['error']).to eq('Not authorized (user)!')
|
|
|
|
|
|
|
|
stub_request(:post, "#{endpoint}src/jsonrpc.php")
|
2018-12-20 14:19:37 +00:00
|
|
|
.with(body: "{\"method\":\"cmdb.object_types\",\"params\":{\"apikey\":\"#{token}\"},\"version\":\"2.0\",\"id\":42}")
|
2018-12-05 10:43:53 +00:00
|
|
|
.to_return(status: 200, body: read_message('object_types_response'), headers: {})
|
|
|
|
|
|
|
|
params = {
|
|
|
|
api_token: token,
|
2018-12-19 17:31:51 +00:00
|
|
|
endpoint: endpoint,
|
2018-12-05 10:43:53 +00:00
|
|
|
client_id: '',
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin)
|
2018-12-05 10:43:53 +00:00
|
|
|
post '/api/v1/integration/idoit/verify', params: params, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-12-05 10:43:53 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(json_response).not_to be_blank
|
2018-12-05 10:43:53 +00:00
|
|
|
expect(json_response['result']).to eq('ok')
|
|
|
|
expect(json_response['response']).to be_truthy
|
|
|
|
expect(json_response['response']['jsonrpc']).to eq('2.0')
|
|
|
|
expect(json_response['response']['result']).to be_truthy
|
|
|
|
|
|
|
|
params = {
|
|
|
|
api_token: token,
|
2018-12-19 17:31:51 +00:00
|
|
|
endpoint: " #{endpoint}/",
|
2018-12-05 10:43:53 +00:00
|
|
|
client_id: '',
|
|
|
|
}
|
|
|
|
post '/api/v1/integration/idoit/verify', params: params, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-12-05 10:43:53 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(json_response).not_to be_blank
|
2018-12-05 10:43:53 +00:00
|
|
|
expect(json_response['result']).to eq('ok')
|
|
|
|
expect(json_response['response']).to be_truthy
|
|
|
|
expect(json_response['response']['jsonrpc']).to eq('2.0')
|
|
|
|
expect(json_response['response']['result']).to be_truthy
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does list all object types' do
|
|
|
|
|
|
|
|
stub_request(:post, "#{endpoint}src/jsonrpc.php")
|
2018-12-20 14:19:37 +00:00
|
|
|
.with(body: "{\"method\":\"cmdb.object_types\",\"params\":{\"apikey\":\"#{token}\"},\"version\":\"2.0\",\"id\":42}")
|
2018-12-05 10:43:53 +00:00
|
|
|
.to_return(status: 200, body: read_message('object_types_response'), headers: {})
|
|
|
|
|
|
|
|
params = {
|
|
|
|
method: 'cmdb.object_types',
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(agent)
|
2018-12-05 10:43:53 +00:00
|
|
|
post '/api/v1/integration/idoit', params: params, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-12-05 10:43:53 +00:00
|
|
|
|
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(json_response).not_to be_blank
|
2018-12-05 10:43:53 +00:00
|
|
|
expect(json_response['result']).to eq('ok')
|
|
|
|
expect(json_response['response']).to be_truthy
|
|
|
|
expect(json_response['response']['jsonrpc']).to eq('2.0')
|
|
|
|
expect(json_response['response']['result']).to be_truthy
|
|
|
|
expect(json_response['response']['result'][0]['id']).to eq('1')
|
|
|
|
expect(json_response['response']['result'][0]['title']).to eq('System service')
|
|
|
|
|
|
|
|
params = {
|
|
|
|
method: 'cmdb.object_types',
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin)
|
2018-12-05 10:43:53 +00:00
|
|
|
post '/api/v1/integration/idoit', params: params, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-12-05 10:43:53 +00:00
|
|
|
|
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(json_response).not_to be_blank
|
2018-12-05 10:43:53 +00:00
|
|
|
expect(json_response['result']).to eq('ok')
|
|
|
|
expect(json_response['response']).to be_truthy
|
|
|
|
expect(json_response['response']['jsonrpc']).to eq('2.0')
|
|
|
|
expect(json_response['response']['result']).to be_truthy
|
|
|
|
expect(json_response['response']['result'][0]['id']).to eq('1')
|
|
|
|
expect(json_response['response']['result'][0]['title']).to eq('System service')
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does query objects' do
|
|
|
|
|
|
|
|
stub_request(:post, "#{endpoint}src/jsonrpc.php")
|
2018-12-20 14:19:37 +00:00
|
|
|
.with(body: "{\"method\":\"cmdb.objects\",\"params\":{\"apikey\":\"#{token}\",\"filter\":{\"ids\":[\"33\"]}},\"version\":\"2.0\",\"id\":42}")
|
2018-12-05 10:43:53 +00:00
|
|
|
.to_return(status: 200, body: read_message('object_types_filter_response'), headers: {})
|
|
|
|
|
|
|
|
params = {
|
|
|
|
method: 'cmdb.objects',
|
|
|
|
filter: {
|
|
|
|
ids: ['33']
|
|
|
|
},
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(agent)
|
2018-12-05 10:43:53 +00:00
|
|
|
post '/api/v1/integration/idoit', params: params, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-12-05 10:43:53 +00:00
|
|
|
|
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(json_response).not_to be_blank
|
2018-12-05 10:43:53 +00:00
|
|
|
expect(json_response['result']).to eq('ok')
|
|
|
|
expect(json_response['response']).to be_truthy
|
|
|
|
expect(json_response['response']['jsonrpc']).to eq('2.0')
|
|
|
|
expect(json_response['response']['result']).to be_truthy
|
|
|
|
expect(json_response['response']['result'][0]['id']).to eq('26')
|
|
|
|
expect(json_response['response']['result'][0]['title']).to eq('demo.example.com')
|
|
|
|
expect(json_response['response']['result'][0]['type_title']).to eq('Virtual server')
|
|
|
|
expect(json_response['response']['result'][0]['cmdb_status_title']).to eq('in operation')
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_message(file)
|
|
|
|
File.read(Rails.root.join('test', 'data', 'idoit', "#{file}.json"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|