2020-01-13 05:47:03 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
RSpec.describe '/api/v1/accounts/{account.id}/contacts/:id/conversations', type: :request do
|
2020-01-13 05:47:03 +00:00
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:contact) { create(:contact, account: account) }
|
|
|
|
let(:inbox_1) { create(:inbox, account: account) }
|
|
|
|
let(:inbox_2) { create(:inbox, account: account) }
|
|
|
|
let(:contact_inbox_1) { create(:contact_inbox, contact: contact, inbox: inbox_1) }
|
|
|
|
let(:contact_inbox_2) { create(:contact_inbox, contact: contact, inbox: inbox_2) }
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
2020-02-07 16:13:52 +00:00
|
|
|
let(:unknown) { create(:user, account: account, role: nil) }
|
2020-01-13 05:47:03 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
create(:inbox_member, user: agent, inbox: inbox_1)
|
|
|
|
2.times.each { create(:conversation, account: account, inbox: inbox_1, contact: contact, contact_inbox: contact_inbox_1) }
|
|
|
|
2.times.each { create(:conversation, account: account, inbox: inbox_2, contact: contact, contact_inbox: contact_inbox_2) }
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/contacts/:id/conversations' do
|
2020-01-13 05:47:03 +00:00
|
|
|
context 'when unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/conversations"
|
2020-01-13 05:47:03 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is logged in' do
|
|
|
|
context 'with user as administrator' do
|
|
|
|
it 'returns conversations from all inboxes' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/conversations", headers: admin.create_new_auth_token
|
2020-01-13 05:47:03 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json_response['payload'].length).to eq 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with user as agent' do
|
|
|
|
it 'returns conversations from the inboxes which agent has access to' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/conversations", headers: agent.create_new_auth_token
|
2020-01-13 05:47:03 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json_response['payload'].length).to eq 2
|
|
|
|
end
|
|
|
|
end
|
2020-02-07 16:13:52 +00:00
|
|
|
|
|
|
|
context 'with user as unknown role' do
|
|
|
|
it 'returns conversations from no inboxes' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/conversations", headers: unknown.create_new_auth_token
|
2020-02-07 16:13:52 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json_response['payload'].length).to eq 0
|
|
|
|
end
|
|
|
|
end
|
2020-01-13 05:47:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|