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

(test) add test on store

This commit is contained in:
Du Peng 2022-10-25 19:10:14 +02:00
parent 0fddd22df8
commit f771a829ab
17 changed files with 1017 additions and 16 deletions

View File

@ -74,3 +74,15 @@ address_6:
placeable_type: InvoicingProfile
created_at: 2016-08-02 11:16:24.412236000 Z
updated_at: 2016-08-02 11:16:24.412236000 Z
address_8:
id: 8
address: 14 rue du Général Choucroute, 44000 NANTES
street_number:
route:
locality:
postal_code:
placeable_id: 9
placeable_type: InvoicingProfile
created_at: 2016-08-02 11:16:24.412236000 Z
updated_at: 2016-08-02 11:16:24.412236000 Z

View File

@ -128,3 +128,33 @@ order_item_24:
is_offered:
created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
updated_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
order_item_25:
id: 25
order_id: 18
orderable_type: Product
orderable_id: 13
amount: 500
quantity: 1
is_offered:
created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
updated_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
order_item_26:
id: 26
order_id: 19
orderable_type: Product
orderable_id: 3
amount: 52300
quantity: 5
is_offered:
created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
updated_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
order_item_27:
id: 27
order_id: 20
orderable_type: Product
orderable_id: 3
amount: 52300
quantity: 5
is_offered:
created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
updated_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>

View File

