From 4feb8f475411cee5f77e1aa78340a40e8af8df62 Mon Sep 17 00:00:00 2001 From: ulferts Date: Fri, 6 Mar 2020 07:07:37 +0100 Subject: [PATCH 1/2] bump factory_bot --- Gemfile | 4 ++-- Gemfile.lock | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index d8a87fe0ab..1daa7d772f 100644 --- a/Gemfile +++ b/Gemfile @@ -249,9 +249,9 @@ group :development, :test do gem 'thin', '~> 1.7.2' # Require factory_bot for usage with openproject plugins testing - gem 'factory_bot', '~> 4.8' + gem 'factory_bot', '~> 5.1.1' # require factory_bot_rails for convenience in core development - gem 'factory_bot_rails', '~> 4.8' + gem 'factory_bot_rails', '~> 5.1.1' # Tracing and profiling gems gem 'flamegraph', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 9b21de399a..e5b945abb0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -477,11 +477,11 @@ GEM eventmachine (1.2.7) excon (0.71.0) execjs (2.7.0) - factory_bot (4.11.1) - activesupport (>= 3.0.0) - factory_bot_rails (4.11.1) - factory_bot (~> 4.11.1) - railties (>= 3.0.0) + factory_bot (5.1.1) + activesupport (>= 4.2.0) + factory_bot_rails (5.1.1) + factory_bot (~> 5.1.0) + railties (>= 4.2.0) faker (1.9.1) i18n (>= 0.7) faraday (0.17.3) @@ -995,8 +995,8 @@ DEPENDENCIES doorkeeper (~> 5.3.1) equivalent-xml (~> 0.6) escape_utils (~> 1.0) - factory_bot (~> 4.8) - factory_bot_rails (~> 4.8) + factory_bot (~> 5.1.1) + factory_bot_rails (~> 5.1.1) faker flamegraph fog-aws From 7fa2abf5fa8ad7d56a7189070664a8b724552d7c Mon Sep 17 00:00:00 2001 From: ulferts Date: Fri, 6 Mar 2020 09:16:09 +0100 Subject: [PATCH 2/2] adapt factory usages --- .../work_packages/create_contract_spec.rb | 39 ++--- .../activities/activity_representer_spec.rb | 22 +-- .../work_package_representer_spec.rb | 16 +- .../bim/spec/factories/bcf_issue_factory.rb | 12 -- .../support/bcf_topic_with_stubbed_comment.rb | 15 ++ .../spec/factories/cost_entry_factory.rb | 13 +- .../spec/features/view_own_rates_spec.rb | 13 +- .../aggregated_cost_entry_representer_spec.rb | 2 +- .../cost_entry_representer_spec.rb | 4 +- .../spec/models/labor_budget_item_spec.rb | 2 +- .../costs/spec/models/user_deletion_spec.rb | 4 +- .../ask_before_destruction_spec.rb | 14 +- ...t_entries_by_work_package_resource_spec.rb | 13 +- .../cost_entries/cost_entry_resource_spec.rb | 13 +- spec/factories/work_package_factory.rb | 4 +- .../query_representer_generation_spec.rb | 17 +- .../create_form_representer_spec.rb | 18 +- .../create_project_form_representer_spec.rb | 96 +++-------- .../update_form_representer_spec.rb | 21 +-- .../work_package_representer_spec.rb | 155 +++++++----------- .../work_package_activity_provider_spec.rb | 41 +++-- spec/models/messages_spec.rb | 2 +- spec/models/relation_spec.rb | 29 ++-- .../ask_before_destruction_spec.rb | 46 +++--- spec/models/work_package/spent_time_spec.rb | 58 +++---- spec/support/user_with_stubbed_permissions.rb | 13 ++ 26 files changed, 301 insertions(+), 381 deletions(-) create mode 100644 modules/bim/spec/support/bcf_topic_with_stubbed_comment.rb create mode 100644 spec/support/user_with_stubbed_permissions.rb diff --git a/modules/backlogs/spec/contracts/work_packages/create_contract_spec.rb b/modules/backlogs/spec/contracts/work_packages/create_contract_spec.rb index e7f22c3425..e48c045ff5 100644 --- a/modules/backlogs/spec/contracts/work_packages/create_contract_spec.rb +++ b/modules/backlogs/spec/contracts/work_packages/create_contract_spec.rb @@ -30,41 +30,38 @@ require 'spec_helper' describe WorkPackages::CreateContract do let(:work_package) do - FactoryBot.build(:work_package, - author: member, - project: project) + WorkPackage.new FactoryBot.attributes_for(:stubbed_work_package, author: other_user, project: project) end - let(:member) { - FactoryBot.create(:user, - member_in_project: project, - member_through_role: role) - } - let (:project) { FactoryBot.create(:project) } - let(:current_user) { member } - let(:permissions) { - [ - :view_work_packages, - :add_work_packages + let(:other_user) { FactoryBot.build_stubbed(:user) } + include_context 'user with stubbed permissions' + let (:project) { FactoryBot.build_stubbed(:project) } + let(:permissions) do + %i[ + view_work_packages + add_work_packages ] - } - let(:role) { FactoryBot.create :role, permissions: permissions } + end let(:changed_values) { [] } - subject(:contract) { described_class.new(work_package, current_user) } + subject(:contract) { described_class.new(work_package, user) } before do allow(work_package).to receive(:changed).and_return(changed_values) end describe 'story points' do + before do + contract.validate + end + context 'has not changed' do - it('is valid') { expect(contract.errors.empty?).to be true } + it('is valid') { expect(contract.errors.symbols_for(:story_points)).to be_empty } end context 'has changed' do let(:changed_values) { ['story_points'] } - it('is valid') { expect(contract.errors.empty?).to be true } + it('is valid') { expect(contract.errors.symbols_for(:story_points)).to be_empty } end end @@ -75,13 +72,13 @@ describe WorkPackages::CreateContract do end context 'has not changed' do - it('is valid') { expect(contract.errors.empty?).to be true } + it('is valid') { expect(contract.errors.symbols_for(:remaining_hours)).to be_empty } end context 'has changed' do let(:changed_values) { ['remaining_hours'] } - it('is valid') { expect(contract.errors.empty?).to be true } + it('is valid') { expect(contract.errors.symbols_for(:remaining_hours)).to be_empty } end end end diff --git a/modules/bim/spec/api/v3/activities/activity_representer_spec.rb b/modules/bim/spec/api/v3/activities/activity_representer_spec.rb index 5355482959..67ec2dc9f9 100644 --- a/modules/bim/spec/api/v3/activities/activity_representer_spec.rb +++ b/modules/bim/spec/api/v3/activities/activity_representer_spec.rb @@ -27,19 +27,17 @@ #++ require 'spec_helper' +require_relative '../../../support/bcf_topic_with_stubbed_comment' describe ::API::V3::Activities::ActivityRepresenter do include API::Bim::Utilities::PathHelper - let(:current_user) do - FactoryBot.build_stubbed(:user).tap do |u| - allow(u) - .to receive(:allowed_to?) do |checked_permission, project| - project == work_package.project && permissions.include?(checked_permission) - end - end - end + include_context 'user with stubbed permissions' + include_context 'bcf_topic with stubbed comment' let(:other_user) { FactoryBot.build_stubbed(:user) } + let(:project) do + work_package.project + end let(:work_package) do journal.journable.tap do |wp| allow(wp) @@ -61,15 +59,11 @@ describe ::API::V3::Activities::ActivityRepresenter do end end let(:changes) { { subject: ["first subject", "second subject"] } } - let(:bcf_topic) do - FactoryBot.build_stubbed(:bcf_issue_with_comment) - end - let(:bcf_comment) { bcf_topic.comments.first } let(:permissions) { %i(edit_work_package_notes view_linked_issues) } - let(:representer) { described_class.new(journal, current_user: current_user) } + let(:representer) { described_class.new(journal, current_user: user) } before do - login_as(current_user) + login_as(user) end subject(:generated) { representer.to_json } diff --git a/modules/bim/spec/api/v3/work_packages/work_package_representer_spec.rb b/modules/bim/spec/api/v3/work_packages/work_package_representer_spec.rb index 5c44ffef05..737b787369 100644 --- a/modules/bim/spec/api/v3/work_packages/work_package_representer_spec.rb +++ b/modules/bim/spec/api/v3/work_packages/work_package_representer_spec.rb @@ -28,6 +28,8 @@ require 'spec_helper' +require_relative '../../../support/bcf_topic_with_stubbed_comment' + describe ::API::V3::WorkPackages::WorkPackageRepresenter do include API::V3::Utilities::PathHelper include API::Bim::Utilities::PathHelper @@ -35,19 +37,9 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do let(:project) do work_package.project end + include_context 'user with stubbed permissions' + include_context 'bcf_topic with stubbed comment' let(:permissions) { %i[view_linked_issues view_work_packages manage_bcf] } - let(:user) do - FactoryBot.build_stubbed(:user).tap do |u| - allow(u) - .to receive(:allowed_to?) do |queried_permissison, queried_project| - queried_project == work_package.project && - permissions.include?(queried_permissison) - end - end - end - let(:bcf_topic) do - FactoryBot.build_stubbed(:bcf_issue_with_comment) - end let(:work_package) do FactoryBot.build_stubbed(:stubbed_work_package, bcf_issue: bcf_topic) end diff --git a/modules/bim/spec/factories/bcf_issue_factory.rb b/modules/bim/spec/factories/bcf_issue_factory.rb index f0a4f10bf0..799ce49f1d 100644 --- a/modules/bim/spec/factories/bcf_issue_factory.rb +++ b/modules/bim/spec/factories/bcf_issue_factory.rb @@ -120,18 +120,6 @@ FactoryBot.define do end factory :bcf_issue_with_comment do - after(:stub) do |issue| - old_id = issue.id - issue.id = nil - - attachment = FactoryBot.build_stubbed(:attachment, description: 'snapshot') - viewpoint = build_stubbed(:bcf_viewpoint, issue: issue, attachments: [attachment]) - - issue.viewpoints = [viewpoint] - issue.comments = [build_stubbed(:bcf_comment, issue: issue, viewpoint: viewpoint)] - issue.id = old_id - end - after(:create) do |issue| viewpoint = create(:bcf_viewpoint, issue: issue) create(:bcf_comment, issue: issue, viewpoint: viewpoint) diff --git a/modules/bim/spec/support/bcf_topic_with_stubbed_comment.rb b/modules/bim/spec/support/bcf_topic_with_stubbed_comment.rb new file mode 100644 index 0000000000..59bcc0023c --- /dev/null +++ b/modules/bim/spec/support/bcf_topic_with_stubbed_comment.rb @@ -0,0 +1,15 @@ +shared_context 'bcf_topic with stubbed comment' do |attributes = {}| + let(:attachment) { FactoryBot.build_stubbed(:attachment, description: 'snapshot') } + let(:viewpoint) { FactoryBot.build_stubbed(:bcf_viewpoint, attachments: [attachment]) } + let(:bcf_comment) { FactoryBot.build_stubbed(:bcf_comment, viewpoint: viewpoint) } + let(:bcf_topic) do + FactoryBot.build_stubbed(:bcf_issue_with_comment, **attributes).tap do |issue| + allow(issue) + .to receive(:viewpoints) + .and_return([viewpoint]) + allow(issue) + .to receive(:comments) + .and_return([bcf_comment]) + end + end +end diff --git a/modules/costs/spec/factories/cost_entry_factory.rb b/modules/costs/spec/factories/cost_entry_factory.rb index 362d0abf6a..bbb267e727 100644 --- a/modules/costs/spec/factories/cost_entry_factory.rb +++ b/modules/costs/spec/factories/cost_entry_factory.rb @@ -27,15 +27,20 @@ #++ FactoryBot.define do - factory :cost_entry do + factory :cost_entry do project - user do FactoryBot.create(:user, member_in_project: project)end - work_package do FactoryBot.create(:work_package, project: project) end + user + work_package cost_type spent_on { Date.today } units { 1 } comments { '' } - created_on do Time.now end + created_on { Time.now } updated_on { Time.now } + + before(:create) do |ce| + ce.work_package.project = ce.project + ce.project.add_member!(ce.user, [FactoryBot.create(:role)]) unless ce.project.users.include?(ce.user) + end end end diff --git a/modules/costs/spec/features/view_own_rates_spec.rb b/modules/costs/spec/features/view_own_rates_spec.rb index 8f8d1dcc5a..9f2ebf6fd6 100644 --- a/modules/costs/spec/features/view_own_rates_spec.rb +++ b/modules/costs/spec/features/view_own_rates_spec.rb @@ -61,8 +61,8 @@ describe 'Only see your own rates', type: :feature, js: true do user: user } let(:other_role) { FactoryBot.create :role, permissions: [] } let(:other_user) { FactoryBot.create :user, - member_in_project: project, - member_through_role: other_role } + member_in_project: project, + member_through_role: other_role } let(:other_hourly_rate) { FactoryBot.create :default_hourly_rate, user: other_user, rate: 11.00 } let(:other_time_entry) { FactoryBot.create :time_entry, user: other_user, @@ -70,10 +70,10 @@ describe 'Only see your own rates', type: :feature, js: true do project: project, work_package: work_package } let(:other_cost_entry) { FactoryBot.create :cost_entry, work_package: work_package, - project: project, - units: 5.00, - user: other_user, - cost_type: cost_type } + project: project, + units: 5.00, + user: other_user, + cost_type: cost_type } before do login_as(user) @@ -83,6 +83,7 @@ describe 'Only see your own rates', type: :feature, js: true do time_entry cost_entry other_hourly_rate + other_user other_time_entry other_cost_entry diff --git a/modules/costs/spec/lib/api/v3/cost_entries/aggregated_cost_entry_representer_spec.rb b/modules/costs/spec/lib/api/v3/cost_entries/aggregated_cost_entry_representer_spec.rb index 65b5d66acf..c4d8e0d66e 100644 --- a/modules/costs/spec/lib/api/v3/cost_entries/aggregated_cost_entry_representer_spec.rb +++ b/modules/costs/spec/lib/api/v3/cost_entries/aggregated_cost_entry_representer_spec.rb @@ -31,7 +31,7 @@ require 'spec_helper' describe ::API::V3::CostEntries::AggregatedCostEntryRepresenter do include API::V3::Utilities::PathHelper - let(:cost_entry) { FactoryBot.build(:cost_entry, id: 42) } + let(:cost_entry) { FactoryBot.build_stubbed(:cost_entry) } let(:representer) { described_class.new(cost_entry.cost_type, cost_entry.units) } subject { representer.to_json } diff --git a/modules/costs/spec/lib/api/v3/cost_entries/cost_entry_representer_spec.rb b/modules/costs/spec/lib/api/v3/cost_entries/cost_entry_representer_spec.rb index 4262e1d87f..96091a310a 100644 --- a/modules/costs/spec/lib/api/v3/cost_entries/cost_entry_representer_spec.rb +++ b/modules/costs/spec/lib/api/v3/cost_entries/cost_entry_representer_spec.rb @@ -31,7 +31,7 @@ require 'spec_helper' describe ::API::V3::CostEntries::CostEntryRepresenter do include API::V3::Utilities::PathHelper - let(:cost_entry) { FactoryBot.build(:cost_entry, id: 42) } + let(:cost_entry) { FactoryBot.build_stubbed(:cost_entry) } let(:representer) { described_class.new(cost_entry, current_user: double('current_user')) } subject { representer.to_json } @@ -53,7 +53,7 @@ describe ::API::V3::CostEntries::CostEntryRepresenter do it_behaves_like 'has a titled link' do let(:link) { 'user' } - let(:href) { api_v3_paths.user cost_entry.user.id } + let(:href) { api_v3_paths.user cost_entry.user_id } let(:title) { cost_entry.user.name } end diff --git a/modules/costs/spec/models/labor_budget_item_spec.rb b/modules/costs/spec/models/labor_budget_item_spec.rb index bcbdea5e35..3beb38644b 100644 --- a/modules/costs/spec/models/labor_budget_item_spec.rb +++ b/modules/costs/spec/models/labor_budget_item_spec.rb @@ -30,7 +30,7 @@ require File.dirname(__FILE__) + '/../spec_helper' describe LaborBudgetItem, type: :model do include Cost::PluginSpecHelper - let(:item) { FactoryBot.build(:labor_budget_item, cost_object: cost_object) } + let(:item) { FactoryBot.build(:labor_budget_item, cost_object: cost_object, user: user) } let(:cost_object) { FactoryBot.build(:variable_cost_object, project: project) } let(:user) { FactoryBot.create(:user) } let(:user2) { FactoryBot.create(:user) } diff --git a/modules/costs/spec/models/user_deletion_spec.rb b/modules/costs/spec/models/user_deletion_spec.rb index 5fdb76790f..292293725c 100644 --- a/modules/costs/spec/models/user_deletion_spec.rb +++ b/modules/costs/spec/models/user_deletion_spec.rb @@ -154,7 +154,7 @@ describe User, '#destroy', type: :model do describe 'WHEN the user has a cost entry' do let(:work_package) { FactoryBot.create(:work_package) } let(:entry) { - FactoryBot.build(:cost_entry, user: user, + FactoryBot.create(:cost_entry, user: user, project: work_package.project, units: 100.0, spent_on: Date.today, @@ -166,7 +166,7 @@ describe User, '#destroy', type: :model do FactoryBot.create(:member, project: work_package.project, user: user, roles: [FactoryBot.build(:role)]) - entry.save! + entry user.destroy diff --git a/modules/costs/spec/models/work_package/ask_before_destruction_spec.rb b/modules/costs/spec/models/work_package/ask_before_destruction_spec.rb index 2a61d9ff5f..3beab8a2e2 100644 --- a/modules/costs/spec/models/work_package/ask_before_destruction_spec.rb +++ b/modules/costs/spec/models/work_package/ask_before_destruction_spec.rb @@ -57,12 +57,12 @@ describe WorkPackage, type: :model do let(:priority) { FactoryBot.create(:priority) } let(:cost_type) { FactoryBot.create(:cost_type) } let(:cost_entry) { - FactoryBot.build(:cost_entry, work_package: work_package, + FactoryBot.create(:cost_entry, work_package: work_package, project: work_package.project, cost_type: cost_type) } let(:cost_entry2) { - FactoryBot.build(:cost_entry, work_package: work_package2, + FactoryBot.create(:cost_entry, work_package: work_package2, project: work_package2.project, cost_type: cost_type) } @@ -71,7 +71,7 @@ describe WorkPackage, type: :model do describe 'w/ the work package having a cost entry' do before do work_package - cost_entry.save! + cost_entry end it 'should be true' do @@ -82,8 +82,8 @@ describe WorkPackage, type: :model do describe 'w/ two work packages having a cost entry' do before do work_package - cost_entry.save! - cost_entry2.save! + cost_entry + cost_entry2 end it 'should be true' do @@ -106,7 +106,7 @@ describe WorkPackage, type: :model do describe 'w/ the work package having a cost entry' do before do work_package - cost_entry.save! + cost_entry end it "should be have 'CostEntry' as class to address" do @@ -129,7 +129,7 @@ describe WorkPackage, type: :model do before do work_package.save! - cost_entry.save! + cost_entry end describe 'w/o a cleanup beeing necessary' do diff --git a/modules/costs/spec/requests/api/cost_entries/cost_entries_by_work_package_resource_spec.rb b/modules/costs/spec/requests/api/cost_entries/cost_entries_by_work_package_resource_spec.rb index 05d505a9ff..d47039109a 100644 --- a/modules/costs/spec/requests/api/cost_entries/cost_entries_by_work_package_resource_spec.rb +++ b/modules/costs/spec/requests/api/cost_entries/cost_entries_by_work_package_resource_spec.rb @@ -33,9 +33,9 @@ describe 'API v3 Cost Entry resource' do include Rack::Test::Methods include API::V3::Utilities::PathHelper - let(:current_user) { + let(:current_user) do FactoryBot.create(:user, member_in_project: project, member_through_role: role) - } + end let(:role) { FactoryBot.create(:role, permissions: permissions) } let(:work_package_permissions) { [:view_work_packages] } let(:cost_entry_permissions) { [:view_cost_entries] } @@ -44,16 +44,15 @@ describe 'API v3 Cost Entry resource' do let(:work_package) { FactoryBot.create(:work_package, project: project) } subject(:response) { last_response } - let(:cost_entry) { - FactoryBot.build(:cost_entry, + let(:cost_entry) do + FactoryBot.create(:cost_entry, project: project, work_package: work_package, user: current_user) - } + end before do - allow(User).to receive(:current).and_return current_user - cost_entry.save! + login_as current_user get get_path end diff --git a/modules/costs/spec/requests/api/cost_entries/cost_entry_resource_spec.rb b/modules/costs/spec/requests/api/cost_entries/cost_entry_resource_spec.rb index f8b779c31a..eb8845c0f6 100644 --- a/modules/costs/spec/requests/api/cost_entries/cost_entry_resource_spec.rb +++ b/modules/costs/spec/requests/api/cost_entries/cost_entry_resource_spec.rb @@ -33,19 +33,18 @@ describe 'API v3 Cost Entry resource' do include Rack::Test::Methods include API::V3::Utilities::PathHelper - let(:current_user) { + let(:current_user) do FactoryBot.create(:user, member_in_project: project, member_through_role: role) - } + end let(:role) { FactoryBot.create(:role, permissions: permissions) } let(:permissions) { [:view_cost_entries] } let(:project) { FactoryBot.create(:project) } subject(:response) { last_response } - let(:cost_entry) { FactoryBot.build(:cost_entry, project: project) } + let(:cost_entry) { FactoryBot.create(:cost_entry, project: project) } before do - allow(User).to receive(:current).and_return current_user - cost_entry.save! + login_as(current_user) get get_path end @@ -80,7 +79,7 @@ describe 'API v3 Cost Entry resource' do end context 'cost entry is his own' do - let(:cost_entry) { FactoryBot.build(:cost_entry, project: project, user: current_user) } + let(:cost_entry) { FactoryBot.create(:cost_entry, project: project, user: current_user) } it 'should return HTTP 200' do expect(response.status).to eql(200) @@ -92,7 +91,7 @@ describe 'API v3 Cost Entry resource' do let(:permissions) { [] } describe 'he can\'t even see own cost entries' do - let(:cost_entry) { FactoryBot.build(:cost_entry, project: project, user: current_user) } + let(:cost_entry) { FactoryBot.create(:cost_entry, project: project, user: current_user) } it_behaves_like 'error response', 403, 'MissingPermission', diff --git a/spec/factories/work_package_factory.rb b/spec/factories/work_package_factory.rb index d577b5427c..ecffb60655 100644 --- a/spec/factories/work_package_factory.rb +++ b/spec/factories/work_package_factory.rb @@ -70,8 +70,8 @@ FactoryBot.define do created_at { Time.now } updated_at { Time.now } - callback(:after_stub) do |wp| - wp.type = wp.project.types.first unless wp.type_id + callback(:after_stub) do |wp, arguments| + wp.type = wp.project.types.first unless wp.type_id || arguments.instance_variable_get(:@overrides).has_key?(:type) end end end diff --git a/spec/lib/api/v3/queries/query_representer_generation_spec.rb b/spec/lib/api/v3/queries/query_representer_generation_spec.rb index a013cfee54..ef9a51f95d 100644 --- a/spec/lib/api/v3/queries/query_representer_generation_spec.rb +++ b/spec/lib/api/v3/queries/query_representer_generation_spec.rb @@ -32,8 +32,10 @@ describe ::API::V3::Queries::QueryRepresenter do include ::API::V3::Utilities::PathHelper let(:query) { FactoryBot.build_stubbed(:query, project: project) } + let(:unpersisted_query) { FactoryBot.build(:query, project: project, user: other_user) } let(:project) { FactoryBot.build_stubbed(:project) } let(:user) { double('current_user', allowed_to?: true, admin: true, admin?: true, active?: true) } + let(:other_user) { FactoryBot.build_stubbed(:user) } let(:embed_links) { true } let(:representer) do described_class.new(query, current_user: user, embed_links: embed_links) @@ -173,7 +175,7 @@ describe ::API::V3::Queries::QueryRepresenter do end context 'when unpersisted' do - let(:query) { FactoryBot.build(:query, project: project) } + let(:query) { unpersisted_query } it_behaves_like 'has an untitled link' do let(:link) { 'update' } @@ -182,7 +184,8 @@ describe ::API::V3::Queries::QueryRepresenter do end context 'when unpersisted outside a project' do - let(:query) { FactoryBot.build(:query) } + let(:project) { nil } + let(:query) { unpersisted_query } it_behaves_like 'has an untitled link' do let(:link) { 'update' } @@ -200,7 +203,7 @@ describe ::API::V3::Queries::QueryRepresenter do end context 'when not persisted' do - let(:query) { FactoryBot.build(:query, project: project) } + let(:query) { unpersisted_query } it_behaves_like 'has no link' do let(:link) { 'delete' } @@ -234,7 +237,7 @@ describe ::API::V3::Queries::QueryRepresenter do end context 'when not persisted and lacking permission' do - let(:query) { FactoryBot.build(:query, project: project) } + let(:query) { unpersisted_query } it_behaves_like 'has no link' do let(:link) { 'updateImmediately' } @@ -244,7 +247,7 @@ describe ::API::V3::Queries::QueryRepresenter do context 'when not persisted and having permission' do let(:permissions) { [:create] } - let(:query) { FactoryBot.build(:query, project: project) } + let(:query) { unpersisted_query } it_behaves_like 'has an untitled link' do let(:link) { 'updateImmediately' } @@ -279,7 +282,7 @@ describe ::API::V3::Queries::QueryRepresenter do end context 'when not persisted and lacking permission' do - let(:query) { FactoryBot.build(:query, project: project) } + let(:query) { unpersisted_query } it_behaves_like 'has no link' do let(:link) { 'updateOrderedWorkPackages' } @@ -289,7 +292,7 @@ describe ::API::V3::Queries::QueryRepresenter do context 'when not persisted and having permission' do let(:permissions) { [:create] } - let(:query) { FactoryBot.build(:query, project: project) } + let(:query) { unpersisted_query } it_behaves_like 'has an untitled link' do let(:link) { 'updateOrderedWorkPackages' } diff --git a/spec/lib/api/v3/work_packages/create_form_representer_spec.rb b/spec/lib/api/v3/work_packages/create_form_representer_spec.rb index 316ddc6331..0fcdfe1126 100644 --- a/spec/lib/api/v3/work_packages/create_form_representer_spec.rb +++ b/spec/lib/api/v3/work_packages/create_form_representer_spec.rb @@ -34,20 +34,20 @@ describe ::API::V3::WorkPackages::CreateFormRepresenter do include API::V3::Utilities::PathHelper let(:errors) { [] } - let(:project) { + let(:project) do FactoryBot.build_stubbed(:project) - } + end let(:work_package) do - wp = FactoryBot.build_stubbed(:work_package, project: project) - allow(wp).to receive(:assignable_versions).and_return [] - wp + FactoryBot.build_stubbed(:stubbed_work_package, project: project).tap do |wp| + allow(wp).to receive(:assignable_versions).and_return [] + end end - let(:current_user) { + let(:current_user) do FactoryBot.build_stubbed(:user) - } - let(:representer) { + end + let(:representer) do described_class.new(work_package, current_user: current_user, errors: errors) - } + end context 'generation' do subject(:generated) { representer.to_json } diff --git a/spec/lib/api/v3/work_packages/create_project_form_representer_spec.rb b/spec/lib/api/v3/work_packages/create_project_form_representer_spec.rb index 08c40563bc..e4ad29535d 100644 --- a/spec/lib/api/v3/work_packages/create_project_form_representer_spec.rb +++ b/spec/lib/api/v3/work_packages/create_project_form_representer_spec.rb @@ -33,18 +33,17 @@ describe ::API::V3::WorkPackages::CreateProjectFormRepresenter do include API::V3::Utilities::PathHelper let(:errors) { [] } - let(:work_package) { - FactoryBot.build(:work_package, - id: 42, - created_at: DateTime.now, - updated_at: DateTime.now) - } - let(:current_user) { - FactoryBot.build(:user, member_in_project: work_package.project) - } - let(:representer) { - described_class.new(work_package, current_user: current_user, errors: errors) - } + let(:project) { work_package.project } + let(:permissions) { %i(edit_work_packages) } + let(:type) { FactoryBot.build_stubbed(:type) } + let(:work_package) do + FactoryBot.build_stubbed(:stubbed_work_package, + type: type) + end + include_context 'user with stubbed permissions' + let(:representer) do + described_class.new(work_package, current_user: user, errors: errors) + end context 'generation' do subject(:generated) { representer.to_json } @@ -114,12 +113,7 @@ describe ::API::V3::WorkPackages::CreateProjectFormRepresenter do end context 'user with insufficient permissions' do - let(:role) { FactoryBot.create(:role, permissions: []) } - let(:current_user) do - FactoryBot.build(:user, - member_in_project: work_package.project, - member_through_role: role) - end + let(:permissions) { [] } it do is_expected.not_to have_json_path('_links/commit/href') @@ -128,11 +122,7 @@ describe ::API::V3::WorkPackages::CreateProjectFormRepresenter do end describe 'customFields' do - before do - allow(current_user).to receive(:allowed_to?).and_return(true) - end - - context 'with project admin priviliges' do + shared_examples_for 'links to project custom field admin' do it 'has a link to set the custom fields for that project' do expected = { href: "/projects/#{work_package.project.identifier}/settings/custom_fields", @@ -144,65 +134,30 @@ describe ::API::V3::WorkPackages::CreateProjectFormRepresenter do end end - context 'without project admin priviliges' do - before do - allow(current_user).to receive(:allowed_to?) - .with(:edit_project, work_package.project) - .and_return(false) - end + context 'with admin privileges' do + include_context 'user with stubbed permissions', admin: true + it_behaves_like 'links to project custom field admin' + end + + context 'without project admin priviliges' do it 'has no link to set the custom fields for that project' do is_expected.to_not have_json_path('_links/customFields') end end - context 'with project and general admin priviliges' do - let(:current_user) { FactoryBot.build_stubbed(:admin) } + context 'with project admin privileges' do + let(:permissions) { [:edit_project] } - before do - allow(current_user).to receive(:allowed_to?) - .with(:edit_project, work_package.project) - .and_return(false) - end - - it 'has a link to set the custom fields for that project' do - expected = { - href: "/projects/#{work_package.project.identifier}/settings/custom_fields", - type: "text/html", - title: "Custom fields" - } - - is_expected - .to be_json_eql(expected.to_json) - .at_path('_links/customFields') - end + it_behaves_like 'links to project custom field admin' end end describe 'configureForm' do - before do - allow(current_user).to receive(:allowed_to?).and_return(true) - end - context "as admin" do - let(:current_user) { FactoryBot.build_stubbed(:admin) } + include_context 'user with stubbed permissions', admin: true context 'with type' do - let(:type) { FactoryBot.build_stubbed(:type) } - let(:work_package) do - FactoryBot.build(:work_package, - id: 42, - created_at: DateTime.now, - updated_at: DateTime.now, - type: type) - end - - before do - allow(current_user).to receive(:allowed_to?) - .with(:edit_project, work_package.project) - .and_return(false) - end - it 'has a link to configure the form' do expected = { href: "/types/#{type.id}/edit?tab=form_configuration", @@ -217,10 +172,7 @@ describe ::API::V3::WorkPackages::CreateProjectFormRepresenter do end context 'without type' do - before do - allow(work_package).to receive(:type).and_return(nil) - allow(work_package).to receive(:type_id).and_return(nil) - end + let(:type) { nil } it 'has no link to configure the form' do is_expected.to_not have_json_path('_links/configureForm') diff --git a/spec/lib/api/v3/work_packages/update_form_representer_spec.rb b/spec/lib/api/v3/work_packages/update_form_representer_spec.rb index a449ee0920..0afe02c49e 100644 --- a/spec/lib/api/v3/work_packages/update_form_representer_spec.rb +++ b/spec/lib/api/v3/work_packages/update_form_representer_spec.rb @@ -32,17 +32,15 @@ describe ::API::V3::WorkPackages::UpdateFormRepresenter do include API::V3::Utilities::PathHelper let(:errors) { [] } + let(:type) { FactoryBot.build_stubbed(:type) } + let(:project) { work_package.project } + let(:permissions) { %i(edit_work_packages) } let(:work_package) do - FactoryBot.build(:work_package, - id: 42, - created_at: Time.now, - updated_at: Time.now) - end - let(:current_user) do - FactoryBot.build(:user, member_in_project: work_package.project) + FactoryBot.build_stubbed(:stubbed_work_package) end + include_context 'user with stubbed permissions' let(:representer) do - described_class.new(work_package, current_user: current_user, errors: errors) + described_class.new(work_package, current_user: user, errors: errors) end context 'generation' do @@ -87,12 +85,7 @@ describe ::API::V3::WorkPackages::UpdateFormRepresenter do end context 'user with insufficient permissions' do - let(:role) { FactoryBot.create(:role, permissions: []) } - let(:current_user) do - FactoryBot.build(:user, - member_in_project: work_package.project, - member_through_role: role) - end + let(:permissions) { [] } it { is_expected.not_to have_json_path('_links/commit/href') } end diff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb index 880cf12dbb..dabf87e966 100644 --- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb +++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb @@ -39,21 +39,36 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do end let(:parent) { nil } let(:priority) { FactoryBot.build_stubbed(:priority, updated_at: Time.now) } + let(:assignee) { nil } + let(:responsible) { nil } + let(:start_date) { Date.today.to_datetime } + let(:due_date) { Date.today.to_datetime } + let(:type_milestone) { false } + let(:estimated_hours) { nil } + let(:derived_estimated_hours) { nil } + let(:spent_hours) { 0 } let(:work_package) do FactoryBot.build_stubbed(:stubbed_work_package, - id: 42, - start_date: Date.today.to_datetime, - due_date: Date.today.to_datetime, + start_date: start_date, + due_date: due_date, done_ratio: 50, estimated_hours: 6.0, parent: parent, type: type, project: project, priority: priority, + assigned_to: assignee, + responsible: responsible, + estimated_hours: estimated_hours, + derived_estimated_hours: derived_estimated_hours, status: status) do |wp| allow(wp) .to receive(:available_custom_fields) .and_return(available_custom_fields) + + allow(wp) + .to receive(:spent_hours) + .and_return(spent_hours) end end let(:all_permissions) do @@ -73,7 +88,13 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do end let(:permissions) { all_permissions } let(:project) { FactoryBot.build_stubbed(:project_with_types) } - let(:type) { project.types.first } + let(:type) do + type = project.types.first + + type.is_milestone = type_milestone + + type + end let(:status) { FactoryBot.build_stubbed(:status, updated_at: Time.now) } let(:available_custom_fields) { [] } @@ -104,12 +125,12 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do describe 'startDate' do it_behaves_like 'has ISO 8601 date only' do - let(:date) { work_package.start_date } + let(:date) { start_date } let(:json_path) { 'startDate' } end context 'no start date' do - let(:work_package) { FactoryBot.build(:work_package, id: 42, start_date: nil) } + let(:start_date) { nil } it 'renders as null' do is_expected.to be_json_eql(nil.to_json).at_path('startDate') @@ -117,14 +138,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do end context 'when the work package has a milestone type' do - let(:milestone_type) do - FactoryBot.build_stubbed(:type, - is_milestone: true) - end - - before do - work_package.type = milestone_type - end + let(:type_milestone) { true } it 'has no startDate' do is_expected.to_not have_json_path('startDate') @@ -133,28 +147,23 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do end describe 'dueDate' do - it_behaves_like 'has ISO 8601 date only' do - let(:date) { work_package.due_date } - let(:json_path) { 'dueDate' } - end + context 'with a non milestone type' do + it_behaves_like 'has ISO 8601 date only' do + let(:date) { work_package.due_date } + let(:json_path) { 'dueDate' } + end - context 'no finish date' do - let(:work_package) { FactoryBot.build(:work_package, id: 42, due_date: nil) } + context 'no finish date' do + let(:due_date) { nil } - it 'renders as null' do - is_expected.to be_json_eql(nil.to_json).at_path('dueDate') + it 'renders as null' do + is_expected.to be_json_eql(nil.to_json).at_path('dueDate') + end end end - context 'when the work package has a milestone type' do - let(:milestone_type) do - FactoryBot.build_stubbed(:type, - is_milestone: true) - end - - before do - work_package.type = milestone_type - end + context 'with a milestone type' do + let(:type_milestone) { true } it 'has no startDate' do is_expected.to_not have_json_path('dueDate') @@ -163,40 +172,24 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do end describe 'date' do - let(:milestone_type) do - FactoryBot.build_stubbed(:type, - is_milestone: true) - end - - before do - work_package.type = milestone_type - end + context 'with a milestone type' do + let(:type_milestone) { true } - it_behaves_like 'has ISO 8601 date only' do - let(:date) { work_package.due_date } # could just as well be start_date - let(:json_path) { 'date' } - end - - context 'no finish date' do - before do - work_package.due_date = nil + it_behaves_like 'has ISO 8601 date only' do + let(:date) { due_date } # could just as well be start_date + let(:json_path) { 'date' } end - it 'renders as null' do - is_expected.to be_json_eql(nil.to_json).at_path('date') - end - end - - context 'when the work package has a non milestone type' do - let(:none_milestone_type) do - FactoryBot.build_stubbed(:type, - is_milestone: false) - end + context 'no finish date' do + let(:due_date) { nil } - before do - work_package.type = none_milestone_type + it 'renders as null' do + is_expected.to be_json_eql(nil.to_json).at_path('date') + end end + end + context 'with a milestone type' do it 'has no date' do is_expected.to_not have_json_path('date') end @@ -229,25 +222,13 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do end describe 'estimatedTime' do - let(:work_package) do - FactoryBot.build_stubbed(:work_package, - id: 42, - created_at: DateTime.now, - updated_at: DateTime.now, - estimated_hours: 6.5) - end + let(:estimated_hours) { 6.5 } it { is_expected.to be_json_eql('PT6H30M'.to_json).at_path('estimatedTime') } end describe 'derivedEstimatedTime' do - let(:work_package) do - FactoryBot.build_stubbed(:work_package, - id: 42, - created_at: DateTime.now, - updated_at: DateTime.now, - derived_estimated_hours: 3.75) - end + let(:derived_estimated_hours) { 3.75 } it { is_expected.to be_json_eql('PT3H45M'.to_json).at_path('derivedEstimatedTime') } end @@ -265,21 +246,13 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do end context 'time entry with single hour' do - before do - allow(work_package) - .to receive(:spent_hours) - .and_return(1.0) - end + let(:spent_hours) { 1.0 } it { is_expected.to be_json_eql('PT1H'.to_json).at_path('spentTime') } end context 'time entry with multiple hours' do - before do - allow(work_package) - .to receive(:spent_hours) - .and_return(42.5) - end + let(:spent_hours) { 42.5 } it { is_expected.to be_json_eql('P1DT18H30M'.to_json).at_path('spentTime') } end @@ -396,9 +369,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do describe 'assignee' do context 'is user' do - let(:work_package) do - FactoryBot.build(:work_package, id: 42, assigned_to: FactoryBot.build_stubbed(:user)) - end + let(:assignee) { FactoryBot.build_stubbed(:user) } it_behaves_like 'has a titled link' do let(:link) { 'assignee' } @@ -408,9 +379,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do end context 'is group' do - let(:work_package) do - FactoryBot.build(:work_package, id: 42, assigned_to: FactoryBot.build_stubbed(:group)) - end + let(:assignee) { FactoryBot.build_stubbed(:group) } it_behaves_like 'has a titled link' do let(:link) { 'assignee' } @@ -428,9 +397,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do describe 'responsible' do context 'is user' do - let(:work_package) do - FactoryBot.build(:work_package, id: 42, responsible: FactoryBot.build_stubbed(:user)) - end + let(:responsible) { FactoryBot.build_stubbed(:user) } it_behaves_like 'has a titled link' do let(:link) { 'responsible' } @@ -440,9 +407,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do end context 'is group' do - let(:work_package) do - FactoryBot.build(:work_package, id: 42, responsible: FactoryBot.build_stubbed(:group)) - end + let(:responsible) { FactoryBot.build_stubbed(:group) } it_behaves_like 'has a titled link' do let(:link) { 'responsible' } diff --git a/spec/models/activities/work_package_activity_provider_spec.rb b/spec/models/activities/work_package_activity_provider_spec.rb index 2803480ae0..8ac8816077 100644 --- a/spec/models/activities/work_package_activity_provider_spec.rb +++ b/spec/models/activities/work_package_activity_provider_spec.rb @@ -33,17 +33,17 @@ describe Activities::WorkPackageActivityProvider, type: :model do let(:work_package_edit_event) { 'work_package-edit' } let(:work_package_closed_event) { 'work_package-closed' } - let(:user) { FactoryBot.create :admin } - let(:role) { FactoryBot.create :role } + let(:user) { FactoryBot.create :admin } + let(:role) { FactoryBot.create :role } let(:status_closed) { FactoryBot.create :closed_status } - let(:work_package) { FactoryBot.build :work_package } - let!(:workflow) { + let!(:work_package) { FactoryBot.create :work_package } + let!(:workflow) do FactoryBot.create :workflow, - old_status: work_package.status, - new_status: status_closed, - type_id: work_package.type_id, - role: role - } + old_status: work_package.status, + new_status: status_closed, + type_id: work_package.type_id, + role: role + end before do allow(ActionMailer::Base).to receive(:perform_deliveries).and_return(false) @@ -52,26 +52,37 @@ describe Activities::WorkPackageActivityProvider, type: :model do describe '#event_type' do describe 'latest events' do context 'when a work package has been created' do - let(:subject) { Activities::WorkPackageActivityProvider.find_events(event_scope, user, Date.yesterday, Date.tomorrow, {}).last.try :event_type } - before do work_package.save! end + let(:subject) do + Activities::WorkPackageActivityProvider + .find_events(event_scope, user, Date.yesterday, Date.tomorrow, {}) + .last + .try :event_type + end it { is_expected.to eq(work_package_edit_event) } end context 'should be selected and ordered correctly' do let!(:work_packages) { (1..20).map { (FactoryBot.create :work_package, author: user).id.to_s } } - let(:subject) { Activities::WorkPackageActivityProvider.find_events(event_scope, user, Date.yesterday, Date.tomorrow, limit: 10).map { |a| a.journable_id.to_s } } + let(:subject) do + Activities::WorkPackageActivityProvider + .find_events(event_scope, user, Date.yesterday, Date.tomorrow, limit: 10) + .map { |a| a.journable_id.to_s } + end it { is_expected.to eq(work_packages.reverse.first(10)) } end context 'when a work package has been created and then closed' do - let(:subject) { Activities::WorkPackageActivityProvider.find_events(event_scope, user, Date.yesterday, Date.tomorrow, limit: 10).first.try :event_type } + let(:subject) do + Activities::WorkPackageActivityProvider + .find_events(event_scope, user, Date.yesterday, Date.tomorrow, limit: 10) + .first + .try :event_type + end before do login_as(user) - work_package.save! - work_package.status = status_closed work_package.save! end diff --git a/spec/models/messages_spec.rb b/spec/models/messages_spec.rb index ef3bb26381..03e71096d8 100644 --- a/spec/models/messages_spec.rb +++ b/spec/models/messages_spec.rb @@ -40,7 +40,7 @@ describe Message, type: :model do end it_behaves_like 'acts_as_attachable included' do - let(:model_instance) { FactoryBot.build(:message) } + let(:model_instance) { FactoryBot.create(:message) } end describe '#project' do diff --git a/spec/models/relation_spec.rb b/spec/models/relation_spec.rb index ab6c3d9cd4..8fe9724b70 100644 --- a/spec/models/relation_spec.rb +++ b/spec/models/relation_spec.rb @@ -67,8 +67,8 @@ describe Relation, type: :model do let(:reversed) { type_hash[:reverse] } let(:relation) do FactoryBot.build_stubbed(:relation, - relation_type: nil, - column_name => column_count) + relation_type: nil, + column_name => column_count) end context 'with the column set to 1' do @@ -106,9 +106,9 @@ describe Relation, type: :model do end let(:relation) do FactoryBot.build_stubbed(:relation, - relation_type: nil, - column_name => 1, - other_column => 1) + relation_type: nil, + column_name => 1, + other_column => 1) end it 'is "mixed"' do @@ -120,12 +120,7 @@ describe Relation, type: :model do end describe '#relation_type=' do - let(:column_name) { 'relates' } - let(:type) { :relates } - let(:relation) do - FactoryBot.build(:relation, - relation_type: 'relates') - end + let(:type) { Relation::TYPE_RELATES } it 'updates the column value' do relation.save! @@ -183,16 +178,16 @@ describe Relation, type: :model do let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) } let(:member_project_to) do FactoryBot.create(:member, - project: to.project, - user: user, - roles: [role]) + project: to.project, + user: user, + roles: [role]) end let(:member_project_from) do FactoryBot.create(:member, - project: from.project, - user: user, - roles: [role]) + project: from.project, + user: user, + roles: [role]) end before do diff --git a/spec/models/work_package/ask_before_destruction_spec.rb b/spec/models/work_package/ask_before_destruction_spec.rb index 3bf05cd21c..8e24144e28 100644 --- a/spec/models/work_package/ask_before_destruction_spec.rb +++ b/spec/models/work_package/ask_before_destruction_spec.rb @@ -45,30 +45,36 @@ describe WorkPackage, type: :model do let(:role) { FactoryBot.create(:role) } let(:role2) { FactoryBot.create(:role) } let(:member) do - FactoryBot.create(:member, principal: user, - roles: [role]) + FactoryBot.create(:member, + principal: user, + roles: [role]) end let(:member2) do - FactoryBot.create(:member, principal: user, - roles: [role2], - project: work_package2.project) + FactoryBot.create(:member, + principal: user, + roles: [role2], + project: work_package2.project) end let(:status) { FactoryBot.create(:status) } let(:priority) { FactoryBot.create(:priority) } + let(:time_entry_hours) { 10 } let(:time_entry) do - FactoryBot.build(:time_entry, work_package: work_package, - project: work_package.project) + FactoryBot.create(:time_entry, + hours: time_entry_hours, + work_package: work_package, + project: work_package.project) end let(:time_entry2) do - FactoryBot.build(:time_entry, work_package: work_package2, - project: work_package2.project) + FactoryBot.create(:time_entry, + work_package: work_package2, + project: work_package2.project) end describe '#cleanup_action_required_before_destructing?' do describe 'w/ the work package having a time entry' do before do work_package - time_entry.save! + time_entry end it 'should be true' do @@ -79,8 +85,8 @@ describe WorkPackage, type: :model do describe 'w/ two work packages having a time entry' do before do work_package - time_entry.save! - time_entry2.save! + time_entry + time_entry2 end it 'should be true' do @@ -103,7 +109,7 @@ describe WorkPackage, type: :model do describe 'w/ the work package having a time entry' do before do work_package - time_entry.save! + time_entry end it "should be have 'TimeEntry' as class to address" do @@ -124,10 +130,9 @@ describe WorkPackage, type: :model do describe '#cleanup_associated_before_destructing_if_required' do before do - work_package.save! + work_package - time_entry.hours = 10 - time_entry.save! + time_entry end describe 'w/o a cleanup beeing necessary' do @@ -189,7 +194,6 @@ describe WorkPackage, type: :model do describe 'w/ "reassign" as action w/ reassigning to a valid work_package' do - context 'with a single work package' do let(:action) do WorkPackage.cleanup_associated_before_destructing_if_required(work_package, @@ -199,9 +203,9 @@ describe WorkPackage, type: :model do end before do - work_package2.save! + work_package2 role2.add_permission! :edit_time_entries - member2.save! + member2 end it 'should return true' do @@ -232,7 +236,7 @@ describe WorkPackage, type: :model do end before do - work_package2.save! + work_package2 role2.add_permission! :edit_time_entries role2.save! member2.save! @@ -268,7 +272,7 @@ describe WorkPackage, type: :model do end before do - work_package2.save! + work_package2 end it 'should return true' do diff --git a/spec/models/work_package/spent_time_spec.rb b/spec/models/work_package/spent_time_spec.rb index c4d6a264d6..63dd2388b2 100644 --- a/spec/models/work_package/spent_time_spec.rb +++ b/spec/models/work_package/spent_time_spec.rb @@ -36,60 +36,59 @@ describe WorkPackage::SpentTime, type: :model do child_work_package_in_other_project.project end let(:work_package) do - FactoryBot.build(:work_package) + FactoryBot.create(:work_package) end let(:child_work_package) do - FactoryBot.build(:work_package, + FactoryBot.create(:work_package, project: project, parent: work_package) end let(:child_work_package_in_other_project) do - FactoryBot.build(:work_package, + FactoryBot.create(:work_package, parent: work_package) end - let(:time_entry) do - FactoryBot.build(:time_entry, + let!(:time_entry) do + FactoryBot.create(:time_entry, work_package: work_package, project: project) end let(:time_entry2) do - FactoryBot.build(:time_entry, + FactoryBot.create(:time_entry, work_package: work_package, project: project) end let(:child_time_entry) do - FactoryBot.build(:time_entry, + FactoryBot.create(:time_entry, work_package: child_work_package, project: project) end let(:child_time_entry_in_other_project) do - FactoryBot.build(:time_entry, + FactoryBot.create(:time_entry, work_package: child_work_package_in_other_project, project: other_project) end let(:role) do FactoryBot.build(:role, - permissions: [:view_time_entries, :view_work_packages]) + permissions: %i[view_time_entries view_work_packages]) end let(:role_without_view_time_entries) do FactoryBot.build(:role, - permissions: [:view_work_packages]) + permissions: [:view_work_packages]) end let(:role_without_view_work_packages) do FactoryBot.build(:role, - permissions: [:view_time_entries]) + permissions: [:view_time_entries]) end let(:user) do FactoryBot.build(:user, - member_in_project: project, - member_through_role: role) + member_in_project: project, + member_through_role: role) end before do - time_entry.save! allow(Setting).to receive(:cross_project_work_package_relations?).and_return(true) - allow(User).to receive(:current).and_return(user) + login_as user end shared_examples_for 'spent hours' do @@ -98,16 +97,12 @@ describe WorkPackage::SpentTime, type: :model do end it 'sums up the spent time of the time entries' do - time_entry2.save! - sum = time_entry.hours + time_entry2.hours expect(subject).to eql sum end it 'inherits the spent time of the descendants' do - child_time_entry.save! - sum = time_entry.hours + child_time_entry.hours expect(subject).to eql sum @@ -115,12 +110,11 @@ describe WorkPackage::SpentTime, type: :model do context 'permissions' do it 'counts the child if that child is in a project in which the user ' + - 'has the necessary permissions' do + 'has the necessary permissions' do FactoryBot.create(:member, - user: user, - project: other_project, - roles: [role]) - child_time_entry_in_other_project.save! + user: user, + project: other_project, + roles: [role]) sum = time_entry.hours + child_time_entry_in_other_project.hours @@ -128,11 +122,11 @@ describe WorkPackage::SpentTime, type: :model do end it 'does not count the child if that child is in a project in which the user ' + - 'lacks the view_time_entries permission' do + 'lacks the view_time_entries permission' do FactoryBot.create(:member, - user: user, - project: other_project, - roles: [role_without_view_time_entries]) + user: user, + project: other_project, + roles: [role_without_view_time_entries]) child_time_entry_in_other_project.save! sum = time_entry.hours @@ -141,11 +135,11 @@ describe WorkPackage::SpentTime, type: :model do end it 'does not count the child if that child is in a project in which the user ' + - 'lacks the view_work_packages permission' do + 'lacks the view_work_packages permission' do FactoryBot.create(:member, - user: user, - project: other_project, - roles: [role_without_view_work_packages]) + user: user, + project: other_project, + roles: [role_without_view_work_packages]) child_time_entry_in_other_project.save! sum = time_entry.hours diff --git a/spec/support/user_with_stubbed_permissions.rb b/spec/support/user_with_stubbed_permissions.rb new file mode 100644 index 0000000000..1c45767e1d --- /dev/null +++ b/spec/support/user_with_stubbed_permissions.rb @@ -0,0 +1,13 @@ +shared_context 'user with stubbed permissions' do |attributes = {}| + let(:user) do + raise "'let(:permissions)' needs to be defined" unless defined?(:permissions) + raise "'let(:project)' needs to be defined" unless defined?(:project) + + FactoryBot.build_stubbed(:user, **attributes).tap do |u| + allow(u) + .to receive(:allowed_to?) do |queried_permission, queried_project| + project == queried_project && permissions.include?(queried_permission) + end + end + end +end