Feature: Knowledge Base APIs (#1002)
- Introduce models & migrations for portals, categories, folders and articles - CRUD API for portals - CRUD API for categories Addresses: #714 Co-authored-by: Sojan <sojan@pepalo.com>
This commit is contained in:
14
spec/factories/kbase/articles.rb
Normal file
14
spec/factories/kbase/articles.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
FactoryBot.define do
|
||||
factory :kbase_article, class: 'Kbase::Article' do
|
||||
account_id { 1 }
|
||||
category_id { 1 }
|
||||
folder_id { 1 }
|
||||
author_id { 1 }
|
||||
title { 'MyString' }
|
||||
content { 'MyText' }
|
||||
status { 1 }
|
||||
views { 1 }
|
||||
seo_title { 'MyString' }
|
||||
seo { '' }
|
||||
end
|
||||
end
|
||||
12
spec/factories/kbase/categories.rb
Normal file
12
spec/factories/kbase/categories.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
FactoryBot.define do
|
||||
factory :kbase_category, class: 'Kbase::Category' do
|
||||
portal { kbase_portal }
|
||||
name { 'MyString' }
|
||||
description { 'MyText' }
|
||||
position { 1 }
|
||||
|
||||
after(:build) do |category|
|
||||
category.account ||= category.portal.account
|
||||
end
|
||||
end
|
||||
end
|
||||
8
spec/factories/kbase/folders.rb
Normal file
8
spec/factories/kbase/folders.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
FactoryBot.define do
|
||||
factory :kbase_folder, class: 'Kbase::Folder' do
|
||||
account_id { 1 }
|
||||
name { 'MyString' }
|
||||
description { 'MyText' }
|
||||
category_id { 1 }
|
||||
end
|
||||
end
|
||||
7
spec/factories/kbase/portals.rb
Normal file
7
spec/factories/kbase/portals.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
FactoryBot.define do
|
||||
factory :kbase_portal, class: 'Kbase::Portal' do
|
||||
account
|
||||
name { Faker::Book.name }
|
||||
slug { SecureRandom.hex }
|
||||
end
|
||||
end
|
||||
@@ -17,4 +17,6 @@ RSpec.describe Account do
|
||||
it { is_expected.to have_many(:webhooks).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:notification_settings).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:events) }
|
||||
it { is_expected.to have_many(:kbase_portals).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:kbase_categories).dependent(:destroy) }
|
||||
end
|
||||
|
||||
17
spec/models/kbase/article_spec.rb
Normal file
17
spec/models/kbase/article_spec.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Kbase::Article, type: :model do
|
||||
context 'with validations' do
|
||||
it { is_expected.to validate_presence_of(:account_id) }
|
||||
it { is_expected.to validate_presence_of(:category_id) }
|
||||
it { is_expected.to validate_presence_of(:author_id) }
|
||||
it { is_expected.to validate_presence_of(:title) }
|
||||
it { is_expected.to validate_presence_of(:content) }
|
||||
end
|
||||
|
||||
describe 'associations' do
|
||||
it { is_expected.to belong_to(:account) }
|
||||
it { is_expected.to belong_to(:category) }
|
||||
it { is_expected.to belong_to(:author) }
|
||||
end
|
||||
end
|
||||
15
spec/models/kbase/category_spec.rb
Normal file
15
spec/models/kbase/category_spec.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Kbase::Category, type: :model do
|
||||
context 'with validations' do
|
||||
it { is_expected.to validate_presence_of(:account_id) }
|
||||
it { is_expected.to validate_presence_of(:name) }
|
||||
end
|
||||
|
||||
describe 'associations' do
|
||||
it { is_expected.to belong_to(:account) }
|
||||
it { is_expected.to belong_to(:portal) }
|
||||
it { is_expected.to have_many(:folders) }
|
||||
it { is_expected.to have_many(:articles) }
|
||||
end
|
||||
end
|
||||
15
spec/models/kbase/folder_spec.rb
Normal file
15
spec/models/kbase/folder_spec.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Kbase::Folder, type: :model do
|
||||
context 'with validations' do
|
||||
it { is_expected.to validate_presence_of(:account_id) }
|
||||
it { is_expected.to validate_presence_of(:category_id) }
|
||||
it { is_expected.to validate_presence_of(:name) }
|
||||
end
|
||||
|
||||
describe 'associations' do
|
||||
it { is_expected.to belong_to(:account) }
|
||||
it { is_expected.to belong_to(:category) }
|
||||
it { is_expected.to have_many(:articles) }
|
||||
end
|
||||
end
|
||||
16
spec/models/kbase/portal_spec.rb
Normal file
16
spec/models/kbase/portal_spec.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Kbase::Portal, type: :model do
|
||||
context 'with validations' do
|
||||
it { is_expected.to validate_presence_of(:account_id) }
|
||||
it { is_expected.to validate_presence_of(:slug) }
|
||||
it { is_expected.to validate_presence_of(:name) }
|
||||
end
|
||||
|
||||
describe 'associations' do
|
||||
it { is_expected.to belong_to(:account) }
|
||||
it { is_expected.to have_many(:categories) }
|
||||
it { is_expected.to have_many(:folders) }
|
||||
it { is_expected.to have_many(:articles) }
|
||||
end
|
||||
end
|
||||
106
spec/requests/api/v1/accounts/kbase/categories_request_spec.rb
Normal file
106
spec/requests/api/v1/accounts/kbase/categories_request_spec.rb
Normal file
@@ -0,0 +1,106 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
let!(:portal) { create(:kbase_portal, name: 'test_portal', account_id: account.id) }
|
||||
let!(:category) { create(:kbase_category, name: 'category', portal: portal, account_id: account.id) }
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/kbase/portals/{portal.id}/categories' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
post "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories", params: {}
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'creates category' do
|
||||
category_params = {
|
||||
category: {
|
||||
name: 'test_category',
|
||||
description: 'test_description',
|
||||
position: 1
|
||||
}
|
||||
}
|
||||
post "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/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')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /api/v1/accounts/{account.id}/kbase/portals/{portal.id}/categories/{category.id}' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories/#{category.id}", params: {}
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'updates category' do
|
||||
category_params = {
|
||||
category: {
|
||||
name: 'test_category_2',
|
||||
description: 'test_description',
|
||||
position: 1
|
||||
}
|
||||
}
|
||||
|
||||
expect(category.name).not_to eql(category_params[:category][:name])
|
||||
|
||||
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/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])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /api/v1/accounts/{account.id}/kbase/portals/{portal.id}/categories/{category.id}' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories/#{category.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}/kbase/portals/#{portal.id}/categories/#{category.id}",
|
||||
headers: agent.create_new_auth_token
|
||||
expect(response).to have_http_status(:success)
|
||||
deleted_category = Kbase::Category.find_by(id: category.id)
|
||||
expect(deleted_category).to be nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/kbase/portals/{portal.id}/categories' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'get all portals' do
|
||||
category2 = create(:kbase_category, name: 'test_category_2', portal: portal)
|
||||
expect(category2.id).not_to be nil
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories",
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
101
spec/requests/api/v1/accounts/kbase/portals_request_spec.rb
Normal file
101
spec/requests/api/v1/accounts/kbase/portals_request_spec.rb
Normal file
@@ -0,0 +1,101 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Api::V1::Accounts::Kbase::Portals', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
let!(:portal) { create(:kbase_portal, name: 'test_portal', account_id: account.id) }
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/kbase/portals' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
post "/api/v1/accounts/#{account.id}/kbase/portals", params: {}
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'creates portal' do
|
||||
portal_params = {
|
||||
portal: {
|
||||
name: 'test_portal',
|
||||
slug: 'test_kbase'
|
||||
}
|
||||
}
|
||||
post "/api/v1/accounts/#{account.id}/kbase/portals",
|
||||
params: portal_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_portal')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /api/v1/accounts/{account.id}/kbase/portals/{portal.id}' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}", params: {}
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'updates portal' do
|
||||
portal_params = {
|
||||
portal: {
|
||||
name: 'updated_test_portal'
|
||||
}
|
||||
}
|
||||
|
||||
expect(portal.name).to eql('test_portal')
|
||||
|
||||
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}",
|
||||
params: portal_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(portal_params[:portal][:name])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /api/v1/accounts/{account.id}/kbase/portals/{portal.id}' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}", params: {}
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'deletes portal' do
|
||||
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}",
|
||||
headers: agent.create_new_auth_token
|
||||
expect(response).to have_http_status(:success)
|
||||
deleted_portal = Kbase::Portal.find_by(id: portal.id)
|
||||
expect(deleted_portal).to be nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/kbase/portals' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/api/v1/accounts/#{account.id}/kbase/portals"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'get all portals' do
|
||||
portal2 = create(:kbase_portal, name: 'test_portal_2', account_id: account.id)
|
||||
expect(portal2.id).not_to be nil
|
||||
get "/api/v1/accounts/#{account.id}/kbase/portals",
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user