Skip to content

Commit 2c32693

Browse files
authored
Merge pull request #1241 from Crown-Commercial-Service/feature/nrmi-39-self-service-user-name-update
NRMI-39
2 parents b94c484 + b090b59 commit 2c32693

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

app/controllers/v1/users_controller.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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
720
end

config/routes.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
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

spec/requests/v1/users_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,42 @@
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
3472
end

0 commit comments

Comments
 (0)