File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,4 +4,17 @@ def index
44
55 render jsonapi : users
66 end
7+
8+ def update_name
9+ user = User . find_by! ( auth_id : current_auth_id )
10+ name_param = params . dig ( '_jsonapi' , 'name' )
11+ result = UpdateUser . new ( user , name_param ) . call
12+
13+ if result . success?
14+ render jsonapi : user , status : :ok
15+ else
16+ render jsonapi_errors : { error : I18n . t ( 'errors.messages.error_updating_user_in_auth0' ) } ,
17+ status : :unprocessable_entity
18+ end
19+ end
720end
Original file line number Diff line number Diff line change 2121 get '/check' , to : 'check#index' , defaults : { format : :json }
2222
2323 namespace :v1 , defaults : { format : :json } do
24- resources :users , only : %i[ index ]
24+ resources :users , only : %i[ index ] do
25+ collection do
26+ patch :update_name
27+ end
28+ end
2529
2630 resources :suppliers , only : %i[ index ]
2731
Original file line number Diff line number Diff line change 3131 . with_value ( true )
3232 end
3333 end
34+
35+ describe 'PATCH /v1/users/update_name' do
36+ it 'updates the name of the current user' do
37+ user = FactoryBot . create ( :user )
38+ stub_auth0_token_request
39+ stub_auth0_update_user_request ( user )
40+
41+ patch '/v1/users/update_name' ,
42+ headers : { 'X-Auth-Id' => JWT . encode ( user . auth_id , 'test' ) } ,
43+ params : {
44+ _jsonapi : {
45+ name : 'New User Name'
46+ }
47+ }
48+
49+ expect ( response ) . to be_successful
50+ expect ( json [ 'data' ] ) . to have_id ( user . id )
51+ expect ( json [ 'data' ] )
52+ . to have_attribute ( :name )
53+ . with_value ( 'New User Name' )
54+ end
55+
56+ it 'returns an error if the Auth0 update fails' do
57+ user = FactoryBot . create ( :user )
58+ stub_auth0_token_request
59+ stub_auth0_update_user_request_failure ( user )
60+
61+ patch '/v1/users/update_name' ,
62+ headers : { 'X-Auth-Id' => JWT . encode ( user . auth_id , 'test' ) } ,
63+ params : {
64+ _jsonapi : {
65+ name : 'New User Name'
66+ }
67+ }
68+
69+ expect ( response . status ) . to eq 422
70+ end
71+ end
3472end
You can’t perform that action at this time.
0 commit comments