2020-02-08 06:28:41 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Canned Responses API', type: :request do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:canned_response, account: account)
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/canned_responses' do
|
2020-02-08 06:28:41 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/canned_responses"
|
2020-02-08 06:28:41 +00:00
|
|
|
|
|
|
|
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
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/canned_responses",
|
2020-02-08 06:28:41 +00:00
|
|
|
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 }
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/canned_responses",
|
2020-02-08 06:28:41 +00:00
|
|
|
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
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/canned_responses' do
|
2020-02-08 06:28:41 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/canned_responses"
|
2020-02-08 06:28:41 +00:00
|
|
|
|
|
|
|
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' }
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/canned_responses",
|
2020-02-08 06:28:41 +00:00
|
|
|
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
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'PUT /api/v1/accounts/{account.id}/canned_responses/:id' do
|
2020-02-08 06:28:41 +00:00
|
|
|
let(:canned_response) { CannedResponse.last }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
put "/api/v1/accounts/#{account.id}/canned_responses/#{canned_response.id}"
|
2020-02-08 06:28:41 +00:00
|
|
|
|
|
|
|
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' }
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
put "/api/v1/accounts/#{account.id}/canned_responses/#{canned_response.id}",
|
2020-02-08 06:28:41 +00:00
|
|
|
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
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'DELETE /api/v1/accounts/{account.id}/canned_responses/:id' do
|
2020-02-08 06:28:41 +00:00
|
|
|
let(:canned_response) { CannedResponse.last }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
delete "/api/v1/accounts/#{account.id}/canned_responses/#{canned_response.id}"
|
2020-02-08 06:28:41 +00:00
|
|
|
|
|
|
|
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
|
2020-03-09 17:57:10 +00:00
|
|
|
delete "/api/v1/accounts/#{account.id}/canned_responses/#{canned_response.id}",
|
2020-02-08 06:28:41 +00:00
|
|
|
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
|