Merge pull request #2970 from urbanrotnik/ur-sci-5185

Folders API endpoints [SCI-5185]
This commit is contained in:
Alex Kriuchykhin 2020-12-02 14:58:43 +01:00 committed by GitHub
commit 7025c90d91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 469 additions and 8 deletions

View file

@ -0,0 +1,56 @@
# frozen_string_literal: true
module Api
module V1
class ProjectFoldersController < BaseController
before_action :load_team
before_action :load_project_folder, only: %i(show update)
def index
project_folders = @team.project_folders
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: project_folders, each_serializer: ProjectFolderSerializer
end
def show
render jsonapi: @project_folder, serializer: ProjectFolderSerializer
end
def create
folder = @team.project_folders.create!(folder_params)
render jsonapi: folder, serializer: ProjectFolderSerializer, status: :created
end
def update
@project_folder.attributes = update_folder_params
if @project_folder.changed? && @project_folder.save!
render jsonapi: @project_folder, serializer: ProjectFolderSerializer
else
render jsonapi: nil, status: :no_content
end
end
private
def load_project_folder
@project_folder = @team.project_folders.find(params.require(:id))
end
def folder_params
raise TypeError unless params.require(:data).require(:type) == 'project_folders'
params.permit(data: { attributes: %i(name parent_folder_id) })[:data][:attributes]
end
def update_folder_params
raise IDMismatchError unless params.require(:data).require(:id).to_i == params[:id].to_i
folder_params
end
end
end
end

View file

@ -61,7 +61,7 @@ module Api
def project_params
raise TypeError unless params.require(:data).require(:type) == 'projects'
params.require(:data).require(:attributes).permit(:name, :visibility, :archived)
params.require(:data).require(:attributes).permit(:name, :visibility, :archived, :project_folder_id)
end
def load_project_for_managing

View file

@ -12,6 +12,7 @@ class Project < ApplicationRecord
uniqueness: { scope: :team_id, case_sensitive: false }
validates :visibility, presence: true
validates :team, presence: true
validate :project_folder_team, if: -> { project_folder.present? }
belongs_to :created_by,
foreign_key: 'created_by_id',
@ -321,4 +322,12 @@ class Project < ApplicationRecord
ensure
report.destroy if report.present?
end
private
def project_folder_team
return if project_folder.team_id == team_id
errors.add(:project_folder, I18n.t('activerecord.errors.models.project.attributes.project_folder.team'))
end
end

View file

@ -8,8 +8,9 @@ class ProjectFolder < ApplicationRecord
length: { minimum: Constants::NAME_MIN_LENGTH,
maximum: Constants::NAME_MAX_LENGTH },
uniqueness: { scope: :team_id, case_sensitive: false }
validate :parent_folder_team, if: -> { parent_folder.present? }
before_validation :inherit_team_from_parent_folder, if: -> { team.blank? && parent_folder.present? }
before_validation :inherit_team_from_parent_folder, on: :create, if: -> { parent_folder.present? }
belongs_to :team, inverse_of: :project_folders, touch: true
belongs_to :parent_folder, class_name: 'ProjectFolder', optional: true
@ -87,4 +88,10 @@ class ProjectFolder < ApplicationRecord
def inherit_team_from_parent_folder
self.team = parent_folder.team
end
def parent_folder_team
return if parent_folder.team_id == team_id
errors.add(:parent_folder, I18n.t('activerecord.errors.models.project_folder.attributes.parent_folder'))
end
end

View file

@ -0,0 +1,14 @@
# frozen_string_literal: true
module Api
module V1
class ProjectFolderSerializer < ActiveModel::Serializer
type :project_folders
attributes :id, :name
belongs_to :team, serializer: TeamSerializer
belongs_to :parent_folder, serializer: ProjectFolderSerializer
has_many :projects, serializer: ProjectSerializer
end
end
end

View file

@ -6,6 +6,8 @@ module Api
type :projects
attributes :name, :visibility, :start_date, :archived
belongs_to :project_folder, serializer: ProjectFolderSerializer
def start_date
I18n.l(object.created_at, format: :full)
end

View file

@ -96,6 +96,11 @@ en:
attributes:
name:
taken: "This project name has to be unique inside a team (this includes the archive)."
project_folder:
team: "Project folder and project should belongs to the same team"
project_folder:
attributes:
parent_folder: "Parent folder and folder should belongs to the same team"
view_state:
attributes:
viewable_id:

View file

@ -701,6 +701,7 @@ Rails.application.routes.draw do
end
end
end
resources :project_folders, only: %i(index show create update)
end
resources :users, only: %i(show) do
resources :user_identities,

