kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
406 lines
15 KiB
406 lines
15 KiB
10 years ago
|
#-- copyright
|
||
|
# OpenProject is a project management system.
|
||
|
# Copyright (C) 2012-2014 the OpenProject Foundation (OPF)
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License version 3.
|
||
|
#
|
||
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
||
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
|
# as published by the Free Software Foundation; either version 2
|
||
|
# of the License, or (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
#
|
||
|
# See doc/COPYRIGHT.rdoc for more details.
|
||
|
#++
|
||
|
|
||
11 years ago
|
require 'spec_helper'
|
||
|
require 'rack/test'
|
||
|
|
||
10 years ago
|
describe 'API v3 Query resource', :type => :request do
|
||
11 years ago
|
include Rack::Test::Methods
|
||
|
|
||
|
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project', :is_public => false) }
|
||
|
let(:current_user) { FactoryGirl.create(:user) }
|
||
11 years ago
|
let(:manage_public_queries_role) { FactoryGirl.create(:role, permissions: [:manage_public_queries]) }
|
||
|
let(:save_queries_role) { FactoryGirl.create(:role, permissions: [:save_queries]) }
|
||
|
let(:role_without_query_permissions) { FactoryGirl.create(:role, permissions: [:view_work_packages]) }
|
||
11 years ago
|
let(:unauthorize_user) { FactoryGirl.create(:user) }
|
||
|
|
||
11 years ago
|
describe '#star' do
|
||
11 years ago
|
let(:star_path) { "/api/v3/queries/#{query.id}/star" }
|
||
|
let(:filters) do
|
||
|
query.filters.map{ |f| {f.field.to_s => { "operator" => f.operator, "values" => f.values }}}
|
||
|
end
|
||
|
let(:expected_response) do
|
||
|
{
|
||
|
"_type" => 'Query',
|
||
|
"_links" => {
|
||
|
"self" => {
|
||
10 years ago
|
"href" => "/api/v3/queries/#{query.id}",
|
||
11 years ago
|
"title" => query.name
|
||
|
}
|
||
|
},
|
||
|
"id" => query.id,
|
||
|
"name" => query.name,
|
||
|
"projectId" => query.project_id,
|
||
|
"projectName" => query.project.name,
|
||
|
"userId" => query.user_id,
|
||
|
"userName" => query.user.try(:name),
|
||
|
"userLogin" => query.user.try(:login),
|
||
|
"userMail" => query.user.try(:mail),
|
||
|
"filters" => filters,
|
||
|
"isPublic" => query.is_public.to_s,
|
||
|
"columnNames" => query.column_names,
|
||
|
"sortCriteria" => query.sort_criteria,
|
||
|
"groupBy" => query.group_by,
|
||
|
"displaySums" => query.display_sums.to_s,
|
||
|
"isStarred" => "true"
|
||
|
}
|
||
11 years ago
|
end
|
||
11 years ago
|
|
||
11 years ago
|
describe 'public queries' do
|
||
|
let(:query) { FactoryGirl.create(:public_query, project: project) }
|
||
|
|
||
|
context 'user with permission to manage public queries' do
|
||
|
before(:each) do
|
||
|
allow(User).to receive(:current).and_return current_user
|
||
|
member = FactoryGirl.build(:member, user: current_user, project: project)
|
||
|
member.role_ids = [manage_public_queries_role.id]
|
||
|
member.save!
|
||
|
end
|
||
|
|
||
|
context 'when starring an unstarred query' do
|
||
|
before(:each) { patch star_path }
|
||
|
|
||
|
it 'should respond with 200' do
|
||
10 years ago
|
expect(last_response.status).to eq(200)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query in HAL+JSON format' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response).to eq(expected_response)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query with "isStarred" property set to true' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response['isStarred']).to eq('true')
|
||
11 years ago
|
end
|
||
|
end
|
||
|
|
||
|
context 'when starring already starred query' do
|
||
|
before(:each) { patch star_path }
|
||
|
|
||
|
it 'should respond with 200' do
|
||
10 years ago
|
expect(last_response.status).to eq(200)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query in HAL+JSON format' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response).to eq(expected_response)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query with "isStarred" property set to true' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response['isStarred']).to eq('true')
|
||
11 years ago
|
end
|
||
|
end
|
||
|
|
||
|
context 'when trying to star nonexistent query' do
|
||
|
let(:star_path) { "/api/v3/queries/999/star" }
|
||
|
before(:each) { patch star_path }
|
||
|
|
||
|
it 'should respond with 404' do
|
||
10 years ago
|
expect(last_response.status).to eq(404)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should respond with explanatory error message' do
|
||
|
parsed_errors = JSON.parse(last_response.body)['errors']
|
||
10 years ago
|
expect(parsed_errors).to eq([{ 'key' => 'not_found', 'messages' => ['Couldn\'t find Query with id=999']}])
|
||
11 years ago
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'user without permission to manage public queries' do
|
||
|
before(:each) do
|
||
|
allow(User).to receive(:current).and_return current_user
|
||
|
member = FactoryGirl.build(:member, user: current_user, project: project)
|
||
|
member.role_ids = [role_without_query_permissions.id]
|
||
|
member.save!
|
||
|
patch star_path
|
||
|
end
|
||
11 years ago
|
|
||
11 years ago
|
it 'should respond with 403' do
|
||
10 years ago
|
expect(last_response.status).to eq(403)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should respond with explanatory error message' do
|
||
|
parsed_errors = JSON.parse(last_response.body)['errors']
|
||
10 years ago
|
expect(parsed_errors).to eq([{ 'key' => 'not_authorized', 'messages' => ['You are not authorize to access this resource']}])
|
||
11 years ago
|
end
|
||
|
end
|
||
11 years ago
|
end
|
||
|
|
||
11 years ago
|
describe 'private queries' do
|
||
|
context 'user with permission to save queries' do
|
||
|
let(:query) { FactoryGirl.create(:private_query, project: project, user: current_user) }
|
||
|
before(:each) do
|
||
|
allow(User).to receive(:current).and_return current_user
|
||
|
member = FactoryGirl.build(:member, user: current_user, project: project)
|
||
|
member.role_ids = [save_queries_role.id]
|
||
|
member.save!
|
||
|
patch star_path
|
||
|
end
|
||
|
|
||
|
context 'starring his own query' do
|
||
|
|
||
|
it 'should respond with 200' do
|
||
10 years ago
|
expect(last_response.status).to eq(200)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query in HAL+JSON format' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response).to eq(expected_response)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query with "isStarred" property set to true' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response['isStarred']).to eq('true')
|
||
11 years ago
|
end
|
||
|
end
|
||
|
|
||
|
context 'trying to star somebody else\'s query' do
|
||
|
let(:another_user) { FactoryGirl.create(:user) }
|
||
|
let(:query) { FactoryGirl.create(:private_query, project: project, user: another_user) }
|
||
|
|
||
|
it 'should respond with 403' do
|
||
10 years ago
|
expect(last_response.status).to eq(403)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should respond with explanatory error message' do
|
||
|
parsed_errors = JSON.parse(last_response.body)['errors']
|
||
10 years ago
|
expect(parsed_errors).to eq([{ 'key' => 'not_authorized', 'messages' => ['You are not authorize to access this resource']}])
|
||
11 years ago
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'user without permission to save queries' do
|
||
|
let(:query) { FactoryGirl.create(:private_query, project: project, user: current_user) }
|
||
|
before(:each) do
|
||
|
allow(User).to receive(:current).and_return current_user
|
||
|
member = FactoryGirl.build(:member, user: current_user, project: project)
|
||
|
member.role_ids = [role_without_query_permissions.id]
|
||
|
member.save!
|
||
|
patch star_path
|
||
|
end
|
||
|
|
||
|
it 'should respond with 403' do
|
||
10 years ago
|
expect(last_response.status).to eq(403)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should respond with explanatory error message' do
|
||
|
parsed_errors = JSON.parse(last_response.body)['errors']
|
||
10 years ago
|
expect(parsed_errors).to eq([{ 'key' => 'not_authorized', 'messages' => ['You are not authorize to access this resource']}])
|
||
11 years ago
|
end
|
||
|
end
|
||
11 years ago
|
|
||
|
end
|
||
11 years ago
|
end
|
||
|
|
||
11 years ago
|
describe '#unstar' do
|
||
11 years ago
|
let(:unstar_path) { "/api/v3/queries/#{query.id}/unstar" }
|
||
|
let(:filters) do
|
||
|
query.filters.map{ |f| {f.field.to_s => { "operator" => f.operator, "values" => f.values }}}
|
||
|
end
|
||
|
let(:expected_response) do
|
||
|
{
|
||
|
"_type" => 'Query',
|
||
|
"_links" => {
|
||
|
"self" => {
|
||
10 years ago
|
"href" => "/api/v3/queries/#{query.id}",
|
||
11 years ago
|
"title" => query.name
|
||
|
}
|
||
|
},
|
||
|
"id" => query.id,
|
||
|
"name" => query.name,
|
||
|
"projectId" => query.project_id,
|
||
|
"projectName" => query.project.name,
|
||
|
"userId" => query.user_id,
|
||
|
"userName" => query.user.try(:name),
|
||
|
"userLogin" => query.user.try(:login),
|
||
|
"userMail" => query.user.try(:mail),
|
||
|
"filters" => filters,
|
||
|
"isPublic" => query.is_public.to_s,
|
||
|
"columnNames" => query.column_names,
|
||
|
"sortCriteria" => query.sort_criteria,
|
||
|
"groupBy" => query.group_by,
|
||
|
"displaySums" => query.display_sums.to_s,
|
||
|
"isStarred" => "true"
|
||
|
}
|
||
|
end
|
||
|
|
||
|
describe 'public queries' do
|
||
|
let(:query) { FactoryGirl.create(:public_query, project: project) }
|
||
|
|
||
|
context 'user with permission to manage public queries' do
|
||
|
before(:each) do
|
||
|
allow(User).to receive(:current).and_return current_user
|
||
|
member = FactoryGirl.build(:member, user: current_user, project: project)
|
||
|
member.role_ids = [manage_public_queries_role.id]
|
||
|
member.save!
|
||
|
end
|
||
|
|
||
|
context 'when unstarring a starred query' do
|
||
|
before(:each) do
|
||
|
FactoryGirl.create(:query_menu_item, query: query)
|
||
|
patch unstar_path
|
||
|
end
|
||
|
|
||
|
it 'should respond with 200' do
|
||
10 years ago
|
expect(last_response.status).to eq(200)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query in HAL+JSON format' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response).to eq(expected_response.tap{ |r| r["isStarred"] = "false" })
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query with "isStarred" property set to false' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response['isStarred']).to eq('false')
|
||
11 years ago
|
end
|
||
|
end
|
||
|
|
||
|
context 'when unstarring an unstarred query' do
|
||
|
before(:each) { patch unstar_path }
|
||
|
|
||
|
it 'should respond with 200' do
|
||
10 years ago
|
expect(last_response.status).to eq(200)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query in HAL+JSON format' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response).to eq(expected_response.tap{ |r| r["isStarred"] = "false" })
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query with "isStarred" property set to true' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response['isStarred']).to eq('false')
|
||
11 years ago
|
end
|
||
|
|
||
|
end
|
||
|
|
||
|
context 'when trying to unstar nonexistent query' do
|
||
|
let(:unstar_path) { "/api/v3/queries/999/unstar" }
|
||
|
before(:each) { patch unstar_path }
|
||
|
|
||
|
it 'should respond with 404' do
|
||
10 years ago
|
expect(last_response.status).to eq(404)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should respond with explanatory error message' do
|
||
|
parsed_errors = JSON.parse(last_response.body)['errors']
|
||
10 years ago
|
expect(parsed_errors).to eq([{ 'key' => 'not_found', 'messages' => ['Couldn\'t find Query with id=999']}])
|
||
11 years ago
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'user without permission to manage public queries' do
|
||
|
before(:each) do
|
||
|
allow(User).to receive(:current).and_return current_user
|
||
|
member = FactoryGirl.build(:member, user: current_user, project: project)
|
||
|
member.role_ids = [role_without_query_permissions.id]
|
||
|
member.save!
|
||
|
patch unstar_path
|
||
|
end
|
||
|
|
||
|
it 'should respond with 403' do
|
||
10 years ago
|
expect(last_response.status).to eq(403)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should respond with explanatory error message' do
|
||
|
parsed_errors = JSON.parse(last_response.body)['errors']
|
||
10 years ago
|
expect(parsed_errors).to eq([{ 'key' => 'not_authorized', 'messages' => ['You are not authorize to access this resource']}])
|
||
11 years ago
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
describe 'private queries' do
|
||
|
context 'user with permission to save queries' do
|
||
|
let(:query) { FactoryGirl.create(:private_query, project: project, user: current_user) }
|
||
|
before(:each) do
|
||
|
allow(User).to receive(:current).and_return current_user
|
||
|
member = FactoryGirl.build(:member, user: current_user, project: project)
|
||
|
member.role_ids = [save_queries_role.id]
|
||
|
member.save!
|
||
|
patch unstar_path
|
||
|
end
|
||
|
|
||
|
context 'unstarring his own query' do
|
||
|
|
||
|
it 'should respond with 200' do
|
||
10 years ago
|
expect(last_response.status).to eq(200)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query in HAL+JSON format' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response).to eq(expected_response.tap{ |r| r["isStarred"] = "false" })
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should return the query with "isStarred" property set to true' do
|
||
|
parsed_response = JSON.parse(last_response.body)
|
||
10 years ago
|
expect(parsed_response['isStarred']).to eq('false')
|
||
11 years ago
|
end
|
||
|
end
|
||
|
|
||
|
context 'trying to unstar somebody else\'s query' do
|
||
|
let(:another_user) { FactoryGirl.create(:user) }
|
||
|
let(:query) { FactoryGirl.create(:private_query, project: project, user: another_user) }
|
||
|
|
||
|
it 'should respond with 403' do
|
||
10 years ago
|
expect(last_response.status).to eq(403)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should respond with explanatory error message' do
|
||
|
parsed_errors = JSON.parse(last_response.body)['errors']
|
||
10 years ago
|
expect(parsed_errors).to eq([{ 'key' => 'not_authorized', 'messages' => ['You are not authorize to access this resource']}])
|
||
11 years ago
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'user without permission to save queries' do
|
||
|
let(:query) { FactoryGirl.create(:private_query, project: project, user: current_user) }
|
||
|
before(:each) do
|
||
|
allow(User).to receive(:current).and_return current_user
|
||
|
member = FactoryGirl.build(:member, user: current_user, project: project)
|
||
|
member.role_ids = [role_without_query_permissions.id]
|
||
|
member.save!
|
||
|
patch unstar_path
|
||
|
end
|
||
|
|
||
|
it 'should respond with 403' do
|
||
10 years ago
|
expect(last_response.status).to eq(403)
|
||
11 years ago
|
end
|
||
|
|
||
|
it 'should respond with explanatory error message' do
|
||
|
parsed_errors = JSON.parse(last_response.body)['errors']
|
||
10 years ago
|
expect(parsed_errors).to eq([{ 'key' => 'not_authorized', 'messages' => ['You are not authorize to access this resource']}])
|
||
11 years ago
|
end
|
||
|
end
|
||
|
end
|
||
11 years ago
|
|
||
|
end
|
||
|
end
|