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_addresses_controller_spec.rb | 47 ------------------- spec/requests/addresses_controller/create_spec.rb | 47 +++++++++++++++++++ .../destroy_addresses_controller_spec.rb | 35 -------------- spec/requests/addresses_controller/destroy_spec.rb | 35 ++++++++++++++ .../index_addresses_controller_spec.rb | 19 -------- spec/requests/addresses_controller/index_spec.rb | 19 ++++++++ .../update_addresses_controller_spec.rb | 54 ---------------------- spec/requests/addresses_controller/update_spec.rb | 54 ++++++++++++++++++++++ 8 files changed, 155 insertions(+), 155 deletions(-) delete mode 100644 spec/requests/addresses_controller/create_addresses_controller_spec.rb create mode 100644 spec/requests/addresses_controller/create_spec.rb delete mode 100644 spec/requests/addresses_controller/destroy_addresses_controller_spec.rb create mode 100644 spec/requests/addresses_controller/destroy_spec.rb delete mode 100644 spec/requests/addresses_controller/index_addresses_controller_spec.rb create mode 100644 spec/requests/addresses_controller/index_spec.rb delete mode 100644 spec/requests/addresses_controller/update_addresses_controller_spec.rb create mode 100644 spec/requests/addresses_controller/update_spec.rb (limited to 'spec/requests/addresses_controller') diff --git a/spec/requests/addresses_controller/create_addresses_controller_spec.rb b/spec/requests/addresses_controller/create_addresses_controller_spec.rb deleted file mode 100644 index befb80b..0000000 --- a/spec/requests/addresses_controller/create_addresses_controller_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'POST /account/addresses', type: :request do - let(:address) { build(:address) } - let(:user) { create(:user_account, role: 'regular') } - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - let(:token) { jwt(user) } - let(:params) do - { number: address.number, street: address.street, country: address.country, zip_code: address.zip_code, - city: address.city } - end - - it_behaves_like 'a POST request' do - let(:route) { '/api/account/addresses' } - let(:expected_error_messages) do - ["Number can't be blank", "Street can't be blank", "Zip code can't be blank", - 'Country is invalid', "City can't be blank"] - end - let(:desired_error_status) { 422 } - let(:expected_text) { [address.number.to_s, address.zip_code.to_s, address.country, address.street, address.city] } - let(:wrong_params) { { number: '', street: '', zip_code: '', city: '', country: 'Not a country' } } - end - - context 'when a similar address already exists in the database' do - it 'builds a relationship between the user and the address, without creating another address' do - address.save - another_user = create(:user_account) - relate_user_and_address(another_user, address) - post('/api/account/addresses', params:, headers:) - expect(Address.count).to eq(1) - expect(another_user.addresses.first.id).to eq(user.addresses.first.id) - expect(UserAccountAddress.count).to eq(2) - end - end - - context 'when the user alread has the same address' do - it 'returns a 422 http status and a error message' do - address.save - relate_user_and_address(user, address) - post('/api/account/addresses', params:, headers:) - expect(response).to have_http_status(422) - expect(response.body).to include('Ya cuenta con esta dirección') - end - end -end diff --git a/spec/requests/addresses_controller/create_spec.rb b/spec/requests/addresses_controller/create_spec.rb new file mode 100644 index 0000000..befb80b --- /dev/null +++ b/spec/requests/addresses_controller/create_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /account/addresses', type: :request do + let(:address) { build(:address) } + let(:user) { create(:user_account, role: 'regular') } + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + let(:token) { jwt(user) } + let(:params) do + { number: address.number, street: address.street, country: address.country, zip_code: address.zip_code, + city: address.city } + end + + it_behaves_like 'a POST request' do + let(:route) { '/api/account/addresses' } + let(:expected_error_messages) do + ["Number can't be blank", "Street can't be blank", "Zip code can't be blank", + 'Country is invalid', "City can't be blank"] + end + let(:desired_error_status) { 422 } + let(:expected_text) { [address.number.to_s, address.zip_code.to_s, address.country, address.street, address.city] } + let(:wrong_params) { { number: '', street: '', zip_code: '', city: '', country: 'Not a country' } } + end + + context 'when a similar address already exists in the database' do + it 'builds a relationship between the user and the address, without creating another address' do + address.save + another_user = create(:user_account) + relate_user_and_address(another_user, address) + post('/api/account/addresses', params:, headers:) + expect(Address.count).to eq(1) + expect(another_user.addresses.first.id).to eq(user.addresses.first.id) + expect(UserAccountAddress.count).to eq(2) + end + end + + context 'when the user alread has the same address' do + it 'returns a 422 http status and a error message' do + address.save + relate_user_and_address(user, address) + post('/api/account/addresses', params:, headers:) + expect(response).to have_http_status(422) + expect(response.body).to include('Ya cuenta con esta dirección') + end + end +end diff --git a/spec/requests/addresses_controller/destroy_addresses_controller_spec.rb b/spec/requests/addresses_controller/destroy_addresses_controller_spec.rb deleted file mode 100644 index df43329..0000000 --- a/spec/requests/addresses_controller/destroy_addresses_controller_spec.rb +++ /dev/null @@ -1,35 +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: 'regular') } - let(:token) { jwt(user) } - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - let(:address) { create(:address) } - - before(:each) do - relate_user_and_address(user, address) - end - - it_behaves_like 'a DELETE request' do - let(:resource) { "/api/account/addresses/#{address.id}" } - let(:nonexistent_resource) { "/api/account/addresses/#{SecureRandom.hex(8)}" } - end - - context 'when the address belongs to other users' do - it "doesn't get deleted" do - another_user = create(:user_account, role: 'regular') - relate_user_and_address(another_user, address) - delete("/api/account/addresses/#{address.id}", headers:) - expect(Address.find_by(id: address.id)).to_not be_nil - end - end - - context "when the address doesn't belong to other users" do - it 'gets deleted' do - delete("/api/account/addresses/#{address.id}", headers:) - expect(Address.find_by(id: address.id)).to be_nil - end - end -end diff --git a/spec/requests/addresses_controller/destroy_spec.rb b/spec/requests/addresses_controller/destroy_spec.rb new file mode 100644 index 0000000..df43329 --- /dev/null +++ b/spec/requests/addresses_controller/destroy_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'DELETE /api/products/product_id', type: :request do + let(:user) { create(:user_account, role: 'regular') } + let(:token) { jwt(user) } + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + let(:address) { create(:address) } + + before(:each) do + relate_user_and_address(user, address) + end + + it_behaves_like 'a DELETE request' do + let(:resource) { "/api/account/addresses/#{address.id}" } + let(:nonexistent_resource) { "/api/account/addresses/#{SecureRandom.hex(8)}" } + end + + context 'when the address belongs to other users' do + it "doesn't get deleted" do + another_user = create(:user_account, role: 'regular') + relate_user_and_address(another_user, address) + delete("/api/account/addresses/#{address.id}", headers:) + expect(Address.find_by(id: address.id)).to_not be_nil + end + end + + context "when the address doesn't belong to other users" do + it 'gets deleted' do + delete("/api/account/addresses/#{address.id}", headers:) + expect(Address.find_by(id: address.id)).to be_nil + end + end +end diff --git a/spec/requests/addresses_controller/index_addresses_controller_spec.rb b/spec/requests/addresses_controller/index_addresses_controller_spec.rb deleted file mode 100644 index 63fc776..0000000 --- a/spec/requests/addresses_controller/index_addresses_controller_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'GET /api/account/addresses', type: :request do - let(:user) { create(:user_account, role: 'regular') } - let(:token) { jwt(user) } - - before(:each) do - create_list(:address, 5) - relate_user_and_addresses(user, Address.all) - end - - it_behaves_like 'a GET index request' do - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - let(:route) { '/api/account/addresses' } - let(:pagination_size) { 5 } - end -end \ No newline at end of file diff --git a/spec/requests/addresses_controller/index_spec.rb b/spec/requests/addresses_controller/index_spec.rb new file mode 100644 index 0000000..63fc776 --- /dev/null +++ b/spec/requests/addresses_controller/index_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'GET /api/account/addresses', type: :request do + let(:user) { create(:user_account, role: 'regular') } + let(:token) { jwt(user) } + + before(:each) do + create_list(:address, 5) + relate_user_and_addresses(user, Address.all) + end + + it_behaves_like 'a GET index request' do + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + let(:route) { '/api/account/addresses' } + let(:pagination_size) { 5 } + end +end \ No newline at end of file diff --git a/spec/requests/addresses_controller/update_addresses_controller_spec.rb b/spec/requests/addresses_controller/update_addresses_controller_spec.rb deleted file mode 100644 index 0c3662e..0000000 --- a/spec/requests/addresses_controller/update_addresses_controller_spec.rb +++ /dev/null @@ -1,54 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'PUT /api/account/addresses', type: :request do - let(:user) { create(:user_account, role: 'regular') } - let(:address) { create(:address) } - let(:new_address) { build(:address) } - let(:token) { jwt(user) } - let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } - let(:params) do - { number: new_address.number, street: new_address.street, country: new_address.country, zip_code: new_address.zip_code, - city: new_address.city } - end - - before(:each) do - relate_user_and_address(user, address) - end - - it_behaves_like 'a PUT request' do - let(:route) { "/api/account/addresses/#{address.id}" } - let(:wrong_route) { "/api/account/addresses/#{SecureRandom.hex(8)}" } - let(:expected_error_messages) do - ["Number can't be blank", "Street can't be blank", "Zip code can't be blank", - 'Country is invalid', "City can't be blank"] - end - let(:expected_text) do - [new_address.number.to_s, new_address.zip_code.to_s, new_address.country, new_address.street, - new_address.city] - end - let(:wrong_params) { { number: '', street: '', zip_code: '', city: '', country: 'Not a country' } } - end - - context 'when the update params coincide with an already existing address for the user' do - it 'returns a 422 http status and an error message' do - new_address.save - relate_user_and_address(user, new_address) - put("/api/account/addresses/#{address.id}", params:, headers:) - expect(response).to have_http_status(422) - expect(response.body).to include('Ya cuenta con esta dirección') - end - end - - context 'when the update params coincide with an already existing address' do - it 'builds a relationship between the user and the address, without creating another address' do - new_address.save - another_user = create(:user_account, role: 'regular') - relate_user_and_address(another_user, new_address) - put("/api/account/addresses/#{address.id}", params:, headers:) - expect(Address.count).to eq(1) - expect(another_user.addresses.first).to eq(user.addresses.find(new_address.id)) - end - end -end diff --git a/spec/requests/addresses_controller/update_spec.rb b/spec/requests/addresses_controller/update_spec.rb new file mode 100644 index 0000000..0c3662e --- /dev/null +++ b/spec/requests/addresses_controller/update_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'PUT /api/account/addresses', type: :request do + let(:user) { create(:user_account, role: 'regular') } + let(:address) { create(:address) } + let(:new_address) { build(:address) } + let(:token) { jwt(user) } + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + let(:params) do + { number: new_address.number, street: new_address.street, country: new_address.country, zip_code: new_address.zip_code, + city: new_address.city } + end + + before(:each) do + relate_user_and_address(user, address) + end + + it_behaves_like 'a PUT request' do + let(:route) { "/api/account/addresses/#{address.id}" } + let(:wrong_route) { "/api/account/addresses/#{SecureRandom.hex(8)}" } + let(:expected_error_messages) do + ["Number can't be blank", "Street can't be blank", "Zip code can't be blank", + 'Country is invalid', "City can't be blank"] + end + let(:expected_text) do + [new_address.number.to_s, new_address.zip_code.to_s, new_address.country, new_address.street, + new_address.city] + end + let(:wrong_params) { { number: '', street: '', zip_code: '', city: '', country: 'Not a country' } } + end + + context 'when the update params coincide with an already existing address for the user' do + it 'returns a 422 http status and an error message' do + new_address.save + relate_user_and_address(user, new_address) + put("/api/account/addresses/#{address.id}", params:, headers:) + expect(response).to have_http_status(422) + expect(response.body).to include('Ya cuenta con esta dirección') + end + end + + context 'when the update params coincide with an already existing address' do + it 'builds a relationship between the user and the address, without creating another address' do + new_address.save + another_user = create(:user_account, role: 'regular') + relate_user_and_address(another_user, new_address) + put("/api/account/addresses/#{address.id}", params:, headers:) + expect(Address.count).to eq(1) + expect(another_user.addresses.first).to eq(user.addresses.find(new_address.id)) + end + end +end -- cgit v1.2.3