Skip to content

Commit

Permalink
move VCR config to test_help.rb instead of initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
amtuannguyen committed Nov 22, 2024
1 parent 5aff064 commit b7c3acc
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 95 deletions.
52 changes: 23 additions & 29 deletions app/lib/alma/fee_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ class Alma::FeeProcessor
attr_accessor :fee_url, :response_code, :error_code, :error_message, :tracking_id

def initialize(url: Settings.alma.api.fee_url)
#@record = payment_record
#@fee_url = prepare_url(url, @record.fee.fee_id, @record.yorku_id) if url != nil
@url_template = url
end

Expand All @@ -19,62 +17,58 @@ def pay_fee!(record)
response = HTTParty.post(fee_url, query: pay_params(record), headers: headers)
@response_code = response.code

if @response_code.to_s == "200"
return true
else
json = JSON.parse response.body
parse_error json
return false
end
if @response_code.to_s == "200"
return true
else
json = JSON.parse response.body
parse_error json
return false
end
end


def pay_params(record)

params = {
user_type_id: "all_unique",
op: "pay",
amount: record.amount,
#method: "CREDIT_CARD",
method: pay_method(record),
reason: "Paying The Fee",
comment: "#{record.user.name} is paying via YU Payment Broker",
external_transaction_id: record.payment_transaction.refnum
}
end

def pay_method(record)
return "CREDIT_CARD" if record == nil || record.payment_transaction == nil
return nil if record == nil || record.payment_transaction == nil
t = record.payment_transaction

if t.cardtype.try(:downcase) == "m"
return "CHECK"
else
return "CREDIT_CARD"
end

end

def prepare_url(url, record)

fee_id = record.fee.fee_id
user_id = record.yorku_id
user_primary_id = record.user_primary_id

with_fee = url.sub("{{fee_id}}", fee_id.to_s)
return with_fee.sub("{{user_primary_id}}", user_id.to_s)
return with_fee.sub("{{user_primary_id}}", user_primary_id.to_s)
end

def headers
{ "Authorization": "apikey #{Settings.alma.api_key}",
"Accept": "application/json",
"Content-Type": "application/json" }
end
{
"Authorization": "apikey #{Settings.alma.api_key}",
"Accept": "application/json",
"Content-Type": "application/json"
}
end

def parse_error(json_response)
if json_response["errorList"] && json_response["errorList"]["error"]
@error_code = json_response["errorList"]["error"].first["errorCode"]
@error_message = json_response["errorList"]["error"].first["errorMessage"]
@tracking_id = json_response["errorList"]["error"].first["trackingId"]
end
end
def parse_error(json_response)
if json_response["errorList"] && json_response["errorList"]["error"]
@error_code = json_response["errorList"]["error"].first["errorCode"]
@error_message = json_response["errorList"]["error"].first["errorMessage"]
@tracking_id = json_response["errorList"]["error"].first["trackingId"]
end
end
end
25 changes: 0 additions & 25 deletions config/initializers/vcr.rb

This file was deleted.

6 changes: 0 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,4 @@
## REPORTS ##
get "/reports" => "reports#index", as: :reports
get "/reports/transaction/:id" => "reports/transactions#show", as: :report_transaction

## DUMMY ALMA API ##
unless Rails.env.production?
get "alma/dummy_api/:user_id/fees/" => "alma/dummy_fees_api#show", as: :alma_dummy_api_get_fees
post "alma/dummy_api/:user_id/fees/:fee_id" => "alma/dummy_fees_api#update", as: :alma_dummy_api_fees
end
end
7 changes: 0 additions & 7 deletions test/controllers/alma/dummy_fees_api_controller_test.rb

This file was deleted.

7 changes: 0 additions & 7 deletions test/controllers/alma/load_fees_controller_test.rb

This file was deleted.

22 changes: 1 addition & 21 deletions test/lib/alma/fee_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
require_relative 'responses_rack_app.rb'

class Alma::FeeProcessorTest < ActionDispatch::IntegrationTest



# NOT TESTING GETTING FEES, as it is part of the alma gem and tested there.
setup do
WebMock.disable_net_connect!(allow_localhost: true)
Expand All @@ -25,32 +22,18 @@ class Alma::FeeProcessorTest < ActionDispatch::IntegrationTest
stub_request(:post, /.*/).with(headers: processor.headers).to_rack(Alma::ResponsesRackApp)

