From a9eadd93d2b5531f903507be0c99c07cc58c5318 Mon Sep 17 00:00:00 2001 From: nfstern02 Date: Tue, 24 Sep 2024 00:51:59 -0500 Subject: [PATCH] refactor code add tests --- .../vye/v1/verifications_controller.rb | 14 +++--- .../vye/v1/verifications_controller_spec.rb | 43 +++++++++++++++++++ .../requests/vye/v1/verify/create_spec.rb | 3 +- 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 modules/vye/spec/controllers/vye/v1/verifications_controller_spec.rb diff --git a/modules/vye/app/controllers/vye/v1/verifications_controller.rb b/modules/vye/app/controllers/vye/v1/verifications_controller.rb index e734d2b65b3..e0a117bbe9c 100644 --- a/modules/vye/app/controllers/vye/v1/verifications_controller.rb +++ b/modules/vye/app/controllers/vye/v1/verifications_controller.rb @@ -9,6 +9,7 @@ class AwardsMismatch < StandardError; end rescue_from EmptyAwards, with: -> { head :unprocessable_entity } rescue_from AwardsMismatch, with: -> { head :unprocessable_entity } + # this is in the models concern NeedsEnrollmentVerification and is aliased to enrollments delegate :pending_verifications, to: :user_info def create @@ -20,18 +21,19 @@ def create pending_verifications.each do |verification| verification.update!(transact_date:, source_ind:) end - +byebug head :no_content end private def cert_through_date - found = nil - pending_verifications.each do |pv| - found = - #find the most farest into the future of this one pv.act_end - end + # act_end is defined as timestamp without time zone + found = Time.new(1970, 1, 1, 0, 0, 0, 0) # '1970-01-01 00:00:00' + + pending_verifications.each { |pv| found = pv.act_end if pv.act_end > found } + + return nil if found.eql?(Time.new(1970, 1, 1, 0, 0, 0, 0)) found end diff --git a/modules/vye/spec/controllers/vye/v1/verifications_controller_spec.rb b/modules/vye/spec/controllers/vye/v1/verifications_controller_spec.rb new file mode 100644 index 00000000000..b9a60108839 --- /dev/null +++ b/modules/vye/spec/controllers/vye/v1/verifications_controller_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'support/controller_spec_helper' + +RSpec.describe Vye::V1::VerificationsController, type: :controller do + let!(:current_user) { create(:user, :accountable) } + + before do + sign_in_as(current_user) + allow_any_instance_of(ApplicationController).to receive(:validate_session).and_return(true) + allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(current_user) + allow_any_instance_of(Vye::V1::VerificationsController).to receive(:authorize).and_return(true) + end + + describe '#create' do + let(:cur_award_ind) { Vye::Award.cur_award_inds[:future] } + let(:now) { Time.parse('2024-03-31T12:00:00-00:00') } + let(:date_last_certified) { Date.new(2024, 2, 15) } + let(:last_day_of_previous_month) { Date.new(2024, 2, 29) } # This is not used only for documentation + let(:award_begin_date) { Date.new(2024, 3, 30) } + let(:today) { Date.new(2024, 3, 31) } # This is not used only for documentation + let(:award_end_date) { Date.new(2024, 4, 1) } + let!(:user_profile) { FactoryBot.create(:vye_user_profile, icn: current_user.icn) } + let!(:user_info) { FactoryBot.create(:vye_user_info, user_profile:, date_last_certified:) } + let!(:award) { FactoryBot.create(:vye_award, user_info:, award_begin_date:, award_end_date:, cur_award_ind:) } + let!(:award2) { FactoryBot.create(:vye_award, user_info:, cur_award_ind:) } + let(:award_ids) { user_info.awards.pluck(:id) } + let!(:subject) { described_class.new } + let!(:params) { { award_ids: } } + + before do + allow(subject).to receive(:params).and_return(params) + allow(subject).to receive(:current_user).and_return(current_user) + end + + it 'sets the transact date to the award end date furthest in the future for all verifications' do + subject.create + + expect(response).to have_http_status(:accepted) + end + end +end diff --git a/modules/vye/spec/requests/vye/v1/verify/create_spec.rb b/modules/vye/spec/requests/vye/v1/verify/create_spec.rb index 848f85bdda5..72f419902da 100644 --- a/modules/vye/spec/requests/vye/v1/verify/create_spec.rb +++ b/modules/vye/spec/requests/vye/v1/verify/create_spec.rb @@ -36,6 +36,7 @@ end end + # TODO: Figure out why the olive_branch_patch does not like passing in json with multiple award_ids describe 'in VYE' do let(:cur_award_ind) { Vye::Award.cur_award_inds[:future] } let(:now) { Time.parse('2024-03-31T12:00:00-00:00') } @@ -44,11 +45,9 @@ let(:award_begin_date) { Date.new(2024, 3, 30) } let(:today) { Date.new(2024, 3, 31) } # This is not used only for documentation let(:award_end_date) { Date.new(2024, 4, 1) } - let!(:user_profile) { FactoryBot.create(:vye_user_profile, icn: current_user.icn) } let!(:user_info) { FactoryBot.create(:vye_user_info, user_profile:, date_last_certified:) } let!(:award) { FactoryBot.create(:vye_award, user_info:, award_begin_date:, award_end_date:, cur_award_ind:) } - let(:award_ids) { user_info.awards.pluck(:id) } let(:headers) { { 'Content-Type' => 'application/json', 'X-Key-Inflection' => 'camel' } }