Skip to content

Commit

Permalink
refactor code add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nfstern02 committed Sep 24, 2024
1 parent 10e9802 commit a9eadd9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
14 changes: 8 additions & 6 deletions modules/vye/app/controllers/vye/v1/verifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions modules/vye/spec/requests/vye/v1/verify/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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') }
Expand All @@ -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' } }
Expand Down

0 comments on commit a9eadd9

Please sign in to comment.