@ -160,3 +160,93 @@ order_15:
coupon_id:
paid_total: 3000
invoice_id: 5822
order_16:
id: 16
statistic_profile_id: 10
operator_profile_id:
token: 9VWkmJDSx7QixRusL7ppWg1666628033284
reference: '005901-<%= DateTime.current.utc.strftime('%m') %>-<%= DateTime.current.utc.strftime('%d') %>'
state: cart
total: 0
created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
updated_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
wallet_amount:
wallet_transaction_id:
payment_method:
footprint:
environment: test
coupon_id:
paid_total:
invoice_id:
order_17:
id: 17
statistic_profile_id:
operator_profile_id:
token: MkI5z9qVxe_YdNYCR_WN6g1666628074732
reference: '005902-<%= DateTime.current.utc.strftime('%m') %>-<%= DateTime.current.utc.strftime('%d') %>'
state: cart
total: 0
created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
updated_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
wallet_amount:
wallet_transaction_id:
payment_method:
footprint:
environment: test
coupon_id:
paid_total:
invoice_id:
order_18:
id: 18
statistic_profile_id: 9
operator_profile_id:
token: KbSmmD_gi9w_CrpwtK9OwA1666687433963
reference: '005902-<%= DateTime.current.utc.strftime('%m') %>-<%= DateTime.current.utc.strftime('%d') %>'
state: cart
total: 500
created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
updated_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
wallet_amount:
wallet_transaction_id:
payment_method:
footprint:
environment: test
coupon_id:
paid_total:
invoice_id:
order_19:
id: 19
statistic_profile_id:
operator_profile_id:
token: 4bB96D-MlqJGBr5T8eui-g1666690417460
reference: '005903-<%= DateTime.current.utc.strftime('%m') %>-<%= DateTime.current.utc.strftime('%d') %>'
state: cart
total: 261500
created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
updated_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
wallet_amount:
wallet_transaction_id:
payment_method:
footprint:
environment: test
coupon_id:
paid_total:
invoice_id:
order_20:
id: 20
statistic_profile_id:
operator_profile_id: 1
token: 0DKxbAOzSXRx-amXyhmDdg1666691976019
reference: '005904-<%= DateTime.current.utc.strftime('%m') %>-<%= DateTime.current.utc.strftime('%d') %>'
state: cart
total: 261500
created_at: <%= DateTime.current.utc.change({:hour => 10}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
updated_at: <%= DateTime.current.utc.change({:hour => 15}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
wallet_amount:
wallet_transaction_id:
payment_method:
footprint:
environment: test
coupon_id:
paid_total:
invoice_id:

View File

@ -230,3 +230,17 @@ pgo33:
item_type: PaymentSchedule
item_id: 13
payment_gateway_object_id: 32
pgo34:
id: 34
item_type: 'User'
item_id: 9
gateway_object_type: 'Stripe::Customer'
gateway_object_id: 'cus_IhIynmoJbzLpwX'
pgo35:
id: 35
item_type: 'User'
item_id: 10
gateway_object_type: 'Stripe::Customer'
gateway_object_id: 'cus_IhIynmoJbzLpwX'

View File

@ -7,8 +7,10 @@ product_3:
is_active: true
product_category_id: 4
amount: 52300
quantity_min: 10
stock: '{"external": 0, "internal": 1}'
quantity_min: 5
stock:
external: 100
internal: 1
low_stock_alert: true
low_stock_threshold: 10
created_at: '2022-09-13 10:05:07.010322'
@ -23,7 +25,9 @@ product_11:
product_category_id: 4
amount: 1000
quantity_min: 1
stock: '{"external": 9, "internal": 0}'
stock:
external: 40
internal: 1
low_stock_alert: false
low_stock_threshold:
created_at: '2022-09-14 13:41:16.207154'
@ -38,7 +42,9 @@ product_12:
product_category_id: 4
amount: 142300
quantity_min: 1
stock: '{"external": 0, "internal": 1}'
stock:
external: 0
internal: 1
low_stock_alert: false
low_stock_threshold:
created_at: '2022-09-14 13:42:36.729625'
@ -53,7 +59,9 @@ product_13:
product_category_id: 5
amount: 500
quantity_min: 1
stock: '{"external": 999, "internal": 0}'
stock:
external: 999
internal: 0
low_stock_alert: false
low_stock_threshold:
created_at: '2022-09-14 13:47:18.123531'
@ -68,7 +76,9 @@ product_14:
product_category_id: 5
amount: 1000
quantity_min: 1
stock: '{"external": 800, "internal": 0}'
stock:
external: 800
internal: 0
low_stock_alert: false
low_stock_threshold:
created_at: '2022-09-14 13:48:05.028266'
@ -83,7 +93,9 @@ product_15:
product_category_id: 5
amount: 1500
quantity_min: 1
stock: '{"external": 595, "internal": 0}'
stock:
external: 595
internal: 0
low_stock_alert: false
low_stock_threshold:
created_at: '2022-09-14 13:49:08.233064'
@ -98,7 +110,9 @@ product_16:
product_category_id: 5
amount: 1300
quantity_min: 1
stock: '{"external": 100, "internal": 0}'
stock:
external: 500
internal: 0
low_stock_alert: false
low_stock_threshold:
created_at: '2022-09-14 13:50:12.584041'
@ -113,7 +127,9 @@ product_17:
product_category_id: 5
amount: 3000
quantity_min: 1
stock: '{"external": 99, "internal": 0}'
stock:
external: 99
internal: 0
low_stock_alert: false
low_stock_threshold:
created_at: '2022-09-14 13:51:12.630109'
@ -128,7 +144,9 @@ product_18:
product_category_id: 5
amount: 4000
quantity_min: 1
stock: '{"external": 12, "internal": 0}'
stock:
external: 12
internal: 0
low_stock_alert: false
low_stock_threshold:
created_at: '2022-09-14 13:52:08.659248'
@ -140,11 +158,13 @@ product_20:
sku: 63-44801
description: <p>un joli sticker en forme de <a target="_blank" rel="noopener noreferrer nofollow"
href="https://www.example.com/hello">smiley</a> qui dit bonjour</p>
is_active: true
is_active: false
product_category_id: 6
amount: 1
quantity_min: 1
stock: '{"external": 15, "internal": 0}'
stock:
external: 15
internal: 0
low_stock_alert: false
low_stock_threshold:
created_at: '2022-09-28 08:16:32.348880'
@ -160,7 +180,9 @@ product_22:
product_category_id: 3
amount: 200
quantity_min: 1
stock: '{"external": 999, "internal": 1000}'
stock:
external: 999
internal: 1000
low_stock_alert: false
low_stock_threshold:
created_at: '2022-10-03 14:00:49.967342'
@ -175,7 +197,9 @@ product_23:
product_category_id: 3
amount: 119
quantity_min: 1
stock: '{"external": 998, "internal": 1000}'
stock:
external: 998
internal: 1000
low_stock_alert: false
low_stock_threshold:
created_at: '2022-10-03 14:01:43.706366'

View File

@ -81,7 +81,8 @@ class Exports::AccountingExportTest < ActionDispatch::IntegrationTest
end
if first_invoice.wallet_transaction_id.nil?
assert_equal first_invoice.total / 100.00, data[0][I18n.t('accounting_export.debit_origin')].to_f, 'Origin debit amount does not match'
assert_equal first_invoice.total / 100.00, data[0][I18n.t('accounting_export.debit_origin')].to_f,
'Origin debit amount does not match'
assert_equal first_invoice.total / 100.00, data[0][I18n.t('accounting_export.debit_euro')].to_f, 'Euro debit amount does not match'
else
warn "WARNING: unable to test accurately accounting export: invoice #{first_invoice.id} is using wallet"
@ -119,6 +120,9 @@ class Exports::AccountingExportTest < ActionDispatch::IntegrationTest
client_row = data[data.length - 4]
item_row = data[data.length - 3]
data.each do |a|
p a
end
if machine_invoice.main_item.object_type == 'Reservation' && machine_invoice.main_item.object.reservable_type == 'Machine'
assert_match I18n.t('accounting_export.Machine_reservation'),
client_row[I18n.t('accounting_export.line_label')],

View File

@ -0,0 +1,178 @@
# frozen_string_literal: true
require 'test_helper'
module Store; end
class Store::UserPayOrderTest < ActionDispatch::IntegrationTest
setup do
@admin = User.find_by(username: 'admin')
@pjproudhon = User.find_by(username: 'pjproudhon')
@panneaux = Product.find_by(slug: 'panneaux-de-mdf')
@cart1 = Order.find_by(token: 'KbSmmD_gi9w_CrpwtK9OwA1666687433963')
end
test 'user pay order by cart with success' do
login_as(@pjproudhon, scope: :user)
invoice_count = Invoice.count
invoice_items_count = InvoiceItem.count
VCR.use_cassette('store_order_pay_by_cart_success') do
post '/api/checkout/payment',
params: {
payment_id: stripe_payment_method,
order_token: @cart1.token
}.to_json, headers: default_headers
end
@cart1.reload
# general assertions
assert_equal 200, response.status
assert_equal invoice_count + 1, Invoice.count
assert_equal invoice_items_count + 1, InvoiceItem.count
# invoice_items assertions
invoice_item = InvoiceItem.last
assert invoice_item.check_footprint
# invoice assertions
invoice = Invoice.last
assert_invoice_pdf invoice
assert_not_nil invoice.debug_footprint
assert_not invoice.payment_gateway_object.blank?
assert_not invoice.total.blank?
assert invoice.check_footprint
# notification
assert_not_empty Notification.where(attached_object: invoice)
assert_equal @cart1.state, 'paid'
assert_equal @cart1.payment_method, 'card'
assert_equal @cart1.paid_total, 500
stock_movement = @panneaux.product_stock_movements.last
assert_equal stock_movement.stock_type, 'external'
assert_equal stock_movement.reason, 'sold'
assert_equal stock_movement.quantity, -1
assert_equal stock_movement.order_item_id, @cart1.order_items.first.id
activity = @cart1.order_activities.last
assert_equal activity.activity_type, 'paid'
assert_equal activity.operator_profile_id, @pjproudhon.invoicing_profile.id
end
test 'user pay order by wallet with success' do
login_as(@pjproudhon, scope: :user)
service = WalletService.new(user: @admin, wallet: @pjproudhon.wallet)
service.credit(@cart1.total / 100)
invoice_count = Invoice.count
invoice_items_count = InvoiceItem.count
users_credit_count = UsersCredit.count
wallet_transactions_count = WalletTransaction.count
post '/api/checkout/payment',
params: {
# payment_method_id: stripe_payment_method,
order_token: @cart1.token
}.to_json, headers: default_headers
@pjproudhon.wallet.reload
@cart1.reload
# general assertions
assert_equal 200, response.status
assert_equal @cart1.state, 'paid'
assert_equal invoice_count + 1, Invoice.count
assert_equal invoice_items_count + 1, InvoiceItem.count
assert_equal users_credit_count, UsersCredit.count
assert_equal wallet_transactions_count + 1, WalletTransaction.count
# invoice_items assertions
invoice_item = InvoiceItem.last
assert invoice_item.check_footprint
# invoice assertions
invoice = Invoice.last
assert_invoice_pdf invoice
assert_not_nil invoice.debug_footprint
assert invoice.payment_gateway_object.blank?
assert_not invoice.total.blank?
assert invoice.check_footprint
# notification
assert_not_empty Notification.where(attached_object: invoice)
# wallet
assert_equal 0, @pjproudhon.wallet.amount
assert_equal 2, @pjproudhon.wallet.wallet_transactions.count
transaction = @pjproudhon.wallet.wallet_transactions.last
assert_equal 'debit', transaction.transaction_type
assert_equal @cart1.paid_total, 0
assert_equal @cart1.wallet_amount / 100.0, transaction.amount
assert_equal @cart1.payment_method, 'wallet'
assert_equal @cart1.wallet_transaction_id, transaction.id
assert_equal invoice.wallet_amount / 100.0, transaction.amount
end
test 'user pay order by wallet and coupon with success' do
login_as(@pjproudhon, scope: :user)
service = WalletService.new(user: @admin, wallet: @pjproudhon.wallet)
service.credit(@cart1.total / 100)
invoice_count = Invoice.count
invoice_items_count = InvoiceItem.count
users_credit_count = UsersCredit.count
wallet_transactions_count = WalletTransaction.count
post '/api/checkout/payment',
params: {
# payment_method_id: stripe_payment_method,
order_token: @cart1.token,
coupon_code: 'GIME3EUR'
}.to_json, headers: default_headers
@pjproudhon.wallet.reload
@cart1.reload
# general assertions
assert_equal 200, response.status
assert_equal @cart1.state, 'paid'
assert_equal invoice_count + 1, Invoice.count
assert_equal invoice_items_count + 1, InvoiceItem.count
assert_equal users_credit_count, UsersCredit.count
assert_equal wallet_transactions_count + 1, WalletTransaction.count
assert_equal Coupon.find_by(code: 'GIME3EUR').id, @cart1.coupon_id
# invoice_items assertions
invoice_item = InvoiceItem.last
assert invoice_item.check_footprint
# invoice assertions
invoice = Invoice.last
assert_invoice_pdf invoice
assert_not_nil invoice.debug_footprint
assert invoice.payment_gateway_object.blank?
assert_not invoice.total.blank?
assert invoice.check_footprint
# notification
assert_not_empty Notification.where(attached_object: invoice)
# wallet
assert_equal 3, @pjproudhon.wallet.amount
assert_equal 2, @pjproudhon.wallet.wallet_transactions.count
transaction = @pjproudhon.wallet.wallet_transactions.last
assert_equal 'debit', transaction.transaction_type
assert_equal @cart1.paid_total, 0
assert_equal @cart1.wallet_amount, 200
assert_equal 2, transaction.amount
end
end

View File

@ -0,0 +1,51 @@
# frozen_string_literal: true
require 'test_helper'
class Cart::AddItemServiceTest < ActiveSupport::TestCase
setup do
@panneaux = Product.find_by(slug: 'panneaux-de-mdf')
@caisse_en_bois = Product.find_by(slug: 'caisse-en-bois')
@cart = Order.find_by(token: 'MkI5z9qVxe_YdNYCR_WN6g1666628074732')
end
test 'add a product to cart' do
cart = Cart::AddItemService.new.call(@cart, @panneaux, 10)
assert_equal cart.total, @panneaux.amount * 10
assert_equal cart.order_items.length, 1
assert_equal cart.order_items.first.amount, @panneaux.amount
assert_equal cart.order_items.first.quantity, 10
end
test 'add a product with quantity min' do
cart = Cart::AddItemService.new.call(@cart, @caisse_en_bois)
assert_equal cart.total, @caisse_en_bois.amount * @caisse_en_bois.quantity_min
assert_equal cart.order_items.length, 1
assert_equal cart.order_items.first.amount, @caisse_en_bois.amount
assert_equal cart.order_items.first.quantity, @caisse_en_bois.quantity_min
end
test 'add two product to cart' do
cart = Cart::AddItemService.new.call(@cart, @panneaux, 10)
cart = Cart::AddItemService.new.call(@cart, @caisse_en_bois)
assert_equal cart.total, (@caisse_en_bois.amount * 5) + (@panneaux.amount * 10)
assert_equal cart.order_items.length, 2
assert_equal cart.order_items.first.amount, @panneaux.amount
assert_equal cart.order_items.first.quantity, 10
assert_equal cart.order_items.last.amount, @caisse_en_bois.amount
assert_equal cart.order_items.last.quantity, 5
end
test 'cannot add a product out of stock' do
assert_raise Cart::OutStockError do
Cart::AddItemService.new.call(@cart, @caisse_en_bois, 101)
end
end
test 'cannot add a product inactive' do
assert_raise Cart::InactiveProductError do
product_inactive = Product.find_by(slug: 'sticker-hello')
Cart::AddItemService.new.call(@cart, product_inactive, 1)
end
end
end

View File

@ -0,0 +1,50 @@
# frozen_string_literal: true
require 'test_helper'
class Cart::CheckCartServiceTest < ActiveSupport::TestCase
setup do
@panneaux = Product.find_by(slug: 'panneaux-de-mdf')
@cart = Order.find_by(token: 'KbSmmD_gi9w_CrpwtK9OwA1666687433963')
end
test 'product is inactive in cart' do
@panneaux.is_active = false
@panneaux.save
errors = Cart::CheckCartService.new.call(@cart)
assert_equal errors[:details].length, 1
assert_equal errors[:details].first[:errors].length, 1
assert_equal errors[:details].first[:errors].first[:error], 'is_active'
assert_equal errors[:details].first[:errors].first[:value], false
end
test 'product is out of stock in cart' do
@panneaux.stock['external'] = 0
@panneaux.save
errors = Cart::CheckCartService.new.call(@cart)
assert_equal errors[:details].length, 1
assert_equal errors[:details].first[:errors].length, 1
assert_equal errors[:details].first[:errors].first[:error], 'stock'
assert_equal errors[:details].first[:errors].first[:value], 0
end
test 'product is less than quantity min in cart' do
@panneaux.quantity_min = 2
@panneaux.save
errors = Cart::CheckCartService.new.call(@cart)
assert_equal errors[:details].length, 1
assert_equal errors[:details].first[:errors].length, 1
assert_equal errors[:details].first[:errors].first[:error], 'quantity_min'
assert_equal errors[:details].first[:errors].first[:value], 2
end
test 'product amount changed in cart' do
@panneaux.amount = 600
@panneaux.save
errors = Cart::CheckCartService.new.call(@cart)
assert_equal errors[:details].length, 1
assert_equal errors[:details].first[:errors].length, 1
assert_equal errors[:details].first[:errors].first[:error], 'amount'
assert_equal errors[:details].first[:errors].first[:value], 600 / 100.0
end
end

View File

@ -0,0 +1,78 @@
# frozen_string_literal: true
require 'test_helper'
class Cart::FindOrCreateServiceTest < ActiveSupport::TestCase
setup do
@jdupond = User.find_by(username: 'jdupond')
@acamus = User.find_by(username: 'acamus')
@admin = User.find_by(username: 'admin')
end
test 'anoymous user create a new cart' do
cart = Cart::FindOrCreateService.new(nil).call(nil)
assert_equal cart.state, 'cart'
assert_equal cart.total, 0
assert_nil cart.statistic_profile_id
assert_nil cart.operator_profile_id
end
test 'user create a new cart' do
cart = Cart::FindOrCreateService.new(@jdupond).call(nil)
assert_equal cart.state, 'cart'
assert_equal cart.statistic_profile_id, @jdupond.statistic_profile.id
assert_equal cart.total, 0
assert_nil cart.operator_profile_id
assert_equal Order.where(statistic_profile_id: @jdupond.statistic_profile.id, state: 'cart').count, 1
end
test 'find cart by token' do
cart = Cart::FindOrCreateService.new(nil).call('MkI5z9qVxe_YdNYCR_WN6g1666628074732')
assert_equal cart.state, 'cart'
assert_equal cart.total, 0
assert_nil cart.statistic_profile_id
assert_nil cart.operator_profile_id
end
test 'get last cart' do
cart = Cart::FindOrCreateService.new(@acamus).call(nil)
assert_equal cart.token, '9VWkmJDSx7QixRusL7ppWg1666628033284'
end
test 'cannot get cart of other user by token' do
cart = Cart::FindOrCreateService.new(@jdupond).call('9VWkmJDSx7QixRusL7ppWg1666628033284')
assert_equal cart.state, 'cart'
assert_equal cart.total, 0
assert_nil cart.operator_profile_id
assert_not_equal cart.token, '9VWkmJDSx7QixRusL7ppWg1666628033284'
end
test 'migrate a cart to user' do
cart = Cart::FindOrCreateService.new(@jdupond).call('MkI5z9qVxe_YdNYCR_WN6g1666628074732')
assert_equal cart.state, 'cart'
assert_equal cart.total, 0
assert_equal cart.statistic_profile_id, @jdupond.statistic_profile.id
assert_nil cart.operator_profile_id
assert_equal Order.where(statistic_profile_id: @jdupond.statistic_profile.id, state: 'cart').count, 1
end
test 'user have only one cart' do
cart = Cart::FindOrCreateService.new(@acamus).call('MkI5z9qVxe_YdNYCR_WN6g1666628074732')
assert_equal cart.token, '9VWkmJDSx7QixRusL7ppWg1666628033284'
assert_equal cart.state, 'cart'
assert_equal cart.total, 0
assert_equal cart.statistic_profile_id, @acamus.statistic_profile.id
assert_nil cart.operator_profile_id
assert_equal Order.where(statistic_profile_id: @acamus.statistic_profile.id, state: 'cart').count, 1
assert_nil Order.find_by(token: 'MkI5z9qVxe_YdNYCR_WN6g1666628074732')
end
test 'admin get a cart' do
cart = Cart::FindOrCreateService.new(@admin).call(nil)
assert_equal cart.state, 'cart'
assert_equal cart.total, 261_500
assert_equal cart.operator_profile_id, @admin.invoicing_profile.id
assert_nil cart.statistic_profile_id
assert_equal Order.where(operator_profile_id: @admin.invoicing_profile.id, state: 'cart').count, 1
end
end

View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
require 'test_helper'
class Cart::RefreshItemServiceTest < ActiveSupport::TestCase
setup do
@panneaux = Product.find_by(slug: 'panneaux-de-mdf')
@cart1 = Order.find_by(token: 'KbSmmD_gi9w_CrpwtK9OwA1666687433963')
@cart2 = Order.find_by(token: 'MkI5z9qVxe_YdNYCR_WN6g1666628074732')
end
test 'refresh total and item amount if product change amount' do
@panneaux.amount = 10_000
@panneaux.save
cart = Cart::RefreshItemService.new.call(@cart1, @panneaux)
assert_equal cart.total, 10_000
assert_equal cart.order_items.first.amount, 10_000
end
test 'cannot refresh total and item amount if product isnt in cart' do
assert_raise ActiveRecord::RecordNotFound do
Cart::RefreshItemService.new.call(@cart2, @panneaux)
end
end
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
require 'test_helper'
class Cart::RemoveItemServiceTest < ActiveSupport::TestCase
setup do
@panneaux = Product.find_by(slug: 'panneaux-de-mdf')
@cart1 = Order.find_by(token: 'KbSmmD_gi9w_CrpwtK9OwA1666687433963')
@cart2 = Order.find_by(token: 'MkI5z9qVxe_YdNYCR_WN6g1666628074732')
end
test 'remove a product to cart' do
cart = Cart::RemoveItemService.new.call(@cart1, @panneaux)
assert_equal cart.total, 0
assert_equal cart.order_items.length, 0
end
test 'cannot remove a product that isnt in cart' do
assert_raise ActiveRecord::RecordNotFound do
Cart::RemoveItemService.new.call(@cart2, @panneaux)
end
end
end

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'test_helper'
class Cart::SetOfferServiceTest < ActiveSupport::TestCase
setup do
@caisse_en_bois = Product.find_by(slug: 'caisse-en-bois')
@panneaux = Product.find_by(slug: 'panneaux-de-mdf')
@cart = Order.find_by(token: '0DKxbAOzSXRx-amXyhmDdg1666691976019')
end
test 'set offer product in cart' do
cart = Cart::SetOfferService.new.call(@cart, @caisse_en_bois, true)
assert_equal cart.total, 0
assert_equal cart.order_items.first.amount, @caisse_en_bois.amount
assert_equal cart.order_items.first.is_offered, true
end
test 'cannot set offer if product that isnt in cart' do
assert_raise ActiveRecord::RecordNotFound do
Cart::SetOfferService.new.call(@cart, @panneaux, true)
end
end
end

View File

@ -0,0 +1,37 @@
# frozen_string_literal: true
require 'test_helper'
class Cart::SetQuantityServiceTest < ActiveSupport::TestCase
setup do
@panneaux = Product.find_by(slug: 'panneaux-de-mdf')
@caisse_en_bois = Product.find_by(slug: 'caisse-en-bois')
@cart1 = Order.find_by(token: 'KbSmmD_gi9w_CrpwtK9OwA1666687433963')
@cart2 = Order.find_by(token: 'MkI5z9qVxe_YdNYCR_WN6g1666628074732')
@cart3 = Order.find_by(token: '4bB96D-MlqJGBr5T8eui-g1666690417460')
end
test 'change quantity of product in cart' do
cart = Cart::SetQuantityService.new.call(@cart1, @panneaux, 10)
assert_equal cart.total, @panneaux.amount * 10
assert_equal cart.order_items.length, 1
end
test 'change quantity of product greater than stock' do
assert_raise Cart::OutStockError do
Cart::SetQuantityService.new.call(@cart1, @panneaux, 1000)
end
end
test 'cannot change quantity less than product quantity min' do
cart = Cart::SetQuantityService.new.call(@cart3, @caisse_en_bois, 1)
assert_equal cart.total, @caisse_en_bois.amount * @caisse_en_bois.quantity_min
assert_equal cart.order_items.first.quantity, @caisse_en_bois.quantity_min
end
test 'cannot change quantity if product that isnt in cart' do
assert_raise ActiveRecord::RecordNotFound do
Cart::SetQuantityService.new.call(@cart2, @panneaux, 10)
end
end
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
require 'test_helper'
class Cart::UpdateTotalServiceTest < ActiveSupport::TestCase
setup do
@panneaux = Product.find_by(slug: 'panneaux-de-mdf')
end
test 'total equal to product amount multiplied quantity' do
order = Order.new
order.order_items.push OrderItem.new(orderable: @panneaux, amount: @panneaux.amount, quantity: 10)
cart = Cart::UpdateTotalService.new.call(order)
assert_equal cart.total, @panneaux.amount * 10
end
test 'total equal to zero if product offered' do
order = Order.new
order.order_items.push OrderItem.new(orderable: @panneaux, amount: @panneaux.amount, quantity: 10, is_offered: true)
cart = Cart::UpdateTotalService.new.call(order)
assert_equal cart.total, 0
end
end

View File

@ -16,7 +16,7 @@ class StoreStatisticServiceTest < ActionDispatch::IntegrationTest
# we should find order id 15 (created today)
stat_order = Stats::Order.search(query: { bool: { must: [{ term: { date: DateTime.current.to_date.iso8601 } },
{ term: { type: 'order' } }] } }).first
{ term: { type: 'store' } }] } }).first
assert_not_nil stat_order
assert_equal @order.id, stat_order['orderId']
check_statistics_on_user(stat_order)

