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

Remove mocking from Cost WP Spec. It created permissions anyways

Klaus Zanders1072973519ed
diff --git a/modules/costs/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/modules/costs/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
index 0f44943fe5f..0dc688c4dc8 100644
--- a/modules/costs/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
+++ b/modules/costs/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
@@ -31,17 +31,17 @@ require 'spec_helper'
RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
include API::V3::Utilities::PathHelper
- let(:project) { create(:project) }
- let(:role) do
- create(:project_role,
- permissions: %i[view_time_entries
- view_cost_entries
- view_cost_rates
- view_work_packages])
+ let(:additional_permissions) do
+ %i[
+ view_time_entries
+ view_cost_entries
+ view_cost_rates
+ ]
end
+
+ let(:project) { create(:project) }
let(:user) do
- create(:user,
- member_with_roles: { project => role })
+ create(:user, member_with_permissions: { project => [:view_work_packages] + additional_permissions })
end
let(:cost_entry_1) do
@@ -130,22 +130,14 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
context 'no view_time_entries permission' do
- before do
- allow(user).to receive(:allowed_to?).and_return false
- end
+ let(:additional_permissions) { [] }
it { is_expected.not_to have_json_path('spentTime') }
end
context 'only view_own_time_entries permission' do
- let(:own_time_entries_role) do
- create(:project_role, permissions: %i[view_own_time_entries
- view_work_packages])
- end
-
let(:user2) do
- create(:user,
- member_with_roles: { project => own_time_entries_role })
+ create(:user, member_with_permissions: { project => %i[view_own_time_entries view_work_packages] })
end
let!(:own_time_entry) do
@@ -177,26 +169,12 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
describe 'laborCosts' do
- before do
- allow(user).to receive(:allowed_to?).and_return false
- end
-
before do
allow(work_package).to receive(:labor_costs).and_return(6000.0)
end
context 'with the :view_hourly_rates and :view_time_entries permission' do
- before do
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_time_entries, work_package.project)
- .and_return true
-
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_hourly_rates, work_package.project)
- .and_return true
- end
+ let(:additional_permissions) { %i[view_hourly_rates view_time_entries] }
it 'is expected to have a laborCosts attribute' do
expect(subject).to be_json_eql('6,000.00 EUR'.to_json).at_path('laborCosts')
@@ -204,17 +182,7 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
context 'with the :view_own_hourly_rate and :view_own_time_entries permission' do
- before do
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_own_time_entries, work_package.project)
- .and_return true
-
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_own_hourly_rate, work_package.project)
- .and_return true
- end
+ let(:additional_permissions) { %i[view_own_hourly_rate view_own_time_entries] }
it 'is expected to have a laborCosts attribute' do
expect(subject).to be_json_eql('6,000.00 EUR'.to_json).at_path('laborCosts')
@@ -229,26 +197,12 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
describe 'materialCosts' do
- before do
- allow(user).to receive(:allowed_to?).and_return false
- end
-
before do
allow(work_package).to receive(:material_costs).and_return(6000.0)
end
context 'with the :view_own_cost_entries and :view_cost_rates permission' do
- before do
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_own_cost_entries, work_package.project)
- .and_return true
-
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_cost_rates, work_package.project)
- .and_return true
- end
+ let(:additional_permissions) { %i[view_own_cost_entries view_cost_rates] }
it 'is expected to have a materialCosts attribute' do
expect(subject).to be_json_eql('6,000.00 EUR'.to_json).at_path('materialCosts')
@@ -256,17 +210,7 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
context 'with the :view_cost_entries and :view_cost_rates permission' do
- before do
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_cost_entries, work_package.project)
- .and_return true
-
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_cost_rates, work_package.project)
- .and_return true
- end
+ let(:additional_permissions) { %i[view_cost_entries view_cost_rates] }
it 'is expected to have a materialCosts attribute' do
expect(subject).to be_json_eql('6,000.00 EUR'.to_json).at_path('materialCosts')
@@ -274,6 +218,8 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
context 'without the user having permission' do
+ let(:additional_permissions) { [] }
+
it 'has no attribute' do
expect(subject).not_to have_json_path('materialCosts')
end
@@ -281,26 +227,12 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
describe 'overallCosts' do
- before do
- allow(user).to receive(:allowed_to?).and_return false
- end
-
before do
allow(work_package).to receive(:overall_costs).and_return(6000.0)
end
context 'with the :view_hourly_rates and :view_time_entries permission' do
- before do
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_time_entries, work_package.project)
- .and_return true
-
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_hourly_rates, work_package.project)
- .and_return true
- end
+ let(:additional_permissions) { %i[view_hourly_rates view_time_entries] }
it 'is expected to have a overallCosts attribute' do
expect(subject).to be_json_eql('6,000.00 EUR'.to_json).at_path('overallCosts')
@@ -308,17 +240,7 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
context 'with the :view_own_hourly_rate and :view_own_time_entries permission' do
- before do
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_own_time_entries, work_package.project)
- .and_return true
-
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_own_hourly_rate, work_package.project)
- .and_return true
- end
+ let(:additional_permissions) { %i[view_own_time_entries view_own_hourly_rate] }
it 'is expected to have a overallCosts attribute' do
expect(subject).to be_json_eql('6,000.00 EUR'.to_json).at_path('overallCosts')
@@ -326,17 +248,7 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
context 'with the :view_own_cost_entries and :view_cost_rates permission' do
- before do
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_own_cost_entries, work_package.project)
- .and_return true
-
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_cost_rates, work_package.project)
- .and_return true
- end
+ let(:additional_permissions) { %i[view_own_cost_entries view_cost_rates] }
it 'is expected to have a overallCosts attribute' do
expect(subject).to be_json_eql('6,000.00 EUR'.to_json).at_path('overallCosts')
@@ -344,17 +256,7 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
context 'with the :view_cost_entries and :view_cost_rates permission' do
- before do
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_cost_entries, work_package.project)
- .and_return true
-
- allow(user)
- .to receive(:allowed_to?)
- .with(:view_cost_rates, work_package.project)
- .and_return true
- end
+ let(:additional_permissions) { %i[view_cost_entries view_cost_rates] }
it 'is expected to have a overallCosts attribute' do
expect(subject).to be_json_eql('6,000.00 EUR'.to_json).at_path('overallCosts')
@@ -362,6 +264,8 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
context 'without the user having permission' do
+ let(:additional_permissions) { [] }
+
it 'has no attribute' do
expect(subject).not_to have_json_path('overallCosts')
end
@@ -381,22 +285,20 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
describe 'timeEntries' do
- it 'exists if user has view_time_entries permission' do
- allow(user).to receive(:allowed_to?).and_return false
- allow(user).to receive(:allowed_to?).with(:view_time_entries,
- work_package.project)
- .and_return true
+ context 'with the :view_time_entries permission' do
+ let(:additional_permissions) { %i[view_time_entries] }
- expect(subject).to have_json_path('_links/timeEntries/href')
+ it 'has timeEntries link' do
+ expect(subject).to have_json_path('_links/timeEntries/href')
+ end
end
- it 'has spentTime link when user only has view_own_time_entries permission' do
- allow(user).to receive(:allowed_to?).and_return false
- allow(user).to receive(:allowed_to?).with(:view_own_time_entries,
- work_package.project)
- .and_return true
+ context 'with the :view_own_time_entries permission' do
+ let(:additional_permissions) { %i[view_own_time_entries] }
- expect(subject).to have_json_path('_links/timeEntries/href')
+ it 'has timeEnries link' do
+ expect(subject).to have_json_path('_links/timeEntries/href')
+ end
end
end
end