Skip to content

Commit

Permalink
Merge pull request #27 from fabianoflorentino/development
Browse files Browse the repository at this point in the history
Development to Main
  • Loading branch information
fabianoflorentino authored Mar 19, 2024
2 parents acabe83 + 5d8acbd commit 71ff74f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
7 changes: 7 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ApplicationController < ActionController::API
rescue_from SharedErrors::CustomerNotFound, with: :customer_not_found
rescue_from SharedErrors::WrongPassword, with: :wrong_password
rescue_from SharedErrors::BalanceEmpty, with: :balance_must_be_zero
rescue_from JWT::DecodeError, with: :unauthorized_request

private

Expand Down Expand Up @@ -36,4 +37,10 @@ def wrong_password(exception)
def balance_must_be_zero(exception)
render json: { error: exception.message }, status: :unprocessable_entity
end

def unauthorized_request(exception)
render json: { error: 'Unauthorized request' }, status: :unauthorized

Rails.logger.error(exception.message)
end
end
8 changes: 7 additions & 1 deletion app/controllers/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Controller to handle the transactions
class TransactionsController < ApplicationController
before_action :authorize_request

def create
TransactionUseCase::Create.new(params[:customer_id], transaction_params).call
render json: { transaction: 'Transaction successfully created!' }, status: :created
Expand All @@ -10,6 +12,10 @@ def create
private

def transaction_params
params.require(:transaction).permit(:amount, :kind, :description)
params.require(:transaction).permit(:amount, :kind, :description, :created_at, :updated_at)
end

def authorize_request
AuthenticationUseCase::Authorize.new(request.headers).call
end
end
25 changes: 15 additions & 10 deletions spec/requests/transactions/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,76 @@
RSpec.describe 'POST - /customers/:id/transactions' do
let(:customer) { create(:customer) }
let(:transaction) { attributes_for(:transaction) }
let(:authorization) { AuthenticationUseCase::Token.new(customer.email, customer.password).call }
let(:headers) { { 'Authorization' => "Bearer #{authorization}" } }
let(:url) { "/customers/#{customer.id}/transactions" }

context 'when the request is valid' do
it 'returns status code 201' do
post(url, params: { transaction: })
post(url, headers:, params: { transaction: })

expect(response).to have_http_status(:created)
end
end

context 'when the request is invalid' do
it 'returns status code 422 if amount is invalid' do
post(url, params: { transaction: { amount: nil } })
post(url, headers:, params: { transaction: { amount: nil } })

expect(response).to have_http_status(:unprocessable_entity)
end

it 'returns status code 422 if amount is less than 0' do
post(url, params: { transaction: { amount: -1 } })
post(url, headers:, params: { transaction: { amount: -1 } })

expect(response).to have_http_status(:unprocessable_entity)
end

it 'returns status code 422 if amount is not integer' do
post(url, params: { transaction: { amount: 1.1 } })
post(url, headers:, params: { transaction: { amount: 1.1 } })

expect(response).to have_http_status(:unprocessable_entity)
end

it 'returns status code 422 if kind is invalid' do
post(url, params: { transaction: { kind: 'x' } })
post(url, headers:, params: { transaction: { kind: 'x' } })

expect(response).to have_http_status(:unprocessable_entity)
end

it 'returns status code 422 if description is invalid' do
description = Faker::Lorem.characters(number: 11)

post(url, params: { transaction: { description: } })
post(url, headers:, params: { transaction: { description: } })

expect(response).to have_http_status(:unprocessable_entity)
end

it 'returns status code 422 if description has special characters' do
description = '!@#$%^&**()[]_+'

post(url, params: { transaction: { description: } })
post(url, headers:, params: { transaction: { description: } })

expect(response).to have_http_status(:unprocessable_entity)
end

it 'returns status code 422 if description is nil' do
description = nil

post(url, params: { transaction: { description: } })
post(url, headers:, params: { transaction: { description: } })

expect(response).to have_http_status(:unprocessable_entity)
end

it 'returns status code 422 if customer_id is invalid' do
customer_id = SecureRandom.uuid

post("/customers/#{customer_id}/transactions", params: { transaction: })
url = "/customers/#{customer_id}/transactions"

expect(response).to have_http_status(:unprocessable_entity)
post(url, headers: { 'Authorization' => 'invalid_token' }, params: { transaction: })

expect(response).to have_http_status(:unauthorized)
expect(response.parsed_body['error']).to eq('Unauthorized request')
end
end
end

0 comments on commit 71ff74f

Please sign in to comment.