1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +01:00
fab-manager/test/test_helper.rb

130 lines
3.9 KiB
Ruby
Raw Normal View History

2019-04-04 11:57:36 +02:00
# frozen_string_literal: true
2016-12-01 13:08:41 +01:00
require 'coveralls'
Coveralls.wear!('rails')
2016-03-23 18:39:41 +01:00
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
2020-03-30 10:59:11 +02:00
require 'action_dispatch'
2016-03-23 18:39:41 +01:00
require 'rails/test_help'
require 'vcr'
require 'sidekiq/testing'
require 'minitest/reporters'
2022-08-29 17:34:09 +02:00
require 'helpers/archive_helper'
2023-03-16 17:17:00 +01:00
require 'helpers/invoice_helper'
require 'helpers/payment_schedule_helper'
2023-02-23 11:48:22 +01:00
require 'fileutils'
2023-03-23 17:39:06 +01:00
# We remove this constraint before running tests, otherwise it will prevent loading the fixtures into the DB
ActiveRecord::Base.connection.execute("DROP RULE IF EXISTS accounting_periods_del_protect ON #{AccountingPeriod.arel_table.name};")
VCR.configure do |config|
2019-04-04 11:57:36 +02:00
config.cassette_library_dir = 'test/vcr_cassettes'
config.hook_into :webmock
config.filter_sensitive_data('sk_test_testfaketestfaketestfake') { Setting.get('stripe_secret_key') }
config.filter_sensitive_data('pk_test_faketestfaketestfaketest') { Setting.get('stripe_public_key') }
2023-03-02 17:13:46 +01:00
config.filter_sensitive_data('github-oauth-app-id') { ENV.fetch('OAUTH_CLIENT_ID') }
config.filter_sensitive_data('github-oauth-app-secret') { ENV.fetch('OAUTH_CLIENT_SECRET') }
config.filter_sensitive_data('oidc-client-id') { ENV.fetch('OIDC_CLIENT_ID') }
config.filter_sensitive_data('oidc-client-secret') { ENV.fetch('OIDC_CLIENT_SECRET') }
config.ignore_request { |req| URI(req.uri).port == 9200 || URI(req.uri).host == '127.0.0.1' }
end
Sidekiq::Testing.fake!
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true)] unless ENV['RM_INFO']
2016-03-23 18:39:41 +01:00
class ActiveSupport::TestCase
2022-08-29 17:34:09 +02:00
include ActionDispatch::TestProcess
include ArchiveHelper
2023-03-16 17:17:00 +01:00
include InvoiceHelper
include PaymentScheduleHelper
2022-08-29 17:34:09 +02:00
2016-03-23 18:39:41 +01:00
# Add more helper methods to be used by all tests here...
ActiveRecord::Migration.check_pending!
fixtures :all
2016-04-05 09:51:12 +02:00
def json_response(body)
JSON.parse(body, symbolize_names: true)
end
2016-04-06 11:27:56 +02:00
2016-04-06 15:22:32 +02:00
def default_headers
2020-03-11 16:18:17 +01:00
{ 'Accept' => Mime[:json], 'Content-Type' => Mime[:json].to_s }
2016-04-06 15:22:32 +02:00
end
def upload_headers
2023-03-02 13:39:23 +01:00
{ 'Accept' => Mime[:json], 'Content-Type' => Mime[:multipart_form].to_s }
end
def open_api_headers(token)
{ 'Accept' => Mime[:json], 'Content-Type' => Mime[:json].to_s, 'Authorization' => "Token token=#{token}" }
end
def stripe_payment_method(error: nil)
2019-04-04 11:57:36 +02:00
number = '4242424242424242'
2016-04-06 16:26:45 +02:00
exp_month = 4
exp_year = Time.current.next_year.year
2019-04-04 11:57:36 +02:00
cvc = '314'
2016-04-06 16:26:45 +02:00
case error
when /card_declined/
2019-04-04 11:57:36 +02:00
number = '4000000000000002'
2016-04-06 16:26:45 +02:00
when /incorrect_number/
2019-04-04 11:57:36 +02:00
number = '4242424242424241'
2016-04-06 16:26:45 +02:00
when /invalid_expiry_month/
exp_month = 15
when /invalid_expiry_year/
exp_year = 1964
when /invalid_cvc/
2019-04-04 11:57:36 +02:00
cvc = '99'
2021-10-14 18:20:10 +02:00
when /require_3ds/
number = '4000002760003184'
2016-04-06 16:26:45 +02:00
end
Stripe::PaymentMethod.create(
2020-06-10 11:33:03 +02:00
{
type: 'card',
card: {
number: number,
exp_month: exp_month,
exp_year: exp_year,
cvc: cvc
}
},
{ api_key: Setting.get('stripe_secret_key') }
2016-04-06 11:27:56 +02:00
).id
end
# Force the statistics export generation worker to run NOW and check the resulting file generated.
# Delete the file afterwards.
# @param export {Export}
def assert_export_xlsx(export)
assert_not_nil export, 'Export was not created'
if export.category == 'statistics'
export_worker = StatisticsExportWorker.new
export_worker.perform(export.id)
assert File.exist?(export.file), 'Export XLSX was not generated'
File.delete(export.file)
else
skip('Unable to test export which is not of the category "statistics"')
end
end
def assert_dates_equal(expected, actual, msg = nil)
assert_not_nil actual, msg
assert_equal expected.to_date, actual.to_date, msg
end
2023-02-24 17:26:55 +01:00
def assert_datetimes_near(expected, actual, msg = nil)
assert_not_nil actual, msg
2023-02-24 17:26:55 +01:00
assert_in_delta expected.to_i, actual.to_i, 1, msg
end
end
class ActionDispatch::IntegrationTest
include Warden::Test::Helpers
Warden.test_mode!
2016-03-23 18:39:41 +01:00
end