View File

@ -0,0 +1,338 @@
---
http_interactions:
- request:
method: post
uri: https://api.stripe.com/v1/payment_methods
body:
encoding: UTF-8
string: type=card&card[number]=4242424242424242&card[exp_month]=4&card[exp_year]=2023&card[cvc]=314
headers:
User-Agent:
- Stripe/v1 RubyBindings/5.29.0
Authorization:
- Bearer sk_test_testfaketestfaketestfake
Content-Type:
- application/x-www-form-urlencoded
Stripe-Version:
- '2019-08-14'
X-Stripe-Client-User-Agent:
- '{"bindings_version":"5.29.0","lang":"ruby","lang_version":"2.6.3 p62 (2019-04-16)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin
MacBook-Pro-Sleede-Peng 20.6.0 Darwin Kernel Version 20.6.0: Wed Nov 10 22:23:07
PST 2021; root:xnu-7195.141.14~1/RELEASE_X86_64 x86_64","hostname":"MacBook-Pro-Sleede-Peng"}'
Accept-Encoding:
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept:
- "*/*"
response:
status:
code: 200
message: OK
headers:
Server:
- nginx
Date:
- Tue, 25 Oct 2022 17:01:39 GMT
Content-Type:
- application/json
Content-Length:
- '930'
Connection:
- keep-alive
Access-Control-Allow-Credentials:
- 'true'
Access-Control-Allow-Methods:
- GET, POST, HEAD, OPTIONS, DELETE
Access-Control-Allow-Origin:
- "*"
Access-Control-Expose-Headers:
- Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
Access-Control-Max-Age:
- '300'
Cache-Control:
- no-cache, no-store
Idempotency-Key:
- f35e1243-3373-4c8a-b489-941ab090e32a
Original-Request:
- req_6m9f3nj5nuWQk9
Request-Id:
- req_6m9f3nj5nuWQk9
Stripe-Should-Retry:
- 'false'
Stripe-Version:
- '2019-08-14'
Strict-Transport-Security:
- max-age=63072000; includeSubDomains; preload
body:
encoding: UTF-8
string: |-
{
"id": "pm_1LwqVT2sOmf47Nz9JSw0Rtly",
"object": "payment_method",
"billing_details": {
"address": {
"city": null,
"country": null,
"line1": null,
"line2": null,
"postal_code": null,
"state": null
},
"email": null,
"name": null,
"phone": null
},
"card": {
"brand": "visa",
"checks": {
"address_line1_check": null,
"address_postal_code_check": null,
"cvc_check": "unchecked"
},
"country": "US",
"exp_month": 4,
"exp_year": 2023,
"fingerprint": "o52jybR7bnmNn6AT",
"funding": "credit",
"generated_from": null,
"last4": "4242",
"networks": {
"available": [
"visa"
],
"preferred": null
},
"three_d_secure_usage": {
"supported": true
},
"wallet": null
},
"created": 1666717299,
"customer": null,
"livemode": false,
"metadata": {},
"type": "card"
}
recorded_at: Tue, 25 Oct 2022 17:01:39 GMT
- request:
method: post
uri: https://api.stripe.com/v1/payment_intents
body:
encoding: UTF-8
string: payment_method=pm_1LwqVT2sOmf47Nz9JSw0Rtly&amount=500&currency=usd&confirmation_method=manual&confirm=true&customer=cus_IhIynmoJbzLpwX
headers:
User-Agent:
- Stripe/v1 RubyBindings/5.29.0
Authorization:
- Bearer sk_test_testfaketestfaketestfake
Content-Type:
- application/x-www-form-urlencoded
X-Stripe-Client-Telemetry:
- '{"last_request_metrics":{"request_id":"req_6m9f3nj5nuWQk9","request_duration_ms":667}}'
Stripe-Version:
- '2019-08-14'
X-Stripe-Client-User-Agent:
- '{"bindings_version":"5.29.0","lang":"ruby","lang_version":"2.6.3 p62 (2019-04-16)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin
MacBook-Pro-Sleede-Peng 20.6.0 Darwin Kernel Version 20.6.0: Wed Nov 10 22:23:07
PST 2021; root:xnu-7195.141.14~1/RELEASE_X86_64 x86_64","hostname":"MacBook-Pro-Sleede-Peng"}'
Accept-Encoding:
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept:
- "*/*"
response:
status:
code: 200
message: OK
headers:
Server:
- nginx
Date:
- Tue, 25 Oct 2022 17:01:41 GMT
Content-Type:
- application/json
Content-Length:
- '4464'
Connection:
- keep-alive
Access-Control-Allow-Credentials:
- 'true'
Access-Control-Allow-Methods:
- GET, POST, HEAD, OPTIONS, DELETE
Access-Control-Allow-Origin:
- "*"
Access-Control-Expose-Headers:
- Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
Access-Control-Max-Age:
- '300'
Cache-Control:
- no-cache, no-store
Idempotency-Key:
- a7152ff2-3b76-46d6-8cfa-60979a27e592
Original-Request:
- req_8ymsTfLVq6gYs5
Request-Id:
- req_8ymsTfLVq6gYs5
Stripe-Should-Retry:
- 'false'
Stripe-Version:
- '2019-08-14'
Strict-Transport-Security:
- max-age=63072000; includeSubDomains; preload
body:
encoding: UTF-8
string: |-
{
"id": "pi_3LwqVU2sOmf47Nz91iSEP5Kz",
"object": "payment_intent",
"amount": 500,
"amount_capturable": 0,
"amount_details": {
"tip": {}
},
"amount_received": 500,
"application": null,
"application_fee_amount": null,
"automatic_payment_methods": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"charges": {
"object": "list",
"data": [
{
"id": "ch_3LwqVU2sOmf47Nz91Twrth0m",
"object": "charge",
"amount": 500,
"amount_captured": 500,
"amount_refunded": 0,
"application": null,
"application_fee": null,
"application_fee_amount": null,
"balance_transaction": "txn_3LwqVU2sOmf47Nz9170kBflD",
"billing_details": {
"address": {
"city": null,
"country": null,
"line1": null,
"line2": null,
"postal_code": null,
"state": null
},
"email": null,
"name": null,
"phone": null
},
"calculated_statement_descriptor": "Stripe",
"captured": true,
"created": 1666717300,
"currency": "usd",
"customer": "cus_IhIynmoJbzLpwX",
"description": null,
"destination": null,
"dispute": null,
"disputed": false,
"failure_balance_transaction": null,
"failure_code": null,
"failure_message": null,
"fraud_details": {},
"invoice": null,
"livemode": false,
"metadata": {},
"on_behalf_of": null,
"order": null,
"outcome": {
"network_status": "approved_by_network",
"reason": null,
"risk_level": "normal",
"risk_score": 22,
"seller_message": "Payment complete.",
"type": "authorized"
},
"paid": true,
"payment_intent": "pi_3LwqVU2sOmf47Nz91iSEP5Kz",
"payment_method": "pm_1LwqVT2sOmf47Nz9JSw0Rtly",
"payment_method_details": {
"card": {
"brand": "visa",
"checks": {
"address_line1_check": null,
"address_postal_code_check": null,
"cvc_check": "pass"
},
"country": "US",
"exp_month": 4,
"exp_year": 2023,
"fingerprint": "o52jybR7bnmNn6AT",
"funding": "credit",
"installments": null,
"last4": "4242",
"mandate": null,
"network": "visa",
"three_d_secure": null,
"wallet": null
},
"type": "card"
},
"receipt_email": null,
"receipt_number": null,
"receipt_url": "https://pay.stripe.com/receipts/payment/CAcaFwoVYWNjdF8xMDNyRTYyc09tZjQ3Tno5KPWs4JoGMgbJTvRAJtU6LBbJYFh6FJvH_a5RzUPRqsu57K44IOuXb8Or12WKgAyL7TIX_J3zBZvUJhl1",
"refunded": false,
"refunds": {
"object": "list",
"data": [],
"has_more": false,
"total_count": 0,
"url": "/v1/charges/ch_3LwqVU2sOmf47Nz91Twrth0m/refunds"
},
"review": null,
"shipping": null,
"source": null,
"source_transfer": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "succeeded",
"transfer_data": null,
"transfer_group": null
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/charges?payment_intent=pi_3LwqVU2sOmf47Nz91iSEP5Kz"
},
"client_secret": "pi_3LwqVU2sOmf47Nz91iSEP5Kz_secret_G8gooGGZf0UBUH5BNUxvHG8vt",
"confirmation_method": "manual",
"created": 1666717300,
"currency": "usd",
"customer": "cus_IhIynmoJbzLpwX",
"description": null,
"invoice": null,
"last_payment_error": null,
"livemode": false,
"metadata": {},
"next_action": null,
"on_behalf_of": null,
"payment_method": "pm_1LwqVT2sOmf47Nz9JSw0Rtly",
"payment_method_options": {
"card": {
"installments": null,
"mandate_options": null,
"network": null,
"request_three_d_secure": "automatic"
}
},
"payment_method_types": [
"card"
],
"processing": null,
"receipt_email": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"source": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "succeeded",
"transfer_data": null,
"transfer_group": null
}
recorded_at: Tue, 25 Oct 2022 17:01:41 GMT
recorded_with: VCR 6.0.0