scinote-web/spec/requests/api/v1/project_folders_controller_spec.rb

207 lines
5.8 KiB
Ruby
Raw Normal View History

2020-11-23 14:09:10 +08:00
# 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(:project_folder) do
2020-11-23 14:09:10 +08:00
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 } }
2020-11-23 14:09:10 +08:00
let(:action) { get(api_v1_team_project_folder_path(params), headers: valid_headers) }
let(:project_folder_id) { project_folder.id }
2020-11-23 14:09:10 +08:00
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 }
2020-11-23 14:09:10 +08:00
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
2020-11-23 14:09:10 +08:00
}
}
}
end
let(:action) { post(api_v1_team_project_folders_path(team_id: team.id), params: params, headers: valid_headers) }
let(:project_folder_name) { 'MyNewFolder' }
2020-11-23 14:09:10 +08:00
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
2020-11-23 14:09:10 +08:00
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
2020-11-23 14:09:10 +08:00
let(:parent_folder_id) { create(:project_folder, team: team).id }
it 'creates new project_folder inside existing project_folder' do
2020-11-23 14:09:10 +08:00
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
2020-11-23 14:09:10 +08:00
end
end
end
context 'when project_folder cannot be created' do
2020-11-23 14:09:10 +08:00
context 'when validation error' do
let(:project_folder_name) { '' }
2020-11-23 14:09:10 +08:00
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,
2020-11-23 14:09:10 +08:00
type: 'project_folders',
attributes: {
name: project_folder_name,
parent_folder_id: parent_folder_id
2020-11-23 14:09:10 +08:00
}
}
}
end
let(:action) do
patch(api_v1_team_project_folder_path(team_id: team.id, id: project_folder_id),
params: params,
headers: valid_headers)
2020-11-23 14:09:10 +08:00
end
let(:project_folder_name) { 'MyUpdatedFolder' }
let(:project_folder_id) { project_folder.id }
2020-11-23 14:09:10 +08:00
let(:parent_folder_id) { nil }
context 'when project_folder can be updated' do
context 'when root project_folder' do
it 'updates project_folder' do
2020-11-23 14:09:10 +08:00
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
2020-11-23 14:09:10 +08:00
let(:parent_folder_id) { create(:project_folder, team: team).id }
it 'updates project_folder\'s parent with existing project_folder' do
2020-11-23 14:09:10 +08:00
action
expect(JSON.parse(response.body).dig('data', 'relationships', 'parent_folder', 'data')).to be_truthy
2020-11-23 14:09:10 +08:00
expect(response).to have_http_status(200)
end
end
context 'when nothing to update' do
let(:project_folder_name) { project_folder.name }
2020-11-23 14:09:10 +08:00
it 'do not update project_folder, returns 204' do
2020-11-23 14:09:10 +08:00
action
expect(response).to have_http_status(204)
end
end
end
context 'when project_folder cannot be updated' do
2020-11-23 14:09:10 +08:00
context 'when validation error' do
let(:project_folder_name) { '' }
2020-11-23 14:09:10 +08:00
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 }
2020-11-23 14:09:10 +08:00
it do
action
expect(JSON.parse(response.body)['errors'].first['title']).to be_eql 'Validation error'
expect(response).to have_http_status 400
2020-11-23 14:09:10 +08:00
end
end
context 'when mismatch IDs' do
let(:project_folder_id) { create(:project_folder, team: team).id }
2020-11-23 14:09:10 +08:00
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