Feat: Article public apis (#4955)
This commit is contained in:
@@ -174,6 +174,23 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['payload'].count).to be 2
|
||||
end
|
||||
|
||||
it 'get all articles with searched text query' do
|
||||
article2 = create(:article,
|
||||
account_id: account.id,
|
||||
portal: portal,
|
||||
category: category,
|
||||
author_id: agent.id,
|
||||
content: 'this is some test and funny content')
|
||||
expect(article2.id).not_to be nil
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles",
|
||||
headers: agent.create_new_auth_token,
|
||||
params: { payload: { query: 'funny' } }
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['payload'].count).to be 1
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/portals/{portal.slug}/articles/{article.id}' do
|
||||
|
||||
@@ -5,7 +5,9 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
let!(:portal) { create(:portal, name: 'test_portal', account_id: account.id) }
|
||||
let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, slug: 'category_slug') }
|
||||
let!(:category_to_link) { create(:category, name: 'linked category', portal: portal, account_id: account.id, slug: 'linked_category_slug') }
|
||||
let!(:category_to_associate) do
|
||||
create(:category, name: 'associated category', portal: portal, account_id: account.id, slug: 'associated_category_slug')
|
||||
end
|
||||
let!(:related_category_1) { create(:category, name: 'related category 1', portal: portal, account_id: account.id, slug: 'category_slug_1') }
|
||||
let!(:related_category_2) { create(:category, name: 'related category 2', portal: portal, account_id: account.id, slug: 'category_slug_2') }
|
||||
|
||||
@@ -29,7 +31,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
locale: 'es',
|
||||
slug: 'test_category_1',
|
||||
parent_category_id: category.id,
|
||||
linked_category_id: category_to_link.id,
|
||||
associated_category_id: category_to_associate.id,
|
||||
related_category_ids: [related_category_1.id, related_category_2.id]
|
||||
}
|
||||
}
|
||||
@@ -44,7 +46,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
locale: 'es',
|
||||
slug: 'test_category_2',
|
||||
parent_category_id: category.id,
|
||||
linked_category_id: category_to_link.id,
|
||||
associated_category_id: category_to_associate.id,
|
||||
related_category_ids: [related_category_1.id, related_category_2.id]
|
||||
}
|
||||
}
|
||||
@@ -61,9 +63,9 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
expect(json_response['payload']['related_categories'][0]['id']).to eql(related_category_1.id)
|
||||
expect(json_response['payload']['related_categories'][1]['id']).to eql(related_category_2.id)
|
||||
expect(json_response['payload']['parent_category']['id']).to eql(category.id)
|
||||
expect(json_response['payload']['linked_category']['id']).to eql(category_to_link.id)
|
||||
expect(json_response['payload']['root_category']['id']).to eql(category_to_associate.id)
|
||||
expect(category.reload.sub_category_ids).to eql([Category.last.id])
|
||||
expect(category_to_link.reload.linked_category_ids).to eql([Category.last.id])
|
||||
expect(category_to_associate.reload.associated_category_ids).to eql([Category.last.id])
|
||||
end
|
||||
|
||||
it 'creates multiple sub_categories under one parent_category' do
|
||||
@@ -79,7 +81,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
expect(category.reload.sub_category_ids).to eql(Category.last(2).pluck(:id))
|
||||
end
|
||||
|
||||
it 'creates multiple linked_categories with one category' do
|
||||
it 'creates multiple associated_categories with one category' do
|
||||
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||
params: category_params,
|
||||
headers: agent.create_new_auth_token
|
||||
@@ -89,7 +91,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
headers: agent.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(category_to_link.reload.linked_category_ids).to eql(Category.last(2).pluck(:id))
|
||||
expect(category_to_associate.reload.associated_category_ids).to eql(Category.last(2).pluck(:id))
|
||||
end
|
||||
|
||||
it 'will throw an error on locale, category_id uniqueness' do
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
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') }
|
||||
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) }
|
||||
|
||||
before do
|
||||
create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id)
|
||||
create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: article.id)
|
||||
create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: article.id)
|
||||
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
|
||||
it 'Fetch all articles in the portal' do
|
||||
get "/public/api/v1/portals/#{portal.slug}/articles"
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
|
||||
expect(json_response['payload'].length).to eql portal.articles.count
|
||||
end
|
||||
|
||||
it 'get all articles with searched text query' do
|
||||
article2 = create(:article,
|
||||
account_id: account.id,
|
||||
portal: portal,
|
||||
category: category,
|
||||
author_id: agent.id,
|
||||
content: 'this is some test and funny content')
|
||||
expect(article2.id).not_to be nil
|
||||
|
||||
get "/public/api/v1/portals/#{portal.slug}/articles",
|
||||
headers: agent.create_new_auth_token,
|
||||
params: { payload: { query: 'funny' } }
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['payload'].count).to be 1
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /public/api/v1/portals/:portal_slug/articles/:id' do
|
||||
it 'Fetch article with the id' do
|
||||
get "/public/api/v1/portals/#{portal.slug}/articles/#{article.id}"
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
|
||||
expect(json_response['title']).to eql article.title
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -27,7 +27,7 @@ RSpec.describe Article, type: :model do
|
||||
before do
|
||||
create(:article, category_id: category_1.id, content: 'This is the content', description: 'this is the description', title: 'this is title',
|
||||
portal_id: portal_1.id, author_id: user.id)
|
||||
create(:article, category_id: category_1.id, title: 'title 1', portal_id: portal_1.id, author_id: user.id)
|
||||
create(:article, category_id: category_1.id, title: 'title 1', content: 'This is the content', portal_id: portal_1.id, author_id: user.id)
|
||||
create(:article, category_id: category_2.id, title: 'title 2', portal_id: portal_2.id, author_id: user.id)
|
||||
create(:article, category_id: category_2.id, title: 'title 3', portal_id: portal_1.id, author_id: user.id)
|
||||
create(:article, category_id: category_3.id, title: 'title 6', portal_id: portal_2.id, author_id: user.id)
|
||||
@@ -76,6 +76,7 @@ RSpec.describe Article, type: :model do
|
||||
it 'returns data with text_search query' do
|
||||
params = { query: 'title' }
|
||||
records = portal_2.articles.search(params)
|
||||
|
||||
expect(records.count).to eq(2)
|
||||
|
||||
params = { query: 'title' }
|
||||
@@ -84,12 +85,13 @@ RSpec.describe Article, type: :model do
|
||||
expect(records.count).to eq(4)
|
||||
|
||||
params = { query: 'the content' }
|
||||
records = portal_2.articles.search(params)
|
||||
records = portal_1.articles.search(params)
|
||||
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
|
||||
it 'returns data with text_search query and locale' do
|
||||
params = { query: 'the title', locale: 'es' }
|
||||
params = { query: 'title', locale: 'es' }
|
||||
records = portal_2.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
@@ -101,7 +103,7 @@ RSpec.describe Article, type: :model do
|
||||
end
|
||||
|
||||
it 'return records with category_slug and text_search query' do
|
||||
params = { category_slug: 'category_2', query: 'the title' }
|
||||
params = { category_slug: 'category_2', query: 'title' }
|
||||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
|
||||
@@ -11,7 +11,7 @@ RSpec.describe Category, type: :model do
|
||||
it { is_expected.to belong_to(:portal) }
|
||||
it { is_expected.to have_many(:articles) }
|
||||
it { is_expected.to have_many(:sub_categories) }
|
||||
it { is_expected.to have_many(:linked_categories) }
|
||||
it { is_expected.to have_many(:associated_categories) }
|
||||
it { is_expected.to have_many(:related_categories) }
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user