2018-09-19 13:54:49 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Settings', type: :request do
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
let(:admin) do
|
|
|
|
create(:admin)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
2020-06-19 09:17:18 +00:00
|
|
|
let(:admin_api) do
|
2018-09-19 13:54:49 +00:00
|
|
|
role_api = create(:role)
|
|
|
|
role_api.permission_grant('admin.api')
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
create(:admin, roles: [role_api])
|
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
|
|
|
|
create(:customer)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'request handling' do
|
|
|
|
|
|
|
|
it 'does settings index with nobody' do
|
|
|
|
|
|
|
|
# index
|
|
|
|
get '/api/v1/settings', 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['settings']).to be_falsey
|
|
|
|
|
|
|
|
# show
|
|
|
|
setting = Setting.find_by(name: 'product_name')
|
|
|
|
get "/api/v1/settings/#{setting.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['error']).to eq('authentication failed')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does settings index with admin' do
|
|
|
|
|
|
|
|
# index
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin)
|
2018-09-19 13:54:49 +00:00
|
|
|
get '/api/v1/settings', 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).to be_truthy
|
|
|
|
hit_api = false
|
|
|
|
hit_product_name = false
|
|
|
|
json_response.each do |setting|
|
|
|
|
if setting['name'] == 'api_token_access'
|
|
|
|
hit_api = true
|
|
|
|
end
|
|
|
|
if setting['name'] == 'product_name'
|
|
|
|
hit_product_name = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
expect(hit_api).to eq(true)
|
|
|
|
expect(hit_product_name).to eq(true)
|
|
|
|
|
|
|
|
# show
|
|
|
|
setting = Setting.find_by(name: 'product_name')
|
|
|
|
get "/api/v1/settings/#{setting.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 eq('product_name')
|
|
|
|
|
|
|
|
setting = Setting.find_by(name: 'api_token_access')
|
|
|
|
get "/api/v1/settings/#{setting.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 eq('api_token_access')
|
|
|
|
|
|
|
|
# update
|
|
|
|
setting = Setting.find_by(name: 'product_name')
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: setting.id,
|
|
|
|
name: 'some_new_name',
|
2018-09-19 13:54:49 +00:00
|
|
|
preferences: {
|
2018-12-19 17:31:51 +00:00
|
|
|
permission: ['admin.branding', 'admin.some_new_permission'],
|
2018-09-19 13:54:49 +00:00
|
|
|
some_new_key: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
put "/api/v1/settings/#{setting.id}", params: 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 eq('product_name')
|
|
|
|
expect(json_response['preferences']['permission'].length).to eq(1)
|
|
|
|
expect(json_response['preferences']['permission'][0]).to eq('admin.branding')
|
|
|
|
expect(json_response['preferences']['some_new_key']).to eq(true)
|
|
|
|
|
|
|
|
# update
|
|
|
|
setting = Setting.find_by(name: 'api_token_access')
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: setting.id,
|
|
|
|
name: 'some_new_name',
|
2018-09-19 13:54:49 +00:00
|
|
|
preferences: {
|
2018-12-19 17:31:51 +00:00
|
|
|
permission: ['admin.branding', 'admin.some_new_permission'],
|
2018-09-19 13:54:49 +00:00
|
|
|
some_new_key: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
put "/api/v1/settings/#{setting.id}", params: 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 eq('api_token_access')
|
|
|
|
expect(json_response['preferences']['permission'].length).to eq(1)
|
|
|
|
expect(json_response['preferences']['permission'][0]).to eq('admin.api')
|
|
|
|
expect(json_response['preferences']['some_new_key']).to eq(true)
|
|
|
|
|
|
|
|
# delete
|
|
|
|
setting = Setting.find_by(name: 'product_name')
|
|
|
|
delete "/api/v1/settings/#{setting.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['error']).to eq('Not authorized (feature not possible)')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does settings index with admin-api' do
|
|
|
|
|
|
|
|
# index
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin_api)
|
2018-09-19 13:54:49 +00:00
|
|
|
get '/api/v1/settings', 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).to be_truthy
|
|
|
|
hit_api = false
|
|
|
|
hit_product_name = false
|
|
|
|
json_response.each do |setting|
|
|
|
|
if setting['name'] == 'api_token_access'
|
|
|
|
hit_api = true
|
|
|
|
end
|
|
|
|
if setting['name'] == 'product_name'
|
|
|
|
hit_product_name = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
expect(hit_api).to eq(true)
|
|
|
|
expect(hit_product_name).to eq(false)
|
|
|
|
|
|
|
|
# show
|
|
|
|
setting = Setting.find_by(name: 'product_name')
|
|
|
|
get "/api/v1/settings/#{setting.id}", params: {}, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
2020-03-19 09:39:51 +00:00
|
|
|
expect(json_response['error']).to eq('Not authorized (required ["admin.branding"])!')
|
2018-09-19 13:54:49 +00:00
|
|
|
|
|
|
|
setting = Setting.find_by(name: 'api_token_access')
|
|
|
|
get "/api/v1/settings/#{setting.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 eq('api_token_access')
|
|
|
|
|
|
|
|
# update
|
|
|
|
setting = Setting.find_by(name: 'product_name')
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: setting.id,
|
|
|
|
name: 'some_new_name',
|
2018-09-19 13:54:49 +00:00
|
|
|
preferences: {
|
2018-12-19 17:31:51 +00:00
|
|
|
permission: ['admin.branding', 'admin.some_new_permission'],
|
2018-09-19 13:54:49 +00:00
|
|
|
some_new_key: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
put "/api/v1/settings/#{setting.id}", params: params, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
2020-03-19 09:39:51 +00:00
|
|
|
expect(json_response['error']).to eq('Not authorized (required ["admin.branding"])!')
|
2018-09-19 13:54:49 +00:00
|
|
|
|
|
|
|
# update
|
|
|
|
setting = Setting.find_by(name: 'api_token_access')
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: setting.id,
|
|
|
|
name: 'some_new_name',
|
2018-09-19 13:54:49 +00:00
|
|
|
preferences: {
|
2018-12-19 17:31:51 +00:00
|
|
|
permission: ['admin.branding', 'admin.some_new_permission'],
|
2018-09-19 13:54:49 +00:00
|
|
|
some_new_key: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
put "/api/v1/settings/#{setting.id}", params: 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 eq('api_token_access')
|
|
|
|
expect(json_response['preferences']['permission'].length).to eq(1)
|
|
|
|
expect(json_response['preferences']['permission'][0]).to eq('admin.api')
|
|
|
|
expect(json_response['preferences']['some_new_key']).to eq(true)
|
|
|
|
|
|
|
|
# delete
|
|
|
|
setting = Setting.find_by(name: 'product_name')
|
|
|
|
delete "/api/v1/settings/#{setting.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['error']).to eq('Not authorized (feature not possible)')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does settings index with agent' do
|
|
|
|
|
|
|
|
# index
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(agent)
|
2018-09-19 13:54:49 +00:00
|
|
|
get '/api/v1/settings', 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['settings']).to be_falsey
|
|
|
|
expect(json_response['error']).to eq('Not authorized (user)!')
|
|
|
|
|
|
|
|
# show
|
|
|
|
setting = Setting.find_by(name: 'product_name')
|
|
|
|
get "/api/v1/settings/#{setting.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['error']).to eq('Not authorized (user)!')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does settings index with customer' do
|
|
|
|
|
|
|
|
# index
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(customer)
|
2018-09-19 13:54:49 +00:00
|
|
|
get '/api/v1/settings', 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['settings']).to be_falsey
|
|
|
|
expect(json_response['error']).to eq('Not authorized (user)!')
|
|
|
|
|
|
|
|
# show
|
|
|
|
setting = Setting.find_by(name: 'product_name')
|
|
|
|
get "/api/v1/settings/#{setting.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['error']).to eq('Not authorized (user)!')
|
|
|
|
|
|
|
|
# delete
|
|
|
|
setting = Setting.find_by(name: 'product_name')
|
|
|
|
delete "/api/v1/settings/#{setting.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['error']).to eq('Not authorized (user)!')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|