feat: Category API to handle linked categories and parent-sub categories (#4879)

This commit is contained in:
Tejaswini Chile
2022-06-28 11:23:20 +05:30
committed by GitHub
parent c0249a1b5b
commit df1bf112ea
15 changed files with 286 additions and 29 deletions

View File

@@ -5,6 +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!(: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') }
describe 'POST /api/v1/accounts/{account.id}/portals/{portal.slug}/categories' do
context 'when it is an unauthenticated user' do
@@ -15,23 +18,76 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
end
context 'when it is an authenticated user' do
category_params = {
category: {
name: 'test_category',
description: 'test_description',
position: 1,
locale: 'es',
slug: 'test_category_1'
let!(:category_params) do
{
category: {
name: 'test_category',
description: 'test_description',
position: 1,
locale: 'es',
slug: 'test_category_1',
parent_category_id: category.id,
linked_category_id: category_to_link.id,
related_category_ids: [related_category_1.id, related_category_2.id]
}
}
}
end
let!(:category_params_2) do
{
category: {
name: 'test_category_2',
description: 'test_description_2',
position: 1,
locale: 'es',
slug: 'test_category_2',
parent_category_id: category.id,
linked_category_id: category_to_link.id,
related_category_ids: [related_category_1.id, related_category_2.id]
}
}
end
it 'creates category' do
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(:success)
json_response = JSON.parse(response.body)
expect(json_response['payload']['name']).to eql('test_category')
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(category.reload.sub_category_ids).to eql([Category.last.id])
expect(category_to_link.reload.linked_category_ids).to eql([Category.last.id])
end
it 'creates multiple sub_categories under one parent_category' 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_2,
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
expect(category.reload.sub_category_ids).to eql(Category.last(2).pluck(:id))
end
it 'creates multiple linked_categories with one category' 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_2,
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))
end
it 'will throw an error on locale, category_id uniqueness' do
@@ -81,18 +137,74 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
category: {
name: 'test_category_2',
description: 'test_description',
position: 1
position: 1,
related_category_ids: [related_category_1.id],
parent_category_id: related_category_2.id
}
}
expect(category.name).not_to eql(category_params[:category][:name])
expect(category.related_categories).to be_empty
expect(category.parent_category).to be_nil
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories/#{category.id}",
params: category_params,
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['payload']['name']).to eql(category_params[:category][:name])
expect(json_response['payload']['related_categories'][0]['id']).to eql(related_category_1.id)
expect(json_response['payload']['parent_category']['id']).to eql(related_category_2.id)
expect(related_category_2.reload.sub_category_ids).to eql([category.id])
end
it 'updates related categories' do
category_params = {
category: {
related_category_ids: [related_category_1.id]
}
}
category.related_categories << related_category_2
category.save!
expect(category.related_category_ids).to eq([related_category_2.id])
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories/#{category.id}",
params: category_params,
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['payload']['name']).to eql(category.name)
expect(json_response['payload']['related_categories'][0]['id']).to eql(related_category_1.id)
expect(category.reload.related_category_ids).to eq([related_category_1.id])
expect(related_category_1.reload.related_category_ids).to be_empty
end
# [category_1, category_2] !== [category_2, category_1]
it 'update reverse associations for related categories' do
category.related_categories << related_category_2
category.save!
expect(category.related_category_ids).to eq([related_category_2.id])
category_params = {
category: {
related_category_ids: [category.id]
}
}
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories/#{related_category_2.id}",
params: category_params,
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
expect(category.reload.related_category_ids).to eq([related_category_2.id])
expect(related_category_2.reload.related_category_ids).to eq([category.id])
end
end
end
@@ -125,7 +237,9 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
end
context 'when it is an authenticated user' do
it 'get all portals' do
it 'get all categories in portal' do
category_count = Category.all.count
category2 = create(:category, name: 'test_category_2', portal: portal, locale: 'es', slug: 'category_slug_2')
expect(category2.id).not_to be nil
@@ -133,7 +247,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['payload'].count).to be 2
expect(json_response['payload'].count).to be(category_count + 1)
end
end
end