137
spec/controllers/api/v1/accounts/articles_controller_spec.rb
Normal file
137
spec/controllers/api/v1/accounts/articles_controller_spec.rb
Normal file
@@ -0,0 +1,137 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
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, locale: 'en', slug: 'category_slug') }
|
||||
let!(:article) { create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id) }
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/portals/{portal.slug}/articles' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles", params: {}
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'creates article' do
|
||||
article_params = {
|
||||
article: {
|
||||
category_id: category.id,
|
||||
description: 'test description',
|
||||
title: 'MyTitle',
|
||||
content: 'This is my content.',
|
||||
status: :published,
|
||||
author_id: agent.id
|
||||
}
|
||||
}
|
||||
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles",
|
||||
params: article_params,
|
||||
headers: agent.create_new_auth_token
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['payload']['title']).to eql('MyTitle')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /api/v1/accounts/{account.id}/portals/{portal.slug}/articles/{article.id}' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.id}", params: {}
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'updates category' do
|
||||
article_params = {
|
||||
article: {
|
||||
title: 'MyTitle2',
|
||||
description: 'test_description'
|
||||
}
|
||||
}
|
||||
|
||||
expect(article.title).not_to eql(article_params[:article][:title])
|
||||
|
||||
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.id}",
|
||||
params: article_params,
|
||||
headers: agent.create_new_auth_token
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['payload']['title']).to eql(article_params[:article][:title])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /api/v1/accounts/{account.id}/portals/{portal.slug}/articles/{article.id}' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.id}", params: {}
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'deletes category' do
|
||||
delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.id}",
|
||||
headers: agent.create_new_auth_token
|
||||
expect(response).to have_http_status(:success)
|
||||
deleted_article = Article.find_by(id: article.id)
|
||||
expect(deleted_article).to be nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/portals/{portal.slug}/articles' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'get all articles' do
|
||||
article2 = create(:article, account_id: account.id, portal: portal, category: category, author_id: agent.id)
|
||||
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: {} }
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['payload'].count).to be 2
|
||||
end
|
||||
|
||||
it 'get all articles with searched params' do
|
||||
article2 = create(:article, account_id: account.id, portal: portal, category: category, author_id: agent.id)
|
||||
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: { category_slug: category.slug } }
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['payload'].count).to be 2
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/portals/{portal.slug}/articles/{article.id}' do
|
||||
it 'get article' do
|
||||
article2 = create(:article, account_id: account.id, portal: portal, category: category, author_id: agent.id)
|
||||
expect(article2.id).not_to be nil
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article2.id}",
|
||||
headers: agent.create_new_auth_token
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
|
||||
expect(json_response['payload']['title']).to eq(article2.title)
|
||||
expect(json_response['payload']['id']).to eq(article2.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -4,7 +4,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
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) }
|
||||
let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, slug: 'category_slug') }
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/portals/{portal.slug}/categories' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
@@ -15,14 +15,17 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'creates category' do
|
||||
category_params = {
|
||||
category: {
|
||||
name: 'test_category',
|
||||
description: 'test_description',
|
||||
position: 1
|
||||
}
|
||||
category_params = {
|
||||
category: {
|
||||
name: 'test_category',
|
||||
description: 'test_description',
|
||||
position: 1,
|
||||
locale: 'es',
|
||||
slug: 'test_category_1'
|
||||
}
|
||||
}
|
||||
|
||||
it 'creates category' do
|
||||
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||
params: category_params,
|
||||
headers: agent.create_new_auth_token
|
||||
@@ -30,6 +33,37 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['payload']['name']).to eql('test_category')
|
||||
end
|
||||
|
||||
it 'will throw an error on locale, category_id uniqueness' do
|
||||
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||
params: category_params,
|
||||
headers: agent.create_new_auth_token
|
||||
|
||||
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||
params: category_params,
|
||||
headers: agent.create_new_auth_token
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['message']).to eql('Locale should be unique in the category and portal')
|
||||
end
|
||||
|
||||
it 'will throw an error slug presence' do
|
||||
category_params = {
|
||||
category: {
|
||||
name: 'test_category',
|
||||
description: 'test_description',
|
||||
position: 1,
|
||||
locale: 'es'
|
||||
}
|
||||
}
|
||||
|
||||
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||
params: category_params,
|
||||
headers: agent.create_new_auth_token
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['message']).to eql("Slug can't be blank")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -92,7 +126,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'get all portals' do
|
||||
category2 = create(:category, name: 'test_category_2', portal: portal)
|
||||
category2 = create(:category, name: 'test_category_2', portal: portal, locale: 'es', slug: 'category_slug_2')
|
||||
expect(category2.id).not_to be nil
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||
|
||||
@@ -2,13 +2,11 @@ FactoryBot.define do
|
||||
factory :article, class: 'Article' do
|
||||
account_id { 1 }
|
||||
category_id { 1 }
|
||||
folder_id { 1 }
|
||||
author_id { 1 }
|
||||
title { 'MyString' }
|
||||
content { 'MyText' }
|
||||
description { 'MyDescrption' }
|
||||
status { 1 }
|
||||
views { 1 }
|
||||
seo_title { 'MyString' }
|
||||
seo { '' }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,4 +14,105 @@ RSpec.describe Article, type: :model do
|
||||
it { is_expected.to belong_to(:category) }
|
||||
it { is_expected.to belong_to(:author) }
|
||||
end
|
||||
|
||||
describe 'search' do
|
||||
let!(:account) { create(:account) }
|
||||
let(:user) { create(:user, account_ids: [account.id], role: :agent) }
|
||||
let!(:portal_1) { create(:portal, account_id: account.id) }
|
||||
let!(:portal_2) { create(:portal, account_id: account.id) }
|
||||
let!(:category_1) { create(:category, slug: 'category_1', locale: 'en', portal_id: portal_1.id) }
|
||||
let!(:category_2) { create(:category, slug: 'category_2', locale: 'es', portal_id: portal_1.id) }
|
||||
let!(:category_3) { create(:category, slug: 'category_3', locale: 'es', portal_id: portal_2.id) }
|
||||
|
||||
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_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)
|
||||
create(:article, category_id: category_2.id, title: 'title 7', portal_id: portal_1.id, author_id: user.id)
|
||||
end
|
||||
|
||||
context 'when no parameters passed' do
|
||||
it 'returns all the articles in portal' do
|
||||
records = portal_1.articles.search({})
|
||||
expect(records.count).to eq(portal_1.articles.count)
|
||||
|
||||
records = portal_2.articles.search({})
|
||||
expect(records.count).to eq(portal_2.articles.count)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when params passed' do
|
||||
it 'returns all the articles with all the params filters' do
|
||||
params = { query: 'title', locale: 'es', category_slug: 'category_3' }
|
||||
records = portal_2.articles.search(params)
|
||||
expect(records.count).to eq(1)
|
||||
|
||||
params = { query: 'this', locale: 'en', category_slug: 'category_1' }
|
||||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when some params missing' do
|
||||
it 'returns data with category slug' do
|
||||
params = { category_slug: 'category_2' }
|
||||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
|
||||
it 'returns data with locale' do
|
||||
params = { locale: 'es' }
|
||||
records = portal_2.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
|
||||
params = { locale: 'en' }
|
||||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
|
||||
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' }
|
||||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(4)
|
||||
|
||||
params = { query: 'the content' }
|
||||
records = portal_2.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' }
|
||||
records = portal_2.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
|
||||
it 'returns records with locale and category_slug' do
|
||||
params = { category_slug: 'category_2', locale: 'es' }
|
||||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
|
||||
it 'return records with category_slug and text_search query' do
|
||||
params = { category_slug: 'category_2', query: 'the title' }
|
||||
records = portal_1.articles.search(params)
|
||||
expect(records.count).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with pagination' do
|
||||
it 'returns paginated articles' do
|
||||
create_list(:article, 30, category_id: category_2.id, title: 'title 1', portal_id: portal_2.id, author_id: user.id)
|
||||
params = { category_slug: 'category_2' }
|
||||
records = portal_2.articles.search(params)
|
||||
expect(records.count).to eq(25)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,4 +12,39 @@ RSpec.describe Category, type: :model do
|
||||
it { is_expected.to have_many(:folders) }
|
||||
it { is_expected.to have_many(:articles) }
|
||||
end
|
||||
|
||||
describe 'search' do
|
||||
let!(:account) { create(:account) }
|
||||
let(:user) { create(:user, account_ids: [account.id], role: :agent) }
|
||||
let!(:portal_1) { create(:portal, account_id: account.id) }
|
||||
let!(:portal_2) { create(:portal, account_id: account.id) }
|
||||
|
||||
before do
|
||||
create(:category, slug: 'category_1', locale: 'en', portal_id: portal_1.id)
|
||||
create(:category, slug: 'category_2', locale: 'es', portal_id: portal_1.id)
|
||||
create(:category, slug: 'category_3', locale: 'es', portal_id: portal_2.id)
|
||||
end
|
||||
|
||||
context 'when no parameters passed' do
|
||||
it 'returns all the articles in portal' do
|
||||
records = portal_1.categories.search({})
|
||||
expect(records.count).to eq(portal_1.categories.count)
|
||||
|
||||
records = portal_2.categories.search({})
|
||||
expect(records.count).to eq(portal_2.categories.count)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when params passed' do
|
||||
it 'returns all the categories with all the params filters' do
|
||||
params = { locale: 'es' }
|
||||
records = portal_2.categories.search(params)
|
||||
expect(records.count).to eq(1)
|
||||
|
||||
params = { locale: 'en' }
|
||||
records = portal_1.categories.search(params)
|
||||
expect(records.count).to eq(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user