Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Review feedback

Oliver Günther1ecee7fb09a3
diff --git a/modules/bcf/app/representers/bcf/api/v2_1/project_extensions/definitions.rb b/modules/bcf/app/representers/bcf/api/v2_1/project_extensions/definitions.rb
index 649f06ce52c..a0ae1ec56aa 100644
--- a/modules/bcf/app/representers/bcf/api/v2_1/project_extensions/definitions.rb
+++ b/modules/bcf/app/representers/bcf/api/v2_1/project_extensions/definitions.rb
@@ -34,31 +34,28 @@ module Bcf::API::V2_1
def initialize(project:, user:)
@project = project
@user = user
- @allowed_service = ::Authorization::UserAllowedService.new(user)
end
def topic_type
- OpenProject::Cache.fetch(project, :topic_type) do
- project.types.pluck(:name)
- end
+ project.types.pluck(:name)
end
- # TODO: This returns all statuses regardless of workflow
def topic_status
- OpenProject::Cache.fetch(Status.all.cache_key) do
- Status.all.pluck(:name)
- end
+ Type
+ .statuses(project.types.pluck(:id))
+ .or(Status.where_default)
+ .pluck(:name)
end
def priority
- OpenProject::Cache.fetch(IssuePriority.all.cache_key) do
+ OpenProject::Cache.fetch(IssuePriority.all.cache_key, 'names') do
IssuePriority.all.pluck(:name)
end
end
def user_id_type
if allowed?(:view_members)
- project.users.pluck(:mail)
+ project.possible_assignees.pluck(:mail)
else
[]
end
@@ -88,26 +85,22 @@ module Bcf::API::V2_1
def topic_actions
if allowed?(:manage_bcf)
- %w[update updateRelatedTopics updateFiles createComment createViewpoint]
+ %w[update updateRelatedTopics updateFiles createViewpoint]
else
[]
end
end
def comment_actions
- if allowed?(:manage_bcf)
- %w[update]
- else
- []
- end
+ []
end
private
- attr_reader :project, :user, :allowed_service
+ attr_reader :project, :user
def allowed?(permission)
- allowed_service.call(permission, project).result
+ user.allowed_to?(permission, project)
end
end
end
diff --git a/modules/bcf/spec/representers/bcf/api/v2_1/project_extensions/definitions_spec.rb b/modules/bcf/spec/representers/bcf/api/v2_1/project_extensions/definitions_spec.rb
new file mode 100644
index 00000000000..1881c54ba42
--- /dev/null
+++ b/modules/bcf/spec/representers/bcf/api/v2_1/project_extensions/definitions_spec.rb
@@ -0,0 +1,135 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2019 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+require_relative '../shared_examples'
+
+describe Bcf::API::V2_1::ProjectExtensions::Definitions, 'rendering' do
+ shared_let(:type_task) { FactoryBot.create :type_task, name: 'My BCF type' }
+ shared_let(:project) { FactoryBot.create(:project, types: [type_task]) }
+ let(:user) { FactoryBot.build_stubbed(:user) }
+ let(:instance) { described_class.new(project: project, user: user) }
+
+ describe '#topic_type' do
+ subject { instance.topic_type }
+
+ it 'returns the project type names' do
+ expect(subject).to eq ['My BCF type']
+ end
+ end
+
+ describe '#topic_status' do
+ let!(:status) { FactoryBot.create :default_status }
+ subject { instance.topic_status }
+
+ it 'returns statuses for the available types' do
+ expect(Type).to receive(:statuses).with([type_task.id]).and_call_original
+ expect(subject).to eq [status.name]
+ end
+ end
+
+ describe '#priority' do
+ let!(:priority) { FactoryBot.create :default_priority }
+ subject { instance.priority }
+
+ it 'returns statuses for the available types' do
+ expect(subject).to eq [priority.name]
+ end
+ end
+
+ describe '#user_id_type' do
+ let!(:other_user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: [:view_work_packages])
+ end
+ subject { instance.user_id_type }
+
+ before do
+ allow(user)
+ .to receive(:allowed_to?).with(:view_members, project)
+ .and_return is_permitted
+ end
+
+ context 'with permissions' do
+ let(:is_permitted) { true }
+
+ it 'returns the user as assignee' do
+ expect(subject).to eq [other_user.mail]
+ end
+ end
+
+ context 'with no permissions' do
+ let(:is_permitted) { false }
+
+ it 'returns nothing' do
+ expect(subject).to eq []
+ end
+ end
+ end
+
+ describe '#project_actions' do
+ subject { instance.project_actions }
+
+ it 'includes nothing if not permitted' do
+ allow(user).to receive(:allowed_to?).and_return false
+ expect(subject).to be_empty
+ end
+
+ it 'includes `update` if edit_project permission' do
+ allow(user).to receive(:allowed_to?).and_return false
+ allow(user).to receive(:allowed_to?).with(:edit_project, project).and_return true
+
+ expect(subject).to include 'update'
+ end
+
+ it 'includes `createTopic` if edit_project permission' do
+ allow(user).to receive(:allowed_to?).and_return false
+ allow(user).to receive(:allowed_to?).with(:manage_bcf, project).and_return true
+
+ expect(subject).to include 'createTopic'
+ end
+ end
+
+ describe '#topic_actions' do
+ subject { instance.topic_actions }
+
+ it 'includes nothing if not permitted' do
+ allow(user).to receive(:allowed_to?).and_return false
+ expect(subject).to be_empty
+ end
+
+ it 'includes `update` if manage_bcf permission' do
+ allow(user).to receive(:allowed_to?).and_return false
+ allow(user).to receive(:allowed_to?).with(:manage_bcf, project).and_return true
+
+ expect(subject).to match_array %w[update updateRelatedTopics updateFiles createViewpoint]
+ end
+ end
+end
diff --git a/modules/bcf/spec/requests/api/bcf/v2_1/project_extensions_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/project_extensions_api_spec.rb
index 603fad1085a..263aa9e00e2 100644
--- a/modules/bcf/spec/requests/api/bcf/v2_1/project_extensions_api_spec.rb
+++ b/modules/bcf/spec/requests/api/bcf/v2_1/project_extensions_api_spec.rb
@@ -35,8 +35,9 @@ describe 'BCF 2.1 project extensions resource', type: :request, content_type: :j
include Rack::Test::Methods
shared_let(:type_task) { FactoryBot.create :type_task }
shared_let(:status) { FactoryBot.create :default_status }
+ shared_let(:priority) { FactoryBot.create :default_priority }
shared_let(:project) { FactoryBot.create(:project, enabled_module_names: [:bcf], types: [type_task]) }
- subject(:response) { JSON.parse(last_response.body) }
+ subject(:response) { last_response }
let(:path) { "/api/bcf/2.1/projects/#{project.id}/extensions" }
@@ -52,15 +53,21 @@ describe 'BCF 2.1 project extensions resource', type: :request, content_type: :j
get path
end
- it 'outputs read-only data', :aggregate_failures do
- expect(response['topic_type']).to include type_task.name
- expect(response['topic_status']).to include status.name
-
- expect(response['user_id_type']).to be_empty
-
- expect(response['project_actions']).to be_empty
- expect(response['topic_actions']).to be_empty
- expect(response['comment_actions']).to be_empty
+ it_behaves_like 'bcf api successful response' do
+ let(:expected_body) do
+ {
+ topic_type: [type_task.name],
+ topic_status: [status.name],
+ priority: [priority.name],
+ snippet_type: [],
+ stage: [],
+ topic_label: [],
+ user_id_type: [],
+ project_actions: [],
+ topic_actions: [],
+ comment_actions: []
+ }
+ end
end
end
@@ -83,17 +90,27 @@ describe 'BCF 2.1 project extensions resource', type: :request, content_type: :j
get path
end
- it 'outputs all actions' do
- expect(response['topic_type']).to include type_task.name
- expect(response['topic_status']).to include status.name
+ it_behaves_like 'bcf api successful response expectation' do
+ let(:expectations) do
+ ->(body) {
+ hash = JSON.parse(body)
+
+ expect(hash.keys).to match_array %w[
+ topic_type topic_status user_id_type project_actions topic_actions comment_actions
+ stage snippet_type priority topic_label
+ ]
+
+ expect(hash['topic_type']).to include type_task.name
+ expect(hash['topic_status']).to include status.name
- expect(response['user_id_type']).to include(current_user.mail)
- expect(response['user_id_type']).to include(other_user.mail)
+ expect(hash['user_id_type']).to include(other_user.mail, current_user.mail)
- expect(response['project_actions']).to eq %w[update createTopic]
+ expect(hash['project_actions']).to eq %w[update createTopic]
- expect(response['topic_actions']).to eq %w[update updateRelatedTopics updateFiles createComment createViewpoint]
- expect(response['comment_actions']).to eq %w[update]
+ expect(hash['topic_actions']).to eq %w[update updateRelatedTopics updateFiles createViewpoint]
+ expect(hash['comment_actions']).to eq []
+ }
+ end
end
end
end
diff --git a/modules/bcf/spec/requests/api/bcf/v2_1/shared_responses.rb b/modules/bcf/spec/requests/api/bcf/v2_1/shared_responses.rb
index bf22d087bc7..8c4f1d1214e 100644
--- a/modules/bcf/spec/requests/api/bcf/v2_1/shared_responses.rb
+++ b/modules/bcf/spec/requests/api/bcf/v2_1/shared_responses.rb
@@ -27,81 +27,55 @@
#++
shared_examples_for 'bcf api successful response' do
- it 'responds 200 OK' do
- expect(subject.status)
- .to eql 200
+ it 'responds correctly with the expected body', :aggregate_failures do
+ expect(subject.status).to eq 200
+ expect(subject.body).to be_json_eql(expected_body.to_json)
+ expect(subject.headers['Content-Type']).to eql 'application/json; charset=utf-8'
end
+end
- it 'returns the resource' do
- expect(subject.body)
- .to be_json_eql(expected_body.to_json)
- end
+shared_examples_for 'bcf api successful response expectation' do
+ it 'responds correctly with the expected body', :aggregate_failures do
+ expect(subject.status).to eq 200
+
+ instance_exec(subject.body, &expectations)
- it 'is has a json content type header' do
- expect(subject.headers['Content-Type'])
- .to eql 'application/json; charset=utf-8'
+ expect(subject.headers['Content-Type']).to eql 'application/json; charset=utf-8'
end
end
shared_examples_for 'bcf api not found response' do
- it 'responds 404 NOT FOUND' do
- expect(subject.status)
- .to eql 404
- end
-
- it 'states a NOT FOUND message' do
- expected = {
- message: 'The requested resource could not be found.'
- }
-
- expect(subject.body)
- .to be_json_eql(expected.to_json)
+ let(:expect_404) do
+ { message: 'The requested resource could not be found.' }
end
- it 'is has a json content type header' do
- expect(subject.headers['Content-Type'])
- .to eql 'application/json; charset=utf-8'
+ it 'responds 404 NOT FOUND', :aggregate_failures do
+ expect(subject.status).to eq 404
+ expect(subject.body).to be_json_eql(expect_404.to_json)
+ expect(subject.headers['Content-Type']).to eql 'application/json; charset=utf-8'
end
end
shared_examples_for 'bcf api not allowed response' do
- it 'responds 403 NOT ALLOWED' do
- expect(subject.status)
- .to eql 403
+ let(:expect_403) do
+ { message: 'You are not authorized to access this resource.' }
end
- it 'states a NOT ALLOWED message' do
- expected = {
- message: 'You are not authorized to access this resource.'
- }
-
- expect(subject.body)
- .to be_json_eql(expected.to_json)
- end
-
- it 'is has a json content type header' do
- expect(subject.headers['Content-Type'])
- .to eql 'application/json; charset=utf-8'
+ it 'responds 403 NOT ALLOWED', :aggregate_failures do
+ expect(subject.status).to eq 403
+ expect(subject.body).to be_json_eql(expect_403.to_json)
+ expect(subject.headers['Content-Type']).to eql 'application/json; charset=utf-8'
end
end
shared_examples_for 'bcf api unprocessable response' do
- it 'responds 403 NOT ALLOWED' do
- expect(subject.status)
- .to eql 422
- end
-
- it 'states a reason message' do
- expected = {
- message: message
- }
-
- expect(subject.body)
- .to be_json_eql(expected.to_json)
+ let(:expect_422) do
+ { message: message }
end
- it 'is has a json content type header' do
- expect(subject.headers['Content-Type'])
- .to eql 'application/json; charset=utf-8'
+ it 'responds 422 UNPROCESSABLE ENTITY', :aggregate_failures do
+ expect(subject.status).to eq 422
+ expect(subject.body).to be_json_eql(expect_403.to_json)
+ expect(subject.headers['Content-Type']).to eql 'application/json; charset=utf-8'
end
end