View file

@ -40,5 +40,27 @@ describe ProjectFolder, type: :model do
expect(project_folder).to validate_uniqueness_of(:name).scoped_to(:team_id).case_insensitive
end
end
describe '#parent_folder_team' do
it 'should validate equals of team and parent_folder team' do
project_folder.save
parent_folder = create(:project_folder, name: 'Parent folder')
project_folder.update(parent_folder: parent_folder)
expect(project_folder.errors).to have_key(:parent_folder)
end
end
end
describe 'Callbacks' do
describe 'inherit_team_from_parent_folder' do
it do
parent_folder = create(:project_folder, name: 'Parent folder')
project_folder.parent_folder = parent_folder
expect { project_folder.save }.to(change { project_folder.team })
end
end
end
end

View file

@ -67,5 +67,15 @@ describe Project, type: :model do
expect(project).to validate_uniqueness_of(:name).scoped_to(:team_id).case_insensitive
end
end
describe '#project_folder_team' do
it 'should validate equals of team and project_folder team' do
project_folder = create(:project_folder, name: 'Folder from another team')
project.project_folder = project_folder
project.save
expect(project.errors).to have_key(:project_folder)
end
end
end
end

View file

@ -0,0 +1,207 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Api::V1::ProjectFoldersController', type: :request do
let(:user) { create :user }
let(:valid_headers) { { 'Authorization': 'Bearer ' + generate_token(user.id) } }
let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, team: team, user: user }
let(:project_folder) do
create :project_folder, team: team
end
describe 'GET index' do
let(:params) { { team_id: team.id } }
let(:action) { get(api_v1_team_project_folders_path(params), headers: valid_headers) }
context 'when has valid params' do
it 'renders 200' do
action
expect(response).to have_http_status(200)
expect(response).to match_json_schema('project_folders/collection')
end
end
context 'when user is not part of the team' do
let(:second_team) { create :team }
let(:params) { { team_id: second_team.id } }
it 'renders 403' do
action
expect(response).to have_http_status(403)
end
end
end
describe 'GET show' do
let(:params) { { team_id: team.id, id: project_folder_id } }
let(:action) { get(api_v1_team_project_folder_path(params), headers: valid_headers) }
let(:project_folder_id) { project_folder.id }
context 'when project_folder found' do
it do
action
expect(response).to have_http_status(200)
expect(response).to match_json_schema('project_folders/resource')
end
end
context 'when project_folder does not exists' do
let(:project_folder_id) { -1 }
it do
action
expect(response).to have_http_status 404
end
end
end
describe 'POST create' do
let(:params) do
{
data: {
type: 'project_folders',
attributes: {
name: project_folder_name,
parent_folder_id: parent_folder_id
}
}
}
end
let(:action) { post(api_v1_team_project_folders_path(team_id: team.id), params: params, headers: valid_headers) }
let(:project_folder_name) { 'MyNewFolder' }
let(:parent_folder_id) { nil }
context 'when project_folder can be created' do
context 'when root project_folder' do
it 'creates new project_folder' do
action
expect(response).to have_http_status(201)
expect(response).to match_json_schema('project_folders/resource')
end
end
context 'when nested project_folder' do
let(:parent_folder_id) { create(:project_folder, team: team).id }
it 'creates new project_folder inside existing project_folder' do
action
expect(response).to have_http_status(201)
expect(response).to match_json_schema('project_folders/resource')
expect(JSON.parse(response.body).dig('data', 'relationships', 'parent_folder', 'data')).to be_truthy
end
end
end
context 'when project_folder cannot be created' do
context 'when validation error' do
let(:project_folder_name) { '' }
it 'should returns validation error' do
action
expect(response).to have_http_status 400
expect(JSON.parse(response.body)['errors'].first['title']).to be_eql 'Validation error'
end
end
end
end
describe 'PATCH update' do
let(:params) do
{
data: {
id: project_folder.id,
type: 'project_folders',
attributes: {
name: project_folder_name,
parent_folder_id: parent_folder_id
}
}
}
end
let(:action) do
patch(api_v1_team_project_folder_path(team_id: team.id, id: project_folder_id),
params: params,
headers: valid_headers)
end
let(:project_folder_name) { 'MyUpdatedFolder' }
let(:project_folder_id) { project_folder.id }
let(:parent_folder_id) { nil }
context 'when project_folder can be updated' do
context 'when root project_folder' do
it 'updates project_folder' do
action
expect(response).to have_http_status(200)
expect(response).to match_json_schema('project_folders/resource')
end
end
context 'when update parent project_folder' do
let(:parent_folder_id) { create(:project_folder, team: team).id }
it 'updates project_folder\'s parent with existing project_folder' do
action
expect(JSON.parse(response.body).dig('data', 'relationships', 'parent_folder', 'data')).to be_truthy
expect(response).to have_http_status(200)
end
end
context 'when nothing to update' do
let(:project_folder_name) { project_folder.name }
it 'do not update project_folder, returns 204' do
action
expect(response).to have_http_status(204)
end
end
end
context 'when project_folder cannot be updated' do
context 'when validation error' do
let(:project_folder_name) { '' }
it 'returns validation error, returns 400' do
action
expect(response).to have_http_status(400)
expect(JSON.parse(response.body)['errors'].first['title']).to be_eql 'Validation error'
end
end
context 'when parent_folder belongs to another team' do
let(:folder_from_another_team) { create(:project_folder) }
let(:parent_folder_id) { folder_from_another_team.id }
it do
action
expect(JSON.parse(response.body)['errors'].first['title']).to be_eql 'Validation error'
expect(response).to have_http_status 400
end
end
context 'when mismatch IDs' do
let(:project_folder_id) { create(:project_folder, team: team).id }
it '' do
action
expect(response).to have_http_status(400)
expect(JSON.parse(response.body)['errors'].first['title']).to be_eql 'Object ID mismatch'
end
end
end
end
end

