Chore: Scope URLs with account_id (#601)

* Chore: Enable Users to create multiple accounts

Addresses: #402
- migrations to split roles and other attributes from users table
- make changes in code to accommodate this change

Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
Sojan Jose
2020-03-09 23:27:10 +05:30
committed by GitHub
parent 2a6670f0da
commit 19ab0fe108
105 changed files with 480 additions and 402 deletions

View File

@@ -5,10 +5,10 @@ RSpec.describe 'Contact Merge Action API', type: :request do
let!(:base_contact) { create(:contact, account: account) }
let!(:mergee_contact) { create(:contact, account: account) }
describe 'POST /api/v1/actions/contact_merge' do
describe 'POST /api/v1/accounts/{account.id}/actions/contact_merge' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/actions/contact_merge'
post "/api/v1/accounts/#{account.id}/actions/contact_merge"
expect(response).to have_http_status(:unauthorized)
end
@@ -24,7 +24,7 @@ RSpec.describe 'Contact Merge Action API', type: :request do
end
it 'merges two contacts by calling contact merge action' do
post '/api/v1/actions/contact_merge',
post "/api/v1/accounts/#{account.id}/actions/contact_merge",
params: { base_contact_id: base_contact.id, mergee_contact_id: mergee_contact.id },
headers: agent.create_new_auth_token,
as: :json

View File

@@ -3,20 +3,20 @@ require 'rails_helper'
RSpec.describe 'Agents API', type: :request do
let(:account) { create(:account) }
describe 'GET /api/v1/agents' do
describe 'GET /api/v1/accounts/{account.id}/agents' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get '/api/v1/agents'
get "/api/v1/accounts/#{account.id}/agents"
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!(:agent) { create(:user, account: account, role: :agent) }
it 'returns all agents of account' do
get '/api/v1/agents',
get "/api/v1/accounts/#{account.id}/agents",
headers: agent.create_new_auth_token,
as: :json
@@ -26,12 +26,12 @@ RSpec.describe 'Agents API', type: :request do
end
end
describe 'DELETE /api/v1/agents/:id' do
describe 'DELETE /api/v1/accounts/{account.id}/agents/:id' do
let(:other_agent) { create(:user, account: account, role: :agent) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/agents/#{other_agent.id}"
delete "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}"
expect(response).to have_http_status(:unauthorized)
end
@@ -41,7 +41,7 @@ RSpec.describe 'Agents API', type: :request do
let(:admin) { create(:user, account: account, role: :administrator) }
it 'deletes an agent' do
delete "/api/v1/agents/#{other_agent.id}",
delete "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}",
headers: admin.create_new_auth_token,
as: :json
@@ -51,12 +51,12 @@ RSpec.describe 'Agents API', type: :request do
end
end
describe 'PUT /api/v1/agents/:id' do
describe 'PUT /api/v1/accounts/{account.id}/agents/:id' do
let(:other_agent) { create(:user, account: account, role: :agent) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
put "/api/v1/agents/#{other_agent.id}"
put "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}"
expect(response).to have_http_status(:unauthorized)
end
@@ -68,7 +68,7 @@ RSpec.describe 'Agents API', type: :request do
params = { name: 'TestUser' }
it 'modifies an agent' do
put "/api/v1/agents/#{other_agent.id}",
put "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}",
params: params,
headers: admin.create_new_auth_token,
as: :json
@@ -79,12 +79,12 @@ RSpec.describe 'Agents API', type: :request do
end
end
describe 'POST /api/v1/agents' do
describe 'POST /api/v1/accounts/{account.id}/agents' do
let(:other_agent) { create(:user, account: account, role: :agent) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/agents'
post "/api/v1/accounts/#{account.id}/agents"
expect(response).to have_http_status(:unauthorized)
end
@@ -96,7 +96,7 @@ RSpec.describe 'Agents API', type: :request do
params = { name: 'NewUser', email: Faker::Internet.email, role: :agent }
it 'creates a new agent' do
post '/api/v1/agents',
post "/api/v1/accounts/#{account.id}/agents",
params: params,
headers: admin.create_new_auth_token,
as: :json

View File

@@ -22,10 +22,10 @@ RSpec.describe 'Callbacks API', type: :request do
allow(koala_oauth).to receive(:exchange_access_token_info).and_return('access_token' => SecureRandom.hex(10))
end
describe 'POST /api/v1/callbacks/register_facebook_page' do
describe 'POST /api/v1/accounts/{account.id}/callbacks/register_facebook_page' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/callbacks/register_facebook_page'
post "/api/v1/accounts/#{account.id}/callbacks/register_facebook_page"
expect(response).to have_http_status(:unauthorized)
end
@@ -35,7 +35,7 @@ RSpec.describe 'Callbacks API', type: :request do
let(:admin) { create(:user, account: account, role: :administrator) }
it 'registers a new facebook page with no avatar' do
post '/api/v1/callbacks/register_facebook_page',
post "/api/v1/accounts/#{account.id}/callbacks/register_facebook_page",
headers: admin.create_new_auth_token,
params: valid_params,
as: :json
@@ -49,7 +49,7 @@ RSpec.describe 'Callbacks API', type: :request do
io.base_uri = URI.parse('https://example.org')
allow_any_instance_of(URI::HTTP).to receive(:open).and_return(io) # rubocop:disable RSpec/AnyInstance
post '/api/v1/callbacks/register_facebook_page',
post "/api/v1/accounts/#{account.id}/callbacks/register_facebook_page",
headers: admin.create_new_auth_token,
params: valid_params,
as: :json
@@ -60,7 +60,7 @@ RSpec.describe 'Callbacks API', type: :request do
it 'registers a new facebook page with avatar on redirect' do
allow_any_instance_of(URI::HTTP).to receive(:open).and_raise(OpenURI::HTTPRedirect.new(nil, nil, URI.parse('https://example.org'))) # rubocop:disable RSpec/AnyInstance
post '/api/v1/callbacks/register_facebook_page',
post "/api/v1/accounts/#{account.id}/callbacks/register_facebook_page",
headers: admin.create_new_auth_token,
params: valid_params,
as: :json
@@ -70,10 +70,10 @@ RSpec.describe 'Callbacks API', type: :request do
end
end
describe 'POST /api/v1/callbacks/get_facebook_pages' do
describe 'POST /api/v1/accounts/{account.id}/callbacks/facebook_pages' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/callbacks/get_facebook_pages'
post "/api/v1/accounts/#{account.id}/callbacks/facebook_pages"
expect(response).to have_http_status(:unauthorized)
end
@@ -83,7 +83,7 @@ RSpec.describe 'Callbacks API', type: :request do
let(:admin) { create(:user, account: account, role: :administrator) }
it 'returns facebook pages of account' do
post '/api/v1/callbacks/get_facebook_pages',
post "/api/v1/accounts/#{account.id}/callbacks/facebook_pages",
headers: admin.create_new_auth_token,
as: :json
@@ -93,10 +93,10 @@ RSpec.describe 'Callbacks API', type: :request do
end
end
describe 'POST /api/v1/callbacks/reauthorize_page' do
describe 'POST /api/v1/accounts/{account.id}/callbacks/reauthorize_page' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/callbacks/reauthorize_page'
post "/api/v1/accounts/#{account.id}/callbacks/reauthorize_page"
expect(response).to have_http_status(:unauthorized)
end
@@ -108,7 +108,7 @@ RSpec.describe 'Callbacks API', type: :request do
it 'reauthorizes the page' do
params = { inbox_id: inbox.id }
post '/api/v1/callbacks/reauthorize_page',
post "/api/v1/accounts/#{account.id}/callbacks/reauthorize_page",
headers: admin.create_new_auth_token,
params: params,
as: :json
@@ -120,7 +120,7 @@ RSpec.describe 'Callbacks API', type: :request do
allow(koala_api).to receive(:get_connections).and_return([])
params = { inbox_id: inbox.id }
post '/api/v1/callbacks/reauthorize_page',
post "/api/v1/accounts/#{account.id}/callbacks/reauthorize_page",
headers: admin.create_new_auth_token,
params: params,
as: :json

View File

@@ -7,10 +7,10 @@ RSpec.describe 'Canned Responses API', type: :request do
create(:canned_response, account: account)
end
describe 'GET /api/v1/canned_responses' do
describe 'GET /api/v1/accounts/{account.id}/canned_responses' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get '/api/v1/canned_responses'
get "/api/v1/accounts/#{account.id}/canned_responses"
expect(response).to have_http_status(:unauthorized)
end
@@ -20,7 +20,7 @@ RSpec.describe 'Canned Responses API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'returns all the canned responses' do
get '/api/v1/canned_responses',
get "/api/v1/accounts/#{account.id}/canned_responses",
headers: agent.create_new_auth_token,
as: :json
@@ -33,7 +33,7 @@ RSpec.describe 'Canned Responses API', type: :request do
params = { search: CannedResponse.last.short_code }
get '/api/v1/canned_responses',
get "/api/v1/accounts/#{account.id}/canned_responses",
params: params,
headers: agent.create_new_auth_token,
as: :json
@@ -44,10 +44,10 @@ RSpec.describe 'Canned Responses API', type: :request do
end
end
describe 'POST /api/v1/canned_responses' do
describe 'POST /api/v1/accounts/{account.id}/canned_responses' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/canned_responses'
post "/api/v1/accounts/#{account.id}/canned_responses"
expect(response).to have_http_status(:unauthorized)
end
@@ -59,7 +59,7 @@ RSpec.describe 'Canned Responses API', type: :request do
it 'creates a new canned response' do
params = { short_code: 'short', content: 'content' }
post '/api/v1/canned_responses',
post "/api/v1/accounts/#{account.id}/canned_responses",
params: params,
headers: agent.create_new_auth_token,
as: :json
@@ -70,12 +70,12 @@ RSpec.describe 'Canned Responses API', type: :request do
end
end
describe 'PUT /api/v1/canned_responses/:id' do
describe 'PUT /api/v1/accounts/{account.id}/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}"
put "/api/v1/accounts/#{account.id}/canned_responses/#{canned_response.id}"
expect(response).to have_http_status(:unauthorized)
end
@@ -87,7 +87,7 @@ RSpec.describe 'Canned Responses API', type: :request do
it 'updates an existing canned response' do
params = { short_code: 'B' }
put "/api/v1/canned_responses/#{canned_response.id}",
put "/api/v1/accounts/#{account.id}/canned_responses/#{canned_response.id}",
params: params,
headers: agent.create_new_auth_token,
as: :json
@@ -98,12 +98,12 @@ RSpec.describe 'Canned Responses API', type: :request do
end
end
describe 'DELETE /api/v1/canned_responses/:id' do
describe 'DELETE /api/v1/accounts/{account.id}/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}"
delete "/api/v1/accounts/#{account.id}/canned_responses/#{canned_response.id}"
expect(response).to have_http_status(:unauthorized)
end
@@ -113,7 +113,7 @@ RSpec.describe 'Canned Responses API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'destroys the canned response' do
delete "/api/v1/canned_responses/#{canned_response.id}",
delete "/api/v1/accounts/#{account.id}/canned_responses/#{canned_response.id}",
headers: agent.create_new_auth_token,
as: :json

View File

@@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe '/api/v1/contacts/:id/conversations', type: :request do
RSpec.describe '/api/v1/accounts/{account.id}/contacts/:id/conversations', type: :request do
let(:account) { create(:account) }
let(:contact) { create(:contact, account: account) }
let(:inbox_1) { create(:inbox, account: account) }
@@ -17,10 +17,10 @@ RSpec.describe '/api/v1/contacts/:id/conversations', type: :request do
2.times.each { create(:conversation, account: account, inbox: inbox_2, contact: contact, contact_inbox: contact_inbox_2) }
end
describe 'GET /api/v1/contacts/:id/conversations' do
describe 'GET /api/v1/accounts/{account.id}/contacts/:id/conversations' do
context 'when unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/contacts/#{contact.id}/conversations"
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/conversations"
expect(response).to have_http_status(:unauthorized)
end
end
@@ -28,7 +28,7 @@ RSpec.describe '/api/v1/contacts/:id/conversations', type: :request do
context 'when user is logged in' do
context 'with user as administrator' do
it 'returns conversations from all inboxes' do
get "/api/v1/contacts/#{contact.id}/conversations", headers: admin.create_new_auth_token
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/conversations", headers: admin.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
@@ -39,7 +39,7 @@ RSpec.describe '/api/v1/contacts/:id/conversations', type: :request do
context 'with user as agent' do
it 'returns conversations from the inboxes which agent has access to' do
get "/api/v1/contacts/#{contact.id}/conversations", headers: agent.create_new_auth_token
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/conversations", headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
@@ -50,7 +50,7 @@ RSpec.describe '/api/v1/contacts/:id/conversations', type: :request do
context 'with user as unknown role' do
it 'returns conversations from no inboxes' do
get "/api/v1/contacts/#{contact.id}/conversations", headers: unknown.create_new_auth_token
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/conversations", headers: unknown.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)

View File

@@ -3,10 +3,10 @@ require 'rails_helper'
RSpec.describe 'Contacts API', type: :request do
let(:account) { create(:account) }
describe 'GET /api/v1/contacts' do
describe 'GET /api/v1/accounts/{account.id}/contacts' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get '/api/v1/contacts'
get "/api/v1/accounts/#{account.id}/contacts"
expect(response).to have_http_status(:unauthorized)
end
@@ -17,7 +17,7 @@ RSpec.describe 'Contacts API', type: :request do
let!(:contact) { create(:contact, account: account) }
it 'returns all contacts' do
get '/api/v1/contacts',
get "/api/v1/accounts/#{account.id}/contacts",
headers: admin.create_new_auth_token,
as: :json
@@ -27,12 +27,12 @@ RSpec.describe 'Contacts API', type: :request do
end
end
describe 'GET /api/v1/contacts/:id' do
describe 'GET /api/v1/accounts/{account.id}/contacts/:id' do
let!(:contact) { create(:contact, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/contacts/#{contact.id}"
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}"
expect(response).to have_http_status(:unauthorized)
end
@@ -42,7 +42,7 @@ RSpec.describe 'Contacts API', type: :request do
let(:admin) { create(:user, account: account, role: :administrator) }
it 'shows the contact' do
get "/api/v1/contacts/#{contact.id}",
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
headers: admin.create_new_auth_token,
as: :json
@@ -52,12 +52,12 @@ RSpec.describe 'Contacts API', type: :request do
end
end
describe 'POST /api/v1/contacts' do
describe 'POST /api/v1/accounts/{account.id}/contacts' do
let(:valid_params) { { contact: { account_id: account.id } } }
context 'when it is an unauthenticated user' do
it 'creates the contact' do
expect { post '/api/v1/contacts', params: valid_params }.to change(Contact, :count).by(1)
expect { post "/api/v1/accounts/#{account.id}/contacts", params: valid_params }.to change(Contact, :count).by(1)
expect(response).to have_http_status(:success)
end
@@ -67,20 +67,23 @@ RSpec.describe 'Contacts API', type: :request do
let(:admin) { create(:user, account: account, role: :administrator) }
it 'creates the contact' do
expect { post '/api/v1/contacts', headers: admin.create_new_auth_token, params: valid_params }.to change(Contact, :count).by(1)
expect do
post "/api/v1/accounts/#{account.id}/contacts", headers: admin.create_new_auth_token,
params: valid_params
end .to change(Contact, :count).by(1)
expect(response).to have_http_status(:success)
end
end
end
describe 'PATCH /api/v1/contacts/:id' do
describe 'PATCH /api/v1/accounts/{account.id}/contacts/:id' do
let!(:contact) { create(:contact, account: account) }
let(:valid_params) { { contact: { name: 'Test Blub' } } }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
put "/api/v1/contacts/#{contact.id}",
put "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
params: valid_params
expect(response).to have_http_status(:unauthorized)
@@ -91,7 +94,7 @@ RSpec.describe 'Contacts API', type: :request do
let(:admin) { create(:user, account: account, role: :administrator) }
it 'updates the contact' do
patch "/api/v1/contacts/#{contact.id}",
patch "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
headers: admin.create_new_auth_token,
params: valid_params,
as: :json
@@ -104,7 +107,7 @@ RSpec.describe 'Contacts API', type: :request do
other_account = create(:account)
other_contact = create(:contact, account: other_account)
patch "/api/v1/contacts/#{other_contact.id}",
patch "/api/v1/accounts/#{account.id}/contacts/#{other_contact.id}",
headers: admin.create_new_auth_token,
params: valid_params,
as: :json

View File

@@ -3,12 +3,12 @@ require 'rails_helper'
RSpec.describe 'Conversation Assignment API', type: :request do
let(:account) { create(:account) }
describe 'POST /api/v1/conversations/<id>/assignments' do
describe 'POST /api/v1/accounts/{account.id}/conversations/<id>/assignments' do
let(:conversation) { create(:conversation, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post api_v1_conversation_assignments_url(conversation.display_id)
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id)
expect(response).to have_http_status(:unauthorized)
end
@@ -20,7 +20,7 @@ RSpec.describe 'Conversation Assignment API', type: :request do
it 'assigns a user to the conversation' do
params = { assignee_id: agent.id }
post api_v1_conversation_assignments_url(conversation.display_id),
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
params: params,
headers: agent.create_new_auth_token,
as: :json

View File

@@ -3,7 +3,7 @@ require 'rails_helper'
RSpec.describe 'Conversation Label API', type: :request do
let(:account) { create(:account) }
describe 'GET /api/v1/conversations/<id>/labels' do
describe 'GET /api/v1/accounts/{account.id}/conversations/<id>/labels' do
let(:conversation) { create(:conversation, account: account) }
before do
@@ -12,7 +12,7 @@ RSpec.describe 'Conversation Label API', type: :request do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get api_v1_conversation_labels_url(conversation)
get api_v1_account_conversation_labels_url(account_id: account.id, conversation_id: conversation.display_id)
expect(response).to have_http_status(:unauthorized)
end
end
@@ -21,7 +21,7 @@ RSpec.describe 'Conversation Label API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'returns all the labels for the conversation' do
get api_v1_conversation_labels_url(conversation.display_id),
get api_v1_account_conversation_labels_url(account_id: account.id, conversation_id: conversation.display_id),
headers: agent.create_new_auth_token,
as: :json
@@ -32,7 +32,7 @@ RSpec.describe 'Conversation Label API', type: :request do
end
end
describe 'POST /api/v1/conversations/<id>/labels' do
describe 'POST /api/v1/accounts/{account.id}/conversations/<id>/labels' do
let(:conversation) { create(:conversation, account: account) }
before do
@@ -41,7 +41,7 @@ RSpec.describe 'Conversation Label API', type: :request do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post api_v1_conversation_labels_url(conversation.display_id),
post api_v1_account_conversation_labels_url(account_id: account.id, conversation_id: conversation.display_id),
params: { labels: 'label3,label4' },
as: :json
@@ -53,7 +53,7 @@ RSpec.describe 'Conversation Label API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'creates labels for the conversation' do
post api_v1_conversation_labels_url(conversation.display_id),
post api_v1_account_conversation_labels_url(account_id: account.id, conversation_id: conversation.display_id),
params: { labels: 'label3,label4' },
headers: agent.create_new_auth_token,
as: :json

View File

@@ -3,12 +3,12 @@ require 'rails_helper'
RSpec.describe 'Conversation Messages API', type: :request do
let(:account) { create(:account) }
describe 'POST /api/v1/conversations/<id>/messages' do
describe 'POST /api/v1/accounts/{account.id}/conversations/<id>/messages' do
let(:conversation) { create(:conversation, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post api_v1_conversation_messages_url(conversation.display_id)
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id)
expect(response).to have_http_status(:unauthorized)
end
@@ -20,7 +20,7 @@ RSpec.describe 'Conversation Messages API', type: :request do
it 'creates a new outgoing message' do
params = { message: 'test-message', private: true }
post api_v1_conversation_messages_url(conversation.display_id),
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
params: params,
headers: agent.create_new_auth_token,
as: :json
@@ -32,12 +32,12 @@ RSpec.describe 'Conversation Messages API', type: :request do
end
end
describe 'GET /api/v1/conversations/:id/messages' do
describe 'GET /api/v1/accounts/{account.id}/conversations/:id/messages' do
let(:conversation) { create(:conversation, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/conversations/#{conversation.display_id}/messages"
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/messages"
expect(response).to have_http_status(:unauthorized)
end
@@ -47,7 +47,7 @@ RSpec.describe 'Conversation Messages API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'shows the conversation' do
get "/api/v1/conversations/#{conversation.display_id}/messages",
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/messages",
headers: agent.create_new_auth_token,
as: :json

View File

@@ -3,10 +3,10 @@ require 'rails_helper'
RSpec.describe 'Conversations API', type: :request do
let(:account) { create(:account) }
describe 'GET /api/v1/conversations' do
describe 'GET /api/v1/accounts/{account.id}/conversations' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get '/api/v1/conversations'
get "/api/v1/accounts/#{account.id}/conversations"
expect(response).to have_http_status(:unauthorized)
end
@@ -21,7 +21,7 @@ RSpec.describe 'Conversations API', type: :request do
end
it 'returns all conversations' do
get '/api/v1/conversations',
get "/api/v1/accounts/#{account.id}/conversations",
headers: agent.create_new_auth_token,
as: :json
@@ -31,12 +31,12 @@ RSpec.describe 'Conversations API', type: :request do
end
end
describe 'GET /api/v1/conversations/:id' do
describe 'GET /api/v1/accounts/{account.id}/conversations/:id' do
let(:conversation) { create(:conversation, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/conversations/#{conversation.display_id}"
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}"
expect(response).to have_http_status(:unauthorized)
end
@@ -46,7 +46,7 @@ RSpec.describe 'Conversations API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'shows the conversation' do
get "/api/v1/conversations/#{conversation.display_id}",
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}",
headers: agent.create_new_auth_token,
as: :json
@@ -56,12 +56,12 @@ RSpec.describe 'Conversations API', type: :request do
end
end
describe 'POST /api/v1/conversations/:id/toggle_status' do
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/toggle_status' do
let(:conversation) { create(:conversation, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post "/api/v1/conversations/#{conversation.display_id}/toggle_status"
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status"
expect(response).to have_http_status(:unauthorized)
end
@@ -73,7 +73,7 @@ RSpec.describe 'Conversations API', type: :request do
it 'toggles the conversation status' do
expect(conversation.status).to eq('open')
post "/api/v1/conversations/#{conversation.display_id}/toggle_status",
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status",
headers: agent.create_new_auth_token,
as: :json
@@ -83,12 +83,12 @@ RSpec.describe 'Conversations API', type: :request do
end
end
describe 'POST /api/v1/conversations/:id/update_last_seen' do
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/update_last_seen' do
let(:conversation) { create(:conversation, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post "/api/v1/conversations/#{conversation.display_id}/update_last_seen"
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/update_last_seen"
expect(response).to have_http_status(:unauthorized)
end
@@ -100,7 +100,7 @@ RSpec.describe 'Conversations API', type: :request do
it 'updates last seen' do
params = { agent_last_seen_at: '-1' }
post "/api/v1/conversations/#{conversation.display_id}/update_last_seen",
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/update_last_seen",
headers: agent.create_new_auth_token,
params: params,
as: :json

View File

@@ -12,10 +12,10 @@ RSpec.describe 'Facebook Indicators API', type: :request do
allow(Facebook::Messenger::Subscriptions).to receive(:subscribe).and_return(true)
end
describe 'POST /api/v1/facebook_indicators/mark_seen' do
describe 'POST /api/v1/accounts/{account.id}/facebook_indicators/mark_seen' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/facebook_indicators/mark_seen'
post "/api/v1/accounts/#{account.id}/facebook_indicators/mark_seen"
expect(response).to have_http_status(:unauthorized)
end
@@ -32,7 +32,7 @@ RSpec.describe 'Facebook Indicators API', type: :request do
access_token: inbox.channel.page_access_token
)
post '/api/v1/facebook_indicators/mark_seen',
post "/api/v1/accounts/#{account.id}/facebook_indicators/mark_seen",
headers: agent.create_new_auth_token,
params: valid_params,
as: :json
@@ -45,7 +45,7 @@ RSpec.describe 'Facebook Indicators API', type: :request do
allow(Facebook::Messenger::Bot).to receive(:deliver).and_raise(Facebook::Messenger::Error)
post '/api/v1/facebook_indicators/mark_seen',
post "/api/v1/accounts/#{account.id}/facebook_indicators/mark_seen",
headers: agent.create_new_auth_token,
params: valid_params,
as: :json
@@ -55,10 +55,10 @@ RSpec.describe 'Facebook Indicators API', type: :request do
end
end
describe 'POST /api/v1/facebook_indicators/typing_on' do
describe 'POST /api/v1/accounts/{account.id}/facebook_indicators/typing_on' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/facebook_indicators/typing_on'
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_on"
expect(response).to have_http_status(:unauthorized)
end
@@ -75,7 +75,7 @@ RSpec.describe 'Facebook Indicators API', type: :request do
access_token: inbox.channel.page_access_token
)
post '/api/v1/facebook_indicators/typing_on',
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_on",
headers: agent.create_new_auth_token,
params: valid_params,
as: :json
@@ -88,7 +88,7 @@ RSpec.describe 'Facebook Indicators API', type: :request do
allow(Facebook::Messenger::Bot).to receive(:deliver).and_raise(Facebook::Messenger::Error)
post '/api/v1/facebook_indicators/typing_on',
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_on",
headers: agent.create_new_auth_token,
params: valid_params,
as: :json
@@ -98,10 +98,10 @@ RSpec.describe 'Facebook Indicators API', type: :request do
end
end
describe 'POST /api/v1/facebook_indicators/typing_off' do
describe 'POST /api/v1/accounts/{account.id}/facebook_indicators/typing_off' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/facebook_indicators/typing_off'
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_off"
expect(response).to have_http_status(:unauthorized)
end
@@ -118,7 +118,7 @@ RSpec.describe 'Facebook Indicators API', type: :request do
access_token: inbox.channel.page_access_token
)
post '/api/v1/facebook_indicators/typing_off',
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_off",
headers: agent.create_new_auth_token,
params: valid_params,
as: :json
@@ -131,7 +131,7 @@ RSpec.describe 'Facebook Indicators API', type: :request do
allow(Facebook::Messenger::Bot).to receive(:deliver).and_raise(Facebook::Messenger::Error)
post '/api/v1/facebook_indicators/typing_off',
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_off",
headers: agent.create_new_auth_token,
params: valid_params,
as: :json

View File

@@ -4,10 +4,10 @@ RSpec.describe 'Inbox Member API', type: :request do
let(:account) { create(:account) }
let(:inbox) { create(:inbox, account: account) }
describe 'POST /api/v1/inbox_members' do
describe 'POST /api/v1/accounts/{account.id}/inbox_members' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/inbox_members'
post "/api/v1/accounts/#{account.id}/inbox_members"
expect(response).to have_http_status(:unauthorized)
end
@@ -19,7 +19,7 @@ RSpec.describe 'Inbox Member API', type: :request do
it 'modifies inbox members' do
params = { inbox_id: inbox.id, user_ids: [agent.id] }
post '/api/v1/inbox_members',
post "/api/v1/accounts/#{account.id}/inbox_members",
headers: agent.create_new_auth_token,
params: params,
as: :json
@@ -32,7 +32,7 @@ RSpec.describe 'Inbox Member API', type: :request do
it 'renders not found when inbox not found' do
params = { inbox_id: nil, user_ids: [agent.id] }
post '/api/v1/inbox_members',
post "/api/v1/accounts/#{account.id}/inbox_members",
headers: agent.create_new_auth_token,
params: params,
as: :json
@@ -43,7 +43,7 @@ RSpec.describe 'Inbox Member API', type: :request do
it 'renders error on invalid params' do
params = { inbox_id: inbox.id, user_ids: ['invalid'] }
post '/api/v1/inbox_members',
post "/api/v1/accounts/#{account.id}/inbox_members",
headers: agent.create_new_auth_token,
params: params,
as: :json
@@ -54,12 +54,12 @@ RSpec.describe 'Inbox Member API', type: :request do
end
end
describe 'GET /api/v1/inbox_members/:id' do
describe 'GET /api/v1/accounts/{account.id}/inbox_members/:id' do
let(:inbox_member) { create(:inbox_member, inbox: inbox) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/inbox_members/#{inbox_member.id}"
get "/api/v1/accounts/#{account.id}/inbox_members/#{inbox_member.id}"
expect(response).to have_http_status(:unauthorized)
end
@@ -69,7 +69,7 @@ RSpec.describe 'Inbox Member API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'returns inbox member' do
get "/api/v1/inbox_members/#{inbox.id}",
get "/api/v1/accounts/#{account.id}/inbox_members/#{inbox.id}",
headers: agent.create_new_auth_token,
as: :json

View File

@@ -3,10 +3,10 @@ require 'rails_helper'
RSpec.describe 'Inboxes API', type: :request do
let(:account) { create(:account) }
describe 'GET /api/v1/inboxes' do
describe 'GET /api/v1/accounts/{account.id}/inboxes' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get '/api/v1/inboxes'
get "/api/v1/accounts/#{account.id}/inboxes"
expect(response).to have_http_status(:unauthorized)
end
@@ -23,7 +23,7 @@ RSpec.describe 'Inboxes API', type: :request do
end
it 'returns all inboxes of current_account as administrator' do
get '/api/v1/inboxes',
get "/api/v1/accounts/#{account.id}/inboxes",
headers: admin.create_new_auth_token,
as: :json
@@ -32,7 +32,7 @@ RSpec.describe 'Inboxes API', type: :request do
end
it 'returns only assigned inboxes of current_account as agent' do
get '/api/v1/inboxes',
get "/api/v1/accounts/#{account.id}/inboxes",
headers: agent.create_new_auth_token,
as: :json
@@ -42,12 +42,12 @@ RSpec.describe 'Inboxes API', type: :request do
end
end
describe 'DELETE /api/v1/inboxes/:id' do
describe 'DELETE /api/v1/accounts/{account.id}/inboxes/:id' do
let(:inbox) { create(:inbox, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/inboxes/#{inbox.id}"
delete "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}"
expect(response).to have_http_status(:unauthorized)
end
@@ -57,7 +57,7 @@ RSpec.describe 'Inboxes API', type: :request do
let(:admin) { create(:user, account: account, role: :administrator) }
it 'deletes inbox' do
delete "/api/v1/inboxes/#{inbox.id}",
delete "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}",
headers: admin.create_new_auth_token,
as: :json
@@ -69,7 +69,7 @@ RSpec.describe 'Inboxes API', type: :request do
other_account = create(:account)
other_inbox = create(:inbox, account: other_account)
delete "/api/v1/inboxes/#{other_inbox.id}",
delete "/api/v1/accounts/#{account.id}/inboxes/#{other_inbox.id}",
headers: admin.create_new_auth_token,
as: :json
@@ -79,7 +79,7 @@ RSpec.describe 'Inboxes API', type: :request do
it 'is unable to delete inbox as agent' do
agent = create(:user, account: account, role: :agent)
delete "/api/v1/inboxes/#{inbox.id}",
delete "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}",
headers: agent.create_new_auth_token,
as: :json
@@ -88,12 +88,12 @@ RSpec.describe 'Inboxes API', type: :request do
end
end
describe 'PATCH /api/v1/inboxes/:id' do
describe 'PATCH /api/v1/accounts/{account.id}/inboxes/:id' do
let(:inbox) { create(:inbox, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
patch "/api/v1/inboxes/#{inbox.id}"
patch "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}"
expect(response).to have_http_status(:unauthorized)
end
@@ -104,7 +104,7 @@ RSpec.describe 'Inboxes API', type: :request do
let(:valid_params) { { inbox: { enable_auto_assignment: false } } }
it 'updates inbox' do
patch "/api/v1/inboxes/#{inbox.id}",
patch "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}",
headers: admin.create_new_auth_token,
params: valid_params,
as: :json
@@ -116,7 +116,7 @@ RSpec.describe 'Inboxes API', type: :request do
it 'will not update inbox for agent' do
agent = create(:user, account: account, role: :agent)
patch "/api/v1/inboxes/#{inbox.id}",
patch "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}",
headers: agent.create_new_auth_token,
params: valid_params,
as: :json

View File

@@ -8,10 +8,10 @@ RSpec.describe 'Label API', type: :request do
conversation.update_labels('label1, label2')
end
describe 'GET /api/v1/labels' do
describe 'GET /api/v1/accounts/{account.id}/labels' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get '/api/v1/labels'
get "/api/v1/accounts/#{account.id}/labels"
expect(response).to have_http_status(:unauthorized)
end
@@ -21,7 +21,7 @@ RSpec.describe 'Label API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'returns all the labels in account' do
get '/api/v1/labels',
get "/api/v1/accounts/#{account.id}/labels",
headers: agent.create_new_auth_token,
as: :json
@@ -32,10 +32,10 @@ RSpec.describe 'Label API', type: :request do
end
end
describe 'GET /api/v1/labels/most_used' do
describe 'GET /api/v1/accounts/{account.id}/labels/most_used' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get '/api/v1/labels'
get "/api/v1/accounts/#{account.id}/labels"
expect(response).to have_http_status(:unauthorized)
end
@@ -45,7 +45,7 @@ RSpec.describe 'Label API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'returns most used labels' do
get '/api/v1/labels/most_used',
get "/api/v1/accounts/#{account.id}/labels/most_used",
headers: agent.create_new_auth_token,
params: { count: 1 },
as: :json

View File

@@ -3,10 +3,10 @@ require 'rails_helper'
RSpec.describe 'Notification Settings API', type: :request do
let(:account) { create(:account) }
describe 'GET /api/v1/user/notification_settings' do
describe 'GET /api/v1/accounts/{account.id}/notification_settings' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get '/api/v1/user/notification_settings'
get "/api/v1/accounts/#{account.id}/notification_settings"
expect(response).to have_http_status(:unauthorized)
end
@@ -16,7 +16,7 @@ RSpec.describe 'Notification Settings API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'returns current user notification settings' do
get '/api/v1/user/notification_settings',
get "/api/v1/accounts/#{account.id}/notification_settings",
headers: agent.create_new_auth_token,
as: :json
@@ -28,10 +28,10 @@ RSpec.describe 'Notification Settings API', type: :request do
end
end
describe 'PUT /api/v1/user/notification_settings' do
describe 'PUT /api/v1/accounts/{account.id}/notification_settings' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
put '/api/v1/user/notification_settings'
put "/api/v1/accounts/#{account.id}/notification_settings"
expect(response).to have_http_status(:unauthorized)
end
@@ -41,7 +41,7 @@ RSpec.describe 'Notification Settings API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'updates the email related notification flags' do
put '/api/v1/user/notification_settings',
put "/api/v1/accounts/#{account.id}/notification_settings",
params: { notification_settings: { selected_email_flags: ['conversation_assignment'] } },
headers: agent.create_new_auth_token,
as: :json

View File

@@ -3,12 +3,12 @@ require 'rails_helper'
RSpec.describe 'Subscriptions API', type: :request do
let(:account) { create(:account) }
describe 'GET /api/v1/subscriptions' do
describe 'GET /api/v1/accounts/{account.id}/subscriptions' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
ENV['BILLING_ENABLED'] = 'true'
get '/api/v1/subscriptions'
get "/api/v1/accounts/#{account.id}/subscriptions"
expect(response).to have_http_status(:unauthorized)
@@ -22,7 +22,7 @@ RSpec.describe 'Subscriptions API', type: :request do
it 'returns all subscriptions' do
ENV['BILLING_ENABLED'] = 'true'
get '/api/v1/subscriptions',
get "/api/v1/accounts/#{account.id}/subscriptions",
headers: agent.create_new_auth_token,
as: :json
@@ -35,7 +35,7 @@ RSpec.describe 'Subscriptions API', type: :request do
it 'throws 404 error if env variable is not set' do
ENV['BILLING_ENABLED'] = nil
get '/api/v1/subscriptions',
get "/api/v1/accounts/#{account.id}/subscriptions",
headers: agent.create_new_auth_token,
as: :json

View File

@@ -7,10 +7,10 @@ RSpec.describe 'Webhooks API', type: :request do
let(:administrator) { create(:user, account: account, role: :administrator) }
let(:agent) { create(:user, account: account, role: :agent) }
describe 'GET /api/v1/account/webhooks' do
describe 'GET /api/v1/accounts/<account_id>/webhooks' do
context 'when it is an authenticated agent' do
it 'returns unauthorized' do
get '/api/v1/account/webhooks',
get "/api/v1/accounts/#{account.id}/webhooks",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
@@ -19,7 +19,7 @@ RSpec.describe 'Webhooks API', type: :request do
context 'when it is an authenticated admin user' do
it 'gets all webhook' do
get '/api/v1/account/webhooks',
get "/api/v1/accounts/#{account.id}/webhooks",
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
@@ -28,10 +28,10 @@ RSpec.describe 'Webhooks API', type: :request do
end
end
describe 'POST /api/v1/account/webhooks' do
describe 'POST /api/v1/accounts/<account_id>/webhooks' do
context 'when it is an authenticated agent' do
it 'returns unauthorized' do
post '/api/v1/account/webhooks',
post "/api/v1/accounts/#{account.id}/webhooks",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
@@ -40,7 +40,7 @@ RSpec.describe 'Webhooks API', type: :request do
context 'when it is an authenticated admin user' do
it 'creates webhook' do
post '/api/v1/account/webhooks',
post "/api/v1/accounts/#{account.id}/webhooks",
params: { account_id: account.id, inbox_id: inbox.id, url: 'https://hello.com' },
headers: administrator.create_new_auth_token,
as: :json
@@ -51,10 +51,10 @@ RSpec.describe 'Webhooks API', type: :request do
end
end
describe 'PUT /api/v1/account/webhooks/:id' do
describe 'PUT /api/v1/accounts/<account_id>/webhooks/:id' do
context 'when it is an authenticated agent' do
it 'returns unauthorized' do
put "/api/v1/account/webhooks/#{webhook.id}",
put "/api/v1/accounts/#{account.id}/webhooks/#{webhook.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
@@ -63,7 +63,7 @@ RSpec.describe 'Webhooks API', type: :request do
context 'when it is an authenticated admin user' do
it 'updates webhook' do
put "/api/v1/account/webhooks/#{webhook.id}",
put "/api/v1/accounts/#{account.id}/webhooks/#{webhook.id}",
params: { url: 'https://hello.com' },
headers: administrator.create_new_auth_token,
as: :json
@@ -73,10 +73,10 @@ RSpec.describe 'Webhooks API', type: :request do
end
end
describe 'DELETE /api/v1/account/webhooks/:id' do
describe 'DELETE /api/v1/accounts/<account_id>/webhooks/:id' do
context 'when it is an authenticated agent' do
it 'returns unauthorized' do
delete "/api/v1/account/webhooks/#{webhook.id}",
delete "/api/v1/accounts/#{account.id}/webhooks/#{webhook.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
@@ -85,7 +85,7 @@ RSpec.describe 'Webhooks API', type: :request do
context 'when it is an authenticated admin user' do
it 'deletes webhook' do
delete "/api/v1/account/webhooks/#{webhook.id}",
delete "/api/v1/accounts/#{account.id}/webhooks/#{webhook.id}",
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)

View File

@@ -5,7 +5,8 @@ RSpec.describe 'Accounts API', type: :request do
context 'when posting to accounts with correct parameters' do
let(:account_builder) { double }
let(:email) { Faker::Internet.email }
let(:user) { create(:user, email: email) }
let(:account) { create(:account) }
let(:user) { create(:user, email: email, account: account) }
before do
allow(AccountBuilder).to receive(:new).and_return(account_builder)