From 4b204a5359e8f27282b21fdc5a1abea11953868d Mon Sep 17 00:00:00 2001 From: Julia Date: Fri, 28 Aug 2026 17:55:52 +0400 Subject: [PATCH 1/5] Add test-prof --- Gemfile | 2 ++ Gemfile.lock | 3 +++ spec/spec_helper.rb | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/Gemfile b/Gemfile index 058db65c85d7..89402f3d42bb 100644 --- a/Gemfile +++ b/Gemfile @@ -167,6 +167,8 @@ group :test do # HTML testing (invoice rendering) gem "rspec-snapshot", "~> 2.0" gem "htmlbeautifier", "~> 1.4" + + gem "test-prof", "~> 1.0" end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index d199c3023456..207c7b547a65 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1004,6 +1004,8 @@ GEM temple (0.10.4) terminal-table (4.0.0) unicode-display_width (>= 1.1.1, < 4) + test-prof (1.6.3) + logger thor (1.5.0) throttling (0.4.1) logger @@ -1181,6 +1183,7 @@ DEPENDENCIES stripe strong_migrations super_diff (~> 0.19.0) + test-prof (~> 1.0) throttling timecop tzinfo-data diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fb043644f15b..055606284c5e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -76,6 +76,10 @@ config.transport.transport_class = Sentry::DummyTransport end +require "test_prof/recipes/rspec/sample" +require "test_prof/recipes/rspec/let_it_be" +require "test_prof/recipes/rspec/factory_default" + RSpec.configure do |config| config.include ActiveJob::TestHelper config.include FactoryBot::Syntax::Methods From dea0c1bd0b82901d8c0cfb3d54bacbc0c6097ca1 Mon Sep 17 00:00:00 2001 From: Julia Date: Mon, 31 Aug 2026 06:13:21 +0400 Subject: [PATCH 2/5] Clean up factory usage in spec/services/l* --- .../services/lifetime_usages/calculate_service_spec.rb | 10 +++++++--- .../lifetime_usages/check_thresholds_service_spec.rb | 6 +++--- .../find_last_and_next_thresholds_service_spec.rb | 3 +-- .../flag_refresh_from_invoice_service_spec.rb | 8 ++++++-- .../flag_refresh_from_plan_update_service_spec.rb | 6 +++++- spec/services/lifetime_usages/update_service_spec.rb | 6 ++++++ .../usage_thresholds/check_service_spec.rb | 9 +++++++-- .../usage_thresholds_completion_service_spec.rb | 3 +-- 8 files changed, 36 insertions(+), 15 deletions(-) diff --git a/spec/services/lifetime_usages/calculate_service_spec.rb b/spec/services/lifetime_usages/calculate_service_spec.rb index 75c3efa1e95a..a261bb1de676 100644 --- a/spec/services/lifetime_usages/calculate_service_spec.rb +++ b/spec/services/lifetime_usages/calculate_service_spec.rb @@ -9,10 +9,14 @@ let(:recalculate_current_usage) { false } let(:recalculate_invoiced_usage) { false } let(:subscription) { create(:subscription, customer:, subscription_at:) } - let(:organization) { customer.organization } - let(:customer) { create(:customer) } + let_it_be(:organization) { create_default(:organization)} + let_it_be(:billable_metric) { create(:billable_metric, organization:, aggregation_type: "count_agg") } + let_it_be(:customer) { create_default(:customer) } + +before_all do + create_default(:plan) +end - let(:billable_metric) { create(:billable_metric, organization:, aggregation_type: "count_agg") } let(:charge) { create(:standard_charge, plan: subscription.plan, billable_metric:, properties: {amount: "10"}) } let(:timestamp) { Time.current } let(:subscription_at) { timestamp - 6.months } diff --git a/spec/services/lifetime_usages/check_thresholds_service_spec.rb b/spec/services/lifetime_usages/check_thresholds_service_spec.rb index 7bb57ec6fd78..41a48834c6f8 100644 --- a/spec/services/lifetime_usages/check_thresholds_service_spec.rb +++ b/spec/services/lifetime_usages/check_thresholds_service_spec.rb @@ -8,10 +8,10 @@ let(:lifetime_usage) { create(:lifetime_usage, subscription:, recalculate_current_usage: true, recalculate_invoiced_usage: true, current_usage_amount_cents:) } let(:current_usage_amount_cents) { 0 } let(:subscription) { create(:subscription, customer_id: customer.id) } - let(:organization) { subscription.organization } - let(:customer) { create(:customer) } + let(:organization) { create_default(:organization)} + let(:customer) { create_default(:customer) } + let(:billable_metric) { create_default(:billable_metric, aggregation_type: "count_agg") } - let(:billable_metric) { create(:billable_metric, aggregation_type: "count_agg") } let(:charge) { create(:standard_charge, plan: subscription.plan, billable_metric:, properties: {amount: "10"}) } let(:timestamp) { Time.current } diff --git a/spec/services/lifetime_usages/find_last_and_next_thresholds_service_spec.rb b/spec/services/lifetime_usages/find_last_and_next_thresholds_service_spec.rb index ca3532cf9ee9..0061ecc9413d 100644 --- a/spec/services/lifetime_usages/find_last_and_next_thresholds_service_spec.rb +++ b/spec/services/lifetime_usages/find_last_and_next_thresholds_service_spec.rb @@ -8,9 +8,8 @@ let(:lifetime_usage) { create(:lifetime_usage, subscription:, organization:, current_usage_amount_cents:) } let(:current_usage_amount_cents) { 0 } + let_it_be(:organization) { create_default(:organization)} let(:plan) { create(:plan) } - let(:organization) { plan.organization } - let(:customer) { create(:customer, organization:) } let(:subscription) { create(:subscription, plan:, customer:) } diff --git a/spec/services/lifetime_usages/flag_refresh_from_invoice_service_spec.rb b/spec/services/lifetime_usages/flag_refresh_from_invoice_service_spec.rb index 296433192300..2a81c8150f13 100644 --- a/spec/services/lifetime_usages/flag_refresh_from_invoice_service_spec.rb +++ b/spec/services/lifetime_usages/flag_refresh_from_invoice_service_spec.rb @@ -8,8 +8,12 @@ let(:invoice) { create(:invoice, :subscription, subscriptions:, organization: customer.organization) } let(:lifetime_usage) { create(:lifetime_usage, subscription: invoice.subscriptions.first) } - let(:customer) { create(:customer) } - let(:plan) { create(:plan, organization: customer.organization) } +before_all do + create_default(:organization) +end + + let_it_be(:customer) { create_default(:customer) } + let_it_be(:plan) { create_default(:plan, organization: customer.organization) } let(:subscriptions) { create_list(:subscription, 1, plan:) } let(:usage_threshold) { create(:usage_threshold, plan:) } diff --git a/spec/services/lifetime_usages/flag_refresh_from_plan_update_service_spec.rb b/spec/services/lifetime_usages/flag_refresh_from_plan_update_service_spec.rb index 18576dfc3a9b..ee2e1c087c6f 100644 --- a/spec/services/lifetime_usages/flag_refresh_from_plan_update_service_spec.rb +++ b/spec/services/lifetime_usages/flag_refresh_from_plan_update_service_spec.rb @@ -5,7 +5,11 @@ RSpec.describe LifetimeUsages::FlagRefreshFromPlanUpdateService do subject { described_class.call(plan:) } - let(:plan) { create(:plan) } +before_all do + create_default(:organization) +end + + let_it_be(:plan) { create(:plan) } let(:result) { subject } describe "#call" do diff --git a/spec/services/lifetime_usages/update_service_spec.rb b/spec/services/lifetime_usages/update_service_spec.rb index f7b0fd0d5eba..d71281319fc8 100644 --- a/spec/services/lifetime_usages/update_service_spec.rb +++ b/spec/services/lifetime_usages/update_service_spec.rb @@ -5,6 +5,12 @@ RSpec.describe LifetimeUsages::UpdateService do subject(:update_service) { described_class.new(lifetime_usage:, params:) } +before_all do + create_default(:organization) +create_default(:customer) +create_default(:plan) +end + let(:lifetime_usage) { create(:lifetime_usage) } let(:params) do { diff --git a/spec/services/lifetime_usages/usage_thresholds/check_service_spec.rb b/spec/services/lifetime_usages/usage_thresholds/check_service_spec.rb index e4240140f0eb..38d885b6b38d 100644 --- a/spec/services/lifetime_usages/usage_thresholds/check_service_spec.rb +++ b/spec/services/lifetime_usages/usage_thresholds/check_service_spec.rb @@ -10,8 +10,13 @@ let(:recalculate_current_usage) { true } let(:recalculate_invoiced_usage) { true } let(:subscription) { create(:subscription, customer_id: customer.id) } - let(:organization) { subscription.organization } - let(:customer) { create(:customer) } + let_it_be(:organization) { create_default(:organization)} + let_it_be(:customer) { create_default(:customer) } + +before_all do + create_default(:plan) +end + let(:historical_usage_amount_cents) { 0 } def create_thresholds(subscription, amounts:, attach_to:, recurring: nil) diff --git a/spec/services/lifetime_usages/usage_thresholds_completion_service_spec.rb b/spec/services/lifetime_usages/usage_thresholds_completion_service_spec.rb index b47c918a505c..1c6029938a7a 100644 --- a/spec/services/lifetime_usages/usage_thresholds_completion_service_spec.rb +++ b/spec/services/lifetime_usages/usage_thresholds_completion_service_spec.rb @@ -8,9 +8,8 @@ let(:lifetime_usage) { create(:lifetime_usage, subscription:, organization:, current_usage_amount_cents:) } let(:current_usage_amount_cents) { 0 } + let_it_be(:organization) { create_default(:organization)} let(:plan) { create(:plan) } - let(:organization) { plan.organization } - let(:customer) { create(:customer, organization:) } let(:subscription) { create(:subscription, plan:, customer:) } From 8fd50ae47764e9707cfcc8d7990ca41838817f1e Mon Sep 17 00:00:00 2001 From: Julia Date: Mon, 31 Aug 2026 06:16:28 +0400 Subject: [PATCH 3/5] Clean up factory usage in spec/services/m* --- spec/services/memberships/create_service_spec.rb | 2 +- spec/services/memberships/revoke_service_spec.rb | 6 +++--- spec/services/memberships/update_service_spec.rb | 8 ++++---- spec/services/metadata/delete_item_key_service_spec.rb | 7 ++++++- spec/services/metadata/update_item_service_spec.rb | 7 ++++++- spec/services/middlewares/activity_log_middleware_spec.rb | 7 +++++++ .../backfill_connections_service_spec.rb | 4 ++-- 7 files changed, 29 insertions(+), 12 deletions(-) diff --git a/spec/services/memberships/create_service_spec.rb b/spec/services/memberships/create_service_spec.rb index b81320b1447d..b0e05038a1f5 100644 --- a/spec/services/memberships/create_service_spec.rb +++ b/spec/services/memberships/create_service_spec.rb @@ -6,7 +6,7 @@ subject(:create_service) { described_class.new(user:, organization:) } let(:user) { create(:user) } - let(:organization) { create(:organization) } + let_it_be(:organization) { create(:organization) } describe "#call" do it "creates a membership" do diff --git a/spec/services/memberships/revoke_service_spec.rb b/spec/services/memberships/revoke_service_spec.rb index 77de7ea4d795..ebab5bdf8b22 100644 --- a/spec/services/memberships/revoke_service_spec.rb +++ b/spec/services/memberships/revoke_service_spec.rb @@ -7,12 +7,12 @@ include_context "with mocked security logger" - let(:organization) { create(:organization) } + let_it_be(:organization) { create(:organization) } let(:admin_role) { create(:role, :admin) } let(:finance_role) { create(:role, :finance) } - let(:user) { create(:user) } - let(:membership) { create(:membership, organization:) } + let_it_be(:user) { create(:user) } + let_it_be(:membership) { create(:membership, organization:) } let(:other_membership) { create(:membership, user:, organization:) } describe "#call" do diff --git a/spec/services/memberships/update_service_spec.rb b/spec/services/memberships/update_service_spec.rb index bcee28e6e8de..64b66ac058ab 100644 --- a/spec/services/memberships/update_service_spec.rb +++ b/spec/services/memberships/update_service_spec.rb @@ -5,9 +5,9 @@ RSpec.describe Memberships::UpdateService do include_context "with mocked security logger" - let(:membership) { create(:membership) } - let(:organization) { membership.organization } - let(:acting_user) { create(:membership, organization:).user } + let_it_be(:organization) { create_default(:organization)} + let_it_be(:membership) { create(:membership) } + let_it_be(:acting_user) { create(:membership, organization:).user } let(:admin_role) { create(:role, :admin) } let!(:manager_role) { create(:role, :manager) } let(:params) { {roles: %w[manager]} } @@ -33,7 +33,7 @@ end context "when admin grants admin role to another member" do - let(:acting_membership) { create(:membership, organization:) } + let_it_be(:acting_membership) { create(:membership, organization:) } let(:acting_user) { acting_membership.user } let(:params) { {roles: %w[admin]} } diff --git a/spec/services/metadata/delete_item_key_service_spec.rb b/spec/services/metadata/delete_item_key_service_spec.rb index bd21024cd197..533138b9ba39 100644 --- a/spec/services/metadata/delete_item_key_service_spec.rb +++ b/spec/services/metadata/delete_item_key_service_spec.rb @@ -5,7 +5,12 @@ RSpec.describe Metadata::DeleteItemKeyService do subject(:service) { described_class.new(item:, key:) } - let(:organization) { create(:organization) } + let_it_be(:organization) { create_default(:organization) } + +before_all do + create_default(:customer) +end + let(:owner) { create(:credit_note, organization:) } let(:item) { create(:item_metadata, owner:, organization:, value:) } let(:value) { {"foo" => "bar", "baz" => "qux"} } diff --git a/spec/services/metadata/update_item_service_spec.rb b/spec/services/metadata/update_item_service_spec.rb index 08b5c998d9be..75576877f19b 100644 --- a/spec/services/metadata/update_item_service_spec.rb +++ b/spec/services/metadata/update_item_service_spec.rb @@ -5,7 +5,12 @@ RSpec.describe Metadata::UpdateItemService do subject(:service) { described_class.new(owner:, value:, partial:) } - let(:organization) { create(:organization) } + let_it_be(:organization) { create_default(:organization) } + +before_all do + create_default(:customer) +end + let(:owner) { create(:credit_note, organization:) } let(:value) { nil } let(:partial) { false } diff --git a/spec/services/middlewares/activity_log_middleware_spec.rb b/spec/services/middlewares/activity_log_middleware_spec.rb index 74176da0d6c7..9f383ba91978 100644 --- a/spec/services/middlewares/activity_log_middleware_spec.rb +++ b/spec/services/middlewares/activity_log_middleware_spec.rb @@ -27,6 +27,13 @@ def call end end +before_all do + create_default(:organization) +create_default(:customer) +create_default(:plan) +end + + let(:subscription) { create(:subscription, name: "My Subscription") } let(:activity_loggable_after_commit) { false } diff --git a/spec/services/multi_customer_connections/backfill_connections_service_spec.rb b/spec/services/multi_customer_connections/backfill_connections_service_spec.rb index 8269b0051fec..e4711e6269ef 100644 --- a/spec/services/multi_customer_connections/backfill_connections_service_spec.rb +++ b/spec/services/multi_customer_connections/backfill_connections_service_spec.rb @@ -5,8 +5,8 @@ RSpec.describe MultiCustomerConnections::BackfillConnectionsService do subject(:result) { described_class.call(organization:, dry_run:, batch_size: 1000) } - let(:organization) { create(:organization) } - let(:customer) { create(:customer, organization:) } + let_it_be(:organization) { create(:organization) } + let_it_be(:customer) { create(:customer, organization:) } let(:dry_run) { false } describe "#call" do From 2a53c4cf82a97bd12acf482b4110c3c7fe61b066 Mon Sep 17 00:00:00 2001 From: Julia Date: Mon, 31 Aug 2026 06:20:33 +0400 Subject: [PATCH 4/5] Clean up factory usage in spec/services/o* --- .../services/order_forms/create_service_spec.rb | 8 +++++++- .../services/order_forms/expire_service_spec.rb | 5 +++-- .../order_forms/mark_as_signed_service_spec.rb | 5 +++-- spec/services/order_forms/void_service_spec.rb | 4 ++-- spec/services/orders/execute_service_spec.rb | 10 ++++++++-- .../orders/one_off/execute_service_spec.rb | 8 ++++---- .../execute_service_spec.rb | 17 +++++++++++------ .../execute_service_spec.rb | 14 +++++++++----- spec/services/orders/update_service_spec.rb | 5 +++-- 9 files changed, 50 insertions(+), 26 deletions(-) diff --git a/spec/services/order_forms/create_service_spec.rb b/spec/services/order_forms/create_service_spec.rb index 123312e1f476..acc94f261554 100644 --- a/spec/services/order_forms/create_service_spec.rb +++ b/spec/services/order_forms/create_service_spec.rb @@ -5,7 +5,13 @@ RSpec.describe OrderForms::CreateService do subject(:create_service) { described_class.new(quote_version:) } - let(:organization) { create(:organization, feature_flags: ["order_forms"]) } + let_it_be(:organization) { create_default(:organization, feature_flags: ["order_forms"]) } + +before_all do + create_default(:customer) +create_default(:plan) +end + let(:quote) { create(:quote, organization:) } let(:quote_version) { create(:quote_version, :approved, quote:, organization:) } diff --git a/spec/services/order_forms/expire_service_spec.rb b/spec/services/order_forms/expire_service_spec.rb index bb53ac975be1..0881e8efe63c 100644 --- a/spec/services/order_forms/expire_service_spec.rb +++ b/spec/services/order_forms/expire_service_spec.rb @@ -5,8 +5,8 @@ RSpec.describe OrderForms::ExpireService do subject(:service) { described_class.new(order_form:) } - let(:organization) { create(:organization, feature_flags: ["order_forms"]) } - let(:customer) { create(:customer, organization:) } + let_it_be(:organization) { create(:organization, feature_flags: ["order_forms"]) } + let_it_be(:customer) { create(:customer, organization:) } let(:quote) { create(:quote, organization:, customer:) } let(:quote_version) { create(:quote_version, :approved, organization:, quote:) } let(:order_form) { create(:order_form, :expired_yesterday, customer:, organization:, quote_version:) } @@ -23,6 +23,7 @@ end context "when the order_forms feature flag is disabled", :premium do + let(:customer) { create(:customer, organization:) } let(:organization) { create(:organization) } it "returns a forbidden failure" do diff --git a/spec/services/order_forms/mark_as_signed_service_spec.rb b/spec/services/order_forms/mark_as_signed_service_spec.rb index 370318f4dcb5..1a483ddba034 100644 --- a/spec/services/order_forms/mark_as_signed_service_spec.rb +++ b/spec/services/order_forms/mark_as_signed_service_spec.rb @@ -5,8 +5,8 @@ RSpec.describe OrderForms::MarkAsSignedService do subject(:service) { described_class.new(order_form:, signed_document:, execution_mode:, execute_at:) } - let(:organization) { create(:organization, feature_flags: ["order_forms"]) } - let(:customer) { create(:customer, organization:) } + let_it_be(:organization) { create(:organization, feature_flags: ["order_forms"]) } + let_it_be(:customer) { create(:customer, organization:) } let(:quote) { create(:quote, customer:, organization:, order_type: :subscription_creation) } let(:order_form) { create(:order_form, customer:, organization:, quote:) } let(:signed_document) { nil } @@ -38,6 +38,7 @@ end context "when the order_forms feature flag is disabled" do + let(:customer) { create(:customer, organization:) } let(:organization) { create(:organization) } it "returns a forbidden failure" do diff --git a/spec/services/order_forms/void_service_spec.rb b/spec/services/order_forms/void_service_spec.rb index 8832d7e8bc59..b0d4ccd0604b 100644 --- a/spec/services/order_forms/void_service_spec.rb +++ b/spec/services/order_forms/void_service_spec.rb @@ -5,8 +5,8 @@ RSpec.describe OrderForms::VoidService do subject(:service) { described_class.new(order_form:) } - let(:organization) { create(:organization, feature_flags: ["order_forms"]) } - let(:customer) { create(:customer, organization:) } + let_it_be(:organization) { create(:organization, feature_flags: ["order_forms"]) } + let_it_be(:customer) { create(:customer, organization:) } let(:quote) { create(:quote, organization:, customer:) } let(:quote_version) { create(:quote_version, :approved, organization:, quote:) } let(:order_form) { create(:order_form, customer:, organization:, quote_version:) } diff --git a/spec/services/orders/execute_service_spec.rb b/spec/services/orders/execute_service_spec.rb index 05f0ea1e9d29..4e37aa7737c3 100644 --- a/spec/services/orders/execute_service_spec.rb +++ b/spec/services/orders/execute_service_spec.rb @@ -5,8 +5,13 @@ RSpec.describe Orders::ExecuteService do subject(:execute_service) { described_class.new(order:) } - let(:organization) { create(:organization, feature_flags: ["order_forms"]) } - let(:customer) { create(:customer, organization:) } + let_it_be(:organization) { create_default(:organization, feature_flags: ["order_forms"]) } + let_it_be(:customer) { create(:customer, organization:) } + +before_all do + create_default(:plan) +end + let(:quote) { create(:quote, organization:, customer:, order_type:) } let(:quote_version) { create(:quote_version, :approved, quote:, organization:) } let(:order_form) { create(:order_form, :signed, organization:, customer:, quote_version:) } @@ -38,6 +43,7 @@ end context "when the order_forms feature flag is disabled" do + let(:customer) { create(:customer, organization:) } let(:organization) { create(:organization) } it "returns a forbidden failure" do diff --git a/spec/services/orders/one_off/execute_service_spec.rb b/spec/services/orders/one_off/execute_service_spec.rb index 4b7a7ba7703b..4b748ae9d9dc 100644 --- a/spec/services/orders/one_off/execute_service_spec.rb +++ b/spec/services/orders/one_off/execute_service_spec.rb @@ -5,10 +5,10 @@ RSpec.describe Orders::OneOff::ExecuteService do subject(:execute_service) { described_class.new(order:) } - let(:organization) { create(:organization) } - let(:billing_entity) { create(:billing_entity, organization:) } - let(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } - let(:add_on) { create(:add_on, organization:, amount_cents: 10_000) } + let_it_be(:organization) { create(:organization) } + let_it_be(:billing_entity) { create(:billing_entity, organization:) } + let_it_be(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } + let_it_be(:add_on) { create(:add_on, organization:, amount_cents: 10_000) } let(:add_on_item) do { "id" => add_on.id, diff --git a/spec/services/orders/subscription_amendment/execute_service_spec.rb b/spec/services/orders/subscription_amendment/execute_service_spec.rb index e1a13edf39db..7db1fba55edc 100644 --- a/spec/services/orders/subscription_amendment/execute_service_spec.rb +++ b/spec/services/orders/subscription_amendment/execute_service_spec.rb @@ -7,11 +7,11 @@ RSpec.describe Orders::SubscriptionAmendment::ExecuteService, :premium do subject(:execute_service) { described_class.new(order:) } - let(:organization) { create(:organization) } - let(:billing_entity) { create(:billing_entity, organization:) } - let(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } + let_it_be(:organization) { create(:organization) } + let_it_be(:billing_entity) { create(:billing_entity, organization:) } + let_it_be(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } - let(:target_plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 50_000) } + let_it_be(:target_plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 50_000) } let(:target_subscription) do create( :subscription, @@ -27,8 +27,8 @@ ) end - let(:plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 100_000) } - let(:billable_metric) { create(:billable_metric, organization:, code: "api_calls") } + let_it_be(:plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 100_000) } + let_it_be(:billable_metric) { create(:billable_metric, organization:, code: "api_calls") } let(:charge) { create(:standard_charge, plan:, billable_metric:, properties: {"amount" => "50"}) } let(:plan_item) do @@ -254,6 +254,11 @@ end context "with usage thresholds" do + let(:plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 100_000) } + let(:target_plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 50_000) } + let(:billable_metric) { create(:billable_metric, organization:, code: "api_calls") } + let(:billing_entity) { create(:billing_entity, organization:) } + let(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } let(:organization) { create(:organization, premium_integrations: ["progressive_billing"]) } let(:plan_overrides) do super().merge( diff --git a/spec/services/orders/subscription_creation/execute_service_spec.rb b/spec/services/orders/subscription_creation/execute_service_spec.rb index 196d14562ba3..06fb75e12737 100644 --- a/spec/services/orders/subscription_creation/execute_service_spec.rb +++ b/spec/services/orders/subscription_creation/execute_service_spec.rb @@ -7,11 +7,11 @@ RSpec.describe Orders::SubscriptionCreation::ExecuteService, :premium do subject(:execute_service) { described_class.new(order:) } - let(:organization) { create(:organization) } - let(:billing_entity) { create(:billing_entity, organization:) } - let(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } - let(:plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 100_000) } - let(:billable_metric) { create(:billable_metric, organization:, code: "api_calls") } + let_it_be(:organization) { create(:organization) } + let_it_be(:billing_entity) { create(:billing_entity, organization:) } + let_it_be(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } + let_it_be(:plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 100_000) } + let_it_be(:billable_metric) { create(:billable_metric, organization:, code: "api_calls") } let(:charge) { create(:standard_charge, plan:, billable_metric:, properties: {"amount" => "50"}) } let(:plan_item) do @@ -269,6 +269,10 @@ end context "with usage thresholds" do + let(:plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 100_000) } + let(:billable_metric) { create(:billable_metric, organization:, code: "api_calls") } + let(:billing_entity) { create(:billing_entity, organization:) } + let(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } let(:organization) { create(:organization, premium_integrations: ["progressive_billing"]) } let(:plan_overrides) do super().merge( diff --git a/spec/services/orders/update_service_spec.rb b/spec/services/orders/update_service_spec.rb index 35e5848c45dd..791ed8b6601b 100644 --- a/spec/services/orders/update_service_spec.rb +++ b/spec/services/orders/update_service_spec.rb @@ -5,8 +5,8 @@ RSpec.describe Orders::UpdateService do subject(:service) { described_class.new(order:, params:) } - let(:organization) { create(:organization, feature_flags: ["order_forms"]) } - let(:customer) { create(:customer, organization:) } + let_it_be(:organization) { create(:organization, feature_flags: ["order_forms"]) } + let_it_be(:customer) { create(:customer, organization:) } let(:order) { create(:order, organization:, customer:) } let(:params) { {execution_mode: "execute_in_lago", execute_at: 1.month.from_now.iso8601} } @@ -35,6 +35,7 @@ end context "when the order_forms feature flag is disabled" do + let(:customer) { create(:customer, organization:) } let(:organization) { create(:organization) } it "returns a forbidden failure" do From a294733d9313d220419a9fa0bd012b219286168e Mon Sep 17 00:00:00 2001 From: Julia Date: Mon, 31 Aug 2026 12:43:18 +0400 Subject: [PATCH 5/5] Fix linters --- .../lifetime_usages/calculate_service_spec.rb | 23 +++++++++---------- .../check_thresholds_service_spec.rb | 2 +- ...d_last_and_next_thresholds_service_spec.rb | 6 ++--- .../flag_refresh_from_invoice_service_spec.rb | 11 ++++----- ...g_refresh_from_plan_update_service_spec.rb | 6 ++--- .../lifetime_usages/update_service_spec.rb | 10 ++++---- .../usage_thresholds/check_service_spec.rb | 12 +++++----- ...sage_thresholds_completion_service_spec.rb | 6 ++--- .../memberships/create_service_spec.rb | 1 + .../memberships/revoke_service_spec.rb | 2 +- .../memberships/update_service_spec.rb | 2 +- .../metadata/delete_item_key_service_spec.rb | 6 ++--- .../metadata/update_item_service_spec.rb | 6 ++--- .../activity_log_middleware_spec.rb | 14 +++++------ .../order_forms/create_service_spec.rb | 8 +++---- .../order_forms/expire_service_spec.rb | 2 +- .../mark_as_signed_service_spec.rb | 2 +- spec/services/orders/execute_service_spec.rb | 8 +++---- .../execute_service_spec.rb | 14 +++++------ .../execute_service_spec.rb | 8 +++---- spec/services/orders/update_service_spec.rb | 2 +- 21 files changed, 73 insertions(+), 78 deletions(-) diff --git a/spec/services/lifetime_usages/calculate_service_spec.rb b/spec/services/lifetime_usages/calculate_service_spec.rb index a261bb1de676..7da597da446e 100644 --- a/spec/services/lifetime_usages/calculate_service_spec.rb +++ b/spec/services/lifetime_usages/calculate_service_spec.rb @@ -6,17 +6,6 @@ subject(:service) { described_class.new(lifetime_usage: lifetime_usage) } let(:lifetime_usage) { create(:lifetime_usage, organization:, subscription:, recalculate_current_usage:, recalculate_invoiced_usage:) } - let(:recalculate_current_usage) { false } - let(:recalculate_invoiced_usage) { false } - let(:subscription) { create(:subscription, customer:, subscription_at:) } - let_it_be(:organization) { create_default(:organization)} - let_it_be(:billable_metric) { create(:billable_metric, organization:, aggregation_type: "count_agg") } - let_it_be(:customer) { create_default(:customer) } - -before_all do - create_default(:plan) -end - let(:charge) { create(:standard_charge, plan: subscription.plan, billable_metric:, properties: {amount: "10"}) } let(:timestamp) { Time.current } let(:subscription_at) { timestamp - 6.months } @@ -32,7 +21,6 @@ precise_coupons_amount_cents: 50 ) end - let(:events) do create_list( :event, @@ -44,6 +32,17 @@ timestamp: ) end + let(:recalculate_current_usage) { false } + let(:recalculate_invoiced_usage) { false } + let(:subscription) { create(:subscription, customer:, subscription_at:) } + + let_it_be(:organization) { create_default(:organization) } + let_it_be(:billable_metric) { create(:billable_metric, organization:, aggregation_type: "count_agg") } + let_it_be(:customer) { create_default(:customer) } + + before_all do + create_default(:plan) + end describe "#recalculate_invoiced_usage" do let(:recalculate_invoiced_usage) { true } diff --git a/spec/services/lifetime_usages/check_thresholds_service_spec.rb b/spec/services/lifetime_usages/check_thresholds_service_spec.rb index 41a48834c6f8..f2efa1300195 100644 --- a/spec/services/lifetime_usages/check_thresholds_service_spec.rb +++ b/spec/services/lifetime_usages/check_thresholds_service_spec.rb @@ -8,7 +8,7 @@ let(:lifetime_usage) { create(:lifetime_usage, subscription:, recalculate_current_usage: true, recalculate_invoiced_usage: true, current_usage_amount_cents:) } let(:current_usage_amount_cents) { 0 } let(:subscription) { create(:subscription, customer_id: customer.id) } - let(:organization) { create_default(:organization)} + let(:organization) { create_default(:organization) } let(:customer) { create_default(:customer) } let(:billable_metric) { create_default(:billable_metric, aggregation_type: "count_agg") } diff --git a/spec/services/lifetime_usages/find_last_and_next_thresholds_service_spec.rb b/spec/services/lifetime_usages/find_last_and_next_thresholds_service_spec.rb index 0061ecc9413d..8a488bdd05d6 100644 --- a/spec/services/lifetime_usages/find_last_and_next_thresholds_service_spec.rb +++ b/spec/services/lifetime_usages/find_last_and_next_thresholds_service_spec.rb @@ -6,12 +6,12 @@ subject(:lifetime_usage_result) { described_class.call(lifetime_usage:) } let(:lifetime_usage) { create(:lifetime_usage, subscription:, organization:, current_usage_amount_cents:) } - let(:current_usage_amount_cents) { 0 } - - let_it_be(:organization) { create_default(:organization)} let(:plan) { create(:plan) } let(:customer) { create(:customer, organization:) } let(:subscription) { create(:subscription, plan:, customer:) } + let(:current_usage_amount_cents) { 0 } + + let_it_be(:organization) { create_default(:organization) } it "computes the amounts" do expect(lifetime_usage_result.last_threshold_amount_cents).to be_nil diff --git a/spec/services/lifetime_usages/flag_refresh_from_invoice_service_spec.rb b/spec/services/lifetime_usages/flag_refresh_from_invoice_service_spec.rb index 2a81c8150f13..8a220c190b88 100644 --- a/spec/services/lifetime_usages/flag_refresh_from_invoice_service_spec.rb +++ b/spec/services/lifetime_usages/flag_refresh_from_invoice_service_spec.rb @@ -6,17 +6,16 @@ subject(:flag_service) { described_class.new(invoice:) } let(:invoice) { create(:invoice, :subscription, subscriptions:, organization: customer.organization) } + let(:subscriptions) { create_list(:subscription, 1, plan:) } + let(:usage_threshold) { create(:usage_threshold, plan:) } let(:lifetime_usage) { create(:lifetime_usage, subscription: invoice.subscriptions.first) } -before_all do - create_default(:organization) -end + before_all do + create_default(:organization) + end let_it_be(:customer) { create_default(:customer) } let_it_be(:plan) { create_default(:plan, organization: customer.organization) } - let(:subscriptions) { create_list(:subscription, 1, plan:) } - - let(:usage_threshold) { create(:usage_threshold, plan:) } before do usage_threshold diff --git a/spec/services/lifetime_usages/flag_refresh_from_plan_update_service_spec.rb b/spec/services/lifetime_usages/flag_refresh_from_plan_update_service_spec.rb index ee2e1c087c6f..fc4b9304c319 100644 --- a/spec/services/lifetime_usages/flag_refresh_from_plan_update_service_spec.rb +++ b/spec/services/lifetime_usages/flag_refresh_from_plan_update_service_spec.rb @@ -5,9 +5,9 @@ RSpec.describe LifetimeUsages::FlagRefreshFromPlanUpdateService do subject { described_class.call(plan:) } -before_all do - create_default(:organization) -end + before_all do + create_default(:organization) + end let_it_be(:plan) { create(:plan) } let(:result) { subject } diff --git a/spec/services/lifetime_usages/update_service_spec.rb b/spec/services/lifetime_usages/update_service_spec.rb index d71281319fc8..1fecd923c2a7 100644 --- a/spec/services/lifetime_usages/update_service_spec.rb +++ b/spec/services/lifetime_usages/update_service_spec.rb @@ -5,11 +5,11 @@ RSpec.describe LifetimeUsages::UpdateService do subject(:update_service) { described_class.new(lifetime_usage:, params:) } -before_all do - create_default(:organization) -create_default(:customer) -create_default(:plan) -end + before_all do + create_default(:organization) + create_default(:customer) + create_default(:plan) + end let(:lifetime_usage) { create(:lifetime_usage) } let(:params) do diff --git a/spec/services/lifetime_usages/usage_thresholds/check_service_spec.rb b/spec/services/lifetime_usages/usage_thresholds/check_service_spec.rb index 38d885b6b38d..f816a77d7b88 100644 --- a/spec/services/lifetime_usages/usage_thresholds/check_service_spec.rb +++ b/spec/services/lifetime_usages/usage_thresholds/check_service_spec.rb @@ -6,18 +6,18 @@ subject(:service) { described_class.new(lifetime_usage:, progressive_billed_amount:) } let(:lifetime_usage) { create(:lifetime_usage, subscription:, historical_usage_amount_cents:, recalculate_current_usage:, recalculate_invoiced_usage:) } + let(:historical_usage_amount_cents) { 0 } let(:progressive_billed_amount) { 0 } let(:recalculate_current_usage) { true } let(:recalculate_invoiced_usage) { true } let(:subscription) { create(:subscription, customer_id: customer.id) } - let_it_be(:organization) { create_default(:organization)} - let_it_be(:customer) { create_default(:customer) } -before_all do - create_default(:plan) -end + let_it_be(:organization) { create_default(:organization) } + let_it_be(:customer) { create_default(:customer) } - let(:historical_usage_amount_cents) { 0 } + before_all do + create_default(:plan) + end def create_thresholds(subscription, amounts:, attach_to:, recurring: nil) model = if attach_to == :subscription diff --git a/spec/services/lifetime_usages/usage_thresholds_completion_service_spec.rb b/spec/services/lifetime_usages/usage_thresholds_completion_service_spec.rb index 1c6029938a7a..15af58c5a955 100644 --- a/spec/services/lifetime_usages/usage_thresholds_completion_service_spec.rb +++ b/spec/services/lifetime_usages/usage_thresholds_completion_service_spec.rb @@ -6,12 +6,12 @@ subject(:result) { described_class.call(lifetime_usage:) } let(:lifetime_usage) { create(:lifetime_usage, subscription:, organization:, current_usage_amount_cents:) } - let(:current_usage_amount_cents) { 0 } - - let_it_be(:organization) { create_default(:organization)} let(:plan) { create(:plan) } let(:customer) { create(:customer, organization:) } let(:subscription) { create(:subscription, plan:, customer:) } + let(:current_usage_amount_cents) { 0 } + + let_it_be(:organization) { create_default(:organization) } def create_threshold(attached_to:, **factory_args) if attached_to == :subscription diff --git a/spec/services/memberships/create_service_spec.rb b/spec/services/memberships/create_service_spec.rb index b0e05038a1f5..afe39f63cfdd 100644 --- a/spec/services/memberships/create_service_spec.rb +++ b/spec/services/memberships/create_service_spec.rb @@ -6,6 +6,7 @@ subject(:create_service) { described_class.new(user:, organization:) } let(:user) { create(:user) } + let_it_be(:organization) { create(:organization) } describe "#call" do diff --git a/spec/services/memberships/revoke_service_spec.rb b/spec/services/memberships/revoke_service_spec.rb index ebab5bdf8b22..ef65389a0138 100644 --- a/spec/services/memberships/revoke_service_spec.rb +++ b/spec/services/memberships/revoke_service_spec.rb @@ -9,11 +9,11 @@ let_it_be(:organization) { create(:organization) } let(:admin_role) { create(:role, :admin) } + let(:other_membership) { create(:membership, user:, organization:) } let(:finance_role) { create(:role, :finance) } let_it_be(:user) { create(:user) } let_it_be(:membership) { create(:membership, organization:) } - let(:other_membership) { create(:membership, user:, organization:) } describe "#call" do context "when revoking my own membership" do diff --git a/spec/services/memberships/update_service_spec.rb b/spec/services/memberships/update_service_spec.rb index 64b66ac058ab..a58f2082361f 100644 --- a/spec/services/memberships/update_service_spec.rb +++ b/spec/services/memberships/update_service_spec.rb @@ -5,7 +5,7 @@ RSpec.describe Memberships::UpdateService do include_context "with mocked security logger" - let_it_be(:organization) { create_default(:organization)} + let_it_be(:organization) { create_default(:organization) } let_it_be(:membership) { create(:membership) } let_it_be(:acting_user) { create(:membership, organization:).user } let(:admin_role) { create(:role, :admin) } diff --git a/spec/services/metadata/delete_item_key_service_spec.rb b/spec/services/metadata/delete_item_key_service_spec.rb index 533138b9ba39..93aa80a1d111 100644 --- a/spec/services/metadata/delete_item_key_service_spec.rb +++ b/spec/services/metadata/delete_item_key_service_spec.rb @@ -7,9 +7,9 @@ let_it_be(:organization) { create_default(:organization) } -before_all do - create_default(:customer) -end + before_all do + create_default(:customer) + end let(:owner) { create(:credit_note, organization:) } let(:item) { create(:item_metadata, owner:, organization:, value:) } diff --git a/spec/services/metadata/update_item_service_spec.rb b/spec/services/metadata/update_item_service_spec.rb index 75576877f19b..aa20b4eb8846 100644 --- a/spec/services/metadata/update_item_service_spec.rb +++ b/spec/services/metadata/update_item_service_spec.rb @@ -7,9 +7,9 @@ let_it_be(:organization) { create_default(:organization) } -before_all do - create_default(:customer) -end + before_all do + create_default(:customer) + end let(:owner) { create(:credit_note, organization:) } let(:value) { nil } diff --git a/spec/services/middlewares/activity_log_middleware_spec.rb b/spec/services/middlewares/activity_log_middleware_spec.rb index 9f383ba91978..c55acf3c213c 100644 --- a/spec/services/middlewares/activity_log_middleware_spec.rb +++ b/spec/services/middlewares/activity_log_middleware_spec.rb @@ -26,17 +26,15 @@ def call attr_reader :subscription end end - -before_all do - create_default(:organization) -create_default(:customer) -create_default(:plan) -end - - let(:subscription) { create(:subscription, name: "My Subscription") } let(:activity_loggable_after_commit) { false } + before_all do + create_default(:organization) + create_default(:customer) + create_default(:plan) + end + def test_service_with_activity_loggable(after_commit:, action_match_updated: false) expect(service_class).to use_middleware(described_class) diff --git a/spec/services/order_forms/create_service_spec.rb b/spec/services/order_forms/create_service_spec.rb index acc94f261554..8497353bd6ec 100644 --- a/spec/services/order_forms/create_service_spec.rb +++ b/spec/services/order_forms/create_service_spec.rb @@ -7,10 +7,10 @@ let_it_be(:organization) { create_default(:organization, feature_flags: ["order_forms"]) } -before_all do - create_default(:customer) -create_default(:plan) -end + before_all do + create_default(:customer) + create_default(:plan) + end let(:quote) { create(:quote, organization:) } let(:quote_version) { create(:quote_version, :approved, quote:, organization:) } diff --git a/spec/services/order_forms/expire_service_spec.rb b/spec/services/order_forms/expire_service_spec.rb index 0881e8efe63c..b17809e553d9 100644 --- a/spec/services/order_forms/expire_service_spec.rb +++ b/spec/services/order_forms/expire_service_spec.rb @@ -6,7 +6,7 @@ subject(:service) { described_class.new(order_form:) } let_it_be(:organization) { create(:organization, feature_flags: ["order_forms"]) } - let_it_be(:customer) { create(:customer, organization:) } + let_it_be(:customer) { create(:customer, organization:) } let(:quote) { create(:quote, organization:, customer:) } let(:quote_version) { create(:quote_version, :approved, organization:, quote:) } let(:order_form) { create(:order_form, :expired_yesterday, customer:, organization:, quote_version:) } diff --git a/spec/services/order_forms/mark_as_signed_service_spec.rb b/spec/services/order_forms/mark_as_signed_service_spec.rb index 1a483ddba034..47faf72273be 100644 --- a/spec/services/order_forms/mark_as_signed_service_spec.rb +++ b/spec/services/order_forms/mark_as_signed_service_spec.rb @@ -6,7 +6,7 @@ subject(:service) { described_class.new(order_form:, signed_document:, execution_mode:, execute_at:) } let_it_be(:organization) { create(:organization, feature_flags: ["order_forms"]) } - let_it_be(:customer) { create(:customer, organization:) } + let_it_be(:customer) { create(:customer, organization:) } let(:quote) { create(:quote, customer:, organization:, order_type: :subscription_creation) } let(:order_form) { create(:order_form, customer:, organization:, quote:) } let(:signed_document) { nil } diff --git a/spec/services/orders/execute_service_spec.rb b/spec/services/orders/execute_service_spec.rb index 4e37aa7737c3..8ad7a5654394 100644 --- a/spec/services/orders/execute_service_spec.rb +++ b/spec/services/orders/execute_service_spec.rb @@ -6,11 +6,11 @@ subject(:execute_service) { described_class.new(order:) } let_it_be(:organization) { create_default(:organization, feature_flags: ["order_forms"]) } - let_it_be(:customer) { create(:customer, organization:) } + let_it_be(:customer) { create(:customer, organization:) } -before_all do - create_default(:plan) -end + before_all do + create_default(:plan) + end let(:quote) { create(:quote, organization:, customer:, order_type:) } let(:quote_version) { create(:quote_version, :approved, quote:, organization:) } diff --git a/spec/services/orders/subscription_amendment/execute_service_spec.rb b/spec/services/orders/subscription_amendment/execute_service_spec.rb index 7db1fba55edc..81b831c16bd8 100644 --- a/spec/services/orders/subscription_amendment/execute_service_spec.rb +++ b/spec/services/orders/subscription_amendment/execute_service_spec.rb @@ -8,10 +8,10 @@ subject(:execute_service) { described_class.new(order:) } let_it_be(:organization) { create(:organization) } - let_it_be(:billing_entity) { create(:billing_entity, organization:) } - let_it_be(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } + let_it_be(:billing_entity) { create(:billing_entity, organization:) } + let_it_be(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } - let_it_be(:target_plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 50_000) } + let_it_be(:target_plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 50_000) } let(:target_subscription) do create( :subscription, @@ -26,11 +26,7 @@ ending_at: 5.months.from_now ) end - - let_it_be(:plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 100_000) } - let_it_be(:billable_metric) { create(:billable_metric, organization:, code: "api_calls") } let(:charge) { create(:standard_charge, plan:, billable_metric:, properties: {"amount" => "50"}) } - let(:plan_item) do { "id" => plan.id, @@ -66,7 +62,6 @@ ] } end - let(:billing_items) { {"plans" => [plan_item]} } let(:quote) do create( @@ -91,6 +86,9 @@ let(:order) { create(:order, organization:, customer:, order_form:, execution_mode:) } let(:execution_mode) { :execute_in_lago } + let_it_be(:plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 100_000) } + let_it_be(:billable_metric) { create(:billable_metric, organization:, code: "api_calls") } + # Records are resolved by id only outside api context, and CurrentContext leaks across spec # files (no global reset), so pin it. The order is built up front so the target subscription # never counts toward what an execution creates. diff --git a/spec/services/orders/subscription_creation/execute_service_spec.rb b/spec/services/orders/subscription_creation/execute_service_spec.rb index 06fb75e12737..e59a7aff7a0e 100644 --- a/spec/services/orders/subscription_creation/execute_service_spec.rb +++ b/spec/services/orders/subscription_creation/execute_service_spec.rb @@ -8,10 +8,10 @@ subject(:execute_service) { described_class.new(order:) } let_it_be(:organization) { create(:organization) } - let_it_be(:billing_entity) { create(:billing_entity, organization:) } - let_it_be(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } - let_it_be(:plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 100_000) } - let_it_be(:billable_metric) { create(:billable_metric, organization:, code: "api_calls") } + let_it_be(:billing_entity) { create(:billing_entity, organization:) } + let_it_be(:customer) { create(:customer, organization:, billing_entity:, currency: "EUR") } + let_it_be(:plan) { create(:plan, organization:, amount_currency: "EUR", amount_cents: 100_000) } + let_it_be(:billable_metric) { create(:billable_metric, organization:, code: "api_calls") } let(:charge) { create(:standard_charge, plan:, billable_metric:, properties: {"amount" => "50"}) } let(:plan_item) do diff --git a/spec/services/orders/update_service_spec.rb b/spec/services/orders/update_service_spec.rb index 791ed8b6601b..2e6cf50e498e 100644 --- a/spec/services/orders/update_service_spec.rb +++ b/spec/services/orders/update_service_spec.rb @@ -6,7 +6,7 @@ subject(:service) { described_class.new(order:, params:) } let_it_be(:organization) { create(:organization, feature_flags: ["order_forms"]) } - let_it_be(:customer) { create(:customer, organization:) } + let_it_be(:customer) { create(:customer, organization:) } let(:order) { create(:order, organization:, customer:) } let(:params) { {execution_mode: "execute_in_lago", execute_at: 1.month.from_now.iso8601} }