2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2018-09-19 13:54:49 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Api Auth On Behalf Of', type: :request 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)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
2020-06-19 09:17:18 +00:00
|
|
|
let(:customer) do
|
2021-04-01 15:14:25 +00:00
|
|
|
create(:customer, firstname: 'Behalf of')
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'request handling' do
|
|
|
|
|
|
|
|
it 'does X-On-Behalf-Of auth - ticket create admin for customer by id' do
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
title: 'a new ticket #3',
|
|
|
|
group: 'Users',
|
|
|
|
priority: '2 normal',
|
|
|
|
state: 'new',
|
2020-06-19 09:17:18 +00:00
|
|
|
customer_id: customer.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
article: {
|
2018-09-19 13:54:49 +00:00
|
|
|
body: 'some test 123',
|
|
|
|
},
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin, on_behalf_of: customer.id)
|
2018-09-19 13:54:49 +00:00
|
|
|
post '/api/v1/tickets', params: params, as: :json
|
2020-12-03 08:07:15 +00:00
|
|
|
expect(response).to have_http_status(:created)
|
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(customer.id).to eq(json_response['created_by_id'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does X-On-Behalf-Of auth - ticket create admin for customer by login (upcase)' do
|
|
|
|
params = {
|
|
|
|
title: 'a new ticket #3',
|
|
|
|
group: 'Users',
|
|
|
|
priority: '2 normal',
|
|
|
|
state: 'new',
|
|
|
|
customer_id: customer.id,
|
|
|
|
article: {
|
|
|
|
body: 'some test 123',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
authenticated_as(admin, on_behalf_of: customer.login.upcase)
|
|
|
|
post '/api/v1/tickets', params: params, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:created)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2020-06-19 09:17:18 +00:00
|
|
|
expect(customer.id).to eq(json_response['created_by_id'])
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does X-On-Behalf-Of auth - ticket create admin for customer by login' do
|
|
|
|
ActivityStream.cleanup(1.year)
|
|
|
|
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
title: 'a new ticket #3',
|
|
|
|
group: 'Users',
|
|
|
|
priority: '2 normal',
|
|
|
|
state: 'new',
|
2020-06-19 09:17:18 +00:00
|
|
|
customer_id: customer.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
article: {
|
2018-09-19 13:54:49 +00:00
|
|
|
body: 'some test 123',
|
|
|
|
},
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin, on_behalf_of: customer.login)
|
2018-09-19 13:54:49 +00:00
|
|
|
post '/api/v1/tickets', params: params, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:created)
|
2018-09-19 13:54:49 +00:00
|
|
|
json_response_ticket = json_response
|
|
|
|
expect(json_response_ticket).to be_a_kind_of(Hash)
|
2020-06-19 09:17:18 +00:00
|
|
|
expect(customer.id).to eq(json_response_ticket['created_by_id'])
|
2018-09-19 13:54:49 +00:00
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin)
|
2018-09-19 13:54:49 +00:00
|
|
|
get '/api/v1/activity_stream?full=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
|
|
|
json_response_activity = json_response
|
|
|
|
expect(json_response_activity).to be_a_kind_of(Hash)
|
|
|
|
|
|
|
|
ticket_created = nil
|
|
|
|
json_response_activity['record_ids'].each do |record_id|
|
|
|
|
activity_stream = ActivityStream.find(record_id)
|
|
|
|
next if activity_stream.object.name != 'Ticket'
|
|
|
|
next if activity_stream.o_id != json_response_ticket['id'].to_i
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-09-19 13:54:49 +00:00
|
|
|
ticket_created = activity_stream
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(ticket_created).to be_truthy
|
2020-06-19 09:17:18 +00:00
|
|
|
expect(customer.id).to eq(ticket_created.created_by_id)
|
2018-09-19 13:54:49 +00:00
|
|
|
|
|
|
|
get '/api/v1/activity_stream', 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
|
|
|
json_response_activity = json_response
|
|
|
|
expect(json_response_activity).to be_a_kind_of(Array)
|
|
|
|
|
|
|
|
ticket_created = nil
|
|
|
|
json_response_activity.each do |record|
|
|
|
|
activity_stream = ActivityStream.find(record['id'])
|
|
|
|
next if activity_stream.object.name != 'Ticket'
|
|
|
|
next if activity_stream.o_id != json_response_ticket['id']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-09-19 13:54:49 +00:00
|
|
|
ticket_created = activity_stream
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(ticket_created).to be_truthy
|
2020-06-19 09:17:18 +00:00
|
|
|
expect(customer.id).to eq(ticket_created.created_by_id)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does X-On-Behalf-Of auth - ticket create admin for customer by email' do
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
title: 'a new ticket #3',
|
|
|
|
group: 'Users',
|
|
|
|
priority: '2 normal',
|
|
|
|
state: 'new',
|
2020-06-19 09:17:18 +00:00
|
|
|
customer_id: customer.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
article: {
|
2018-09-19 13:54:49 +00:00
|
|
|
body: 'some test 123',
|
|
|
|
},
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin, on_behalf_of: customer.email)
|
2018-09-19 13:54:49 +00:00
|
|
|
post '/api/v1/tickets', params: params, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:created)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2020-06-19 09:17:18 +00:00
|
|
|
expect(customer.id).to eq(json_response['created_by_id'])
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does X-On-Behalf-Of auth - ticket create admin for unknown' do
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
title: 'a new ticket #3',
|
|
|
|
group: 'Users',
|
|
|
|
priority: '2 normal',
|
|
|
|
state: 'new',
|
2020-06-19 09:17:18 +00:00
|
|
|
customer_id: customer.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
article: {
|
2018-09-19 13:54:49 +00:00
|
|
|
body: 'some test 123',
|
|
|
|
},
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin, on_behalf_of: 99_449_494_949)
|
2018-09-19 13:54:49 +00:00
|
|
|
post '/api/v1/tickets', params: params, as: :json
|
2021-02-04 08:28:41 +00:00
|
|
|
expect(response).to have_http_status(:forbidden)
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(@response.header).not_to be_key('Access-Control-Allow-Origin')
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response['error']).to eq("No such user '99449494949'")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does X-On-Behalf-Of auth - ticket create customer for admin' do
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
title: 'a new ticket #3',
|
|
|
|
group: 'Users',
|
|
|
|
priority: '2 normal',
|
|
|
|
state: 'new',
|
2020-06-19 09:17:18 +00:00
|
|
|
customer_id: customer.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
article: {
|
2018-09-19 13:54:49 +00:00
|
|
|
body: 'some test 123',
|
|
|
|
},
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(customer, on_behalf_of: admin.email)
|
2018-09-19 13:54:49 +00:00
|
|
|
post '/api/v1/tickets', params: params, as: :json
|
2021-02-04 08:28:41 +00:00
|
|
|
expect(response).to have_http_status(:forbidden)
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(@response.header).not_to be_key('Access-Control-Allow-Origin')
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response['error']).to eq("Current user has no permission to use 'X-On-Behalf-Of'!")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does X-On-Behalf-Of auth - ticket create admin for customer by email but no permitted action' do
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
title: 'a new ticket #3',
|
|
|
|
group: 'secret1234',
|
|
|
|
priority: '2 normal',
|
|
|
|
state: 'new',
|
2020-06-19 09:17:18 +00:00
|
|
|
customer_id: customer.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
article: {
|
2018-09-19 13:54:49 +00:00
|
|
|
body: 'some test 123',
|
|
|
|
},
|
|
|
|
}
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin, on_behalf_of: customer.email)
|
2018-09-19 13:54:49 +00:00
|
|
|
post '/api/v1/tickets', params: params, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
expect(@response.header).not_to be_key('Access-Control-Allow-Origin')
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response['error']).to eq('No lookup value found for \'group\': "secret1234"')
|
|
|
|
end
|
2021-04-01 15:14:25 +00:00
|
|
|
|
|
|
|
context 'when Token Admin has no ticket.* permission' do
|
|
|
|
|
|
|
|
let(:admin) { create(:user, firstname: 'Requester', roles: [admin_user_role]) }
|
|
|
|
|
|
|
|
let(:token) { create(:token, user: admin, permissions: %w[admin.user]) }
|
|
|
|
|
|
|
|
let(:admin_user_role) do
|
|
|
|
create(:role).tap { |role| role.permission_grant('admin.user') }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates Ticket because of behalf of user permission' do
|
|
|
|
params = {
|
|
|
|
title: 'a new ticket #3',
|
|
|
|
group: 'Users',
|
|
|
|
priority: '2 normal',
|
|
|
|
state: 'new',
|
|
|
|
customer_id: customer.id,
|
|
|
|
article: {
|
|
|
|
body: 'some test 123',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
authenticated_as(admin, on_behalf_of: customer.email, token: token)
|
|
|
|
post '/api/v1/tickets', params: params, as: :json
|
|
|
|
expect(response).to have_http_status(:created)
|
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(customer.id).to eq(json_response['created_by_id'])
|
|
|
|
end
|
|
|
|
end
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
end
|