diff --git a/test/fixtures/files/products/pla-filament.jpg b/test/fixtures/files/products/pla-filament.jpg new file mode 100644 index 000000000..9cfe2e362 Binary files /dev/null and b/test/fixtures/files/products/pla-filament.jpg differ diff --git a/test/fixtures/files/products/pla-filament2.jpg b/test/fixtures/files/products/pla-filament2.jpg new file mode 100644 index 000000000..ebc8baac7 Binary files /dev/null and b/test/fixtures/files/products/pla-filament2.jpg differ diff --git a/test/integration/events/recurrence_test.rb b/test/integration/events/recurrence_test.rb index 8d22c5c68..ef15d736b 100644 --- a/test/integration/events/recurrence_test.rb +++ b/test/integration/events/recurrence_test.rb @@ -30,8 +30,8 @@ class Events::RecurrenceTest < ActionDispatch::IntegrationTest recurrence: 'week', recurrence_end_at: 10.weeks.from_now.utc, event_files_attributes: [ - { attachment: fixture_file_upload('/files/document.pdf', 'document/pdf', true) }, - { attachment: fixture_file_upload('/files/document2.pdf', 'document/pdf', true) } + { attachment: fixture_file_upload('/files/document.pdf', 'application/pdf', true) }, + { attachment: fixture_file_upload('/files/document2.pdf', 'application/pdf', true) } ], event_price_categories_attributes: [ { price_category_id: 1, amount: 10 }, diff --git a/test/integration/machines_test.rb b/test/integration/machines_test.rb index 8a8c117d8..9eb12add9 100644 --- a/test/integration/machines_test.rb +++ b/test/integration/machines_test.rb @@ -20,8 +20,8 @@ class MachinesTest < ActionDispatch::IntegrationTest description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore...', spec: 'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium...', machine_files_attributes: [ - { attachment: fixture_file_upload('/files/document.pdf', 'document/pdf', true) }, - { attachment: fixture_file_upload('/files/document2.pdf', 'document/pdf', true) } + { attachment: fixture_file_upload('/files/document.pdf', 'application/pdf', true) }, + { attachment: fixture_file_upload('/files/document2.pdf', 'application/pdf', true) } ], disabled: false } diff --git a/test/integration/products_test.rb b/test/integration/products_test.rb new file mode 100644 index 000000000..96a827523 --- /dev/null +++ b/test/integration/products_test.rb @@ -0,0 +1,140 @@ +# frozen_string_literal: true + +require 'test_helper' + +class ProductsTest < ActionDispatch::IntegrationTest + def setup + @admin = User.find_by(username: 'admin') + login_as(@admin, scope: :user) + end + + test 'create a product' do + name = 'PLA Filament 3mm' + post '/api/products', + params: { + product: { + name: name, + slug: 'pla-filament-3mm', + sku: 'TOL-12953', + description: '3mm red PLA plastic filament for 3D printing. PLA is a great general purpose filament with great surface ' \ + 'finish, is easy to print, and even biodegradable.', + is_active: true, + product_category_id: 3, + amount: 174, + quantity_min: 5, + low_stock_alert: true, + low_stock_threshold: 100, + machine_ids: [4, 6], + product_files_attributes: [ + { attachment: fixture_file_upload('/files/document.pdf', 'application/pdf', true) }, + { attachment: fixture_file_upload('/files/document2.pdf', 'application/pdf', true) } + ], + product_images_attributes: [ + { attachment: fixture_file_upload('/files/products/pla-filament.jpg', 'image/jpg'), is_main: true }, + { attachment: fixture_file_upload('/files/products/pla-filament2.jpg', 'image/jpg'), is_main: false } + ], + advanced_accounting_attributes: { + code: '704611', + analytical_section: '9D441C' + }, + product_stock_movements_attributes: [ + { stock_type: 'internal', quantity: 100, reason: 'inward_stock' }, + { stock_type: 'external', quantity: 14, reason: 'other_in' } + ] + } + }, + headers: upload_headers + + # Check response format & status + assert_equal 201, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the poduct was correctly created + db_product = Product.where(name: name).first + assert_not_nil db_product + assert_equal 2, db_product.product_images.count + assert_equal 2, db_product.product_files.count + assert_equal name, db_product.name + assert_equal '704611', db_product.advanced_accounting.code + assert_equal '9D441C', db_product.advanced_accounting.analytical_section + assert_not_empty db_product.description + assert_equal true, db_product.is_active + assert_equal 3, db_product.product_category_id + assert_equal 17_400, db_product.amount + assert_equal 5, db_product.quantity_min + assert_equal true, db_product.low_stock_alert + assert_equal 100, db_product.low_stock_threshold + assert_equal [4, 6], db_product.machine_ids + assert_equal 'pla-filament-3mm', db_product.slug + assert_equal 'TOL-12953', db_product.sku + assert_equal 100, db_product.stock['internal'] + assert_equal 14, db_product.stock['external'] + end + + test 'update a product' do + db_product = Product.find(3) + + description = '
Cette caisse en bois masif est vraiment superbe !
' + put '/api/products/3', + params: { + product: { + description: description, + amount: 52_300, + product_stock_movements_attributes: [ + { stock_type: 'external', quantity: 20, reason: 'damaged' }, + { stock_type: 'internal', quantity: 1, reason: 'sold' } + ] + } + }.to_json, + headers: default_headers + + # Check response format & status + assert_equal 200, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the product was correctly updated + db_product.reload + assert_equal description, db_product.description + assert_equal 80, db_product.stock['external'] + assert_equal 0, db_product.stock['internal'] + product = json_response(response.body) + assert_equal description, product[:description] + assert_equal 80, product[:stock][:external] + assert_equal 0, product[:stock][:internal] + end + + test 'delete a product' do + delete '/api/products/3', headers: default_headers + assert_response :success + assert_empty response.body + end + + test 'clone a product' do + name = 'Panneau de contre-plaquƩ peuplier 15 mm' + put '/api/products/15/clone', + params: { + product: { + name: name, + sku: '12-4614', + is_active: false + } + }.to_json, + headers: default_headers + assert_response :success + assert_equal Mime[:json], response.content_type + + # Check the new product + product = Product.last + original = Product.find(15) + assert_equal name, product.name + assert_equal '12-4614', product.sku + assert_not product.is_active + assert_equal original.product_category_id, product.product_category_id + assert_equal original.amount, product.amount + assert_equal original.quantity_min, product.quantity_min + assert_equal original.low_stock_alert, product.low_stock_alert + assert_equal 0, product.stock['internal'] + assert_equal 0, product.stock['external'] + assert_not_equal original.slug, product.slug + end +end diff --git a/test/integration/spaces_test.rb b/test/integration/spaces_test.rb index bcc34080c..11143f6f1 100644 --- a/test/integration/spaces_test.rb +++ b/test/integration/spaces_test.rb @@ -21,8 +21,8 @@ class SpacesTest < ActionDispatch::IntegrationTest characteristics: 'Sed fermentum ante ut elit lobortis, id auctor libero cursus. Sed augue lectus, mollis at luctus eu...', default_places: 6, space_files_attributes: [ - { attachment: fixture_file_upload('/files/document.pdf', 'document/pdf', true) }, - { attachment: fixture_file_upload('/files/document2.pdf', 'document/pdf', true) } + { attachment: fixture_file_upload('/files/document.pdf', 'application/pdf', true) }, + { attachment: fixture_file_upload('/files/document2.pdf', 'application/pdf', true) } ], disabled: false }