2021-04-29 16:53:32 +00:00
|
|
|
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) }
|
2022-02-04 09:08:18 +00:00
|
|
|
let!(:campaign) { create(:campaign, account: account, trigger_rules: { url: 'https://test.com' }) }
|
2021-04-29 16:53:32 +00:00
|
|
|
|
|
|
|
it 'returns unauthorized for agents' do
|
2021-06-29 15:29:41 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/campaigns",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
2021-04-29 16:53:32 +00:00
|
|
|
|
|
|
|
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
|
2022-02-04 09:08:18 +00:00
|
|
|
let(:campaign) { create(:campaign, account: account, trigger_rules: { url: 'https://test.com' }) }
|
2021-04-29 16:53:32 +00:00
|
|
|
|
|
|
|
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",
|
2021-04-30 13:15:24 +00:00
|
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
2021-04-29 16:53:32 +00:00
|
|
|
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",
|
2021-04-30 13:15:24 +00:00
|
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
2021-04-29 16:53:32 +00:00
|
|
|
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",
|
2021-04-30 13:15:24 +00:00
|
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
2021-04-29 16:53:32 +00:00
|
|
|
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
|
2021-07-14 06:54:09 +00:00
|
|
|
|
2022-02-04 09:08:18 +00:00
|
|
|
it 'creates a new ongoing campaign' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/campaigns",
|
|
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message', trigger_rules: { url: 'https://test.com' } },
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'throws error when invalid url provided for ongoing campaign' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/campaigns",
|
|
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message', trigger_rules: { url: 'javascript' } },
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
end
|
|
|
|
|
2021-07-14 06:54:09 +00:00
|
|
|
it 'creates a new oneoff campaign' do
|
|
|
|
twilio_sms = create(:channel_twilio_sms, account: account)
|
|
|
|
twilio_inbox = create(:inbox, channel: twilio_sms)
|
|
|
|
label1 = create(:label, account: account)
|
|
|
|
label2 = create(:label, account: account)
|
2022-02-14 14:39:21 +00:00
|
|
|
scheduled_at = 2.days.from_now
|
2021-07-14 06:54:09 +00:00
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/campaigns",
|
|
|
|
params: {
|
|
|
|
inbox_id: twilio_inbox.id, title: 'test', message: 'test message',
|
2022-02-14 14:39:21 +00:00
|
|
|
scheduled_at: scheduled_at,
|
2021-07-14 06:54:09 +00:00
|
|
|
audience: [{ type: 'Label', id: label1.id }, { type: 'Label', id: label2.id }]
|
|
|
|
},
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_data = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(response_data[:campaign_type]).to eq('one_off')
|
|
|
|
expect(response_data[:scheduled_at].present?).to eq true
|
2022-02-14 14:39:21 +00:00
|
|
|
expect(response_data[:scheduled_at]).to eq(scheduled_at.to_i)
|
2021-07-14 06:54:09 +00:00
|
|
|
expect(response_data[:audience].pluck(:id)).to include(label1.id, label2.id)
|
|
|
|
end
|
2021-04-29 16:53:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PATCH /api/v1/accounts/{account.id}/campaigns/:id' do
|
|
|
|
let(:inbox) { create(:inbox, account: account) }
|
2022-02-04 09:08:18 +00:00
|
|
|
let!(:campaign) { create(:campaign, account: account, trigger_rules: { url: 'https://test.com' }) }
|
2021-04-29 16:53:32 +00:00
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}",
|
2021-04-30 13:15:24 +00:00
|
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
2021-04-29 16:53:32 +00:00
|
|
|
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}",
|
2021-04-30 13:15:24 +00:00
|
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
2021-04-29 16:53:32 +00:00
|
|
|
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}",
|
2021-04-30 13:15:24 +00:00
|
|
|
params: { inbox_id: inbox.id, title: 'test', message: 'test message' },
|
2021-04-29 16:53:32 +00:00
|
|
|
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
|
2021-06-15 08:44:59 +00:00
|
|
|
|
|
|
|
describe 'DELETE /api/v1/accounts/{account.id}/campaigns/:id' do
|
|
|
|
let(:inbox) { create(:inbox, account: account) }
|
2022-02-04 09:08:18 +00:00
|
|
|
let!(:campaign) { create(:campaign, account: account, trigger_rules: { url: 'https://test.com' }) }
|
2021-06-15 08:44:59 +00:00
|
|
|
|
|
|
|
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
|
2021-04-29 16:53:32 +00:00
|
|
|
end
|