From e0931545e89c23783d22a055a7689704f8946b07 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 16 Feb 2023 16:08:24 +0100 Subject: [PATCH] (quality) remove obsolete NotificationType.find_by_name --- app/workers/reservation_reminder_worker.rb | 2 +- test/integration/abuses_test.rb | 2 +- test/integration/auth_providers_test.rb | 2 +- test/integration/events/recurrence_test.rb | 4 +- .../exports/members_export_test.rb | 2 +- .../subscriptions/create_as_admin_test.rb | 6 +- .../subscriptions/create_as_user_test.rb | 51 +--------------- .../create_with_payment_schedule_test.rb | 59 +++++++++++++++++++ .../subscriptions/free_extension_test.rb | 2 +- .../subscriptions/renew_as_admin_test.rb | 4 +- .../subscriptions/renew_as_user_test.rb | 2 +- .../previews/notifications_mailer_preview.rb | 2 +- .../trainings/authorization_service_test.rb | 4 +- .../trainings/auto_cancel_service_test.rb | 12 ++-- .../trainings/invalidation_service_test.rb | 10 ++-- 15 files changed, 89 insertions(+), 75 deletions(-) create mode 100644 test/integration/subscriptions/create_with_payment_schedule_test.rb diff --git a/app/workers/reservation_reminder_worker.rb b/app/workers/reservation_reminder_worker.rb index 613d6a41c..ca68ebbbc 100644 --- a/app/workers/reservation_reminder_worker.rb +++ b/app/workers/reservation_reminder_worker.rb @@ -21,7 +21,7 @@ class ReservationReminderWorker already_sent = Notification.where( attached_object_type: Reservation.name, attached_object_id: r.id, - notification_type_id: NotificationType.find_by_name('notify_member_reservation_reminder') + notification_type_id: NotificationType.find_by(name: 'notify_member_reservation_reminder') ).count next if already_sent.positive? diff --git a/test/integration/abuses_test.rb b/test/integration/abuses_test.rb index 8e5a6e0b4..827e6f0ec 100644 --- a/test/integration/abuses_test.rb +++ b/test/integration/abuses_test.rb @@ -30,7 +30,7 @@ class AbusesTest < ActionDispatch::IntegrationTest assert_equal 'Project', abuse[:reporting][:signaled_type], 'signaled object type mismatch' # Check notifications were sent for every admins - notifications = Notification.where(notification_type_id: NotificationType.find_by_name('notify_admin_abuse_reported'), # rubocop:disable Rails/DynamicFindBy + notifications = Notification.where(notification_type_id: NotificationType.find_by(name: 'notify_admin_abuse_reported'), attached_object_type: 'Abuse', attached_object_id: abuse[:reporting][:id]) assert_not_empty notifications, 'no notifications were created' diff --git a/test/integration/auth_providers_test.rb b/test/integration/auth_providers_test.rb index d9f573ac4..b21860788 100644 --- a/test/integration/auth_providers_test.rb +++ b/test/integration/auth_providers_test.rb @@ -145,7 +145,7 @@ class AuthProvidersTest < ActionDispatch::IntegrationTest # check resulting notification notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_user_auth_migration'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_user_auth_migration'), attached_object_type: 'User', attached_object_id: user.id ) diff --git a/test/integration/events/recurrence_test.rb b/test/integration/events/recurrence_test.rb index d372efb46..b5c4548e4 100644 --- a/test/integration/events/recurrence_test.rb +++ b/test/integration/events/recurrence_test.rb @@ -92,7 +92,7 @@ class Events::RecurrenceTest < ActionDispatch::IntegrationTest amount: 20, category_id: 2, recurrence: 'month', - recurrence_end_at: 3.months.from_now.utc + 2.weeks + recurrence_end_at: 2.weeks.from_now.utc + 3.months } }, headers: upload_headers @@ -103,7 +103,7 @@ class Events::RecurrenceTest < ActionDispatch::IntegrationTest # Check the events were correctly created db_events = Event.where(title: name) - assert_equal 4, db_events.count + assert_equal 3, db_events.count assert(db_events.all? { |event| !event.event_image.attachment.nil? }) assert(db_events.all? { |event| !event.description.empty? }) assert(db_events.all? { |event| event.availability.start_at.to_date >= 2.weeks.from_now.to_date }) diff --git a/test/integration/exports/members_export_test.rb b/test/integration/exports/members_export_test.rb index d2513a16c..a2a8f44bd 100644 --- a/test/integration/exports/members_export_test.rb +++ b/test/integration/exports/members_export_test.rb @@ -41,7 +41,7 @@ class Exports::MembersExportTest < ActionDispatch::IntegrationTest # test data wb = workbook[I18n.t('export_members.members')] member = User.find(wb.sheet_data[1][0].value) - assert_equal (member.is_allow_newsletter ? 1 : 0), wb.sheet_data[1][4].value + assert_equal (member.is_allow_newsletter ? 1 : 0), wb.sheet_data[1][5].value # Clean XLSX file require 'fileutils' diff --git a/test/integration/subscriptions/create_as_admin_test.rb b/test/integration/subscriptions/create_as_admin_test.rb index bf38753b6..76a1ec91e 100644 --- a/test/integration/subscriptions/create_as_admin_test.rb +++ b/test/integration/subscriptions/create_as_admin_test.rb @@ -52,10 +52,12 @@ class Subscriptions::CreateAsAdminTest < ActionDispatch::IntegrationTest # Check that the user benefit from prices of his plan printer = Machine.find_by(slug: 'imprimante-3d') - assert_equal 15, (printer.prices.find_by(group_id: user.group_id, plan_id: user.subscription.plan_id).amount / 100.00), 'machine hourly price does not match' + assert_equal 15, + (printer.prices.find_by(group_id: user.group_id, plan_id: user.subscription.plan_id).amount / 100.00), + 'machine hourly price does not match' # Check notification was sent to the user - notification = Notification.find_by(notification_type_id: NotificationType.find_by_name('notify_member_subscribed_plan'), + notification = Notification.find_by(notification_type_id: NotificationType.find_by(name: 'notify_member_subscribed_plan'), attached_object_type: 'Subscription', attached_object_id: subscription.id) assert_not_nil notification, 'user notification was not created' assert_equal user.id, notification.receiver_id, 'wrong user notified' diff --git a/test/integration/subscriptions/create_as_user_test.rb b/test/integration/subscriptions/create_as_user_test.rb index cb449eeb3..2ea35a3da 100644 --- a/test/integration/subscriptions/create_as_user_test.rb +++ b/test/integration/subscriptions/create_as_user_test.rb @@ -59,7 +59,7 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest # Check notifications were sent for every admins notifications = Notification.where( - notification_type_id: NotificationType.find_by_name('notify_admin_subscribed_plan'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_admin_subscribed_plan'), attached_object_type: 'Subscription', attached_object_id: subscription[:id] ) @@ -164,7 +164,7 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest # Check notifications were sent for every admins notifications = Notification.where( - notification_type_id: NotificationType.find_by_name('notify_admin_subscribed_plan'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_admin_subscribed_plan'), attached_object_type: 'Subscription', attached_object_id: subscription[:id] ) @@ -191,53 +191,6 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest assert_equal invoice.wallet_transaction_id, transaction.id end - test 'user takes a subscription with payment schedule' do - plan = Plan.find_by(group_id: @user.group.id, type: 'Plan', base_name: 'Abonnement mensualisable') - payment_schedule_count = PaymentSchedule.count - payment_schedule_items_count = PaymentScheduleItem.count - - VCR.use_cassette('subscriptions_user_create_with_payment_schedule') do - post '/api/stripe/setup_subscription', - params: { - payment_method_id: stripe_payment_method, - cart_items: { - items: [ - { - subscription: { - plan_id: plan.id - } - } - ], - payment_schedule: true, - payment_method: 'cart' - } - }.to_json, headers: default_headers - - # Check response format & status - assert_equal 201, response.status, response.body - assert_equal Mime[:json], response.content_type - - # Check the response - sub = json_response(response.body) - assert_not_nil sub[:id] - end - - # Check generalities - assert_equal payment_schedule_count + 1, PaymentSchedule.count, 'missing the payment schedule' - assert_equal payment_schedule_items_count + 12, PaymentScheduleItem.count, 'missing some payment schedule items' - - # Check the correct plan was subscribed - result = json_response(response.body) - assert_equal PaymentSchedule.last.id, result[:id], 'payment schedule id does not match' - subscription = PaymentSchedule.find(result[:id]).payment_schedule_objects.first.object - assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match' - - # Check that the user has the correct subscription - assert_not_nil @user.subscription, "user's subscription was not found" - assert_not_nil @user.subscription.plan, "user's subscribed plan was not found" - assert_equal plan.id, @user.subscription.plan_id, "user's plan does not match" - end - test 'user takes a subscription but does not confirm 3DS' do plan = Plan.find_by(group_id: @user.group.id, type: 'Plan', base_name: 'Abonnement mensualisable') payment_schedule_count = PaymentSchedule.count diff --git a/test/integration/subscriptions/create_with_payment_schedule_test.rb b/test/integration/subscriptions/create_with_payment_schedule_test.rb new file mode 100644 index 000000000..b2c589423 --- /dev/null +++ b/test/integration/subscriptions/create_with_payment_schedule_test.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require 'test_helper' + +module Subscriptions; end + +class Subscriptions::CreateWithPaymentScheduleTest < Minitest::Test + setup do + @user = User.find_by(username: 'jdupond') + login_as(@user, scope: :user) + end + + test 'user takes a subscription with payment schedule' do + plan = Plan.find_by(group_id: @user.group.id, type: 'Plan', base_name: 'Abonnement mensualisable') + payment_schedule_count = PaymentSchedule.count + payment_schedule_items_count = PaymentScheduleItem.count + + VCR.use_cassette('subscriptions_user_create_with_payment_schedule') do + post '/api/stripe/setup_subscription', + params: { + payment_method_id: stripe_payment_method, + cart_items: { + items: [ + { + subscription: { + plan_id: plan.id + } + } + ], + payment_schedule: true, + payment_method: 'cart' + } + }.to_json, headers: default_headers + + # Check response format & status + assert_equal 201, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the response + sub = json_response(response.body) + assert_not_nil sub[:id] + end + + # Check generalities + assert_equal payment_schedule_count + 1, PaymentSchedule.count, 'missing the payment schedule' + assert_equal payment_schedule_items_count + 12, PaymentScheduleItem.count, 'missing some payment schedule items' + + # Check the correct plan was subscribed + result = json_response(response.body) + assert_equal PaymentSchedule.last.id, result[:id], 'payment schedule id does not match' + subscription = PaymentSchedule.find(result[:id]).payment_schedule_objects.first.object + assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match' + + # Check that the user has the correct subscription + assert_not_nil @user.subscription, "user's subscription was not found" + assert_not_nil @user.subscription.plan, "user's subscribed plan was not found" + assert_equal plan.id, @user.subscription.plan_id, "user's plan does not match" + end +end diff --git a/test/integration/subscriptions/free_extension_test.rb b/test/integration/subscriptions/free_extension_test.rb index 72077df59..33b9c34ec 100644 --- a/test/integration/subscriptions/free_extension_test.rb +++ b/test/integration/subscriptions/free_extension_test.rb @@ -49,7 +49,7 @@ class Subscriptions::FreeExtensionTest < ActionDispatch::IntegrationTest # Check notification was sent to the user notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_member_subscription_extended'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_member_subscription_extended'), attached_object_type: 'Subscription', attached_object_id: subscription[:id] ) diff --git a/test/integration/subscriptions/renew_as_admin_test.rb b/test/integration/subscriptions/renew_as_admin_test.rb index 485e89407..80db5fca9 100644 --- a/test/integration/subscriptions/renew_as_admin_test.rb +++ b/test/integration/subscriptions/renew_as_admin_test.rb @@ -64,7 +64,7 @@ class Subscriptions::RenewAsAdminTest < ActionDispatch::IntegrationTest # Check notification was sent to the user notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_member_subscribed_plan'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_member_subscribed_plan'), attached_object_type: 'Subscription', attached_object_id: subscription[:id] ) @@ -118,7 +118,7 @@ class Subscriptions::RenewAsAdminTest < ActionDispatch::IntegrationTest # Check notification was sent to the user notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_member_subscribed_plan'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_member_subscribed_plan'), attached_object_type: 'Subscription', attached_object_id: subscription[:id] ) diff --git a/test/integration/subscriptions/renew_as_user_test.rb b/test/integration/subscriptions/renew_as_user_test.rb index 3b505ec00..005e5fe64 100644 --- a/test/integration/subscriptions/renew_as_user_test.rb +++ b/test/integration/subscriptions/renew_as_user_test.rb @@ -62,7 +62,7 @@ class Subscriptions::RenewAsUserTest < ActionDispatch::IntegrationTest # Check notifications were sent for every admins notifications = Notification.where( - notification_type_id: NotificationType.find_by_name('notify_admin_subscribed_plan'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_admin_subscribed_plan'), attached_object_type: 'Subscription', attached_object_id: subscription[:id] ) diff --git a/test/mailers/previews/notifications_mailer_preview.rb b/test/mailers/previews/notifications_mailer_preview.rb index 0831b8a47..3321f459b 100644 --- a/test/mailers/previews/notifications_mailer_preview.rb +++ b/test/mailers/previews/notifications_mailer_preview.rb @@ -2,7 +2,7 @@ class NotificationsMailerPreview < ActionMailer::Preview def notify_user_auth_migration - notif = Notification.where(notification_type_id: NotificationType.find_by_name('notify_user_auth_migration')).first + notif = Notification.find_by(notification_type_id: NotificationType.find_by(name: 'notify_user_auth_migration')) NotificationsMailer.send_mail_by(notif) end end diff --git a/test/services/trainings/authorization_service_test.rb b/test/services/trainings/authorization_service_test.rb index f5c32cbd7..09ee9bd0f 100644 --- a/test/services/trainings/authorization_service_test.rb +++ b/test/services/trainings/authorization_service_test.rb @@ -21,7 +21,7 @@ class Trainings::AuthorizationServiceTest < ActiveSupport::TestCase ) # jump to the future and proceed with auto revocations - travel_to(DateTime.current + 6.months + 1.day) + travel_to(6.months.from_now + 1.day) Trainings::AuthorizationService.auto_cancel_authorizations(@training) # Check authorization was revoked @@ -30,7 +30,7 @@ class Trainings::AuthorizationServiceTest < ActiveSupport::TestCase # Check notification was sent notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_member_training_authorization_expired'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_member_training_authorization_expired'), attached_object_type: 'Training', attached_object_id: @training.id ) diff --git a/test/services/trainings/auto_cancel_service_test.rb b/test/services/trainings/auto_cancel_service_test.rb index 1dae26ae5..4a5971484 100644 --- a/test/services/trainings/auto_cancel_service_test.rb +++ b/test/services/trainings/auto_cancel_service_test.rb @@ -31,7 +31,7 @@ class Trainings::AutoCancelServiceTest < ActiveSupport::TestCase # Check notification was sent to the user notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_member_training_auto_cancelled'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_member_training_auto_cancelled'), attached_object_type: 'SlotsReservation', attached_object_id: r.slots_reservations.first&.id ) @@ -40,7 +40,7 @@ class Trainings::AutoCancelServiceTest < ActiveSupport::TestCase # Check notification was sent to the admin notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_admin_training_auto_cancelled'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_admin_training_auto_cancelled'), attached_object_type: 'Availability', attached_object_id: @availability.id ) @@ -95,12 +95,12 @@ class Trainings::AutoCancelServiceTest < ActiveSupport::TestCase # Check no notifications were sent assert_empty Notification.where( - notification_type_id: NotificationType.find_by_name('notify_member_training_auto_cancelled'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_member_training_auto_cancelled'), attached_object_type: 'SlotsReservation', attached_object_id: [r1.slots_reservations.first&.id, r2.slots_reservations.first&.id, r3.slots_reservations.first&.id] ) assert_nil Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_admin_training_auto_cancelled'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_admin_training_auto_cancelled'), attached_object_type: 'Availability', attached_object_id: @availability.id ) @@ -137,7 +137,7 @@ class Trainings::AutoCancelServiceTest < ActiveSupport::TestCase # Check notification was sent to the user notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_member_training_auto_cancelled'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_member_training_auto_cancelled'), attached_object_type: 'SlotsReservation', attached_object_id: r.slots_reservations.first&.id ) @@ -146,7 +146,7 @@ class Trainings::AutoCancelServiceTest < ActiveSupport::TestCase # Check notification was sent to the admin notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_admin_training_auto_cancelled'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_admin_training_auto_cancelled'), attached_object_type: 'Availability', attached_object_id: @availability.id ) diff --git a/test/services/trainings/invalidation_service_test.rb b/test/services/trainings/invalidation_service_test.rb index 7c80cc1dc..9b3e37921 100644 --- a/test/services/trainings/invalidation_service_test.rb +++ b/test/services/trainings/invalidation_service_test.rb @@ -21,7 +21,7 @@ class Trainings::InvalidationServiceTest < ActiveSupport::TestCase ) # jump to the future and proceed with auto invalidations - travel_to(DateTime.current + 6.months + 1.day) + travel_to(6.months.from_now + 1.day) Trainings::InvalidationService.auto_invalidate(@training) # Check authorization was revoked @@ -30,7 +30,7 @@ class Trainings::InvalidationServiceTest < ActiveSupport::TestCase # Check notification was sent notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_member_training_invalidated'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_member_training_invalidated'), attached_object_type: 'Training', attached_object_id: @training.id ) @@ -51,7 +51,7 @@ class Trainings::InvalidationServiceTest < ActiveSupport::TestCase # User reserves a machine authorized by this training machine = @training.machines.first - slot = machine.availabilities.where('start_at > ?', DateTime.current).first&.slots&.first + slot = machine.availabilities.where('start_at > ?', Time.current).first&.slots&.first Reservation.create!( reservable_id: machine.id, reservable_type: Machine.name, @@ -60,7 +60,7 @@ class Trainings::InvalidationServiceTest < ActiveSupport::TestCase ) # jump to the future and proceed with auto invalidations - travel_to(DateTime.current + 6.months + 1.day) + travel_to(6.months.from_now + 1.day) Trainings::InvalidationService.auto_invalidate(@training) # Check authorization was not revoked @@ -69,7 +69,7 @@ class Trainings::InvalidationServiceTest < ActiveSupport::TestCase # Check notification was not sent notification = Notification.find_by( - notification_type_id: NotificationType.find_by_name('notify_member_training_invalidated'), # rubocop:disable Rails/DynamicFindBy + notification_type_id: NotificationType.find_by(name: 'notify_member_training_invalidated'), attached_object_type: 'Training', attached_object_id: @training.id )