Skip to content

Commit

Permalink
Migrated remaining Minitest controller tests to RSpec.
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfschmidt authored and thorsteneckel committed Dec 5, 2018
1 parent 6ff974e commit 14437f9
Show file tree
Hide file tree
Showing 19 changed files with 3,256 additions and 3,373 deletions.
50 changes: 0 additions & 50 deletions .gitlab-ci.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ test:rspec:postgresql:
- rake zammad:db:init - rake zammad:db:init
- rake test:units - rake test:units
- ruby -I test/ test/integration/object_manager_test.rb - ruby -I test/ test/integration/object_manager_test.rb
- ruby -I test/ test/integration/object_manager_attributes_controller_test.rb
- ruby -I test/ test/integration/package_test.rb - ruby -I test/ test/integration/package_test.rb
- ruby -I test/ test/integration/monitoring_controller_test.rb
- rake db:drop - rake db:drop


test:unit:mysql: test:unit:mysql:
Expand Down Expand Up @@ -168,18 +166,6 @@ test:integration:user_agent:
- rake db:drop - rake db:drop
allow_failure: true allow_failure: true


test:integration:user_device:
<<: *artifacts_error
stage: test
variables:
RAILS_ENV: "test"
tags:
- core
script:
- rake zammad:db:unseeded
- ruby -I test/ test/integration/user_device_controller_test.rb
- rake db:drop

test:integration:slack: test:integration:slack:
<<: *artifacts_error <<: *artifacts_error
stage: test stage: test
Expand Down Expand Up @@ -207,42 +193,6 @@ test:integration:clearbit:
- rake db:drop - rake db:drop
allow_failure: true allow_failure: true


test:integration:telegram:
<<: *artifacts_error
stage: test
variables:
RAILS_ENV: "test"
tags:
- core
script:
- rake zammad:db:init
- ruby -I test test/integration/telegram_controller_test.rb
- rake db:drop

test:integration:twilio:
<<: *artifacts_error
stage: test
variables:
RAILS_ENV: "test"
tags:
- core
script:
- rake zammad:db:init
- ruby -I test test/integration/twilio_sms_controller_test.rb
- rake db:drop

test:integration:idoit:
<<: *artifacts_error
stage: test
variables:
RAILS_ENV: "test"
tags:
- core
script:
- rake zammad:db:init
- ruby -I test test/integration/idoit_controller_test.rb
- rake db:drop

### Elasticsearch ### Elasticsearch


.script_elasticsearch_template: &script_elasticsearch_definition .script_elasticsearch_template: &script_elasticsearch_definition
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Lint/BooleanSymbol:
Enabled: true Enabled: true
Exclude: Exclude:
- "db/seeds/object_manager_attributes.rb" - "db/seeds/object_manager_attributes.rb"
- "test/integration/object_manager_attributes_controller_test.rb" - "spec/requests/integration/object_manager_attributes_spec.rb"
- "test/integration/object_manager_test.rb" - "test/integration/object_manager_test.rb"


Lint/InterpolationCheck: Lint/InterpolationCheck:
Expand Down
18 changes: 18 additions & 0 deletions spec/factories/user_device.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
FactoryBot.define do
sequence :fingerprint do |n|
"fingerprint#{n}"
end
end

FactoryBot.define do

factory :user_device do
user_id 1
name 'test 1'
location 'some location'
user_agent 'some user agent'
ip '127.0.0.1'
fingerprint { generate(:fingerprint) }
end

end
154 changes: 154 additions & 0 deletions spec/requests/integration/idoit_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,154 @@
require 'rails_helper'

RSpec.describe 'Idoit', type: :request do

let!(:admin_user) do
create(:admin_user, groups: Group.all)
end
let!(:agent_user) do
create(:agent_user, groups: Group.all)
end
let!(:customer_user) do
create(:customer_user)
end
let!(:token) do
'some_token'
end
let!(:endpoint) do
'https://idoit.example.com/i-doit/'
end

before(:each) do
Setting.set('idoit_integration', true)
Setting.set('idoit_config', {
api_token: token,
endpoint: endpoint,
client_id: '',
})
end

describe 'request handling' do

it 'does unclear urls' do

params = {
api_token: token,
endpoint: endpoint,
client_id: '',
}
authenticated_as(agent_user)
post '/api/v1/integration/idoit/verify', params: params, as: :json
expect(response).to have_http_status(401)
expect(json_response).to be_a_kind_of(Hash)
expect(json_response).to_not be_blank
expect(json_response['error']).to eq('Not authorized (user)!')

stub_request(:post, "#{endpoint}src/jsonrpc.php")
.with(body: "{\"method\":\"cmdb.object_types\",\"params\":{\"apikey\":\"#{token}\"},\"version\":\"2.0\"}")
.to_return(status: 200, body: read_message('object_types_response'), headers: {})

params = {
api_token: token,
endpoint: endpoint,
client_id: '',
}
authenticated_as(admin_user)
post '/api/v1/integration/idoit/verify', params: params, as: :json
expect(response).to have_http_status(200)
expect(json_response).to be_a_kind_of(Hash)
expect(json_response).to_not be_blank
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,
endpoint: " #{endpoint}/",
client_id: '',
}
post '/api/v1/integration/idoit/verify', params: params, as: :json
expect(response).to have_http_status(200)
expect(json_response).to be_a_kind_of(Hash)
expect(json_response).to_not be_blank
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")
.with(body: "{\"method\":\"cmdb.object_types\",\"params\":{\"apikey\":\"#{token}\"},\"version\":\"2.0\"}")
.to_return(status: 200, body: read_message('object_types_response'), headers: {})

params = {
method: 'cmdb.object_types',
}
authenticated_as(agent_user)
post '/api/v1/integration/idoit', params: params, as: :json
expect(response).to have_http_status(200)

expect(json_response).to be_a_kind_of(Hash)
expect(json_response).to_not be_blank
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',
}
authenticated_as(admin_user)
post '/api/v1/integration/idoit', params: params, as: :json
expect(response).to have_http_status(200)

expect(json_response).to be_a_kind_of(Hash)
expect(json_response).to_not be_blank
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")
.with(body: "{\"method\":\"cmdb.objects\",\"params\":{\"apikey\":\"#{token}\",\"filter\":{\"ids\":[\"33\"]}},\"version\":\"2.0\"}")
.to_return(status: 200, body: read_message('object_types_filter_response'), headers: {})

params = {
method: 'cmdb.objects',
filter: {
ids: ['33']
},
}
authenticated_as(agent_user)
post '/api/v1/integration/idoit', params: params, as: :json
expect(response).to have_http_status(200)

expect(json_response).to be_a_kind_of(Hash)
expect(json_response).to_not be_blank
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
Loading

0 comments on commit 14437f9

Please sign in to comment.