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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 198 additions and 132 deletions

View file

@ -1,4 +1,5 @@
class Api::V1::Accounts::Kbase::CategoriesController < Api::V1::Accounts::Kbase::BaseController class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseController
before_action :portal
before_action :fetch_category, except: [:index, :create] before_action :fetch_category, except: [:index, :create]
def index def index
@ -24,6 +25,10 @@ class Api::V1::Accounts::Kbase::CategoriesController < Api::V1::Accounts::Kbase:
@category = @portal.categories.find(params[:id]) @category = @portal.categories.find(params[:id])
end end
def portal
@portal ||= Current.account.portals.find_by(slug: params[:portal_id])
end
def category_params def category_params
params.require(:category).permit( params.require(:category).permit(
:name, :description, :position :name, :description, :position

View file

@ -1,9 +0,0 @@
class Api::V1::Accounts::Kbase::BaseController < Api::V1::Accounts::BaseController
before_action :portal
private
def portal
@portal ||= Current.account.kbase_portals.find_by(slug: params[:portal_id])
end
end

View file

@ -1,14 +1,14 @@
class Api::V1::Accounts::Kbase::PortalsController < Api::V1::Accounts::BaseController class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController
before_action :fetch_portal, except: [:index, :create] before_action :fetch_portal, except: [:index, :create]
def index def index
@portals = Current.account.kbase_portals @portals = Current.account.portals
end end
def show; end def show; end
def create def create
@portal = Current.account.kbase_portals.create!(portal_params) @portal = Current.account.portals.create!(portal_params)
end end
def update def update
@ -23,7 +23,7 @@ class Api::V1::Accounts::Kbase::PortalsController < Api::V1::Accounts::BaseContr
private private
def fetch_portal def fetch_portal
@portal = Current.account.kbase_portals.find_by(slug: permitted_params[:id]) @portal = Current.account.portals.find_by(slug: permitted_params[:id])
end end
def permitted_params def permitted_params
@ -32,7 +32,7 @@ class Api::V1::Accounts::Kbase::PortalsController < Api::V1::Accounts::BaseContr
def portal_params def portal_params
params.require(:portal).permit( params.require(:portal).permit(
:account_id, :color, :custom_domain, :header_text, :homepage_link, :name, :page_title, :slug :account_id, :color, :custom_domain, :header_text, :homepage_link, :name, :page_title, :slug, :archived
) )
end end
end end

View file

@ -51,9 +51,9 @@ class Account < ApplicationRecord
has_many :facebook_pages, dependent: :destroy_async, class_name: '::Channel::FacebookPage' has_many :facebook_pages, dependent: :destroy_async, class_name: '::Channel::FacebookPage'
has_many :hooks, dependent: :destroy_async, class_name: 'Integrations::Hook' has_many :hooks, dependent: :destroy_async, class_name: 'Integrations::Hook'
has_many :inboxes, dependent: :destroy_async has_many :inboxes, dependent: :destroy_async
has_many :kbase_articles, dependent: :destroy_async, class_name: '::Kbase::Article' has_many :articles, dependent: :destroy_async, class_name: '::Article'
has_many :kbase_categories, dependent: :destroy_async, class_name: '::Kbase::Category' has_many :categories, dependent: :destroy_async, class_name: '::Category'
has_many :kbase_portals, dependent: :destroy_async, class_name: '::Kbase::Portal' has_many :portals, dependent: :destroy_async, class_name: '::Portal'
has_many :labels, dependent: :destroy_async has_many :labels, dependent: :destroy_async
has_many :line_channels, dependent: :destroy_async, class_name: '::Channel::Line' has_many :line_channels, dependent: :destroy_async, class_name: '::Channel::Line'
has_many :mentions, dependent: :destroy_async has_many :mentions, dependent: :destroy_async

View file

@ -1,6 +1,6 @@
# == Schema Information # == Schema Information
# #
# Table name: kbase_articles # Table name: articles
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# content :text # content :text
@ -16,7 +16,7 @@
# folder_id :integer # folder_id :integer
# portal_id :integer not null # portal_id :integer not null
# #
class Kbase::Article < ApplicationRecord class Article < ApplicationRecord
belongs_to :account belongs_to :account
belongs_to :category belongs_to :category
belongs_to :portal belongs_to :portal

View file

@ -1,6 +1,6 @@
# == Schema Information # == Schema Information
# #
# Table name: kbase_categories # Table name: categories
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# description :text # description :text
@ -14,9 +14,10 @@
# #
# Indexes # Indexes
# #
# index_kbase_categories_on_locale_and_account_id (locale,account_id) # index_categories_on_locale (locale)
# index_categories_on_locale_and_account_id (locale,account_id)
# #
class Kbase::Category < ApplicationRecord class Category < ApplicationRecord
belongs_to :account belongs_to :account
belongs_to :portal belongs_to :portal
has_many :folders, dependent: :destroy_async has_many :folders, dependent: :destroy_async

View file

@ -1,6 +1,6 @@
# == Schema Information # == Schema Information
# #
# Table name: kbase_folders # Table name: folders
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# name :string # name :string
@ -9,7 +9,7 @@
# account_id :integer not null # account_id :integer not null
# category_id :integer not null # category_id :integer not null
# #
class Kbase::Folder < ApplicationRecord class Folder < ApplicationRecord
belongs_to :account belongs_to :account
belongs_to :category belongs_to :category
has_many :articles, dependent: :nullify has_many :articles, dependent: :nullify

View file

@ -1,8 +1,9 @@
# == Schema Information # == Schema Information
# #
# Table name: kbase_portals # Table name: portals
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# archived :boolean default(FALSE)
# color :string # color :string
# config :jsonb # config :jsonb
# custom_domain :string # custom_domain :string
@ -17,13 +18,14 @@
# #
# Indexes # Indexes
# #
# index_kbase_portals_on_slug (slug) UNIQUE # index_portals_on_slug (slug) UNIQUE
# #
class Kbase::Portal < ApplicationRecord class Portal < ApplicationRecord
belongs_to :account belongs_to :account
has_many :categories, dependent: :destroy_async has_many :categories, dependent: :destroy_async
has_many :folders, through: :categories has_many :folders, through: :categories
has_many :articles, dependent: :destroy_async has_many :articles, dependent: :destroy_async
has_many :users, through: :portals_members
validates :account_id, presence: true validates :account_id, presence: true
validates :name, presence: true validates :name, presence: true

View file

@ -90,6 +90,7 @@ class User < ApplicationRecord
has_many :notifications, dependent: :destroy_async has_many :notifications, dependent: :destroy_async
has_many :team_members, dependent: :destroy_async has_many :team_members, dependent: :destroy_async
has_many :teams, through: :team_members has_many :teams, through: :team_members
has_many :portals, through: :portals_members
before_validation :set_password_and_uid, on: :create before_validation :set_password_and_uid, on: :create

View file

@ -6,4 +6,5 @@ json.homepage_link portal.homepage_link
json.name portal.name json.name portal.name
json.page_title portal.page_title json.page_title portal.page_title
json.slug portal.slug json.slug portal.slug
json.archived portal.archived
json.config portal.config json.config portal.config

View file

@ -154,14 +154,15 @@ Rails.application.routes.draw do
end end
resources :working_hours, only: [:update] resources :working_hours, only: [:update]
namespace :kbase do resources :portals do
resources :portals do member do
resources :categories do post :archive
resources :folders end
end resources :categories do
resources :articles resources :folders
end end
end end
resources :articles
end end
end end
# end of account scoped api routes # end of account scoped api routes

View file

@ -0,0 +1,5 @@
class ChangeKbasePortalsToPortals < ActiveRecord::Migration[6.1]
def change
rename_table :kbase_portals, :portals
end
end

View file

@ -0,0 +1,9 @@
class CreatePortalsMembersJoinTable < ActiveRecord::Migration[6.1]
def change
create_join_table :portals, :users, table_name: :portals_members do |t|
t.index :portal_id
t.index :user_id
t.index [:portal_id, :user_id], unique: true
end
end
end

View file

@ -0,0 +1,5 @@
class ChangeKbaseCategoriesToCategories < ActiveRecord::Migration[6.1]
def change
rename_table :kbase_categories, :categories
end
end

View file

@ -0,0 +1,5 @@
class ChangeKbaseFoldersToFolders < ActiveRecord::Migration[6.1]
def change
rename_table :kbase_folders, :folders
end
end

View file

@ -0,0 +1,5 @@
class ChangeKbaseArticlesToArticles < ActiveRecord::Migration[6.1]
def change
rename_table :kbase_articles, :articles
end
end

View file

@ -0,0 +1,5 @@
class AddArchiveColumnToPortal < ActiveRecord::Migration[6.1]
def change
add_column :portals, :archived, :boolean, default: false
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_05_06_163839) do ActiveRecord::Schema.define(version: 2022_05_11_072655) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements" enable_extension "pg_stat_statements"
@ -111,6 +111,21 @@ ActiveRecord::Schema.define(version: 2022_05_06_163839) do
t.index ["account_id"], name: "index_agent_bots_on_account_id" t.index ["account_id"], name: "index_agent_bots_on_account_id"
end end
create_table "articles", force: :cascade do |t|
t.integer "account_id", null: false
t.integer "portal_id", null: false
t.integer "category_id"
t.integer "folder_id"
t.integer "author_id"
t.string "title"
t.text "description"
t.text "content"
t.integer "status"
t.integer "views"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "attachments", id: :serial, force: :cascade do |t| create_table "attachments", id: :serial, force: :cascade do |t|
t.integer "file_type", default: 0 t.integer "file_type", default: 0
t.string "external_url" t.string "external_url"
@ -169,6 +184,19 @@ ActiveRecord::Schema.define(version: 2022_05_06_163839) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "categories", force: :cascade do |t|
t.integer "account_id", null: false
t.integer "portal_id", null: false
t.string "name"
t.text "description"
t.integer "position"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "locale", default: "en"
t.index ["locale", "account_id"], name: "index_categories_on_locale_and_account_id"
t.index ["locale"], name: "index_categories_on_locale"
end
create_table "channel_api", force: :cascade do |t| create_table "channel_api", force: :cascade do |t|
t.integer "account_id", null: false t.integer "account_id", null: false
t.string "webhook_url" t.string "webhook_url"
@ -434,6 +462,14 @@ ActiveRecord::Schema.define(version: 2022_05_06_163839) do
t.index ["name", "account_id"], name: "index_email_templates_on_name_and_account_id", unique: true t.index ["name", "account_id"], name: "index_email_templates_on_name_and_account_id", unique: true
end end
create_table "folders", force: :cascade do |t|
t.integer "account_id", null: false
t.integer "category_id", null: false
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "inbox_members", id: :serial, force: :cascade do |t| create_table "inbox_members", id: :serial, force: :cascade do |t|
t.integer "user_id", null: false t.integer "user_id", null: false
t.integer "inbox_id", null: false t.integer "inbox_id", null: false
@ -486,56 +522,6 @@ ActiveRecord::Schema.define(version: 2022_05_06_163839) do
t.jsonb "settings", default: {} t.jsonb "settings", default: {}
end end
create_table "kbase_articles", force: :cascade do |t|
t.integer "account_id", null: false
t.integer "portal_id", null: false
t.integer "category_id"
t.integer "folder_id"
t.integer "author_id"
t.string "title"
t.text "description"
t.text "content"
t.integer "status"
t.integer "views"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "kbase_categories", force: :cascade do |t|
t.integer "account_id", null: false
t.integer "portal_id", null: false
t.string "name"
t.text "description"
t.integer "position"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "locale", default: "en"
t.index ["locale", "account_id"], name: "index_kbase_categories_on_locale_and_account_id"
end
create_table "kbase_folders", force: :cascade do |t|
t.integer "account_id", null: false
t.integer "category_id", null: false
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "kbase_portals", force: :cascade do |t|
t.integer "account_id", null: false
t.string "name", null: false
t.string "slug", null: false
t.string "custom_domain"
t.string "color"
t.string "homepage_link"
t.string "page_title"
t.text "header_text"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.jsonb "config", default: {"allowed_locales"=>["en"]}
t.index ["slug"], name: "index_kbase_portals_on_slug", unique: true
end
create_table "labels", force: :cascade do |t| create_table "labels", force: :cascade do |t|
t.string "title" t.string "title"
t.text "description" t.text "description"
@ -653,6 +639,30 @@ ActiveRecord::Schema.define(version: 2022_05_06_163839) do
t.datetime "updated_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false
end end
create_table "portals", force: :cascade do |t|
t.integer "account_id", null: false
t.string "name", null: false
t.string "slug", null: false
t.string "custom_domain"
t.string "color"
t.string "homepage_link"
t.string "page_title"
t.text "header_text"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.jsonb "config", default: {"allowed_locales"=>["en"]}
t.boolean "archived", default: false
t.index ["slug"], name: "index_portals_on_slug", unique: true
end
create_table "portals_members", id: false, force: :cascade do |t|
t.bigint "portal_id", null: false
t.bigint "user_id", null: false
t.index ["portal_id", "user_id"], name: "index_portals_members_on_portal_id_and_user_id", unique: true
t.index ["portal_id"], name: "index_portals_members_on_portal_id"
t.index ["user_id"], name: "index_portals_members_on_user_id"
end
create_table "reporting_events", force: :cascade do |t| create_table "reporting_events", force: :cascade do |t|
t.string "name" t.string "name"
t.float "value" t.float "value"

View file

@ -1,15 +1,15 @@
require 'rails_helper' 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(:account) { create(:account) }
let(:agent) { create(:user, account: account, role: :agent) } let(:agent) { create(:user, account: account, role: :agent) }
let!(:portal) { create(:kbase_portal, name: 'test_portal', account_id: account.id) } let!(:portal) { create(:portal, name: 'test_portal', account_id: account.id) }
let!(:category) { create(:kbase_category, name: 'category', portal: 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 context 'when it is an unauthenticated user' do
it 'returns unauthorized' 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) expect(response).to have_http_status(:unauthorized)
end end
end end
@ -23,7 +23,7 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
position: 1 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, params: category_params,
headers: agent.create_new_auth_token headers: agent.create_new_auth_token
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
@ -33,10 +33,10 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
end end
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 context 'when it is an unauthenticated user' do
it 'returns unauthorized' 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) expect(response).to have_http_status(:unauthorized)
end end
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]) 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, params: category_params,
headers: agent.create_new_auth_token headers: agent.create_new_auth_token
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
@ -63,39 +63,39 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
end end
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 context 'when it is an unauthenticated user' do
it 'returns unauthorized' 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) expect(response).to have_http_status(:unauthorized)
end end
end end
context 'when it is an authenticated user' do context 'when it is an authenticated user' do
it 'deletes category' 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 headers: agent.create_new_auth_token
expect(response).to have_http_status(:success) 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 expect(deleted_category).to be nil
end end
end 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 context 'when it is an unauthenticated user' do
it 'returns unauthorized' 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) expect(response).to have_http_status(:unauthorized)
end end
end end
context 'when it is an authenticated user' do context 'when it is an authenticated user' do
it 'get all portals' 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 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 headers: agent.create_new_auth_token
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
FactoryBot.define do FactoryBot.define do
factory :kbase_portal, class: 'Kbase::Portal' do factory :portal, class: 'Portal' do
account account
name { Faker::Book.name } name { Faker::Book.name }
slug { SecureRandom.hex } 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(:webhooks).dependent(:destroy_async) }
it { is_expected.to have_many(:notification_settings).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(:reporting_events) }
it { is_expected.to have_many(:kbase_portals).dependent(:destroy_async) } it { is_expected.to have_many(:portals).dependent(:destroy_async) }
it { is_expected.to have_many(:kbase_categories).dependent(:destroy_async) } it { is_expected.to have_many(:categories).dependent(:destroy_async) }
it { is_expected.to have_many(:teams).dependent(:destroy_async) } it { is_expected.to have_many(:teams).dependent(:destroy_async) }
describe 'usage_limits' do describe 'usage_limits' do

View file

@ -1,6 +1,6 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Kbase::Article, type: :model do RSpec.describe Article, type: :model do
context 'with validations' do context 'with validations' do
it { is_expected.to validate_presence_of(:account_id) } 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(:category_id) }

View file

@ -1,6 +1,6 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Kbase::Category, type: :model do RSpec.describe Category, type: :model do
context 'with validations' do context 'with validations' do
it { is_expected.to validate_presence_of(:account_id) } it { is_expected.to validate_presence_of(:account_id) }
it { is_expected.to validate_presence_of(:name) } it { is_expected.to validate_presence_of(:name) }

View file

@ -1,6 +1,6 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Kbase::Folder, type: :model do RSpec.describe Folder, type: :model do
context 'with validations' do context 'with validations' do
it { is_expected.to validate_presence_of(:account_id) } 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(:category_id) }

View file

@ -1,6 +1,6 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Kbase::Portal, type: :model do RSpec.describe Portal, type: :model do
context 'with validations' do context 'with validations' do
it { is_expected.to validate_presence_of(:account_id) } it { is_expected.to validate_presence_of(:account_id) }
it { is_expected.to validate_presence_of(:slug) } it { is_expected.to validate_presence_of(:slug) }