View file

@ -2,7 +2,7 @@
require 'rails_helper'
RSpec.describe "Api::V1::ProjectsController", type: :request do
RSpec.describe 'Api::V1::ProjectsController', type: :request do
before :all do
@user = create(:user)
@teams = create_list(:team, 2, created_by: @user)
@ -147,6 +147,40 @@ RSpec.describe "Api::V1::ProjectsController", type: :request do
)
)
end
context 'when includes project_folder relation' do
let(:request_body) do
{
data: {
type: 'projects',
attributes: {
name: 'Project name',
visibility: 'hidden',
project_folder_id: project_folder.id
}
}
}
end
let(:project_folder) { create :project_folder, team: @teams.first }
it 'renders 201' do
action
expect(response).to have_http_status(201)
expect(JSON.parse(response.body).dig('data', 'relationships', 'project_folder', 'data')).to be_truthy
end
context 'when folder from a different team' do
let(:project_folder) { create :project_folder, team: @teams.last }
it do
action
expect(JSON.parse(response.body)['errors'].first['title']).to be_eql 'Validation error'
expect(response).to have_http_status 400
end
end
end
end
context 'when has missing param' do
@ -217,6 +251,27 @@ RSpec.describe "Api::V1::ProjectsController", type: :request do
)
)
end
context 'when includes project_folder relation' do
let(:request_body) do
{
data: {
type: 'projects',
attributes: {
project_folder_id: project_folder.id
}
}
}
end
let(:project_folder) { create :project_folder, team: @teams.first }
it 'renders 201' do
action
expect(response).to have_http_status(200)
expect(JSON.parse(response.body).dig('data', 'relationships', 'project_folder', 'data')).to be_truthy
end
end
end
context 'when has missing param' do

View file

@ -0,0 +1,13 @@
{
"id": "file:/project_folders/collection.json#",
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "file:/project_folders/item.json#"
}
}
},
"required": ["data", "links"]
}

View file

@ -0,0 +1,46 @@
{
"id": "file:/project_folders/item.json#",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"attributes": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"name"
]
},
"relationships": {
"type": "object",
"properties": {
"team": {
"type": "object"
},
"parent_folder": {
"type": "object"
},
"projects": {
"type": "object"
}
},
"additionalProperties": false,
"required": ["team"]
}
},
"additionalProperties": false,
"required": ["id", "type", "attributes"]
}

View file

@ -0,0 +1,14 @@
{
"id": "file:/project_folders/resource.json#",
"type": "object",
"properties":{
"data": {
"$ref": "file:/project_folders/item.json#"
}
},
"required": ["data"]
}

View file

@ -1,11 +1,11 @@
{
"id": "file:/collection.json#",
"id": "file:/status_items/collection.json#",
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "file:/item.json#"
"$ref": "file:/status_items/item.json#"
}
}
},

View file

@ -1,5 +1,5 @@
{
"id": "file:/item.json#",
"id": "file:/status_items/item.json#",
"type": "object",
"properties": {
"id": {

View file

@ -1,9 +1,9 @@
{
"id": "file:/resource.json#",
"id": "file:/status_items/resource.json#",
"type": "object",
"properties":{
"data": {
"$ref": "file:/item.json#"
"$ref": "file:/status_items/item.json#"
}
},
"required": ["data"]