Files
openfoodnetwork/spec/services/permissions/order_spec.rb
Neal Chambers f47f6efcd0 Safely autocorrect Rails/DurationArithmetic
Inspecting 1481 files
...........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................C.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................C.C.................................................................................................................................................................................................................................................................................C..............C..........................................................C...............................C....C..........CC................................................................

Offenses:

app/services/create_order_cycle.rb:41:24: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
      orders_close_at: Time.zone.now + 1.month
                       ^^^^^^^^^^^^^^^^^^^^^^^
spec/jobs/order_cycle_closing_job_spec.rb:7:74: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
    create(:order_cycle, automatic_notifications: true, orders_close_at: Time.zone.now - 1.minute)
                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^
spec/jobs/order_cycle_closing_job_spec.rb:10:74: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
    create(:order_cycle, automatic_notifications: true, orders_close_at: Time.zone.now + 1.minute)
                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^
spec/jobs/order_cycle_closing_job_spec.rb:13:75: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
    create(:order_cycle, automatic_notifications: false, orders_close_at: Time.zone.now - 1.minute)
                                                                          ^^^^^^^^^^^^^^^^^^^^^^^^
spec/jobs/order_cycle_opened_job_spec.rb:7:42: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
    create(:order_cycle, orders_open_at: Time.zone.now - 1.hour)
                                         ^^^^^^^^^^^^^^^^^^^^^^
spec/jobs/order_cycle_opened_job_spec.rb:13:42: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
    create(:order_cycle, orders_open_at: Time.zone.now + 1.minute)
                                         ^^^^^^^^^^^^^^^^^^^^^^^^
spec/services/permissions/order_spec.rb:26:58: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
                                           completed_at: Time.zone.now - 1.year)
                                                         ^^^^^^^^^^^^^^^^^^^^^^
spec/services/terms_of_service_spec.rb:21:71: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
      allow(customer).to receive(:terms_and_conditions_accepted_at) { Time.zone.now - 1.week }
                                                                      ^^^^^^^^^^^^^^^^^^^^^^
spec/services/terms_of_service_spec.rb:22:59: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
      allow(TermsOfServiceFile).to receive(:updated_at) { Time.zone.now - 2.weeks }
                                                          ^^^^^^^^^^^^^^^^^^^^^^^
spec/services/terms_of_service_spec.rb:36:71: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
      allow(customer).to receive(:terms_and_conditions_accepted_at) { Time.zone.now - 1.week }
                                                                      ^^^^^^^^^^^^^^^^^^^^^^
spec/services/terms_of_service_spec.rb:38:45: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
        ActiveStorage::Blob.new(created_at: Time.zone.now - 2.weeks)
                                            ^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/bulk_order_management_spec.rb:647:57: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
                                          completed_at: Time.zone.now + 1.week,
                                                        ^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/bulk_order_management_spec.rb:652:57: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
                                          completed_at: Time.zone.now + 2.weeks,
                                                        ^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/bulk_order_management_spec.rb:661:40: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
          oc3.update!(orders_close_at: Time.zone.now + 2.weeks)
                                       ^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/bulk_order_management_spec.rb:662:39: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
          oc3.update!(orders_open_at: Time.zone.now + 1.week)
                                      ^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/order_cycles/list_spec.rb:128:18: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
    oc_open_at = Time.zone.now - 2.weeks
                 ^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/order_cycles/list_spec.rb:129:19: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
    oc_close_at = Time.zone.now + 2.weeks
                  ^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/orders_spec.rb:289:40: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
          order2.update!(completed_at: Time.zone.now - 2.weeks)
                                       ^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/orders_spec.rb:290:40: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
          order3.update!(completed_at: Time.zone.now - 3.weeks)
                                       ^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/orders_spec.rb:291:40: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
          order4.update!(completed_at: Time.zone.now - 4.weeks)
                                       ^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/orders_spec.rb:292:40: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
          order5.update!(completed_at: Time.zone.now - 5.weeks)
                                       ^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/reports/orders_and_fulfillment_spec.rb:113:31: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
        let(:completed_at1) { Time.zone.now - 1500.hours } # 1500 hours in the past
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/reports/orders_and_fulfillment_spec.rb:114:31: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
        let(:completed_at2) { Time.zone.now - 1700.hours } # 1700 hours in the past
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/reports/orders_and_fulfillment_spec.rb:115:33: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
        let(:datetime_start1) { Time.zone.now - 1600.hours } # 1600 hours in the past
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/reports/orders_and_fulfillment_spec.rb:116:33: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
        let(:datetime_start2) { Time.zone.now - 1800.hours } # 1600 hours in the past
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/reports/orders_and_fulfillment_spec.rb:117:30: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
        let(:datetime_end) { Time.zone.now - 1400.hours } # 1400 hours in the past
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/system/admin/reports/packing_report_spec.rb:13:26: C: [Corrected] Style/RedundantParentheses: Don't use parentheses around a method call.
  let!(:open_datetime) { (1.month.ago).strftime("%Y-%m-%d 00:00") }
                         ^^^^^^^^^^^^^
