Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/controllers/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@ def index

render jsonapi: users
end

def update_name
user = User.find_by!(auth_id: current_auth_id)
name_param = params.dig('_jsonapi', 'name')
result = UpdateUser.new(user, name_param).call

if result.success?
render jsonapi: user, status: :ok
else
render jsonapi_errors: { error: I18n.t('errors.messages.error_updating_user_in_auth0') },
status: :unprocessable_entity
end
end
end
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
get '/check', to: 'check#index', defaults: { format: :json }

namespace :v1, defaults: { format: :json } do
resources :users, only: %i[index]
resources :users, only: %i[index] do
collection do
patch :update_name
end
end

resources :suppliers, only: %i[index]

Expand Down
38 changes: 38 additions & 0 deletions spec/requests/v1/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,42 @@
.with_value(true)
end
end

describe 'PATCH /v1/users/update_name' do
it 'updates the name of the current user' do
user = FactoryBot.create(:user)
stub_auth0_token_request
stub_auth0_update_user_request(user)

patch '/v1/users/update_name',
headers: { 'X-Auth-Id' => JWT.encode(user.auth_id, 'test') },
params: {
_jsonapi: {
name: 'New User Name'
}
}

expect(response).to be_successful
expect(json['data']).to have_id(user.id)
expect(json['data'])
.to have_attribute(:name)
.with_value('New User Name')
end

it 'returns an error if the Auth0 update fails' do
user = FactoryBot.create(:user)
stub_auth0_token_request
stub_auth0_update_user_request_failure(user)

patch '/v1/users/update_name',
headers: { 'X-Auth-Id' => JWT.encode(user.auth_id, 'test') },
params: {
_jsonapi: {
name: 'New User Name'
}
}

expect(response.status).to eq 422
end
end
end