2020-09-25 21:02:34 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2022-05-16 08:29:59 +00:00
|
|
|
RSpec.describe 'Api::V1::Accounts::Portals', type: :request do
|
2020-09-25 21:02:34 +00:00
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
2022-07-04 14:59:44 +00:00
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
|
|
|
let(:agent_1) { create(:user, account: account, role: :agent) }
|
|
|
|
let(:agent_2) { create(:user, account: account, role: :agent) }
|
2022-05-16 08:29:59 +00:00
|
|
|
let!(:portal) { create(:portal, slug: 'portal-1', name: 'test_portal', account_id: account.id) }
|
2022-04-20 10:30:37 +00:00
|
|
|
|
2022-07-04 14:59:44 +00:00
|
|
|
before { create(:portal_member, user: agent, portal: portal) }
|
|
|
|
|
2022-05-16 08:29:59 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/portals' do
|
2022-04-20 10:30:37 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2022-05-16 08:29:59 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/portals"
|
2022-04-20 10:30:37 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'get all portals' do
|
2022-05-16 08:29:59 +00:00
|
|
|
portal2 = create(:portal, name: 'test_portal_2', account_id: account.id, slug: 'portal-2')
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(portal2.id).not_to be_nil
|
2022-05-16 08:29:59 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/portals",
|
2022-04-20 10:30:37 +00:00
|
|
|
headers: agent.create_new_auth_token
|
2022-07-04 14:59:44 +00:00
|
|
|
|
2022-04-20 10:30:37 +00:00
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
2022-07-04 14:59:44 +00:00
|
|
|
expect(json_response['payload'].length).to be 2
|
|
|
|
expect(json_response['payload'][0]['id']).to be portal.id
|
2022-04-20 10:30:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-16 08:29:59 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/portals/{portal.slug}' do
|
2022-04-20 10:30:37 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2022-05-16 08:29:59 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/portals"
|
2022-07-04 14:59:44 +00:00
|
|
|
|
2022-04-20 10:30:37 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'get one portals' do
|
2022-05-16 08:29:59 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}",
|
2022-04-20 10:30:37 +00:00
|
|
|
headers: agent.create_new_auth_token
|
2022-07-04 14:59:44 +00:00
|
|
|
|
2022-04-20 10:30:37 +00:00
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response['name']).to eq portal.name
|
2022-10-21 20:43:15 +00:00
|
|
|
expect(json_response['meta']['all_articles_count']).to eq 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns portal articles metadata' do
|
|
|
|
portal.update(config: { allowed_locales: %w[en es], default_locale: 'en' })
|
|
|
|
en_cat = create(:category, locale: :en, portal_id: portal.id, slug: 'en-cat')
|
|
|
|
es_cat = create(:category, locale: :es, portal_id: portal.id, slug: 'es-cat')
|
|
|
|
create(:article, category_id: en_cat.id, portal_id: portal.id, author_id: agent.id)
|
|
|
|
create(:article, category_id: en_cat.id, portal_id: portal.id, author_id: admin.id)
|
|
|
|
create(:article, category_id: es_cat.id, portal_id: portal.id, author_id: agent.id)
|
|
|
|
|
|
|
|
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}?locale=en",
|
|
|
|
headers: agent.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response['name']).to eq portal.name
|
|
|
|
expect(json_response['meta']['all_articles_count']).to eq 2
|
|
|
|
expect(json_response['meta']['mine_articles_count']).to eq 1
|
2022-04-20 10:30:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-25 21:02:34 +00:00
|
|
|
|
2022-05-16 08:29:59 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/portals' do
|
2020-09-25 21:02:34 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2022-07-04 14:59:44 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/portals",
|
|
|
|
params: {},
|
|
|
|
headers: agent.create_new_auth_token
|
|
|
|
|
2020-09-25 21:02:34 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'creates portal' do
|
2022-07-11 07:13:24 +00:00
|
|
|
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
|
|
|
|
|
2020-09-25 21:02:34 +00:00
|
|
|
portal_params = {
|
|
|
|
portal: {
|
|
|
|
name: 'test_portal',
|
2022-12-03 15:07:56 +00:00
|
|
|
slug: 'test_kbase',
|
|
|
|
custom_domain: 'https://support.chatwoot.dev'
|
2022-07-11 07:13:24 +00:00
|
|
|
},
|
|
|
|
logo: file
|
2020-09-25 21:02:34 +00:00
|
|
|
}
|
2022-05-16 08:29:59 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/portals",
|
2020-09-25 21:02:34 +00:00
|
|
|
params: portal_params,
|
2022-07-04 14:59:44 +00:00
|
|
|
headers: admin.create_new_auth_token
|
|
|
|
|
2020-09-25 21:02:34 +00:00
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
2022-04-20 10:30:37 +00:00
|
|
|
expect(json_response['name']).to eql('test_portal')
|
2022-07-11 07:13:24 +00:00
|
|
|
expect(json_response['logo']['filename']).to eql('avatar.png')
|
2022-12-03 15:07:56 +00:00
|
|
|
expect(json_response['custom_domain']).to eql('support.chatwoot.dev')
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-16 08:29:59 +00:00
|
|
|
describe 'PUT /api/v1/accounts/{account.id}/portals/{portal.slug}' do
|
2020-09-25 21:02:34 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2022-05-16 08:29:59 +00:00
|
|
|
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}", params: {}
|
2022-07-04 14:59:44 +00:00
|
|
|
|
2020-09-25 21:02:34 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'updates portal' do
|
|
|
|
portal_params = {
|
|
|
|
portal: {
|
2022-07-11 07:13:24 +00:00
|
|
|
name: 'updated_test_portal',
|
|
|
|
config: { 'allowed_locales' => %w[en es] }
|
2020-09-25 21:02:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(portal.name).to eql('test_portal')
|
|
|
|
|
2022-05-16 08:29:59 +00:00
|
|
|
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}",
|
2020-09-25 21:02:34 +00:00
|
|
|
params: portal_params,
|
2022-07-04 14:59:44 +00:00
|
|
|
headers: admin.create_new_auth_token
|
|
|
|
|
2020-09-25 21:02:34 +00:00
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
2022-04-20 10:30:37 +00:00
|
|
|
expect(json_response['name']).to eql(portal_params[:portal][:name])
|
2022-08-03 05:03:48 +00:00
|
|
|
expect(json_response['config']).to eql({ 'allowed_locales' => [{ 'articles_count' => 0, 'categories_count' => 0, 'code' => 'en' },
|
|
|
|
{ 'articles_count' => 0, 'categories_count' => 0, 'code' => 'es' }] })
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|
2022-05-16 08:29:59 +00:00
|
|
|
|
|
|
|
it 'archive portal' do
|
|
|
|
portal_params = {
|
|
|
|
portal: {
|
|
|
|
archived: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(portal.archived).to be_falsy
|
|
|
|
|
|
|
|
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}",
|
|
|
|
params: portal_params,
|
2022-07-04 14:59:44 +00:00
|
|
|
headers: admin.create_new_auth_token
|
|
|
|
|
2022-05-16 08:29:59 +00:00
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response['archived']).to eql(portal_params[:portal][:archived])
|
|
|
|
|
|
|
|
portal.reload
|
|
|
|
expect(portal.archived).to be_truthy
|
|
|
|
end
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-16 08:29:59 +00:00
|
|
|
describe 'DELETE /api/v1/accounts/{account.id}/portals/{portal.slug}' do
|
2020-09-25 21:02:34 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2022-05-16 08:29:59 +00:00
|
|
|
delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}", params: {}
|
2020-09-25 21:02:34 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'deletes portal' do
|
2022-05-16 08:29:59 +00:00
|
|
|
delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}",
|
2022-07-04 14:59:44 +00:00
|
|
|
headers: admin.create_new_auth_token
|
2020-09-25 21:02:34 +00:00
|
|
|
expect(response).to have_http_status(:success)
|
2022-05-16 08:29:59 +00:00
|
|
|
deleted_portal = Portal.find_by(id: portal.slug)
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(deleted_portal).to be_nil
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-07-04 14:59:44 +00:00
|
|
|
|
|
|
|
describe 'PUT /api/v1/accounts/{account.id}/portals/{portal.slug}/add_members' do
|
|
|
|
let(:new_account) { create(:account) }
|
|
|
|
let(:new_agent) { create(:user, account: new_account, role: :agent) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/add_members", params: {}
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'add members to the portal' do
|
|
|
|
portal_params = {
|
|
|
|
portal: {
|
|
|
|
member_ids: [agent_1.id, agent_2.id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(portal.members.count).to be(1)
|
|
|
|
|
|
|
|
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/add_members",
|
|
|
|
params: portal_params,
|
|
|
|
headers: admin.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(portal.reload.member_ids).to include(agent_1.id)
|
|
|
|
expect(json_response['portal_members'].length).to be(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|