spec/system/admin/reports/packing_report_spec.rb:13:27: C: [Corrected] Rails/DurationArithmetic: Do not add or subtract duration.
  let!(:open_datetime) { (Time.zone.now - 1.month).strftime("%Y-%m-%d 00:00") }
                          ^^^^^^^^^^^^^^^^^^^^^^^

1481 files inspected, 28 offenses detected, 28 offenses corrected
2023-08-01 22:50:21 +09:00

217 lines
8.7 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
module Permissions
describe Order do
let(:user) { double(:user) }
let(:permissions) { Permissions::Order.new(user) }
let!(:basic_permissions) { OpenFoodNetwork::Permissions.new(user) }
let(:distributor) { create(:distributor_enterprise) }
let(:coordinator) { create(:distributor_enterprise) }
let(:order_cycle) {
create(:simple_order_cycle, coordinator: coordinator, distributors: [distributor])
}
let(:order_completed) {
create(:completed_order_with_totals, order_cycle: order_cycle, distributor: distributor )
}
let(:order_cancelled) {
create(:order, order_cycle: order_cycle, distributor: distributor, state: 'canceled' )
}
let(:order_cart) {
create(:order, order_cycle: order_cycle, distributor: distributor, state: 'cart' )
}
let(:order_from_last_year) {
create(:completed_order_with_totals, order_cycle: order_cycle, distributor: distributor,
completed_at: 1.year.ago)
}
before { allow(OpenFoodNetwork::Permissions).to receive(:new) { basic_permissions } }
describe "finding orders that are visible in reports" do
let(:random_enterprise) { create(:distributor_enterprise) }
let(:order) { create(:order, order_cycle: order_cycle, distributor: distributor ) }
let!(:line_item) { create(:line_item, order: order) }
let!(:producer) { create(:supplier_enterprise) }
before do
allow(basic_permissions).to receive(:coordinated_order_cycles) { Enterprise.where("1=0") }
end
context "as the hub through which the order was placed" do
before do
allow(basic_permissions).to receive(:managed_enterprises) {
Enterprise.where(id: distributor)
}
end
it "should let me see the order" do
expect(permissions.visible_orders).to include order
end
end
context "as the coordinator of the order cycle through which the order was placed" do
before do
allow(basic_permissions).to receive(:managed_enterprises) {
Enterprise.where(id: coordinator)
}
allow(basic_permissions).to receive(:coordinated_order_cycles) {
OrderCycle.where(id: order_cycle)
}
end
it "should let me see the order" do
expect(permissions.visible_orders).to include order
end
context "with search params" do
let(:search_params) { { completed_at_gt: Time.zone.now.yesterday.strftime('%Y-%m-%d') } }
let(:permissions) { Permissions::Order.new(user, search_params) }
it "only returns completed, non-cancelled orders within search filter range" do
expect(permissions.visible_orders).to include order_completed
expect(permissions.visible_orders).to_not include order_cancelled
expect(permissions.visible_orders).to_not include order_cart
expect(permissions.visible_orders).to_not include order_from_last_year
end
end
end
context "as a producer which has granted P-OC to the distributor of an order" do
before do
allow(basic_permissions).to receive(:managed_enterprises) {
Enterprise.where(id: producer)
}
create(:enterprise_relationship, parent: producer, child: distributor,
permissions_list: [:add_to_order_cycle])
end
context "which contains my products" do
before do
line_item.product.supplier = producer
line_item.product.save
end
it "should let me see the order" do
expect(permissions.visible_orders).to include order
end
end
context "which does not contain my products" do
it "should not let me see the order" do
expect(permissions.visible_orders).to_not include order
end
end
end
context "as an enterprise that is a distributor in the order cycle, " \
"but not the distributor of the order" do
before do
allow(basic_permissions).to receive(:managed_enterprises) {
Enterprise.where(id: random_enterprise)
}
end
it "should not let me see the order" do
expect(permissions.visible_orders).to_not include order
end
end
end
describe "finding line items that are visible in reports" do
let(:random_enterprise) { create(:distributor_enterprise) }
let(:order) { create(:order, order_cycle: order_cycle, distributor: distributor ) }
let!(:line_item1) { create(:line_item, order: order) }
let!(:line_item2) { create(:line_item, order: order) }
let!(:producer) { create(:supplier_enterprise) }
before do
allow(basic_permissions).to receive(:coordinated_order_cycles) { Enterprise.where("1=0") }
end
context "as the hub through which the parent order was placed" do
before do
allow(basic_permissions).to receive(:managed_enterprises) {
Enterprise.where(id: distributor)
}
end
it "should let me see the line_items" do
expect(permissions.visible_line_items).to include line_item1, line_item2
end
end
context "as the coordinator of the order cycle through which the parent order was placed" do
before do
allow(basic_permissions).to receive(:managed_enterprises) {
Enterprise.where(id: coordinator)
}
allow(basic_permissions).to receive(:coordinated_order_cycles) {
OrderCycle.where(id: order_cycle)
}
end
it "should let me see the line_items" do
expect(permissions.visible_line_items).to include line_item1, line_item2
end
end
context "as the manager producer which has granted P-OC to the distributor " \
"of the parent order" do
before do
allow(basic_permissions).to receive(:managed_enterprises) {
Enterprise.where(id: producer)
}
create(:enterprise_relationship, parent: producer, child: distributor,
permissions_list: [:add_to_order_cycle])
line_item1.product.supplier = producer
line_item1.product.save
end
it "should let me see the line_items pertaining to variants I produce" do
ps = permissions.visible_line_items
expect(ps).to include line_item1
expect(ps).to_not include line_item2
end
end
context "as an enterprise that is a distributor in the order cycle, " \
"but not the distributor of the parent order" do
before do
allow(basic_permissions).to receive(:managed_enterprises) {
Enterprise.where(id: random_enterprise)
}
end
it "should not let me see the line_items" do
expect(permissions.visible_line_items).to_not include line_item1, line_item2
end
end
context "with search params" do
let!(:line_item3) { create(:line_item, order: order_completed) }
let!(:line_item4) { create(:line_item, order: order_cancelled) }
let!(:line_item5) { create(:line_item, order: order_cart) }
let!(:line_item6) { create(:line_item, order: order_from_last_year) }
let(:search_params) { { completed_at_gt: Time.zone.now.yesterday.strftime('%Y-%m-%d') } }
let(:permissions) { Permissions::Order.new(user, search_params) }
before do
allow(user).to receive(:has_spree_role?) { "admin" }
end
it "only returns line items from completed, " \
"non-cancelled orders within search filter range" do
expect(permissions.visible_line_items).to include order_completed.line_items.first
expect(permissions.visible_line_items).to_not include order_cancelled.line_items.first
expect(permissions.visible_line_items).to_not include order_cart.line_items.first
expect(permissions.visible_line_items)
.to_not include order_from_last_year.line_items.first
end
end
end
end
end