assert processor.pay_fee! record

end

should "use defaul Settings fee url or be able to change feel url if need be" do
new_url = "https://somewhere_else"
transaction = create :payment_transaction
record = create :payment_record, payment_transaction: transaction, user: transaction.user

processor = Alma::FeeProcessor.new
prepared_url = Settings.alma.api.fee_url.sub("{{fee_id}}", record.fee.fee_id.to_s).sub("{{user_primary_id}}", record.yorku_id.to_s)
prepared_url = Settings.alma.api.fee_url.sub("{{fee_id}}", record.fee.fee_id.to_s).sub("{{user_primary_id}}", record.user_primary_id.to_s)

assert_equal prepared_url, processor.prepare_url(Settings.alma.api.fee_url, record)

# processor.fee_url = new_url
# assert_equal new_url, processor.fee_url
end

# should "should prepare url by replacing fee and user id placeholders" do
# transaction = create :payment_transaction
# record = create :payment_record, payment_transaction: transaction, user: transaction.user
#
# processor = Alma::FeeProcessor.new url: "https:://api/{{fee_id}}/{{user_primary_id}}"
# assert_equal "https:://api/#{record.fee.fee_id}/#{record.yorku_id}", processor.prepare_url()
# end


should "parse error" do
transaction = create :payment_transaction
record = create :payment_record, payment_transaction: transaction, user: transaction.user
Expand Down Expand Up @@ -86,7 +69,6 @@ class Alma::FeeProcessorTest < ActionDispatch::IntegrationTest
record = create :payment_record, payment_transaction: mc
assert_equal "CHECK", processor.pay_method(record)
assert_equal processor.pay_params(record)[:method], "CHECK"

end

should "return proper pay params from record" do
Expand All @@ -101,9 +83,7 @@ class Alma::FeeProcessorTest < ActionDispatch::IntegrationTest
assert_equal params[:op], "pay"
assert_equal params[:amount], record.amount
assert_equal params[:method], "CREDIT_CARD"
assert_equal params[:reason], "Paying The Fee"
assert_equal params[:comment], "#{record.user.name} is paying via YU Payment Broker"
assert_equal params[:external_transaction_id], t.refnum

end
end
26 changes: 26 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'mocha/minitest'
require 'database_cleaner/active_record'
require 'rails-controller-testing'
require 'vcr'

# Configure shoulda-matchers to use Minitest
require 'shoulda/matchers'
Expand Down Expand Up @@ -72,3 +73,28 @@ def self.logged_in_with_user(&block)
end
end
end


VCR.configure do |config|
config.cassette_library_dir = 'test/cassettes' # Directory to save cassettes
config.hook_into :webmock # Use WebMock to intercept HTTP requests

config.filter_sensitive_data('ALMA_API_KEY') { Settings.alma.api_key }
config.filter_sensitive_data('ALMA_TEST_USER_PRIMARY_ID') { Settings.alma.test_user_primary_id }
config.filter_sensitive_data('ALMA_TEST_USER_UNIV_ID') { Settings.alma.test_user_univ_id }
config.filter_sensitive_data('ALMA_TEST_USER_PASSWORD') { Settings.alma.test_user_password }

config.filter_sensitive_data('YPB_APPLICATION_PASSWORD') { Settings.ypb.application_password }
config.filter_sensitive_data('YPB_APPLICATION_ID') { Settings.ypb.application_id }
config.filter_sensitive_data('YPB_APPLICATION_NAME') { Settings.ypb.application_name }

config.filter_sensitive_data('YPB_APPLICATION_LAW_PASSWORD') { Settings.ypb.application_law_password }
config.filter_sensitive_data('YPB_APPLICATION_LAW_ID') { Settings.ypb.application_law_id }
config.filter_sensitive_data('YPB_APPLICATION_LAW_NAME') { Settings.ypb.application_law_name }

if !Settings.alma.test_text_to_filter.nil?
Settings.alma.test_text_to_filter.each_with_index do |t,i|
config.filter_sensitive_data("FILTERED_TEXT_#{i}") { t }
end
end
end

0 comments on commit b7c3acc

Please sign in to comment.