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

merge two request spec files

ulfertsa929ad8fa628
diff --git a/spec/requests/api/v3/activities_api_spec.rb b/spec/requests/api/v3/activities_api_spec.rb
index 2f31c1a3658..dd586da22b0 100644
--- a/spec/requests/api/v3/activities_api_spec.rb
+++ b/spec/requests/api/v3/activities_api_spec.rb
@@ -29,73 +29,115 @@
require 'spec_helper'
require 'rack/test'
-describe API::V3::Activities::ActivitiesAPI, type: :request do
+describe API::V3::Activities::ActivitiesAPI, type: :request, content_type: :json do
include Rack::Test::Methods
include API::V3::Utilities::PathHelper
- using_shared_fixtures :admin
-
- let(:comment) { 'This is a test comment!' }
-
- shared_examples_for 'safeguarded API' do
- it { expect(last_response.status).to eq(403) }
+ let(:current_user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: permissions)
end
+ let(:project) { FactoryBot.create(:project, public: false) }
+ let(:work_package) { FactoryBot.create(:work_package, author: current_user, project: project) }
+ let(:permissions) { %i[view_work_packages edit_work_package_notes] }
+ let(:activity) { work_package.journals.first }
+ let(:comment) { 'This is a new test comment!' }
+
+ shared_examples_for 'valid activity request' do |type|
+ subject { last_response }
+
+ it 'returns an activity of the correct type' do
+ expect(subject.body).to be_json_eql(type.to_json).at_path('_type')
+ expect(subject.body).to be_json_eql(activity.id.to_json).at_path('id')
+ end
- shared_examples_for 'valid activity request' do
- before do allow(User).to receive(:current).and_return(admin) end
-
- subject { last_response.body }
-
- it { is_expected.to be_json_eql('Activity::Comment'.to_json).at_path('_type') }
-
- it { is_expected.to be_json_eql(comment.to_json).at_path('comment/raw') }
+ it 'responds 200 OK' do
+ expect(subject.status).to eq(200)
+ end
end
- shared_examples_for 'invalid activity request' do |message|
- before do allow(User).to receive(:current).and_return(admin) end
+ shared_examples_for 'valid activity patch request' do
+ it 'updates the activity comment' do
+ expect(last_response.body).to be_json_eql(comment.to_json).at_path('comment/raw')
+ end
- it_behaves_like 'constraint violation' do
- let(:message) { message }
+ it 'changes the comment' do
+ expect(activity.reload.notes).to eql comment
end
end
describe 'PATCH /api/v3/activities/:activityId' do
- let(:work_package) { FactoryBot.create(:work_package) }
- let(:wp_journal) { FactoryBot.build(:journal_work_package_journal) }
- let(:journal) {
- FactoryBot.create(:work_package_journal,
- data: wp_journal,
- journable_id: work_package.id)
- }
-
- shared_context 'edit activity' do
- before {
- patch api_v3_paths.activity(journal.id),
- { comment: comment }.to_json, 'CONTENT_TYPE' => 'application/json'
- }
+ let(:params) { { comment: comment } }
+ before do
+ login_as(current_user)
+ patch api_v3_paths.activity(activity.id), params.to_json
end
- it_behaves_like 'safeguarded API' do
- include_context 'edit activity'
- end
+ it_behaves_like 'valid activity request', 'Activity::Comment'
- it_behaves_like 'valid activity request' do
- include_context 'edit activity'
- end
+ it_behaves_like 'valid activity patch request'
it_behaves_like 'invalid activity request', 'Version is invalid' do
- let(:errors) {
- ActiveModel::Errors.new(journal).tap do |e|
+ let(:errors) do
+ ActiveModel::Errors.new(work_package.journals.first).tap do |e|
e.add(:version)
end
- }
-
- before do
+ end
+ let(:activity) do
allow_any_instance_of(Journal).to receive(:save).and_return(false)
allow_any_instance_of(Journal).to receive(:errors).and_return(errors)
+
+ work_package.journals.first
end
- include_context 'edit activity'
+ it_behaves_like 'constraint violation' do
+ let(:message) { 'Version is invalid' }
+ end
+ end
+
+ context 'without sufficient permissions' do
+ let(:permissions) { [:view_work_packages] }
+
+ it_behaves_like 'unauthorized access'
+ end
+ end
+
+ describe '#get api' do
+ let(:get_path) { api_v3_paths.activity activity.id }
+
+ before do
+ login_as(current_user)
+ end
+
+ context 'logged in user' do
+ before do
+ get get_path
+ end
+
+ it_behaves_like 'valid activity request', 'Activity'
+
+ context 'requesting nonexistent activity' do
+ let(:get_path) { api_v3_paths.activity 9999 }
+
+ it_behaves_like 'not found' do
+ let(:id) { 9999 }
+ let(:type) { 'Journal' }
+ end
+ end
+
+ context 'without sufficient permissions' do
+ let(:permissions) { [] }
+
+ it_behaves_like 'unauthorized access'
+ end
+ end
+
+ context 'anonymous user' do
+ it_behaves_like 'handling anonymous user' do
+ let(:project) { FactoryBot.create(:project, public: true) }
+ let(:path) { get_path }
+ end
end
end
end
diff --git a/spec/requests/api/v3/activity_resource_spec.rb b/spec/requests/api/v3/activity_resource_spec.rb
deleted file mode 100644
index 35c50221f16..00000000000
--- a/spec/requests/api/v3/activity_resource_spec.rb
+++ /dev/null
@@ -1,123 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2020 the OpenProject GmbH
-#
-# 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 'rack/test'
-
-describe 'API v3 Activity resource', type: :request do
- include Rack::Test::Methods
- include API::V3::Utilities::PathHelper
-
- let(:current_user) {
- FactoryBot.create(:user, member_in_project: project, member_through_role: role)
- }
- let(:project) { FactoryBot.create(:project, public: false) }
- let(:work_package) { FactoryBot.create(:work_package, author: current_user, project: project) }
- let(:role) { FactoryBot.create(:role, permissions: permissions) }
- let(:permissions) { [:view_work_packages, :edit_work_package_notes] }
- let(:activity) { work_package.journals.first }
-
- before do
- allow(User).to receive(:current).and_return current_user
- work_package.save!
- end
-
- describe '#get' do
- subject(:response) { last_response }
-
- context 'logged in user' do
- let(:get_path) { api_v3_paths.activity activity.id }
- before do
- get get_path
- end
-
- it 'should respond with 200' do
- expect(subject.status).to eq(200)
- end
-
- it 'should respond with correct activity' do
- expect(subject.body).to include_json('Activity'.to_json).at_path('_type')
- expect(subject.body).to be_json_eql(activity.id.to_json).at_path('id')
- end
-
- context 'requesting nonexistent activity' do
- let(:get_path) { api_v3_paths.activity 9999 }
-
- it_behaves_like 'not found' do
- let(:id) { 9999 }
- let(:type) { 'Journal' }
- end
- end
-
- context 'requesting activity without sufficient permissions' do
- let(:another_project) { FactoryBot.create(:project, public: false) }
- let(:another_work_package) { FactoryBot.create(:work_package, project: another_project) }
- let(:another_activity) { FactoryBot.create(:work_package_journal, journable: another_work_package) }
- let(:get_path) { api_v3_paths.activity another_activity.id }
-
- it_behaves_like 'unauthorized access'
- end
- end
-
- it_behaves_like 'handling anonymous user' do
- let(:project) { FactoryBot.create(:project, public: true) }
- let(:path) { api_v3_paths.activity activity.id }
- end
- end
-
- describe '#patch' do
- subject(:response) { last_response }
- let(:patch_path) { api_v3_paths.activity activity.id }
- let(:valid_params) {
- {
- comment: 'a fancy comment!'
- }
- }
-
- context 'authorized user' do
- before do
- patch patch_path, valid_params.to_json, 'CONTENT_TYPE' => 'application/json'
- end
-
- subject(:response) { last_response }
-
- it 'should respond with HTTP OK' do
- expect(subject.status).to eq(200)
- end
-
- it 'changes the comment' do
- activity.reload
- expect(activity.notes).to eql 'a fancy comment!'
- end
-
- it 'responds with the updated activity' do
- expect(subject.body).to be_json_eql('a fancy comment!'.to_json).at_path('comment/raw')
- end
- end
- end
-end