1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

test subscription with schedule by user

This commit is contained in:
Sylvain 2020-12-15 15:48:13 +01:00
parent 908ccf5bab
commit ccff463165
5 changed files with 34 additions and 20 deletions

View File

@ -1,6 +1,6 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require_relative 'config/application'
Rails.application.load_tasks

View File

@ -72,7 +72,7 @@ class API::PaymentsController < API::ApiController
user = User.find(params[:user_id])
key = Setting.get('stripe_secret_key')
@intent = Stripe::SetupIntent.create({ customer: user.stp_customer_id }, { api_key: key })
render json: { client_secret: @intent.client_secret }
render json: { id: @intent.id, client_secret: @intent.client_secret }
end
def confirm_payment_schedule

View File

@ -10,7 +10,7 @@ require 'action_view/railtie'
require 'action_mailer/railtie'
require 'active_job/railtie'
# require 'action_cable/engine'
require 'rails/test_unit/railtie' if Rails.env.test?
require 'rails/test_unit/railtie'
# require 'sprockets/railtie'
require 'elasticsearch/rails/instrumentation'
require 'elasticsearch/persistence/model'
@ -56,6 +56,7 @@ module Fablab
config.generators do |g|
g.orm :active_record
g.test_framework :mini_test
end
if Rails.env.development?

View File

@ -172,29 +172,42 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest
test 'user takes a subscription with payment schedule' do
plan = Plan.find_by(group_id: @user.group.id, type: 'Plan', base_name: 'Abonnement mensualisable')
VCR.use_cassette('subscriptions_user_setup_intent') do
get "/api/payments/setup_intent/#{@user.id}"
end
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the correct object was signaled
setup_intent = json_response(response.body)
VCR.use_cassette('subscriptions_user_create_with_payment_schedule') do
post '/api/payments/confirm_payment',
get "/api/payments/setup_intent/#{@user.id}"
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the response
setup_intent = json_response(response.body)
assert_not_nil setup_intent[:client_secret]
assert_not_nil setup_intent[:id]
assert_match /^#{setup_intent[:id]}_secret_/, setup_intent[:client_secret]
# Confirm the intent
stripe_res = Stripe::SetupIntent.confirm(
setup_intent[:id],
{ payment_method: stripe_payment_method },
{ api_key: Setting.get('stripe_secret_key') }
)
# check the confirmation
assert_equal setup_intent[:id], stripe_res.id
assert_equal 'succeeded', stripe_res.status
assert_equal 'off_session', stripe_res.usage
post '/api/payments/confirm_payment_schedule',
params: {
payment_method_id: stripe_payment_method,
setup_intent_id: setup_intent[:id],
cart_items: {
subscription: {
plan_id: plan.id,
payment_schedule: true,
payment_method: 'stripe'
}
},
setup_intent_id: setup_intent[:client_secret]
}
}.to_json, headers: default_headers
end

View File

@ -4,7 +4,7 @@ require 'coveralls'
Coveralls.wear!('rails')
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
require_relative '../config/environment'
require 'action_dispatch'
require 'rails/test_help'
require 'vcr'
@ -23,7 +23,7 @@ Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true)]
class ActiveSupport::TestCase
# Add more helper methods to be used by all tests here...
ActiveRecord::Migration.check_pending!
fixtures :all
def json_response(body)