Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬
Ruby actioncablechat-widgetconversationcustomer-supportdashboarddesigndockerdocker-imageherokuintercomjavascriptlivechatopensourcerailsrubysassvuejswhatsappzendesk
require 'rails_helper'
RSpec.describe 'Public Articles API', type: :request do
let!(:account) { create(:account) }
let(:agent) { create(:user, account: account, role: :agent) }
let!(:portal) { create(:portal, slug: 'test-portal', config: { allowed_locales: %w[en es] }, custom_domain: 'www.example.com') }
let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'en', slug: 'category_slug') }
let!(:category_2) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'es', slug: 'category_slug') }
let!(:article) do
create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id,
content: 'This is a *test* content with ^markdown^', views: 0)
end
let!(:translated_article) do
create(:article, category: category_2, portal: portal, author: agent, associated_article_id: article.id, views: 5)
end
before do
ENV['HELPCENTER_URL'] = ENV.fetch('FRONTEND_URL', nil)
create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, views: 15)
create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, views: 1)
create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id, views: 4)
create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id, status: :draft)
end
describe 'GET /public/api/v1/portals/:slug/articles' do
it 'Fetch all articles in the portal' do
get "/hc/#{portal.slug}/#{category.locale}/categories/#{category.slug}/articles"
expect(response).to have_http_status(:success)
end
it 'Fetches only the published articles in the portal' do
get "/hc/#{portal.slug}/#{category_2.locale}/categories/#{category.slug}/articles.json"
expect(response).to have_http_status(:success)
response_data = JSON.parse(response.body, symbolize_names: true)[:payload]
expect(response_data.length).to eq(2)
end
it 'get all articles with searched text query' do
long_content = ([('intro ' * 30).strip, 'funny', ('tail ' * 30).strip].join(' ')).strip
create(:article,
account_id: account.id,
portal: portal,
category: category,
author_id: agent.id,
content: long_content)
get "/hc/#{portal.slug}/#{category.locale}/categories/#{category.slug}/articles.json", params: { query: 'funny' }
expect(response).to have_http_status(:success)
response_data = JSON.parse(response.body, symbolize_names: true)[:payload]
expect(response_data.length).to eq(1)
expect(response_data[0].keys).to match_array(%i[id category_id title content link])
expect(response_data[0][:content]).to include('funny')
expect(response_data[0][:content].length).to be < long_content.length
end
it 'limits search results to the current locale' do
create(:article,
account_id: account.id,
portal: portal,
category: category,
author_id: agent.id,
title: 'English locale result',
content: 'shared-search-term in english')
create(:article,
account_id: account.id,
portal: portal,
category: category_2,
author_id: agent.id,
title: 'Spanish locale result',
content: 'shared-search-term in spanish')
get "/hc/#{portal.slug}/#{category.locale}/articles.json", params: { query: 'shared-search-term' }
expect(response).to have_http_status(:success)
response_data = JSON.parse(response.body, symbolize_names: true)[:payload]
expect(response_data.pluck(:title)).to eq(['English locale result'])
end
it 'treats whitespace-only queries as empty searches' do
get "/hc/#{portal.slug}/#{category.locale}/articles.json", params: { query: ' ' }
expect(response).to have_http_status(:success)
response_data = JSON.parse(response.body, symbolize_names: true)[:payload]
expect(response_data.length).to eq(3)
expect(response_data.first).to include(:description, :slug, :portal)
end
it 'get all popular articles if sort params is passed' do
get "/hc/#{portal.slug}/#{category.locale}/articles.json", params: { sort: 'views' }
expect(response).to have_http_status(:success)
response_data = JSON.parse(response.body, symbolize_names: true)[:payload]
expect(response_data.length).to eq(3)
expect(response_data[0][:views]).to eq(15)
expect(response_data[1][:views]).to eq(1)
expect(response_data.last[:id]).to eq(article.id)
end
it 'limits results based on per_page parameter' do
get "/hc/#{portal.slug}/#{category.locale}/articles.json", params: { per_page: 2 }
expect(response).to have_http_status(:success)
response_data = JSON.parse(response.body, symbolize_names: true)[:payload]
expect(response_data.length).to eq(2)
# Only count articles in the current locale (category.locale is 'en')
expect(JSON.parse(response.body, symbolize_names: true)[:meta][:articles_count]).to eq(3)
end
it 'returns articles count from all locales when locale parameter is not present' do
get "/hc/#{portal.slug}/articles.json"
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body, symbolize_names: true)[:meta][:articles_count]).to eq(5)
end
it 'uses default items per page if per_page is less than 1' do
get "/hc/#{portal.slug}/#{category.locale}/articles.json", params: { per_page: 0 }
expect(response).to have_http_status(:success)
response_data = JSON.parse(response.body, symbolize_names: true)[:payload]
expect(response_data.length).to eq(3)
end
end
describe 'GET /public/api/v1/portals/:slug/articles/:id' do
it 'Fetch article with the id' do
get "/hc/#{portal.slug}/articles/#{article.slug}"
expect(response).to have_http_status(:success)
expect(response.body).to include(ChatwootMarkdownRenderer.new(article.content).render_article)
expect(article.reload.views).to eq 0 # View count should not increment on show
end
it 'does not increment the view count if the article is not published' do
draft_article = create(:article, category: category, status: :draft, portal: portal, account_id: account.id, author_id: agent.id, views: 0)
get "/hc/#{portal.slug}/articles/#{draft_article.slug}"
expect(response).to have_http_status(:success)
expect(draft_article.reload.views).to eq 0
end
it 'returns the article with the id with a different locale' do
article_in_locale = create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id)
get "/hc/#{portal.slug}/articles/#{article_in_locale.slug}"
expect(response).to have_http_status(:success)
end
it 'resolves the locale from the article itself for an uncategorized article' do
uncategorized_article = create(:article, category: nil, locale: 'es', portal: portal,
account_id: account.id, author_id: agent.id)
get "/hc/#{portal.slug}/articles/#{uncategorized_article.slug}"
expect(response).to have_http_status(:success)
expect(response.body).to include('lang="es"')
end
it 'links to the matching published translation from the locale switcher' do
get "/hc/#{portal.slug}/articles/#{article.slug}"
expect(response).to have_http_status(:success)
expect(response.body).to include(%(value="/hc/#{portal.slug}/articles/#{translated_article.slug}"))
end
%i[draft archived].each do |status|
it "falls back to the locale home for a #{status} translation" do
translated_article.update!(status: status)
get "/hc/#{portal.slug}/articles/#{article.slug}"
expect(response).to have_http_status(:success)
expect(response.body).to include(%(value="/hc/#{portal.slug}/es"))
expect(response.body).not_to include("/hc/#{portal.slug}/articles/#{translated_article.slug}")
end
end
it 'links to the original article and sibling translations from a translated article' do
portal.update!(config: portal.config.merge('allowed_locales' => %w[en es fr]))
french_article = create(:article, locale: 'fr', portal: portal, author: agent, associated_article_id: article.id)
get "/hc/#{portal.slug}/articles/#{french_article.slug}"
expect(response).to have_http_status(:success)
expect(response.body).to include(
%(value="/hc/#{portal.slug}/articles/#{article.slug}"),
%(value="/hc/#{portal.slug}/articles/#{translated_article.slug}"),
%(value="/hc/#{portal.slug}/articles/#{french_article.slug}")
)
end
it 'returns not found for an article in another portal' do
foreign_article = create(:article)
get "/hc/#{portal.slug}/articles/#{foreign_article.slug}"
expect(response).to have_http_status(:not_found)
end
it 'excludes translations from other portals' do
portal.update!(config: portal.config.merge('allowed_locales' => %w[en es fr]))
foreign_portal = create(:portal, config: { allowed_locales: ['fr'] })
foreign_article = create(:article, portal: foreign_portal, locale: 'fr', associated_article_id: article.id)
get "/hc/#{portal.slug}/articles/#{article.slug}"
expect(response).to have_http_status(:success)
expect(response.body).to include(%(value="/hc/#{portal.slug}/fr"))
expect(response.body).not_to include(foreign_article.slug)
end
it 'links to the matching article in the documentation layout' do
portal.update!(config: portal.config.merge('layout' => 'documentation'))
get "/hc/#{portal.slug}/articles/#{article.slug}"
links = Nokogiri::HTML(response.body).css('header details a').map { |link| link['href'] }
expect(links).to include("/hc/#{portal.slug}/articles/#{translated_article.slug}")
end
it 'uses the category locale after a translated category changes language' do
portal.update!(config: portal.config.merge('allowed_locales' => %w[en es fr]))
admin = create(:user, account: portal.account, role: :administrator)
put "/api/v1/accounts/#{portal.account_id}/portals/#{portal.slug}/categories/#{category_2.id}",
params: { category: { locale: 'fr' } }, headers: admin.create_new_auth_token
expect(response).to have_http_status(:success)
expect([category_2.reload.locale, translated_article.reload.locale]).to eq(%w[fr es])
get "/hc/#{portal.slug}/articles/#{article.slug}"
switchers = Nokogiri::HTML(response.body).css('select.locale-switcher')
expect(switchers.size).to eq(2)
switchers.each do |switcher|
options = switcher.css('option').to_h { |option| [option.text, option['value']] }
expect(options).to include('Spanish (es)' => "/hc/#{portal.slug}/es",
'French (fr)' => "/hc/#{portal.slug}/articles/#{translated_article.slug}")
end
end
end
describe 'GET /public/api/v1/portals/:slug/articles/:slug.md (markdown)' do
it 'serves the raw article markdown for a published article' do
get "/hc/#{portal.slug}/articles/#{article.slug}.md"
expect(response).to have_http_status(:success)
expect(response.headers['Content-Type']).to include('text/markdown')
expect(response.body).to eq(article.content)
end
it 'returns 404 for a draft article' do
draft_article = create(:article, category: category, status: :draft, portal: portal, account_id: account.id, author_id: agent.id)
get "/hc/#{portal.slug}/articles/#{draft_article.slug}.md"
expect(response).to have_http_status(:not_found)
end
it 'returns 404 if the article does not exist' do
get "/hc/#{portal.slug}/articles/non-existent-article.md"
expect(response).to have_http_status(:not_found)
end
end
describe 'GET /public/api/v1/portals/:slug/articles/:slug.png (tracking pixel)' do
it 'serves a PNG image and increments view count for published article' do
get "/hc/#{portal.slug}/articles/#{article.slug}.png"
expect(response).to have_http_status(:success)
expect(response.headers['Content-Type']).to eq('image/png')
expect(response.headers['Cache-Control']).to include('max-age=86400')
expect(response.headers['Cache-Control']).to include('private')
expect(article.reload.views).to eq 1
end
it 'serves a PNG image but does not increment view count for draft article' do
draft_article = create(:article, category: category, status: :draft, portal: portal, account_id: account.id, author_id: agent.id, views: 0)
get "/hc/#{portal.slug}/articles/#{draft_article.slug}.png"
expect(response).to have_http_status(:success)
expect(response.headers['Content-Type']).to eq('image/png')
expect(response.headers['Cache-Control']).to include('max-age=86400')
expect(response.headers['Cache-Control']).to include('private')
expect(draft_article.reload.views).to eq 0
end
it 'returns 404 if article does not exist' do
get "/hc/#{portal.slug}/articles/non-existent-article.png"
expect(response).to have_http_status(:not_found)
end
it 'sets proper cache headers for performance' do
get "/hc/#{portal.slug}/articles/#{article.slug}.png"
expect(response.headers['Cache-Control']).to include('max-age=86400')
expect(response.headers['Cache-Control']).to include('private')
expect(response.headers['Content-Type']).to eq('image/png')
end
end
describe 'documentation layout sidebar for a region-variant locale' do
let!(:th_portal) do
create(:portal, slug: 'th-portal', custom_domain: 'th.example.com',
config: { allowed_locales: ['th_TH'], default_locale: 'th_TH', layout: 'documentation' })
end
let!(:th_category) do
create(:category, name: 'TH Category', portal: th_portal, account_id: account.id, locale: 'th_TH', slug: 'th-cat')
end
let!(:th_article) do
create(:article, category: th_category, portal: th_portal, account_id: account.id, author_id: agent.id, locale: 'th_TH')
end
before do
create(:article, category: th_category, portal: th_portal, account_id: account.id, author_id: agent.id,
locale: 'th_TH', title: 'Sibling In Sidebar', status: :published)
end
it 'lists the category and sibling articles using the full portal locale' do
host! 'th.example.com'
get "/hc/#{th_portal.slug}/articles/#{th_article.slug}"
expect(response).to have_http_status(:success)
expect(response.body).to include('Sibling In Sidebar')
end
end
end