From 2ae0cd8a8dc6d630ee1a7a84ddf6111609dbdeb7 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 30 Mar 2023 17:29:40 -0600 Subject: Mejora la estructura de los specs --- .../create_products_controller_spec.rb | 48 ---------------------- spec/requests/products_controller/create_spec.rb | 48 ++++++++++++++++++++++ .../destroy_products_controller_spec.rb | 15 ------- spec/requests/products_controller/destroy_spec.rb | 15 +++++++ .../index_products_controller_spec.rb | 15 ------- spec/requests/products_controller/index_spec.rb | 15 +++++++ .../show_products_controller_spec.rb | 17 -------- spec/requests/products_controller/show_spec.rb | 17 ++++++++ .../update_products_controller_spec.rb | 40 ------------------ spec/requests/products_controller/update_spec.rb | 40 ++++++++++++++++++ 10 files changed, 135 insertions(+), 135 deletions(-) delete mode 100644 spec/requests/products_controller/create_products_controller_spec.rb create mode 100644 spec/requests/products_controller/create_spec.rb delete mode 100644 spec/requests/products_controller/destroy_products_controller_spec.rb create mode 100644 spec/requests/products_controller/destroy_spec.rb delete mode 100644 spec/requests/products_controller/index_products_controller_spec.rb create mode 100644 spec/requests/products_controller/index_spec.rb delete mode 100644 spec/requests/products_controller/show_products_controller_spec.rb create mode 100644 spec/requests/products_controller/show_spec.rb delete mode 100644 spec/requests/products_controller/update_products_controller_spec.rb create mode 100644 spec/requests/products_controller/update_spec.rb (limited to 'spec/requests/products_controller') diff --git a/spec/requests/products_controller/create_products_controller_spec.rb b/spec/requests/products_controller/create_products_controller_spec.rb deleted file mode 100644 index 400fd18..0000000 --- a/spec/requests/products_controller/create_products_controller_spec.rb +++ /dev/null @@ -1,48 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'POST /api/products', type: :request do - let(:product) { build(:product) } - let(:picture) { fixture_file_upload('800px-Stray_cat_on_wall.jpg', 'image/jpeg') } - let(:categories) do - c = '' - product.categories.each { |category| c += "#{category}," } - c - end - - it_behaves_like 'a POST request' do - let(:user) { create(:user_account, role: 'master') } - let(:token) { jwt(user) } - let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } - let(:route) { '/api/products' } - let(:expected_error_messages) do - ["Name can't be blank", 'Unitary price must be greater than 0', 'Bulk price must be greater than 0', - 'Available quantity must be greater than 0', 'Company must exist'] - end - let(:desired_error_status) { 422 } - let(:expected_text) do - product.categories.concat([product.name, product.unitary_price.to_i.to_s, product.bulk_price.to_i.to_s, - product.available_quantity.to_s, product.company.short_name, 'picture', 'http']) - end - let(:params) do - { name: product.name, unitary_price: product.unitary_price, bulk_price: product.bulk_price, picture:, - available_quantity: product.available_quantity, categories:, company_id: product.company_id } - end - let(:wrong_params) do - JSON.generate({ name: '', unitary_price: -10, bulk_price: -10, available_quantity: -10, categories: '', - company_id: nil }) - end - end - - it_behaves_like 'a POST request that requires a master user' do - let(:user) { create(:user_account, role: 'regular') } - let(:token) { jwt(user) } - let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } - let(:route) { '/api/products' } - let(:params) do - { name: product.name, unitary_price: product.unitary_price, bulk_price: product.bulk_price, picture:, - available_quantity: product.available_quantity, categories:, company_id: product.company_id } - end - end -end \ No newline at end of file diff --git a/spec/requests/products_controller/create_spec.rb b/spec/requests/products_controller/create_spec.rb new file mode 100644 index 0000000..400fd18 --- /dev/null +++ b/spec/requests/products_controller/create_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /api/products', type: :request do + let(:product) { build(:product) } + let(:picture) { fixture_file_upload('800px-Stray_cat_on_wall.jpg', 'image/jpeg') } + let(:categories) do + c = '' + product.categories.each { |category| c += "#{category}," } + c + end + + it_behaves_like 'a POST request' do + let(:user) { create(:user_account, role: 'master') } + let(:token) { jwt(user) } + let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { '/api/products' } + let(:expected_error_messages) do + ["Name can't be blank", 'Unitary price must be greater than 0', 'Bulk price must be greater than 0', + 'Available quantity must be greater than 0', 'Company must exist'] + end + let(:desired_error_status) { 422 } + let(:expected_text) do + product.categories.concat([product.name, product.unitary_price.to_i.to_s, product.bulk_price.to_i.to_s, + product.available_quantity.to_s, product.company.short_name, 'picture', 'http']) + end + let(:params) do + { name: product.name, unitary_price: product.unitary_price, bulk_price: product.bulk_price, picture:, + available_quantity: product.available_quantity, categories:, company_id: product.company_id } + end + let(:wrong_params) do + JSON.generate({ name: '', unitary_price: -10, bulk_price: -10, available_quantity: -10, categories: '', + company_id: nil }) + end + end + + it_behaves_like 'a POST request that requires a master user' do + let(:user) { create(:user_account, role: 'regular') } + let(:token) { jwt(user) } + let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { '/api/products' } + let(:params) do + { name: product.name, unitary_price: product.unitary_price, bulk_price: product.bulk_price, picture:, + available_quantity: product.available_quantity, categories:, company_id: product.company_id } + end + end +end \ No newline at end of file diff --git a/spec/requests/products_controller/destroy_products_controller_spec.rb b/spec/requests/products_controller/destroy_products_controller_spec.rb deleted file mode 100644 index 54a57d2..0000000 --- a/spec/requests/products_controller/destroy_products_controller_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'DELETE /api/products/product_id', type: :request do - let(:user) { create(:user_account, role: 'master') } - let(:token) { jwt(user) } - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - - it_behaves_like 'a DELETE request' do - let(:product) { create(:product, public_id: SecureRandom.hex(12)) } - let(:resource) { "/api/products/#{product.public_id}" } - let(:nonexistent_resource) { "/api/products/#{SecureRandom.hex(8)}" } - end -end diff --git a/spec/requests/products_controller/destroy_spec.rb b/spec/requests/products_controller/destroy_spec.rb new file mode 100644 index 0000000..54a57d2 --- /dev/null +++ b/spec/requests/products_controller/destroy_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'DELETE /api/products/product_id', type: :request do + let(:user) { create(:user_account, role: 'master') } + let(:token) { jwt(user) } + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + + it_behaves_like 'a DELETE request' do + let(:product) { create(:product, public_id: SecureRandom.hex(12)) } + let(:resource) { "/api/products/#{product.public_id}" } + let(:nonexistent_resource) { "/api/products/#{SecureRandom.hex(8)}" } + end +end diff --git a/spec/requests/products_controller/index_products_controller_spec.rb b/spec/requests/products_controller/index_products_controller_spec.rb deleted file mode 100644 index a4decce..0000000 --- a/spec/requests/products_controller/index_products_controller_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'GET /api/products', type: :request do - before(:each) do - create_list(:product, 26) - end - - it_behaves_like 'a GET index request' do - let(:headers) { {} } - let(:route) { '/api/products' } - let(:pagination_size) { 25 } - end -end diff --git a/spec/requests/products_controller/index_spec.rb b/spec/requests/products_controller/index_spec.rb new file mode 100644 index 0000000..a4decce --- /dev/null +++ b/spec/requests/products_controller/index_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/products', type: :request do + before(:each) do + create_list(:product, 26) + end + + it_behaves_like 'a GET index request' do + let(:headers) { {} } + let(:route) { '/api/products' } + let(:pagination_size) { 25 } + end +end diff --git a/spec/requests/products_controller/show_products_controller_spec.rb b/spec/requests/products_controller/show_products_controller_spec.rb deleted file mode 100644 index 9b2461c..0000000 --- a/spec/requests/products_controller/show_products_controller_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'GET /api/products/:public_id', type: :request do - it_behaves_like 'a GET resource request' do - let(:headers) { {} } - let(:product) { create(:product, public_id: SecureRandom.hex(12)) } - let(:route) { "/api/products/#{product.public_id}" } - let(:invalid_route) { "/api/products/#{Faker::Lorem.word}" } - let(:expected_text) do - product.categories.concat([product.name, product.id.to_s, product.unitary_price.to_i.to_s, - product.bulk_price.to_i.to_s, product.available_quantity.to_s, product.company.name, - product.company.short_name]) - end - end -end diff --git a/spec/requests/products_controller/show_spec.rb b/spec/requests/products_controller/show_spec.rb new file mode 100644 index 0000000..9b2461c --- /dev/null +++ b/spec/requests/products_controller/show_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/products/:public_id', type: :request do + it_behaves_like 'a GET resource request' do + let(:headers) { {} } + let(:product) { create(:product, public_id: SecureRandom.hex(12)) } + let(:route) { "/api/products/#{product.public_id}" } + let(:invalid_route) { "/api/products/#{Faker::Lorem.word}" } + let(:expected_text) do + product.categories.concat([product.name, product.id.to_s, product.unitary_price.to_i.to_s, + product.bulk_price.to_i.to_s, product.available_quantity.to_s, product.company.name, + product.company.short_name]) + end + end +end diff --git a/spec/requests/products_controller/update_products_controller_spec.rb b/spec/requests/products_controller/update_products_controller_spec.rb deleted file mode 100644 index f916d6a..0000000 --- a/spec/requests/products_controller/update_products_controller_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'PUT /api/products/product_id', type: :request do - let(:user) { create(:user_account, role: 'master') } - let(:product) { create(:product, public_id: SecureRandom.hex(12)) } - let(:new_product) { build(:product) } - let(:picture) { fixture_file_upload('800px-Stray_cat_on_wall.jpg', 'image/png') } - let(:token) { jwt(user) } - let(:categories) do - c = '' - new_product.categories.each { |category| c += "#{category}," } - c - end - - it_behaves_like 'a PUT request' do - let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } - let(:route) { "/api/products/#{product.public_id}" } - let(:wrong_route) { "/api/products/#{SecureRandom.hex(8)}" } - let(:expected_error_messages) do - ["Name can't be blank", 'Unitary price must be greater than 0', 'Bulk price must be greater than 0', - 'Available quantity must be greater than 0', 'Company must exist'] - end - let(:desired_error_status) { 422 } - let(:expected_text) do - new_product.categories.concat([new_product.name, new_product.unitary_price.to_i.to_s, - new_product.bulk_price.to_i.to_s, new_product.available_quantity.to_s, - new_product.company.short_name, 'picture', 'http']) - end - let(:params) do - { name: new_product.name, unitary_price: new_product.unitary_price, bulk_price: new_product.bulk_price, picture:, - available_quantity: new_product.available_quantity, categories:, company_id: new_product.company_id } - end - let(:wrong_params) do - JSON.generate({ name: '', unitary_price: -10, bulk_price: -10, available_quantity: -10, - categories: '', company_id: nil }) - end - end -end diff --git a/spec/requests/products_controller/update_spec.rb b/spec/requests/products_controller/update_spec.rb new file mode 100644 index 0000000..f916d6a --- /dev/null +++ b/spec/requests/products_controller/update_spec.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'PUT /api/products/product_id', type: :request do + let(:user) { create(:user_account, role: 'master') } + let(:product) { create(:product, public_id: SecureRandom.hex(12)) } + let(:new_product) { build(:product) } + let(:picture) { fixture_file_upload('800px-Stray_cat_on_wall.jpg', 'image/png') } + let(:token) { jwt(user) } + let(:categories) do + c = '' + new_product.categories.each { |category| c += "#{category}," } + c + end + + it_behaves_like 'a PUT request' do + let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { "/api/products/#{product.public_id}" } + let(:wrong_route) { "/api/products/#{SecureRandom.hex(8)}" } + let(:expected_error_messages) do + ["Name can't be blank", 'Unitary price must be greater than 0', 'Bulk price must be greater than 0', + 'Available quantity must be greater than 0', 'Company must exist'] + end + let(:desired_error_status) { 422 } + let(:expected_text) do + new_product.categories.concat([new_product.name, new_product.unitary_price.to_i.to_s, + new_product.bulk_price.to_i.to_s, new_product.available_quantity.to_s, + new_product.company.short_name, 'picture', 'http']) + end + let(:params) do + { name: new_product.name, unitary_price: new_product.unitary_price, bulk_price: new_product.bulk_price, picture:, + available_quantity: new_product.available_quantity, categories:, company_id: new_product.company_id } + end + let(:wrong_params) do + JSON.generate({ name: '', unitary_price: -10, bulk_price: -10, available_quantity: -10, + categories: '', company_id: nil }) + end + end +end -- cgit v1.2.3