2018-09-19 13:54:49 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'User Organization', type: :request, searchindex: true do
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
let!(:admin) do
|
|
|
|
create(:admin, groups: Group.all)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
2020-06-19 09:17:18 +00:00
|
|
|
let!(:agent) do
|
|
|
|
create(:agent, groups: Group.all)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
2020-06-19 09:17:18 +00:00
|
|
|
let!(:customer) do
|
|
|
|
create(:customer)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
let!(:organization) do
|
|
|
|
create(:organization, name: 'Rest Org', note: 'Rest Org A')
|
|
|
|
end
|
|
|
|
let!(:organization2) do
|
|
|
|
create(:organization, name: 'Rest Org #2', note: 'Rest Org B')
|
|
|
|
end
|
|
|
|
let!(:organization3) do
|
|
|
|
create(:organization, name: 'Rest Org #3', note: 'Rest Org C')
|
|
|
|
end
|
2020-06-19 09:17:18 +00:00
|
|
|
let!(:customer2) do
|
|
|
|
create(:customer, organization: organization)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
before do
|
2018-09-19 13:54:49 +00:00
|
|
|
configure_elasticsearch do
|
|
|
|
|
|
|
|
travel 1.minute
|
|
|
|
|
|
|
|
rebuild_searchindex
|
|
|
|
|
|
|
|
# execute background jobs
|
|
|
|
Scheduler.worker(true)
|
|
|
|
|
|
|
|
sleep 6
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'request handling' do
|
|
|
|
|
|
|
|
it 'does organization index with agent' do
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(agent)
|
2018-09-19 13:54:49 +00:00
|
|
|
get '/api/v1/organizations', params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Array)
|
|
|
|
expect(json_response[0]['member_ids']).to be_a_kind_of(Array)
|
|
|
|
expect(json_response.length >= 3).to be_truthy
|
|
|
|
|
|
|
|
get '/api/v1/organizations?limit=40&page=1&per_page=2', params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response.class).to eq(Array)
|
|
|
|
organizations = Organization.order(:id).limit(2)
|
|
|
|
expect(json_response[0]['id']).to eq(organizations[0].id)
|
|
|
|
expect(json_response[0]['member_ids']).to eq(organizations[0].member_ids)
|
|
|
|
expect(json_response[1]['id']).to eq(organizations[1].id)
|
|
|
|
expect(json_response[1]['member_ids']).to eq(organizations[1].member_ids)
|
|
|
|
expect(json_response.count).to eq(2)
|
|
|
|
|
|
|
|
get '/api/v1/organizations?limit=40&page=2&per_page=2', params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response.class).to eq(Array)
|
|
|
|
organizations = Organization.order(:id).limit(4)
|
|
|
|
expect(json_response[0]['id']).to eq(organizations[2].id)
|
|
|
|
expect(json_response[0]['member_ids']).to eq(organizations[2].member_ids)
|
|
|
|
expect(json_response[1]['id']).to eq(organizations[3].id)
|
|
|
|
expect(json_response[1]['member_ids']).to eq(organizations[3].member_ids)
|
|
|
|
|
|
|
|
expect(json_response.count).to eq(2)
|
|
|
|
|
|
|
|
# show/:id
|
|
|
|
get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response['member_ids']).to be_a_kind_of(Array)
|
|
|
|
expect(json_response['members']).to be_falsey
|
|
|
|
expect('Rest Org').to eq(json_response['name'])
|
|
|
|
|
|
|
|
get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response['member_ids']).to be_a_kind_of(Array)
|
|
|
|
expect(json_response['members']).to be_falsey
|
|
|
|
expect('Rest Org #2').to eq(json_response['name'])
|
|
|
|
|
|
|
|
# search as agent
|
|
|
|
Scheduler.worker(true)
|
|
|
|
get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response.class).to eq(Array)
|
2020-02-18 14:49:52 +00:00
|
|
|
organization = json_response.detect { |object| object['name'] == 'Zammad Foundation' }
|
|
|
|
expect(organization['name']).to eq('Zammad Foundation')
|
|
|
|
expect(organization['member_ids']).to be_truthy
|
|
|
|
expect(organization['members']).to be_falsey
|
2018-09-19 13:54:49 +00:00
|
|
|
|
|
|
|
get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&expand=true", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response.class).to eq(Array)
|
2020-02-18 14:49:52 +00:00
|
|
|
organization = json_response.detect { |object| object['name'] == 'Zammad Foundation' }
|
|
|
|
expect(organization['name']).to eq('Zammad Foundation')
|
|
|
|
expect(organization['member_ids']).to be_truthy
|
|
|
|
expect(organization['members']).to be_truthy
|
2018-09-19 13:54:49 +00:00
|
|
|
|
|
|
|
get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&label=true", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response.class).to eq(Array)
|
2020-02-18 14:49:52 +00:00
|
|
|
organization = json_response.detect { |object| object['label'] == 'Zammad Foundation' }
|
|
|
|
expect(organization['label']).to eq('Zammad Foundation')
|
|
|
|
expect(organization['value']).to eq('Zammad Foundation')
|
|
|
|
expect(organization['member_ids']).to be_falsey
|
|
|
|
expect(organization['members']).to be_falsey
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does organization index with customer1' do
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(customer)
|
2018-09-19 13:54:49 +00:00
|
|
|
get '/api/v1/organizations', params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Array)
|
|
|
|
expect(json_response.length).to eq(0)
|
|
|
|
|
|
|
|
# show/:id
|
|
|
|
get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response['name']).to be_nil
|
|
|
|
|
|
|
|
get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response['name']).to be_nil
|
|
|
|
|
|
|
|
# search
|
|
|
|
Scheduler.worker(true)
|
|
|
|
get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does organization index with customer2' do
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(customer2)
|
2018-09-19 13:54:49 +00:00
|
|
|
get '/api/v1/organizations', params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Array)
|
|
|
|
expect(json_response.length).to eq(1)
|
|
|
|
|
|
|
|
# show/:id
|
|
|
|
get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect('Rest Org').to eq(json_response['name'])
|
|
|
|
|
|
|
|
get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response['name']).to be_nil
|
|
|
|
|
|
|
|
# search
|
|
|
|
Scheduler.worker(true)
|
|
|
|
get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|