123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- require 'rack/handler/puma'
- # this cop is disabled to speed up testing by avoiding the overhead of multiple requests
- RSpec.describe UserAgent, :aggregate_failures, integration: true do
- include ZammadSpecSupportRequest
- def host_with_port
- host = 'http://localhost:3000'
- if ENV['CI'].present?
- host = 'http://build:3000'
- end
- host
- end
- puma_thread = nil
- # we need a running web server, otherwise the requests will fail
- before :all do # rubocop:disable RSpec/BeforeAfterAll
- ENV['CI_BASIC_AUTH_USER'] = 'basic_auth_user'
- ENV['CI_BASIC_AUTH_PASSWORD'] = 'test123'
- ENV['CI_BEARER_TOKEN'] = 'test_bearer_123'
- puma_thread = Thread.new do
- app = Rack::Builder.new do
- map '/' do
- run Rails.application
- end
- end.to_app
- Rack::Handler::Puma.run app, Port: 3000
- end
- sleep 0.25
- # wait for server to start
- server_started = false
- 10.times do
- next if server_started
- server_started = system("curl -sSf #{host_with_port} > /dev/null")
- sleep 0.2 if !server_started
- end
- end
- after :all do # rubocop:disable RSpec/BeforeAfterAll
- puma_thread.kill
- end
- shared_context 'when doing user agent tests' do
- let(:host) { host_with_port }
- shared_examples 'successful request' do
- it 'returns a response' do
- expect(response).to be_success
- expect(response.code).to eq(code)
- end
- end
- shared_examples 'successful request with json body' do
- it 'returns a response' do
- expect(response).to be_success
- expect(response.code).to eq(code)
- expect(json_response).to include(expected_body)
- end
- end
- shared_examples 'successful get request' do
- it 'returns a response' do
- expect(response).to be_success
- expect(response.code).to eq(code)
- expect(response.header).to include('content-type' => content_type)
- expect(json_response).to include(expected_body)
- end
- end
- shared_examples 'successful post/put request' do
- include_examples 'successful request with json body'
- end
- shared_examples 'successful delete request' do
- include_examples 'successful request with json body'
- end
- shared_examples 'successful redirect request' do
- include_examples 'successful request with json body'
- end
- shared_examples 'unsuccessful request with body' do
- it 'returns a response' do
- expect(response).not_to be_success
- expect(response.code).to eq(code)
- expect(response.body).to be_present
- end
- end
- shared_examples 'unsuccessful request without body' do
- it 'returns a response' do
- expect(response).not_to be_success
- expect(response.code).to eq(code)
- expect(response.body).to be_nil
- end
- end
- shared_examples 'unsuccessful get/post/put/delete request' do
- it 'returns a response' do
- expect(response).not_to be_success
- expect(response.code).to eq(code)
- expect(response.body).to eq(expected_body)
- end
- end
- shared_examples 'ftp requests' do
- it 'returns a response' do
- expect(response).to be_success
- expect(response.code).to eq(code)
- expect(response.body).to match(expected_body)
- end
- end
- describe '#get' do
- context 'without http basic auth' do
- subject(:response) { described_class.get(request_url) }
- context 'with code 200' do
- let(:code) { '200' }
- let(:content_type) { 'application/json; charset=utf-8' }
- let(:request_url) { "#{host}/test/get/1?submitted=123" }
- let(:expected_body) do
- {
- 'method' => 'get',
- 'submitted' => '123',
- 'content_type_requested' => nil,
- }
- end
- include_examples 'successful get request'
- end
- context 'with code 202' do
- let(:code) { '202' }
- let(:content_type) { 'application/json; charset=utf-8' }
- let(:request_url) { "#{host}/test/get_accepted/1?submitted=123" }
- let(:expected_body) do
- {
- 'method' => 'get',
- 'submitted' => '123',
- 'content_type_requested' => nil,
- }
- end
- include_examples 'successful get request'
- end
- context 'with code 404' do
- let(:code) { '404' }
- let(:request_url) { "#{host}/test/not_existing" }
- include_examples 'unsuccessful request with body'
- end
- end
- context 'with http basic auth' do
- subject(:response) do
- described_class.get(request_url, {}, {
- user: 'basic_auth_user',
- password: password,
- })
- end
- context 'with code 200' do
- let(:code) { '200' }
- let(:content_type) { 'application/json; charset=utf-8' }
- let(:request_url) { "#{host}/test_basic_auth/get/1?submitted=123" }
- let(:password) { 'test123' }
- let(:expected_body) do
- {
- 'method' => 'get',
- 'submitted' => '123',
- 'content_type_requested' => nil,
- }
- end
- include_examples 'successful get request'
- end
- context 'with code 401' do
- let(:code) { '401' }
- let(:request_url) { "#{host}/test_basic_auth/get/1?submitted=123" }
- let(:password) { 'test<>123' }
- let(:expected_body) { "HTTP Basic: Access denied.\n" }
- include_examples 'unsuccessful get/post/put/delete request'
- end
- end
- context 'with bearer token auth' do
- subject(:response) do
- described_class.get(request_url, {}, {
- bearer_token: bearer_token,
- })
- end
- context 'with code 200' do
- let(:code) { '200' }
- let(:content_type) { 'application/json; charset=utf-8' }
- let(:request_url) { "#{host}/test_bearer_auth/get/1?submitted=123" }
- let(:bearer_token) { 'test_bearer_123' }
- let(:expected_body) do
- {
- 'method' => 'get',
- 'submitted' => '123',
- 'content_type_requested' => nil,
- }
- end
- include_examples 'successful get request'
- end
- context 'with code 401' do
- let(:code) { '401' }
- let(:request_url) { "#{host}/test_bearer_auth/get/1?submitted=123" }
- let(:bearer_token) { 'wrong_test_bearer' }
- let(:expected_body) { "HTTP Token: Access denied.\n" }
- include_examples 'unsuccessful get/post/put/delete request'
- end
- end
- context 'when timeouts are raised' do
- subject(:response) do
- described_class.get(request_url, {}, {
- open_timeout: 0,
- read_timeout: 0,
- })
- end
- let(:request_url) { "#{host}/test/get/1?submitted=123" }
- let(:code) { 0 }
- include_examples 'unsuccessful request without body'
- end
- context 'with content type set to json' do
- subject(:response) { described_class.get(request_url, request_params, request_options) }
- context 'with code 200' do
- let(:code) { '200' }
- let(:content_type) { 'application/json; charset=utf-8' }
- let(:request_url) { "#{host}/test/get/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:request_options) { { json: true } }
- let(:expected_body) do
- {
- 'method' => 'get',
- 'content_type_requested' => nil,
- 'submitted' => 'some value',
- }
- end
- include_examples 'successful get request'
- end
- context 'with code 404' do
- let(:code) { '404' }
- let(:request_url) { "#{host}/test/not_existing" }
- let(:request_params) { { submitted: { key: 'some value' } } }
- let(:request_options) { { json: true } }
- include_examples 'unsuccessful request with body'
- end
- end
- end
- describe '#post' do
- context 'without http basic auth' do
- subject(:response) { described_class.post(request_url, request_params) }
- context 'with code 201' do
- let(:code) { '201' }
- let(:request_url) { "#{host}/test/post/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:expected_body) do
- {
- 'method' => 'post',
- 'submitted' => 'some value',
- 'content_type_requested' => 'application/x-www-form-urlencoded',
- }
- end
- include_examples 'successful post/put request'
- end
- context 'with code 404' do
- let(:code) { '404' }
- let(:request_url) { "#{host}/test/not_existing" }
- let(:request_params) { { submitted: 'some value' } }
- include_examples 'unsuccessful request with body'
- end
- end
- context 'with http basic auth' do
- subject(:response) do
- described_class.post(request_url, request_params, {
- user: 'basic_auth_user',
- password: password,
- })
- end
- context 'with code 201' do
- let(:code) { '201' }
- let(:request_url) { "#{host}/test_basic_auth/post/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:password) { 'test123' }
- let(:expected_body) do
- {
- 'method' => 'post',
- 'submitted' => 'some value',
- 'content_type_requested' => 'application/x-www-form-urlencoded',
- }
- end
- include_examples 'successful post/put request'
- end
- context 'with code 401' do
- let(:code) { '401' }
- let(:request_url) { "#{host}/test_basic_auth/post/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:password) { 'test<>123' }
- let(:expected_body) { "HTTP Basic: Access denied.\n" }
- include_examples 'unsuccessful get/post/put/delete request'
- end
- end
- context 'with bearer token auth' do
- subject(:response) do
- described_class.post(request_url, request_params, {
- bearer_token: bearer_token,
- })
- end
- context 'with code 201' do
- let(:code) { '201' }
- let(:request_url) { "#{host}/test_bearer_auth/post/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:bearer_token) { 'test_bearer_123' }
- let(:expected_body) do
- {
- 'method' => 'post',
- 'submitted' => 'some value',
- 'content_type_requested' => 'application/x-www-form-urlencoded',
- }
- end
- include_examples 'successful post/put request'
- end
- context 'with code 401' do
- let(:code) { '401' }
- let(:request_url) { "#{host}/test_bearer_auth/post/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:bearer_token) { 'wrong_test_bearer' }
- let(:expected_body) { "HTTP Token: Access denied.\n" }
- include_examples 'unsuccessful get/post/put/delete request'
- end
- end
- context 'when timeouts are raised' do
- subject(:response) do
- described_class.post(request_url, request_params, {
- open_timeout: 0,
- read_timeout: 0,
- })
- end
- let(:request_url) { "#{host}/test/post/1" }
- let(:request_params) { { submitted: 'timeout' } }
- let(:code) { 0 }
- include_examples 'unsuccessful request without body'
- end
- context 'with content type set to json' do
- subject(:response) { described_class.post(request_url, request_params, request_options) }
- context 'with code 201' do
- let(:code) { '201' }
- let(:content_type) { 'application/json; charset=utf-8' }
- let(:request_url) { "#{host}/test/post/1" }
- let(:request_params) { { submitted: { key: 'some value' } } }
- let(:request_options) { { json: true } }
- let(:expected_body) do
- {
- 'method' => 'post',
- 'content_type_requested' => 'application/json',
- 'submitted' => {
- 'key' => 'some value',
- },
- }
- end
- include_examples 'successful post/put request'
- end
- end
- end
- describe '#put' do
- subject(:response) { described_class.put(request_url, request_params) }
- context 'without http basic auth' do
- context 'with code 200' do
- let(:code) { '200' }
- let(:request_url) { "#{host}/test/put/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:expected_body) do
- {
- 'method' => 'put',
- 'submitted' => 'some value',
- 'content_type_requested' => 'application/x-www-form-urlencoded',
- }
- end
- include_examples 'successful post/put request'
- end
- context 'with code 404' do
- let(:code) { '404' }
- let(:request_url) { "#{host}/test/not_existing" }
- let(:request_params) { { submitted: 'some value' } }
- include_examples 'unsuccessful request with body'
- end
- end
- context 'with http basic auth' do
- subject(:response) do
- described_class.put(request_url, request_params, {
- user: 'basic_auth_user',
- password: password,
- })
- end
- let(:password) { 'test123' }
- let(:submit_value) { 'some value' }
- context 'with code 200' do
- let(:code) { '200' }
- let(:request_url) { "#{host}/test_basic_auth/put/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:expected_body) do
- {
- 'method' => 'put',
- 'submitted' => 'some value',
- 'content_type_requested' => 'application/x-www-form-urlencoded',
- }
- end
- include_examples 'successful post/put request'
- end
- context 'with code 401' do
- let(:code) { '401' }
- let(:request_url) { "#{host}/test_basic_auth/put/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:password) { 'test<>123' }
- let(:expected_body) { "HTTP Basic: Access denied.\n" }
- include_examples 'unsuccessful get/post/put/delete request'
- end
- end
- context 'with bearer token auth' do
- subject(:response) do
- described_class.put(request_url, request_params, {
- bearer_token: bearer_token,
- })
- end
- context 'with code 200' do
- let(:code) { '200' }
- let(:request_url) { "#{host}/test_bearer_auth/put/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:bearer_token) { 'test_bearer_123' }
- let(:expected_body) do
- {
- 'method' => 'put',
- 'submitted' => 'some value',
- 'content_type_requested' => 'application/x-www-form-urlencoded',
- }
- end
- include_examples 'successful post/put request'
- end
- context 'with code 401' do
- let(:code) { '401' }
- let(:request_url) { "#{host}/test_bearer_auth/put/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:bearer_token) { 'wrong_test_bearer' }
- let(:expected_body) { "HTTP Token: Access denied.\n" }
- include_examples 'unsuccessful get/post/put/delete request'
- end
- end
- end
- describe '#delete' do
- context 'without http basic auth' do
- subject(:response) { described_class.delete(request_url) }
- context 'with code 200' do
- let(:code) { '200' }
- let(:request_url) { "#{host}/test/delete/1" }
- let(:expected_body) do
- {
- 'method' => 'delete',
- 'content_type_requested' => nil,
- }
- end
- include_examples 'successful delete request'
- end
- context 'with code 404' do
- let(:code) { '404' }
- let(:request_url) { "#{host}/test/not_existing" }
- include_examples 'unsuccessful request with body'
- end
- end
- context 'with http basic auth' do
- subject(:response) do
- described_class.delete(request_url, {}, {
- user: 'basic_auth_user',
- password: password,
- })
- end
- context 'with code 200' do
- let(:code) { '200' }
- let(:content_type) { 'application/json; charset=utf-8' }
- let(:request_url) { "#{host}/test_basic_auth/delete/1" }
- let(:password) { 'test123' }
- let(:expected_body) do
- {
- 'method' => 'delete',
- 'content_type_requested' => nil,
- }
- end
- include_examples 'successful delete request'
- end
- context 'with code 401' do
- let(:code) { '401' }
- let(:request_url) { "#{host}/test_basic_auth/delete/1" }
- let(:password) { 'test<>123' }
- let(:expected_body) { "HTTP Basic: Access denied.\n" }
- include_examples 'unsuccessful get/post/put/delete request'
- end
- end
- context 'with bearer token auth' do
- subject(:response) do
- described_class.delete(request_url, {}, {
- bearer_token: bearer_token,
- })
- end
- context 'with code 200' do
- let(:code) { '200' }
- let(:content_type) { 'application/json; charset=utf-8' }
- let(:request_url) { "#{host}/test_bearer_auth/delete/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:bearer_token) { 'test_bearer_123' }
- let(:expected_body) do
- {
- 'method' => 'delete',
- 'content_type_requested' => nil,
- }
- end
- include_examples 'successful delete request'
- end
- context 'with code 401' do
- let(:code) { '401' }
- let(:request_url) { "#{host}/test_bearer_auth/delete/1" }
- let(:request_params) { { submitted: 'some value' } }
- let(:bearer_token) { 'wrong_test_bearer' }
- let(:expected_body) { "HTTP Token: Access denied.\n" }
- include_examples 'unsuccessful get/post/put/delete request'
- end
- end
- end
- describe '#request' do
- context 'without http basic auth' do
- subject(:response) { described_class.request(request_url) }
- context 'with code 200' do
- let(:code) { '200' }
- let(:content_type) { 'application/json; charset=utf-8' }
- let(:request_url) { "#{host}/test/redirect" }
- let(:expected_body) do
- {
- 'method' => 'get',
- 'submitted' => 'abc',
- 'content_type_requested' => nil,
- }
- end
- include_examples 'successful redirect request'
- end
- end
- context 'with http basic auth' do
- subject(:response) do
- described_class.request(request_url, {
- user: 'basic_auth_user',
- password: password,
- })
- end
- context 'with code 200' do
- let(:code) { '200' }
- let(:request_url) { "#{host}/test_basic_auth/redirect" }
- let(:password) { 'test123' }
- let(:expected_body) do
- {
- 'method' => 'get',
- 'submitted' => 'abc',
- 'content_type_requested' => nil,
- }
- end
- include_examples 'successful redirect request'
- end
- context 'with code 401' do
- let(:code) { '401' }
- let(:request_url) { "#{host}/test_basic_auth/redirect" }
- let(:password) { 'test<>123' }
- let(:expected_body) { "HTTP Basic: Access denied.\n" }
- include_examples 'unsuccessful get/post/put/delete request'
- end
- end
- context 'when ftp', integration: true, required_envs: ['FTP_URL'] do
- subject(:response) do
- described_class.request(request_url)
- end
- context 'with code 200' do
- let(:code) { '200' }
- let(:request_url) { "#{ENV['FTP_URL']}/zammad.txt" }
- let(:expected_body) { %r{zammad}i }
- include_examples 'ftp requests'
- end
- context 'with code 550' do
- let(:code) { '550' }
- let(:request_url) { "#{ENV['FTP_URL']}/nonexisting.txt" }
- include_examples 'unsuccessful request without body'
- end
- context 'with a not existing URL' do
- let(:code) { 0 }
- let(:request_url) { 'http://not.existing.host.tld/test.php' }
- include_examples 'unsuccessful request without body'
- end
- end
- end
- end
- describe 'testing without proxy' do
- include_context 'when doing user agent tests'
- end
- describe 'testing with proxy', required_envs: %w[CI_PROXY_URL CI_PROXY_USER CI_PROXY_PASSWORD] do
- before do
- Setting.set('proxy', ENV['CI_PROXY_URL'])
- Setting.set('proxy_username', ENV['CI_PROXY_USER'])
- Setting.set('proxy_password', ENV['CI_PROXY_PASSWORD'])
- end
- include_context 'when doing user agent tests'
- end
- end
|