summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/api/addresses_controller.rb8
-rw-r--r--app/controllers/application_controller.rb6
-rw-r--r--app/controllers/services/address_services_base.rb22
-rw-r--r--app/controllers/services/addresses/address_services_base.rb24
-rw-r--r--app/controllers/services/addresses/create_address_service.rb21
-rw-r--r--app/controllers/services/addresses/destroy_address_service.rb20
-rw-r--r--app/controllers/services/addresses/update_address_service.rb30
-rw-r--r--app/controllers/services/create_address_service.rb19
-rw-r--r--app/controllers/services/update_address_service.rb28
-rw-r--r--app/models/address.rb2
-rw-r--r--spec/rails_helper.rb1
-rw-r--r--spec/requests/addresses_controller/create_addresses_controller_spec.rb47
-rw-r--r--spec/requests/addresses_controller/destroy_addresses_controller_spec.rb35
-rw-r--r--spec/requests/addresses_controller/index_addresses_controller_spec.rb19
-rw-r--r--spec/requests/addresses_controller/update_addresses_controller_spec.rb54
-rw-r--r--spec/requests/companies_controller/update_companies_controller_spec.rb5
-rw-r--r--spec/support/address_helper.rb18
-rw-r--r--spec/support/shared_examples/requests/put_request.rb2
18 files changed, 280 insertions, 81 deletions
diff --git a/app/controllers/api/addresses_controller.rb b/app/controllers/api/addresses_controller.rb
index e233fcc..3319607 100644
--- a/app/controllers/api/addresses_controller.rb
+++ b/app/controllers/api/addresses_controller.rb
@@ -10,7 +10,7 @@ module Api
end
def create
- @service = Services::CreateAddressService.new(current_user_account, service_params)
+ @service = Services::Addresses::CreateAddressService.new(current_user_account, service_params)
begin
unless @service.call
@@ -27,7 +27,7 @@ module Api
end
def update
- @service = Services::UpdateAddressService.new(current_user_account, params[:id], service_params)
+ @service = Services::Addresses::UpdateAddressService.new(current_user_account, params[:id], service_params)
begin
case @service.call
@@ -45,11 +45,11 @@ module Api
end
def destroy
- @address = Address.find_by(id: params[:id])
+ @address = current_user_account.addresses.find_by(id: params[:id])
render json: { error_message: 'Not found' }, status: :not_found and return if @address.nil?
- @address.destroy
+ Services::Addresses::DestroyAddressService.new(current_user_account, params, @address).call
render status: :see_other
end
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 4fc614c..8737c85 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -6,9 +6,9 @@ class ApplicationController < ActionController::API
ActiveStorage::Current.url_options = { protocol: request.protocol, host: request.host, port: request.port }
end
- def logic(permitted_params)
- @logic = Object.const_get("#{self.class}::#{action_name.camelize}Logic").new(permitted_params)
- end
+ # def logic(permitted_params)
+ # @logic = Object.const_get("#{self.class}::#{action_name.camelize}Logic").new(permitted_params)
+ # end
private
diff --git a/app/controllers/services/address_services_base.rb b/app/controllers/services/address_services_base.rb
deleted file mode 100644
index 176b901..0000000
--- a/app/controllers/services/address_services_base.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-# frozen_string_literal: true
-
-module Services
- # The base class for all addresses' services.
- class AddressServicesBase
- def initialize(user_account, params)
- @user_account = user_account
- @params = params
- end
-
- private
-
- def already_existing_address
- @already_existing_address ||= Address.find_by(@params)
- end
-
- def add_address_to_user_addresses
- already_existing_address.user_accounts << @user_account
- already_existing_address.save
- end
- end
-end
diff --git a/app/controllers/services/addresses/address_services_base.rb b/app/controllers/services/addresses/address_services_base.rb
new file mode 100644
index 0000000..18e13aa
--- /dev/null
+++ b/app/controllers/services/addresses/address_services_base.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+module Services
+ module Addresses
+ # The base class for all addresses' services.
+ class AddressServicesBase
+ def initialize(user_account, params)
+ @user_account = user_account
+ @params = params
+ end
+
+ private
+
+ def already_existing_address
+ @already_existing_address ||= Address.find_by(@params)
+ end
+
+ def add_address_to_user_addresses
+ already_existing_address.user_accounts << @user_account
+ already_existing_address.save
+ end
+ end
+ end
+end
diff --git a/app/controllers/services/addresses/create_address_service.rb b/app/controllers/services/addresses/create_address_service.rb
new file mode 100644
index 0000000..b634a2b
--- /dev/null
+++ b/app/controllers/services/addresses/create_address_service.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+module Services
+ module Addresses
+ # The service for address creation.
+ class CreateAddressService < AddressServicesBase
+ attr_reader :address
+
+ def call
+ if already_existing_address.nil?
+ @address = Address.new(@params)
+ @address.user_accounts << @user_account
+ @address.save
+ else
+ add_address_to_user_addresses
+ @address = already_existing_address
+ end
+ end
+ end
+ end
+end
diff --git a/app/controllers/services/addresses/destroy_address_service.rb b/app/controllers/services/addresses/destroy_address_service.rb
new file mode 100644
index 0000000..06c30df
--- /dev/null
+++ b/app/controllers/services/addresses/destroy_address_service.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+module Services
+ module Addresses
+ # The service for address deletion.
+ class DestroyAddressService < AddressServicesBase
+ def initialize(user_account, params, address)
+ super(user_account, params)
+ @address = address
+ end
+
+ def call
+ UserAccountAddress.find_by(address_id: @address.id, user_account_id: @user_account.id).destroy
+ return unless @address.user_accounts.count.zero?
+
+ @address.destroy
+ end
+ end
+ end
+end
diff --git a/app/controllers/services/addresses/update_address_service.rb b/app/controllers/services/addresses/update_address_service.rb
new file mode 100644
index 0000000..b9ffbde
--- /dev/null
+++ b/app/controllers/services/addresses/update_address_service.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+module Services
+ module Addresses
+ # The service for address creation.
+ class UpdateAddressService < AddressServicesBase
+ attr_reader :address
+
+ def initialize(user_account, address_id, params)
+ super(user_account, params)
+ @address_id = address_id
+ end
+
+ def call
+ @address = @user_account.addresses.find_by(id: @address_id)
+
+ return :not_found if @address.nil?
+
+ if already_existing_address.nil?
+ return :unprocessable_entity unless @address.update(@params)
+ else
+ add_address_to_user_addresses
+ @address.user_account_addresses.find_by(user_account_id: @user_account.id).destroy
+ @address.destroy if @address.user_accounts.count.zero?
+ @address = already_existing_address
+ end
+ end
+ end
+ end
+end
diff --git a/app/controllers/services/create_address_service.rb b/app/controllers/services/create_address_service.rb
deleted file mode 100644
index 431ccd1..0000000
--- a/app/controllers/services/create_address_service.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-# frozen_string_literal: true
-
-module Services
- # The service for address creation.
- class CreateAddressService < AddressServicesBase
- attr_reader :address
-
- def call
- if already_existing_address.nil?
- @address = Address.new(@params)
- @address.user_accounts << @user_account
- @address.save
- else
- add_address_to_user_addresses
- @address = already_existing_address
- end
- end
- end
-end
diff --git a/app/controllers/services/update_address_service.rb b/app/controllers/services/update_address_service.rb
deleted file mode 100644
index 26a596e..0000000
--- a/app/controllers/services/update_address_service.rb
+++ /dev/null
@@ -1,28 +0,0 @@
-# frozen_string_literal: true
-
-module Services
- # The service for address creation.
- class UpdateAddressService < AddressServicesBase
- attr_reader :address
-
- def initialize(user_account, address_id, params)
- super(user_account, params)
- @address_id = address_id
- end
-
- def call
- @address = Address.find_by(id: @address_id)
-
- return :not_found if @address.nil?
-
- if already_existing_address.nil?
- return :unprocessable_entity unless @address.update(@params)
- else
- add_address_to_user_addresses
- @address.user_account_addresses.find_by(user_account_id: @user_account.id).destroy
- @address.destroy if @address.user_accounts.count.zero?
- @address = already_existing_address
- end
- end
- end
-end
diff --git a/app/models/address.rb b/app/models/address.rb
index 6d542ab..6986dd4 100644
--- a/app/models/address.rb
+++ b/app/models/address.rb
@@ -21,6 +21,6 @@ class Address < ApplicationRecord
def valid_country
return if ISO3166::Country.codes.include?(country)
- errors.add(:country, 'Invalid country')
+ errors.add(:country, 'is invalid')
end
end
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index a30ead5..939fe71 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -63,6 +63,7 @@ RSpec.configure do |config|
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
config.include SessionsHelper, type: :request
+ config.include AddressHelper, type: :request
end
Shoulda::Matchers.configure do |config|
diff --git a/spec/requests/addresses_controller/create_addresses_controller_spec.rb b/spec/requests/addresses_controller/create_addresses_controller_spec.rb
new file mode 100644
index 0000000..befb80b
--- /dev/null
+++ b/spec/requests/addresses_controller/create_addresses_controller_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
new file mode 100644
index 0000000..df43329
--- /dev/null
+++ b/spec/requests/addresses_controller/destroy_addresses_controller_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
new file mode 100644
index 0000000..63fc776
--- /dev/null
+++ b/spec/requests/addresses_controller/index_addresses_controller_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
new file mode 100644
index 0000000..0c3662e
--- /dev/null
+++ b/spec/requests/addresses_controller/update_addresses_controller_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
diff --git a/spec/requests/companies_controller/update_companies_controller_spec.rb b/spec/requests/companies_controller/update_companies_controller_spec.rb
index 76c60e4..fd23365 100644
--- a/spec/requests/companies_controller/update_companies_controller_spec.rb
+++ b/spec/requests/companies_controller/update_companies_controller_spec.rb
@@ -10,18 +10,17 @@ RSpec.describe 'PUT /api/companies/company_id', type: :request do
let(:token) { jwt(user) }
it_behaves_like 'a PUT request' do
- let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } }
+ let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } }
let(:route) { "/api/companies/#{company.short_name}" }
let(:wrong_route) { "/api/companies/#{SecureRandom.hex(8)}" }
let(:expected_error_messages) do
["Name can't be blank", "Short name can't be blank", "Country can't be blank"]
end
- let(:desired_error_status) { 422 }
let(:expected_text) { [new_company.name, new_company.short_name, new_company.country, 'logo', 'http'] }
let(:params) do
{ name: new_company.name, short_name: new_company.short_name, country: new_company.country,
logo: fixture_file_upload('tres castillos-2.png', 'image/png') }
end
- let(:wrong_params) { JSON.generate({ name: '', short_name: '', country: '' }) }
+ let(:wrong_params) { { name: '', short_name: '', country: '' } }
end
end
diff --git a/spec/support/address_helper.rb b/spec/support/address_helper.rb
new file mode 100644
index 0000000..7a678d5
--- /dev/null
+++ b/spec/support/address_helper.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# Module to form relationships between addresses and users.
+module AddressHelper
+ def relate_user_and_addresses(user, addresses)
+ addresses.each do |address|
+ address.user_accounts << user
+ address.save
+ end
+ end
+
+ def relate_user_and_address(user, address)
+ address.user_accounts << user
+ address.save
+ end
+end
diff --git a/spec/support/shared_examples/requests/put_request.rb b/spec/support/shared_examples/requests/put_request.rb
index a85d842..1b6c518 100644
--- a/spec/support/shared_examples/requests/put_request.rb
+++ b/spec/support/shared_examples/requests/put_request.rb
@@ -12,7 +12,7 @@ RSpec.shared_examples 'a PUT request' do |account: false|
context 'with incorrect parameters' do
it 'returns a client side error http status and an error message' do
put(route, params: wrong_params, headers:)
- expect(response).to have_http_status(desired_error_status)
+ expect(response).to have_http_status(422)
expect(response.body).to include_strings(expected_error_messages)
end
end