0f377da109
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
184 lines
6.6 KiB
Ruby
184 lines
6.6 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe 'Campaigns API', type: :request do
|
|
let(:account) { create(:account) }
|
|
|
|
describe 'GET /api/v1/accounts/{account.id}/campaigns' do
|
|
context 'when it is an unauthenticated user' do
|
|
it 'returns unauthorized' do
|
|
get "/api/v1/accounts/#{account.id}/campaigns"
|
|
|
|
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) }
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
|
let!(:campaign) { create(:campaign, account: account) }
|
|
|
|
it 'returns unauthorized for agents' do
|
|
get "/api/v1/accounts/#{account.id}/campaigns"
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
|
|
it 'returns all campaigns to administrators' do
|
|
get "/api/v1/accounts/#{account.id}/campaigns",
|
|
headers: administrator.create_new_auth_token,
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
|
body = JSON.parse(response.body, symbolize_names: true)
|
|
expect(body.first[:id]).to eq(campaign.display_id)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'GET /api/v1/accounts/{account.id}/campaigns/:id' do
|
|
let(:campaign) { create(:campaign, account: account) }
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
it 'returns unauthorized' do
|
|
get "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_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) }
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
|
|
|
it 'returns unauthorized for agents' do
|
|
get "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
|
|
headers: agent.create_new_auth_token,
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
|
|
it 'shows the campaign for administrators' do
|
|
get "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
|
|
headers: administrator.create_new_auth_token,
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
|
expect(JSON.parse(response.body, symbolize_names: true)[:id]).to eq(campaign.display_id)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'POST /api/v1/accounts/{account.id}/campaigns' do
|
|
let(:inbox) { create(:inbox, account: account) }
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
it 'returns unauthorized' do
|
|
post "/api/v1/accounts/#{account.id}/campaigns",
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
|
as: :json
|
|
|
|
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) }
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
|
|
|
it 'returns unauthorized for agents' do
|
|
post "/api/v1/accounts/#{account.id}/campaigns",
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
|
headers: agent.create_new_auth_token,
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
|
|
it 'creates a new campaign' do
|
|
post "/api/v1/accounts/#{account.id}/campaigns",
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
|
headers: administrator.create_new_auth_token,
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
|
expect(JSON.parse(response.body, symbolize_names: true)[:title]).to eq('test')
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'PATCH /api/v1/accounts/{account.id}/campaigns/:id' do
|
|
let(:inbox) { create(:inbox, account: account) }
|
|
let!(:campaign) { create(:campaign, account: account) }
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
it 'returns unauthorized' do
|
|
patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
|
as: :json
|
|
|
|
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) }
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
|
|
|
it 'returns unauthorized for agents' do
|
|
patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
|
headers: agent.create_new_auth_token,
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
|
|
it 'updates the campaign' do
|
|
patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
|
headers: administrator.create_new_auth_token,
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
|
expect(JSON.parse(response.body, symbolize_names: true)[:title]).to eq('test')
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'DELETE /api/v1/accounts/{account.id}/campaigns/:id' do
|
|
let(:inbox) { create(:inbox, account: account) }
|
|
let!(:campaign) { create(:campaign, account: account) }
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
it 'returns unauthorized' do
|
|
delete "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
|
|
as: :json
|
|
|
|
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) }
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
|
|
|
it 'return unauthorized if agent' do
|
|
delete "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
|
|
headers: agent.create_new_auth_token,
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
|
|
it 'delete campaign if admin' do
|
|
delete "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
|
|
headers: administrator.create_new_auth_token,
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
|
expect(::Campaign.exists?(campaign.display_id)).to eq false
|
|
end
|
|
end
|
|
end
|
|
end
|