feat: Portal endpoint (#4633)

This commit is contained in:
Tejaswini Chile
2022-05-16 13:59:59 +05:30
committed by GitHub
parent 6535624cd6
commit 938fb887c4
39 changed files with 198 additions and 132 deletions

View File

@@ -1,15 +1,15 @@
require 'rails_helper'
RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
RSpec.describe 'Api::V1::Accounts::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) }
let!(:portal) { create(:portal, name: 'test_portal', account_id: account.id) }
let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id) }
describe 'POST /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}/categories' do
describe 'POST /api/v1/accounts/{account.id}/portals/{portal.slug}/categories' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories", params: {}
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
@@ -23,7 +23,7 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
position: 1
}
}
post "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories",
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)
@@ -33,10 +33,10 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
end
end
describe 'PUT /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}/categories/{category.id}' do
describe 'PUT /api/v1/accounts/{account.id}/portals/{portal.slug}/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.slug}/categories/#{category.id}", params: {}
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories/#{category.id}", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
@@ -53,7 +53,7 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
expect(category.name).not_to eql(category_params[:category][:name])
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories/#{category.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)
@@ -63,39 +63,39 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
end
end
describe 'DELETE /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}/categories/{category.id}' do
describe 'DELETE /api/v1/accounts/{account.id}/portals/{portal.slug}/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.slug}/categories/#{category.id}", params: {}
delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/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.slug}/categories/#{category.id}",
delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/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)
deleted_category = 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.slug}/categories' do
describe 'GET /api/v1/accounts/{account.id}/portals/{portal.slug}/categories' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories"
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/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)
category2 = create(:category, name: 'test_category_2', portal: portal)
expect(category2.id).not_to be nil
get "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories",
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)

View File

@@ -1,23 +1,23 @@
require 'rails_helper'
RSpec.describe 'Api::V1::Accounts::Kbase::Portals', type: :request do
RSpec.describe 'Api::V1::Accounts::Portals', type: :request do
let(:account) { create(:account) }
let(:agent) { create(:user, account: account, role: :agent) }
let!(:portal) { create(:kbase_portal, slug: 'portal-1', name: 'test_portal', account_id: account.id) }
let!(:portal) { create(:portal, slug: 'portal-1', name: 'test_portal', account_id: account.id) }
describe 'GET /api/v1/accounts/{account.id}/kbase/portals' do
describe 'GET /api/v1/accounts/{account.id}/portals' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/kbase/portals"
get "/api/v1/accounts/#{account.id}/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, slug: 'portal-2')
portal2 = create(:portal, name: 'test_portal_2', account_id: account.id, slug: 'portal-2')
expect(portal2.id).not_to be nil
get "/api/v1/accounts/#{account.id}/kbase/portals",
get "/api/v1/accounts/#{account.id}/portals",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
@@ -26,17 +26,17 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Portals', type: :request do
end
end
describe 'GET /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}' do
describe 'GET /api/v1/accounts/{account.id}/portals/{portal.slug}' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/kbase/portals"
get "/api/v1/accounts/#{account.id}/portals"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'get one portals' do
get "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}",
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
@@ -45,10 +45,10 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Portals', type: :request do
end
end
describe 'POST /api/v1/accounts/{account.id}/kbase/portals' do
describe 'POST /api/v1/accounts/{account.id}/portals' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post "/api/v1/accounts/#{account.id}/kbase/portals", params: {}
post "/api/v1/accounts/#{account.id}/portals", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
@@ -61,7 +61,7 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Portals', type: :request do
slug: 'test_kbase'
}
}
post "/api/v1/accounts/#{account.id}/kbase/portals",
post "/api/v1/accounts/#{account.id}/portals",
params: portal_params,
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
@@ -71,10 +71,10 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Portals', type: :request do
end
end
describe 'PUT /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}' do
describe 'PUT /api/v1/accounts/{account.id}/portals/{portal.slug}' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}", params: {}
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
@@ -89,30 +89,50 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Portals', type: :request do
expect(portal.name).to eql('test_portal')
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}",
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}",
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['name']).to eql(portal_params[:portal][:name])
end
it 'archive portal' do
portal_params = {
portal: {
archived: true
}
}
expect(portal.archived).to be_falsy
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}",
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['archived']).to eql(portal_params[:portal][:archived])
portal.reload
expect(portal.archived).to be_truthy
end
end
end
describe 'DELETE /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}' do
describe 'DELETE /api/v1/accounts/{account.id}/portals/{portal.slug}' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}", params: {}
delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}", 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.slug}",
delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
deleted_portal = Kbase::Portal.find_by(id: portal.slug)
deleted_portal = Portal.find_by(id: portal.slug)
expect(deleted_portal).to be nil
end
end

View File

@@ -1,5 +1,5 @@
FactoryBot.define do
factory :kbase_article, class: 'Kbase::Article' do
factory :article, class: 'Article' do
account_id { 1 }
category_id { 1 }
folder_id { 1 }

View File

@@ -1,6 +1,6 @@
FactoryBot.define do
factory :kbase_category, class: 'Kbase::Category' do
portal { kbase_portal }
factory :category, class: 'Category' do
portal { portal }
name { 'MyString' }
description { 'MyText' }
position { 1 }

View File

@@ -1,5 +1,5 @@
FactoryBot.define do
factory :kbase_folder, class: 'Kbase::Folder' do
factory :folder, class: 'Folder' do
account_id { 1 }
name { 'MyString' }
description { 'MyText' }

View File

@@ -1,5 +1,5 @@
FactoryBot.define do
factory :kbase_portal, class: 'Kbase::Portal' do
factory :portal, class: 'Portal' do
account
name { Faker::Book.name }
slug { SecureRandom.hex }

View File

@@ -19,8 +19,8 @@ RSpec.describe Account do
it { is_expected.to have_many(:webhooks).dependent(:destroy_async) }
it { is_expected.to have_many(:notification_settings).dependent(:destroy_async) }
it { is_expected.to have_many(:reporting_events) }
it { is_expected.to have_many(:kbase_portals).dependent(:destroy_async) }
it { is_expected.to have_many(:kbase_categories).dependent(:destroy_async) }
it { is_expected.to have_many(:portals).dependent(:destroy_async) }
it { is_expected.to have_many(:categories).dependent(:destroy_async) }
it { is_expected.to have_many(:teams).dependent(:destroy_async) }
describe 'usage_limits' do

View File

@@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe Kbase::Article, type: :model do
RSpec.describe 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) }

View File

@@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe Kbase::Category, type: :model do
RSpec.describe 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) }

View File

@@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe Kbase::Folder, type: :model do
RSpec.describe 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) }

View File

@@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe Kbase::Portal, type: :model do
RSpec.describe 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) }