123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- RSpec.describe 'Macro', authenticated_as: :user, type: :request do
- let(:successful_params) do
- {
- name: 'asd',
- perform: {
- 'ticket.state_id': {
- value: '2'
- }
- },
- ux_flow_next_up: 'none',
- note: '',
- group_ids: nil,
- active: true
- }
- end
- describe '#create' do
- before do
- post '/api/v1/macros', params: successful_params, as: :json
- end
- context 'when user is not allowed to create macro' do
- let(:user) { create(:agent) }
- it 'does not create macro' do
- expect(response).to have_http_status(:forbidden)
- end
- end
- context 'when user is allowed to create macros' do
- let(:user) { create(:admin) }
- it 'creates macro' do
- expect(response).to have_http_status(:created)
- end
- end
- end
- describe '#update' do
- let(:macro) { create(:macro, name: 'test') }
- before do
- put "/api/v1/macros/#{macro.id}", params: successful_params, as: :json
- end
- context 'when user is not allowed to update macro' do
- let(:user) { create(:agent) }
- it 'does not update macro' do
- expect(response).to have_http_status(:forbidden)
- end
- it 'macro is not changed' do
- expect(macro.reload.name).to eq 'test'
- end
- end
- context 'when user is allowed to update macros' do
- let(:user) { create(:admin) }
- it 'request is successful' do
- expect(response).to have_http_status(:ok)
- end
- it 'macro is changed' do
- expect(macro.reload.name).to eq 'asd'
- end
- end
- end
- describe '#destroy' do
- let(:macro) { create(:macro) }
- before do
- delete "/api/v1/macros/#{macro.id}", as: :json
- end
- context 'when user is not allowed to destroy macro' do
- let(:user) { create(:agent) }
- it 'does not destroy macro' do
- expect(response).to have_http_status(:forbidden)
- end
- it 'macro is not destroyed' do
- expect(macro).not_to be_destroyed
- end
- end
- context 'when user is allowed to create macros' do
- let(:user) { create(:admin) }
- it 'request is successful' do
- expect(response).to have_http_status(:ok)
- end
- it 'macro is destroyed' do
- expect(Macro).not_to exist(macro.id)
- end
- end
- end
- describe '#index' do
- before do
- get '/api/v1/macros', as: :json
- end
- context 'when user is not allowed to use macros' do
- let(:user) { create(:customer) }
- it 'returns exception' do
- expect(response).to have_http_status(:forbidden)
- end
- end
- context 'when user is allowed to use macros' do
- let(:user) { create(:agent) }
- it 'request is successful' do
- expect(response).to have_http_status(:ok)
- end
- it 'returns array of macros' do
- expect(json_response.pluck('id')).to eq [Macro.first.id]
- end
- end
- context 'when user is admin only' do
- let(:user) { create(:admin_only) }
- it 'returns array of macros' do
- expect(json_response.pluck('id')).to eq [Macro.first.id]
- end
- end
- end
- describe '#show' do
- let(:macro) { create(:macro, groups: create_list(:group, 1)) }
- before do
- get "/api/v1/macros/#{macro.id}", as: :json
- end
- context 'when user is not allowed to use macros' do
- let(:user) { create(:customer) }
- it 'returns exception' do
- expect(response).to have_http_status(:forbidden)
- end
- end
- context 'when user is allowed to use macros' do
- let(:user) { create(:agent) }
- it 'returns exception when user has no access to related group' do
- expect(response).to have_http_status(:not_found)
- end
- context 'when user has acess to this group' do
- let(:user) { create(:agent, groups: macro.groups) }
- it 'returns macro when user has access to related group' do
- expect(response).to have_http_status(:ok)
- end
- end
- end
- context 'when user is admin only' do
- let(:user) { create(:admin_only) }
- it 'returns array of macros' do
- expect(response).to have_http_status(:ok)
- end
- end
- end
- end
|