1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

(test) add admin offer product to himself

Also: split AdminPayOrderTest in two files
This commit is contained in:
Sylvain 2022-11-17 17:14:31 +01:00
parent c684b892a7
commit 451cd0e915
5 changed files with 212 additions and 180 deletions

View File

@ -1,5 +1,8 @@
# frozen_string_literal: true
# module definition
module Cart; end
# Provides methods for set offer to item in cart
class Cart::SetOfferService
def call(order, orderable, is_offered)

View File

@ -0,0 +1,209 @@
# frozen_string_literal: true
require 'test_helper'
module Store; end
class Store::AdminOrderForHimselfTest < ActionDispatch::IntegrationTest
setup do
@admin = User.find_by(username: 'admin')
@pjproudhon = User.find_by(username: 'pjproudhon')
@caisse_en_bois = Product.find_by(slug: 'caisse-en-bois')
@panneaux = Product.find_by(slug: 'panneaux-de-mdf')
@cart1 = Order.find_by(token: '0DKxbAOzSXRx-amXyhmDdg1666691976019')
end
test 'admin pay himself order by card with success' do
login_as(@admin, scope: :user)
invoice_count = Invoice.count
invoice_items_count = InvoiceItem.count
VCR.use_cassette('store_order_admin_pay_by_card_success') do
post '/api/checkout/payment',
params: {
payment_id: stripe_payment_method,
order_token: @cart1.token,
customer_id: @admin.id
}.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 + 2, 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 @cart1.payment_gateway_object.blank?
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, 262_500
stock_movement = @caisse_en_bois.product_stock_movements.last
assert_equal stock_movement.stock_type, 'external'
assert_equal stock_movement.reason, 'sold'
assert_equal stock_movement.quantity, -5
assert_equal stock_movement.order_item_id, @cart1.order_items.first.id
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, -2
assert_equal stock_movement.order_item_id, @cart1.order_items.last.id
activity = @cart1.order_activities.last
assert_equal activity.activity_type, 'paid'
assert_equal activity.operator_profile_id, @admin.invoicing_profile.id
end
test 'admin pay himself order by card and wallet with success' do
login_as(@admin, scope: :user)
service = WalletService.new(user: @admin, wallet: @admin.wallet)
service.credit(1000)
invoice_count = Invoice.count
invoice_items_count = InvoiceItem.count
users_credit_count = UsersCredit.count
wallet_transactions_count = WalletTransaction.count
VCR.use_cassette('store_order_admin_pay_by_cart_and_wallet_success') do
post '/api/checkout/payment',
params: {
payment_id: stripe_payment_method,
order_token: @cart1.token,
customer_id: @admin.id
}.to_json, headers: default_headers
end
@admin.wallet.reload
@cart1.reload
# general assertions
assert_equal 200, response.status
assert_equal invoice_count + 1, Invoice.count
assert_equal invoice_items_count + 2, 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 @cart1.payment_gateway_object.blank?
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, 162_500
assert_equal users_credit_count, UsersCredit.count
assert_equal wallet_transactions_count + 1, WalletTransaction.count
# wallet
assert_equal 0, @admin.wallet.amount
assert_equal 2, @admin.wallet.wallet_transactions.count
transaction = @admin.wallet.wallet_transactions.last
assert_equal 'debit', transaction.transaction_type
assert_equal @cart1.wallet_amount / 100.0, transaction.amount
assert_equal @cart1.wallet_transaction_id, transaction.id
assert_equal invoice.wallet_amount / 100.0, transaction.amount
end
test 'admin pay himself order by wallet with success' do
login_as(@admin, scope: :user)
service = WalletService.new(user: @admin, wallet: @admin.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: {
order_token: @cart1.token,
customer_id: @admin.id
}.to_json, headers: default_headers
@admin.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 + 2, 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, @admin.wallet.amount
assert_equal 2, @admin.wallet.wallet_transactions.count
transaction = @admin.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 'admin cannot offer products to himself' do
login_as(@admin, scope: :user)
put '/api/cart/set_offer',
params: {
order_token: @cart1.token,
customer_id: @admin.id,
is_offered: true,
orderable_id: @caisse_en_bois.id
}.to_json, headers: default_headers
assert_equal 403, response.status
end
end

View File

@ -13,129 +13,6 @@ class Store::AdminPayOrderTest < ActionDispatch::IntegrationTest
@cart1 = Order.find_by(token: '0DKxbAOzSXRx-amXyhmDdg1666691976019')
end
test 'admin pay himself order by cart with success' do
login_as(@admin, scope: :user)
invoice_count = Invoice.count
invoice_items_count = InvoiceItem.count
VCR.use_cassette('store_order_admin_pay_by_cart_success') do
post '/api/checkout/payment',
params: {
payment_id: stripe_payment_method,
order_token: @cart1.token,
customer_id: @admin.id
}.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 + 2, 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 @cart1.payment_gateway_object.blank?
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, 262_500
stock_movement = @caisse_en_bois.product_stock_movements.last
assert_equal stock_movement.stock_type, 'external'
assert_equal stock_movement.reason, 'sold'
assert_equal stock_movement.quantity, -5
assert_equal stock_movement.order_item_id, @cart1.order_items.first.id
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, -2
assert_equal stock_movement.order_item_id, @cart1.order_items.last.id
activity = @cart1.order_activities.last
assert_equal activity.activity_type, 'paid'
assert_equal activity.operator_profile_id, @admin.invoicing_profile.id
end
test 'admin pay himself order by cart and wallet with success' do
login_as(@admin, scope: :user)
service = WalletService.new(user: @admin, wallet: @admin.wallet)
service.credit(1000)
invoice_count = Invoice.count
invoice_items_count = InvoiceItem.count
users_credit_count = UsersCredit.count
wallet_transactions_count = WalletTransaction.count
VCR.use_cassette('store_order_admin_pay_by_cart_and_wallet_success') do
post '/api/checkout/payment',
params: {
payment_id: stripe_payment_method,
order_token: @cart1.token,
customer_id: @admin.id
}.to_json, headers: default_headers
end
@admin.wallet.reload
@cart1.reload
# general assertions
assert_equal 200, response.status
assert_equal invoice_count + 1, Invoice.count
assert_equal invoice_items_count + 2, 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 @cart1.payment_gateway_object.blank?
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, 162_500
assert_equal users_credit_count, UsersCredit.count
assert_equal wallet_transactions_count + 1, WalletTransaction.count
# wallet
assert_equal 0, @admin.wallet.amount
assert_equal 2, @admin.wallet.wallet_transactions.count
transaction = @admin.wallet.wallet_transactions.last
assert_equal 'debit', transaction.transaction_type
assert_equal @cart1.wallet_amount / 100.0, transaction.amount
assert_equal @cart1.wallet_transaction_id, transaction.id
assert_equal invoice.wallet_amount / 100.0, transaction.amount
end
test 'admin pay user order by local with success' do
login_as(@admin, scope: :user)
@ -254,63 +131,6 @@ class Store::AdminPayOrderTest < ActionDispatch::IntegrationTest
assert_equal activity.operator_profile_id, @admin.invoicing_profile.id
end
test 'admin pay himself order by wallet with success' do
login_as(@admin, scope: :user)
service = WalletService.new(user: @admin, wallet: @admin.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: {
order_token: @cart1.token,
customer_id: @admin.id
}.to_json, headers: default_headers
@admin.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 + 2, 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, @admin.wallet.amount
assert_equal 2, @admin.wallet.wallet_transactions.count
transaction = @admin.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 'admin pay user order by wallet with success' do
login_as(@admin, scope: :user)