2018-10-12 23:43:36 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2020-11-23 19:42:03 +08:00
|
|
|
RSpec.describe 'Api::V1::ProjectsController', type: :request do
|
2018-10-12 23:43:36 +08:00
|
|
|
before :all do
|
|
|
|
@user = create(:user)
|
2022-06-07 00:21:57 +08:00
|
|
|
@another_user = create(:user)
|
|
|
|
@team1 = create(:team, created_by: @user)
|
|
|
|
@team2 = create(:team, created_by: @another_user)
|
|
|
|
|
2018-10-12 23:43:36 +08:00
|
|
|
# valid_projects
|
2021-05-29 23:25:20 +08:00
|
|
|
2.times do
|
2022-06-07 00:21:57 +08:00
|
|
|
project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team1)
|
2021-05-29 23:25:20 +08:00
|
|
|
end
|
2023-05-19 17:34:31 +08:00
|
|
|
2.times do
|
|
|
|
project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @teams.first, archived: true)
|
|
|
|
end
|
2018-10-12 23:43:36 +08:00
|
|
|
|
|
|
|
# unaccessable_projects
|
|
|
|
create(:project, name: Faker::Name.unique.name,
|
2022-06-07 00:21:57 +08:00
|
|
|
created_by: @another_user, team: @team2)
|
2018-10-12 23:43:36 +08:00
|
|
|
create(:project, name: Faker::Name.unique.name,
|
2022-06-07 00:21:57 +08:00
|
|
|
created_by: @another_user, team: @team2)
|
2018-10-12 23:43:36 +08:00
|
|
|
|
|
|
|
@valid_headers =
|
|
|
|
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET projects, #index' do
|
|
|
|
it 'Response with correct projects' do
|
|
|
|
hash_body = nil
|
2022-06-07 00:21:57 +08:00
|
|
|
get api_v1_team_projects_path(team_id: @team1.id),
|
2018-10-12 23:43:36 +08:00
|
|
|
headers: @valid_headers
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body[:data]).to match(
|
2021-06-07 16:34:58 +08:00
|
|
|
JSON.parse(
|
|
|
|
ActiveModelSerializers::SerializableResource
|
2022-06-07 00:21:57 +08:00
|
|
|
.new(@team1.projects, each_serializer: Api::V1::ProjectSerializer)
|
2021-06-07 16:34:58 +08:00
|
|
|
.to_json
|
|
|
|
)['data']
|
2018-10-12 23:43:36 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-05-19 17:34:31 +08:00
|
|
|
it 'Response with correct projects, only active' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_projects_path(team_id: @teams.first.id, filter: { archived: false }),
|
|
|
|
headers: @valid_headers
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body[:data].pluck('attributes').pluck('archived').none?).to be(true)
|
|
|
|
expect(hash_body[:data]).to match(
|
|
|
|
JSON.parse(
|
|
|
|
ActiveModelSerializers::SerializableResource
|
|
|
|
.new(@teams.first.projects.active, each_serializer: Api::V1::ProjectSerializer)
|
|
|
|
.to_json
|
|
|
|
)['data']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'Response with correct projects, only archived' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_projects_path(team_id: @teams.first.id, filter: { archived: true }),
|
|
|
|
headers: @valid_headers
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body[:data].pluck('attributes').pluck('archived').all?).to be(true)
|
|
|
|
expect(hash_body[:data]).to match(
|
|
|
|
JSON.parse(
|
|
|
|
ActiveModelSerializers::SerializableResource
|
|
|
|
.new(@teams.first.projects.archived, each_serializer: Api::V1::ProjectSerializer)
|
|
|
|
.to_json
|
|
|
|
)['data']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-10-12 23:43:36 +08:00
|
|
|
it 'When invalid request, user in not member of the team' do
|
|
|
|
hash_body = nil
|
2022-06-07 00:21:57 +08:00
|
|
|
get api_v1_team_projects_path(team_id: @team2.id),
|
2018-10-12 23:43:36 +08:00
|
|
|
headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(403)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 403)
|
2018-10-12 23:43:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, non existing team' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_projects_path(team_id: -1), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-10-12 23:43:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET project, #show' do
|
|
|
|
it 'When valid request, user can read project' do
|
|
|
|
hash_body = nil
|
2022-06-07 00:21:57 +08:00
|
|
|
get api_v1_team_project_path(team_id: @team1.id,
|
|
|
|
id: @team1.projects.first.id),
|
2018-10-12 23:43:36 +08:00
|
|
|
headers: @valid_headers
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body[:data]).to match(
|
2021-06-07 16:34:58 +08:00
|
|
|
JSON.parse(
|
|
|
|
ActiveModelSerializers::SerializableResource
|
2022-06-07 00:21:57 +08:00
|
|
|
.new(@team1.projects.first, serializer: Api::V1::ProjectSerializer)
|
2021-06-07 16:34:58 +08:00
|
|
|
.to_json
|
|
|
|
)['data']
|
2018-10-12 23:43:36 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, user in not member of the team' do
|
|
|
|
hash_body = nil
|
2022-06-07 00:21:57 +08:00
|
|
|
get api_v1_team_project_path(team_id: @team2.id,
|
|
|
|
id: @team2.projects.first.id),
|
2018-10-12 23:43:36 +08:00
|
|
|
headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(403)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 403)
|
2018-10-12 23:43:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, non existing project' do
|
|
|
|
hash_body = nil
|
2022-06-07 00:21:57 +08:00
|
|
|
get api_v1_team_project_path(team_id: @team1.id, id: -1),
|
2018-10-12 23:43:36 +08:00
|
|
|
headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-10-12 23:43:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, project from another team' do
|
|
|
|
hash_body = nil
|
2022-06-07 00:21:57 +08:00
|
|
|
get api_v1_team_project_path(team_id: @team1.id,
|
|
|
|
id: @team2.projects.first.id),
|
2018-10-12 23:43:36 +08:00
|
|
|
headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-10-12 23:43:36 +08:00
|
|
|
end
|
|
|
|
end
|
2020-09-16 22:34:35 +08:00
|
|
|
|
|
|
|
describe 'POST project, #create' do
|
|
|
|
before :all do
|
|
|
|
@valid_headers['Content-Type'] = 'application/json'
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:action) do
|
2022-06-07 00:21:57 +08:00
|
|
|
post(api_v1_team_projects_path(team_id: @team1.id), params: request_body.to_json, headers: @valid_headers)
|
2020-09-16 22:34:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has valid params' do
|
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'projects',
|
|
|
|
attributes: {
|
|
|
|
name: 'Project name',
|
|
|
|
visibility: 'hidden'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates new project' do
|
|
|
|
expect { action }.to change { Project.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status 201' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status 201
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns well formated response' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(json).to match(
|
|
|
|
hash_including(
|
|
|
|
data: hash_including(
|
|
|
|
type: 'projects',
|
|
|
|
attributes: hash_including(name: 'Project name', visibility: 'hidden')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2020-11-23 19:42:03 +08:00
|
|
|
|
2020-12-02 18:44:37 +08:00
|
|
|
context 'when includes project_folder relation' do
|
2020-11-23 19:42:03 +08:00
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'projects',
|
|
|
|
attributes: {
|
|
|
|
name: 'Project name',
|
2020-12-02 18:44:37 +08:00
|
|
|
visibility: 'hidden',
|
|
|
|
project_folder_id: project_folder.id
|
2020-11-23 19:42:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2022-06-07 00:21:57 +08:00
|
|
|
let(:project_folder) { create :project_folder, team: @team1 }
|
2020-11-23 19:42:03 +08:00
|
|
|
|
|
|
|
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
|
2020-12-02 20:13:58 +08:00
|
|
|
|
|
|
|
context 'when folder from a different team' do
|
2022-06-07 00:21:57 +08:00
|
|
|
let(:project_folder) { create :project_folder, team: @team2 }
|
2020-12-02 20:13:58 +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
|
|
|
|
end
|
|
|
|
end
|
2020-11-23 19:42:03 +08:00
|
|
|
end
|
2020-09-16 22:34:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has missing param' do
|
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'projects',
|
|
|
|
attributes: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 400' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PATCH project, #update' do
|
|
|
|
before :all do
|
|
|
|
@valid_headers['Content-Type'] = 'application/json'
|
|
|
|
@project = @user.teams.first.projects.first
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:action) do
|
|
|
|
patch(
|
|
|
|
api_v1_team_project_path(
|
|
|
|
team_id: @project.team.id,
|
|
|
|
id: @project.id
|
|
|
|
),
|
|
|
|
params: request_body.to_json,
|
|
|
|
headers: @valid_headers
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has valid params' do
|
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'projects',
|
|
|
|
attributes: {
|
|
|
|
name: 'New project name',
|
|
|
|
visibility: 'hidden',
|
|
|
|
archived: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status 200' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status 200
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns well formated response' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(json).to match(
|
|
|
|
hash_including(
|
|
|
|
data: hash_including(
|
|
|
|
type: 'projects',
|
|
|
|
attributes: hash_including(name: 'New project name', visibility: 'hidden', archived: true)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2020-11-23 19:42:03 +08:00
|
|
|
|
2020-12-02 18:44:37 +08:00
|
|
|
context 'when includes project_folder relation' do
|
2020-11-23 19:42:03 +08:00
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'projects',
|
2020-12-02 18:44:37 +08:00
|
|
|
attributes: {
|
|
|
|
project_folder_id: project_folder.id
|
2020-11-23 19:42:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2022-06-07 00:21:57 +08:00
|
|
|
let(:project_folder) { create :project_folder, team: @team1 }
|
2020-11-23 19:42:03 +08:00
|
|
|
|
|
|
|
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
|
2020-09-16 22:34:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has missing param' do
|
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'projects',
|
|
|
|
attributes: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 400' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-12 23:43:36 +08:00
|
|
|
end
|