feat: Fetching the portal data related to a specific custom domain (#5249)

This commit is contained in:
Tejaswini Chile 2022-09-07 12:22:24 +05:30 committed by GitHub
parent 6574407636
commit db73d033b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 74 additions and 35 deletions

View file

@ -3,7 +3,7 @@ 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] }) }
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_2_slug') }
let!(:article) { create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id) }
@ -15,9 +15,9 @@ RSpec.describe 'Public Articles API', type: :request do
create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id)
end
describe 'GET /public/api/v1/portals/:portal_slug/articles' do
describe 'GET /public/api/v1/portals/:slug/articles' do
it 'Fetch all articles in the portal' do
get "/public/api/v1/portals/#{portal.slug}/articles"
get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/articles"
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
@ -35,7 +35,7 @@ RSpec.describe 'Public Articles API', type: :request do
content: 'this is some test and funny content')
expect(article2.id).not_to be_nil
get "/public/api/v1/portals/#{portal.slug}/articles",
get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/articles",
headers: agent.create_new_auth_token,
params: { query: 'funny' }
expect(response).to have_http_status(:success)
@ -45,9 +45,9 @@ RSpec.describe 'Public Articles API', type: :request do
end
end
describe 'GET /public/api/v1/portals/:portal_slug/articles/:id' do
describe 'GET /public/api/v1/portals/:slug/articles/:id' do
it 'Fetch article with the id' do
get "/public/api/v1/portals/#{portal.slug}/articles/#{article.id}"
get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/#{article.id}"
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)

View file

@ -2,7 +2,7 @@ require 'rails_helper'
RSpec.describe 'Public Categories API', type: :request do
let!(:account) { create(:account) }
let!(:portal) { create(:portal, slug: 'test-portal') }
let!(:portal) { create(:portal, slug: 'test-portal', custom_domain: 'www.example.com') }
before do
create(:category, slug: 'test-category-1', portal_id: portal.id, account_id: account.id)
@ -12,26 +12,19 @@ RSpec.describe 'Public Categories API', type: :request do
describe 'GET /public/api/v1/portals/:portal_slug/categories' do
it 'Fetch all categories in the portal' do
get "/public/api/v1/portals/#{portal.slug}/categories"
get "/hc/#{portal.slug}/categories"
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['payload'].length).to eql portal.categories.count
end
end
describe 'GET /public/api/v1/portals/:portal_slug/categories/:slug' do
it 'Fetch category with the slug' do
category_slug = 'test-category-3'
category_locale = 'en'
get "/public/api/v1/portals/#{portal.slug}/categories/#{category_slug}"
get "/hc/#{portal.slug}/#{category_locale}/categories"
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['slug']).to eql category_slug
expect(json_response['meta']['articles_count']).to be 0
end
end
end

View file

@ -2,7 +2,7 @@ require 'rails_helper'
RSpec.describe 'Public Portals API', type: :request do
let!(:account) { create(:account) }
let!(:portal) { create(:portal, slug: 'test-portal', account_id: account.id) }
let!(:portal) { create(:portal, slug: 'test-portal', account_id: account.id, custom_domain: 'www.example.com') }
before do
create(:portal, slug: 'test-portal-1', account_id: account.id)
@ -11,12 +11,24 @@ RSpec.describe 'Public Portals API', type: :request do
describe 'GET /public/api/v1/portals/{portal_slug}' do
it 'Show portal and categories belonging to the portal' do
get "/public/api/v1/portals/#{portal.slug}"
get "/hc/#{portal.slug}/en"
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['slug']).to eql 'test-portal'
expect(json_response['meta']['articles_count']).to be 0
end
it 'Throws unauthorised error for unknown domain' do
portal.update(custom_domain: 'www.something.com')
get "/hc/#{portal.slug}/en"
expect(response).to have_http_status(:unauthorized)
json_response = JSON.parse(response.body)
expect(json_response['error']).to eql "Domain: www.example.com is not registered with us. \
Please send us an email at support@chatwoot.com with the custom domain name and account API key"
end
end
end