Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions lib/tasks/dev.rake
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ namespace :dev do
enrollment_status = InPersonEnrollment.statuses[raw_enrollment_status]
is_established = ['pending', 'passed', 'failed', 'expired'].include?(raw_enrollment_status)

create_in_usps = !!ENV['CREATE_PENDING_ENROLLMENT_IN_USPS']

InPersonEnrollment.transaction do
(0...num_users).each do |n|
email_addr = "testuser#{n}@example.com"
Expand All @@ -101,22 +103,48 @@ namespace :dev do
if is_established
unless raw_enrollment_status == 'pending' && !user.pending_in_person_enrollment.nil?
profile = Profile.new(user: user)

# Convert index to a string of letters to be a valid last name for the USPS API
usps_compatible_number_alternative = n.to_s.chars.map do |c|
('a'.ord + c.to_i).chr
end.join('')
Comment on lines +108 to +110
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment here would aid readability, something as simple as

# Convert index to a string of letters to be a valid last name for the USPS API

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added


pii = Pii::Attributes.new_from_hash(
first_name: 'Test',
last_name: "User #{n}",
last_name: "User #{usps_compatible_number_alternative}",
dob: '1970-05-01',
ssn: "666-#{n}", # doesn't need to be legit 9 digits, just unique
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to worry about duplicates across multiple runs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicates are expected, and that's fine.

address1: '1200 FORESTVILLE DR',
city: 'GREAT FALLS',
state: 'VA',
zipcode: '22066',
)
personal_key = profile.encrypt_pii(pii, pw)
enrollment = InPersonEnrollment.create!(
user: user,
profile: profile,
status: enrollment_status,
enrollment_established_at: Time.zone.now - random.rand(0..5).days,
unique_id: SecureRandom.hex(9),
enrollment_code: SecureRandom.hex(16),
)
enrollment.profile.activate if raw_enrollment_status == 'passed'

if raw_enrollment_status === 'pending' && create_in_usps
enrollment = InPersonEnrollment.find_or_initialize_by(
user: user,
status: :establishing,
profile: profile,
)
enrollment.save!

UspsInPersonProofing::EnrollmentHelper.schedule_in_person_enrollment(
user,
pii,
)
else
enrollment = InPersonEnrollment.create!(
user: user,
profile: profile,
status: enrollment_status,
enrollment_established_at: Time.zone.now - random.rand(0..5).days,
unique_id: SecureRandom.hex(9),
enrollment_code: SecureRandom.hex(16),
)

enrollment.profile.activate if raw_enrollment_status == 'passed'
end
Rails.logger.warn "email=#{email_addr} personal_key=#{personal_key}"
end
else
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/tasks/dev_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'rake'

describe 'dev rake tasks' do
include UspsIppHelper

before do
Rake.application.rake_require 'tasks/dev'
Rake::Task.define_task(:environment)
Expand All @@ -22,13 +24,15 @@
prev_enrollment_status = nil
prev_verified = nil
prev_scrypt_cost = nil
prev_pending_in_usps = nil

before(:each) do
prev_num_users = ENV['NUM_USERS']
prev_progress = ENV['PROGRESS']
prev_enrollment_status = ENV['ENROLLMENT_STATUS']
prev_verified = ENV['VERIFIED']
prev_scrypt_cost = ENV['SCRYPT_COST']
prev_pending_in_usps = ENV['CREATE_PENDING_ENROLLMENT_IN_USPS']

ENV['PROGRESS'] = 'no'
ENV['NUM_USERS'] = '10'
Expand All @@ -43,6 +47,7 @@
ENV['ENROLLMENT_STATUS'] = prev_enrollment_status
ENV['VERIFIED'] = prev_verified
ENV['SCRYPT_COST'] = prev_scrypt_cost
ENV['CREATE_PENDING_ENROLLMENT_IN_USPS'] = prev_pending_in_usps
end

describe 'dev:random_users' do
Expand Down Expand Up @@ -309,6 +314,35 @@
expect(last_record.profile).to be_instance_of(Profile)
expect(last_record.profile.active).to be(true)
end

it 'creates the enrollment in USPS IPPaaS when CREATE_PENDING_ENROLLMENT_IN_USPS is truthy' do
ENV['CREATE_PENDING_ENROLLMENT_IN_USPS'] = '1'
stub_request_token
stub_request_enroll

expect(User.count).to eq 0
expect(InPersonEnrollment.count).to eq 0

Rake::Task['dev:random_in_person_users'].invoke

expect(User.count).to eq 10
expect(InPersonEnrollment.count).to eq 10
expect(InPersonEnrollment.distinct.count('user_id')).to eq 10
expect(InPersonEnrollment.pending.count).to eq 10

# Spot check attributes on last record
last_record = InPersonEnrollment.last
expect(last_record.attributes).to include(
'status' => 'pending',
'enrollment_established_at' => respond_to(:to_date),
'unique_id' => an_instance_of(String),

# Check for the enrollment code in the stubbed response
'enrollment_code' => '2048702198804353',
)
expect(last_record.profile).to be_instance_of(Profile)
expect(last_record.profile.active).to be(false)
end
end
end
end