Skip to content

Commit

Permalink
Use uid instead of id on profile update && delete api
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehor authored and Camille committed Sep 26, 2019
1 parent f46a436 commit 53edd09
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions app/api/v2/admin/profiles.rb
Expand Up @@ -37,7 +37,7 @@ class Profiles < Grape::API
{ code: 422, message: 'Validation errors' }
]
params do
requires :id, type: Integer
requires :uid, type: String
optional :first_name, type: String
optional :last_name, type: String
optional :dob, type: Date
Expand All @@ -49,10 +49,10 @@ class Profiles < Grape::API
end

put do
target_profile = Profile.find_by(id: params[:id])
target_profile = User.find_by(uid: params[:uid])&.profile
return error!({ errors: ['admin.profiles.doesnt_exist'] }, 404) if target_profile.nil?

unless target_profile.update(declared(params, include_missing: false))
unless target_profile.update(declared(params.except(:uid), include_missing: false))
code_error!(target_profile.errors.details, 422)
end

Expand All @@ -67,11 +67,11 @@ class Profiles < Grape::API
{ code: 422, message: 'Validation errors' }
]
params do
requires :id, type: Integer
requires :uid, type: String
end

delete do
target_profile = Profile.find_by(id: params[:id])
target_profile = User.find_by(uid: params[:uid])&.profile
return error!({ errors: ['admin.profiles.doesnt_exist'] }, 404) if target_profile.nil?

present target_profile.destroy, with: API::V2::Entities::Profile
Expand Down
12 changes: 6 additions & 6 deletions spec/api/v2/admin/profiles_spec.rb
Expand Up @@ -52,7 +52,7 @@

describe 'DELETE /api/v2/admin/profiles' do
context 'successful response' do
let(:do_request) { delete '/api/v2/admin/profiles', params: { id: profile1.id }, headers: auth_header }
let(:do_request) { delete '/api/v2/admin/profiles', params: { uid: profile1.user.uid }, headers: auth_header }

it 'delete profile' do
expect { do_request }.to change { Profile.count }.by(-1)
Expand All @@ -65,7 +65,7 @@

context 'unsuccessful response' do
it 'return error while profiles doesnt exist' do
delete '/api/v2/admin/profiles', params: { id: 0 }, headers: auth_header
delete '/api/v2/admin/profiles', params: { uid: '0' }, headers: auth_header

result = JSON.parse(response.body)
expect(response.code).to eq '404'
Expand All @@ -89,7 +89,7 @@

context 'successful response' do
it 'returns completed profile' do
put '/api/v2/admin/profiles', params: request_params.merge(id: profile2.id), headers: auth_header
put '/api/v2/admin/profiles', params: request_params.merge(uid: profile2.user.uid), headers: auth_header

expect(response.status).to eq(200)
profile = Profile.find_by(request_params)
Expand All @@ -104,7 +104,7 @@
let!(:profile) { create(:profile, user: test_user, last_name: nil, first_name: nil) }

it 'returns partial profile' do
put '/api/v2/admin/profiles', params: request_params.except(:first_name).merge(id: profile.id), headers: auth_header
put '/api/v2/admin/profiles', params: request_params.except(:first_name).merge(uid: test_user.uid), headers: auth_header

expect(response.status).to eq(200)
profile = Profile.find_by(request_params.except(:first_name))
Expand All @@ -115,7 +115,7 @@
end

it 'returns completed profile' do
put '/api/v2/admin/profiles', params: request_params.merge(id: profile.id), headers: auth_header
put '/api/v2/admin/profiles', params: request_params.merge(uid: test_user.uid), headers: auth_header

expect(response.status).to eq(200)
profile = Profile.find_by(request_params)
Expand All @@ -128,7 +128,7 @@

context 'unccessful response' do
it 'renders an error when profile doesnt exist' do
put '/api/v2/admin/profiles', params: { id: 0 }, headers: auth_header
put '/api/v2/admin/profiles', params: { uid: '0' }, headers: auth_header
expect_status.to eq(404)
expect_body.to eq(errors: ['admin.profiles.doesnt_exist'])
end
Expand Down

0 comments on commit 53edd09

Please sign in to comment.