diff options
author | HombreLaser <sebastian-440@live.com> | 2023-03-13 19:48:40 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-03-13 19:48:40 -0600 |
commit | 5c899df07753a31543f8dad725a26ccddc44dfe2 (patch) | |
tree | cc13040e9f859688ec2ec454486528394bbc1cde | |
parent | 3dae1ce143d006cc75940b746a8eb74982e6e861 (diff) |
Añade método update a UserAccountsController
-rw-r--r-- | app/controllers/api/user_accounts_controller.rb | 14 | ||||
-rw-r--r-- | app/controllers/authenticated_controller.rb | 2 | ||||
-rw-r--r-- | app/models/user_account.rb | 9 | ||||
-rw-r--r-- | config/routes.rb | 1 | ||||
-rw-r--r-- | spec/models/company_spec.rb | 8 | ||||
-rw-r--r-- | spec/requests/user_accounts_controller/create_user_accounts_spec.rb | 12 | ||||
-rw-r--r-- | spec/requests/user_accounts_controller/update_user_accounts_spec.rb | 24 | ||||
-rw-r--r-- | spec/support/shared_examples/requests/put_request.rb | 12 |
8 files changed, 68 insertions, 14 deletions
diff --git a/app/controllers/api/user_accounts_controller.rb b/app/controllers/api/user_accounts_controller.rb index 600e168..1bbdcf5 100644 --- a/app/controllers/api/user_accounts_controller.rb +++ b/app/controllers/api/user_accounts_controller.rb @@ -6,7 +6,7 @@ module Api skip_before_action :validate_jwt, only: [:create] def show - render json: Serializers::UserAccountSerializer.new(current_user_account).serializable_hash + render json: serialized_object.serializable_hash end def create @@ -17,8 +17,20 @@ module Api render json: @user_account.errors.full_messages, status: :unprocessable_entity end + def update + if current_user_account.update(permitted_params) + render json: serialized_object.serializable_hash, status: :ok + else + render json: current_user_account.errors.full_messages, status: :unprocessable_entity + end + end + private + def serialized_object + Serializers::UserAccountSerializer.new(current_user_account) + end + def service_params { email: @user_account.email, role: @user_account.role } diff --git a/app/controllers/authenticated_controller.rb b/app/controllers/authenticated_controller.rb index 56159ab..cb4f34d 100644 --- a/app/controllers/authenticated_controller.rb +++ b/app/controllers/authenticated_controller.rb @@ -15,7 +15,7 @@ class AuthenticatedController < ApplicationController def current_user_role return if decoded_token.nil? - + decoded_token[0]['aud'] end diff --git a/app/models/user_account.rb b/app/models/user_account.rb index e50961b..cd99441 100644 --- a/app/models/user_account.rb +++ b/app/models/user_account.rb @@ -9,7 +9,8 @@ class UserAccount < ApplicationRecord has_secure_password validations: false validates :email, presence: true - validates :password, presence: true + validates :email, uniqueness: true + validates :password, presence: true, if: :no_password? validates_format_of :email, with: /\A(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})\z/i validates :first_name, presence: true @@ -19,4 +20,10 @@ class UserAccount < ApplicationRecord def full_name "#{first_name} #{last_name}" end + + private + + def no_password? + password_digest.nil? + end end diff --git a/config/routes.rb b/config/routes.rb index 0237992..1abb376 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,7 @@ Rails.application.routes.draw do post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' get '/account', to: 'user_accounts#show' + put '/account', to: 'user_accounts#update' resources :companies, only: %i[index show create update] resources :user_accounts, only: %i[create] resources :refresh_tokens, only: %i[create] diff --git a/spec/models/company_spec.rb b/spec/models/company_spec.rb index 7fe3a87..0d254a8 100644 --- a/spec/models/company_spec.rb +++ b/spec/models/company_spec.rb @@ -3,9 +3,17 @@ require 'rails_helper' RSpec.describe Company, type: :model do + let(:company) { create(:company) } + it { should validate_presence_of(:name) } it { should validate_presence_of(:short_name) } it { should validate_presence_of(:country) } it { should validate_uniqueness_of(:short_name).ignoring_case_sensitivity } it { should have_one_attached(:logo) } + + describe '#to_param' do + it "returns the model's url param" do + expect(company.to_param).to eq(company.short_name) + end + end end diff --git a/spec/requests/user_accounts_controller/create_user_accounts_spec.rb b/spec/requests/user_accounts_controller/create_user_accounts_spec.rb index 3fd5990..a45e02b 100644 --- a/spec/requests/user_accounts_controller/create_user_accounts_spec.rb +++ b/spec/requests/user_accounts_controller/create_user_accounts_spec.rb @@ -4,18 +4,18 @@ require 'rails_helper' RSpec.describe 'POST /api/user_accounts', type: :request do it_behaves_like 'a POST request' do - let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } + let(:headers) { {} } let(:route) { '/api/user_accounts' } let(:expected_error_messages) do - ["Password can't be blank", 'Email is invalid', "First name can't be blank", "Last name can't be blank" ] + ["Password can't be blank", 'Email is invalid', "First name can't be blank", "Last name can't be blank"] end let(:desired_error_status) { 422 } let(:expected_text) { %w[token refresh] } - let(:user) { create(:user_account) } + let(:user) { build(:user_account) } let(:params) do - JSON.generate({ email: user.email, password: user.password, - first_name: user.first_name, last_name: user.last_name }) + { email: user.email, password: user.password, + first_name: user.first_name, last_name: user.last_name } end - let(:wrong_params) { JSON.generate({ email: 'notanemail', password: '' }) } + let(:wrong_params) { { email: 'notanemail', password: '', first_name: '', last_name: '' } } end end diff --git a/spec/requests/user_accounts_controller/update_user_accounts_spec.rb b/spec/requests/user_accounts_controller/update_user_accounts_spec.rb new file mode 100644 index 0000000..ba9cc69 --- /dev/null +++ b/spec/requests/user_accounts_controller/update_user_accounts_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'PUT /api/account', type: :request do + let(:user) { create(:user_account) } + let(:data) { build(:user_account) } + let(:token) { jwt(user) } + + it_behaves_like 'a PUT request' do + let(:account) { true } + let(:route) { '/api/account' } + let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } } + let(:params) do + { email: data.email, password: data.password, first_name: data.first_name, last_name: data.last_name } + end + let(:expected_text) { [] } + let(:desired_error_status) { 422 } + let(:wrong_params) { { email: 'notanemail', password: '', first_name: '', last_name: '' } } + let(:expected_error_messages) do + ["Password can't be blank", 'Email is invalid', "First name can't be blank", "Last name can't be blank"] + end + end +end diff --git a/spec/support/shared_examples/requests/put_request.rb b/spec/support/shared_examples/requests/put_request.rb index 9e46235..d329b78 100644 --- a/spec/support/shared_examples/requests/put_request.rb +++ b/spec/support/shared_examples/requests/put_request.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.shared_examples 'a PUT request' do +RSpec.shared_examples 'a PUT request' do |account: false| context 'with correct parameters' do it 'returns a successful http status and a response with the putted data' do put(route, params:, headers:) @@ -17,10 +17,12 @@ RSpec.shared_examples 'a PUT request' do end end - context 'to an nonexistent resource' do - it 'returns a 404 status' do - put(wrong_route, params:, headers:) - expect(response).to have_http_status(404) + if account + context 'to an nonexistent resource' do + it 'returns a 404 status' do + put(wrong_route, params:, headers:) + expect(response).to have_http_status(404) + end end end end |