diff --git a/spec/controllers/api/v1/canned_responses_controller_spec.rb b/spec/controllers/api/v1/canned_responses_controller_spec.rb new file mode 100644 index 000000000..b0a1e9846 --- /dev/null +++ b/spec/controllers/api/v1/canned_responses_controller_spec.rb @@ -0,0 +1,125 @@ +require 'rails_helper' + +RSpec.describe 'Canned Responses API', type: :request do + let(:account) { create(:account) } + + before do + create(:canned_response, account: account) + end + + describe 'GET /api/v1/canned_responses' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + get '/api/v1/canned_responses' + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:agent) { create(:user, account: account, role: :agent) } + + it 'returns all the canned responses' do + get '/api/v1/canned_responses', + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(JSON.parse(response.body)).to eq(account.canned_responses.as_json) + end + + it 'returns all the canned responses the user searched for' do + create(:canned_response, account: account) + + params = { search: CannedResponse.last.short_code } + + get '/api/v1/canned_responses', + params: params, + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(JSON.parse(response.body)).to eq([CannedResponse.last].as_json) + end + end + end + + describe 'POST /api/v1/canned_responses' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + post '/api/v1/canned_responses' + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:agent) { create(:user, account: account, role: :agent) } + + it 'creates a new canned response' do + params = { short_code: 'short', content: 'content' } + + post '/api/v1/canned_responses', + params: params, + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(CannedResponse.count).to eq(2) + end + end + end + + describe 'PUT /api/v1/canned_responses/:id' do + let(:canned_response) { CannedResponse.last } + + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + put "/api/v1/canned_responses/#{canned_response.id}" + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:agent) { create(:user, account: account, role: :agent) } + + it 'updates an existing canned response' do + params = { short_code: 'B' } + + put "/api/v1/canned_responses/#{canned_response.id}", + params: params, + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(canned_response.reload.short_code).to eq('B') + end + end + end + + describe 'DELETE /api/v1/canned_responses/:id' do + let(:canned_response) { CannedResponse.last } + + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + delete "/api/v1/canned_responses/#{canned_response.id}" + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:agent) { create(:user, account: account, role: :agent) } + + it 'destroys the canned response' do + delete "/api/v1/canned_responses/#{canned_response.id}", + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(CannedResponse.count).to eq(0) + end + end + end +end diff --git a/spec/factories/canned_responses.rb b/spec/factories/canned_responses.rb new file mode 100644 index 000000000..079004710 --- /dev/null +++ b/spec/factories/canned_responses.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :canned_response do + content { 'Content' } + sequence(:short_code) { |n| "CODE#{n}" } + account + end +end