Merge branch 'fix-user-identities-api' into 'master'

Fix user identities API

Added ability to update or set the identity of an existing user, like the documentation said it was possible, but actually wasn't.

@DouweM or @stanhu could you please take a look at my code, and see if its sound?

/cc @JobV 

See merge request !1398
This commit is contained in:
Douwe Maan 2015-09-23 09:13:23 +00:00
commit b4bedfdfb9
2 changed files with 25 additions and 0 deletions

View file

@ -121,6 +121,17 @@ module API
User.where(username: attrs[:username]). User.where(username: attrs[:username]).
where.not(id: user.id).count > 0 where.not(id: user.id).count > 0
identity_attrs = attributes_for_keys [:provider, :extern_uid]
if identity_attrs.any?
identity = user.identities.find_by(provider: identity_attrs[:provider])
if identity
identity.update_attributes(identity_attrs)
else
identity = user.identities.build(identity_attrs)
identity.save
end
end
if user.update_attributes(attrs) if user.update_attributes(attrs)
present user, with: Entities::UserFull present user, with: Entities::UserFull
else else

View file

@ -7,6 +7,7 @@ describe API::API, api: true do
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let(:key) { create(:key, user: user) } let(:key) { create(:key, user: user) }
let(:email) { create(:email, user: user) } let(:email) { create(:email, user: user) }
let(:omniauth_user) { create(:omniauth_user) }
describe "GET /users" do describe "GET /users" do
context "when unauthenticated" do context "when unauthenticated" do
@ -230,6 +231,19 @@ describe API::API, api: true do
expect(user.reload.username).to eq(user.username) expect(user.reload.username).to eq(user.username)
end end
it "should update user's existing identity" do
put api("/users/#{omniauth_user.id}", admin), provider: 'ldapmain', extern_uid: '654321'
expect(response.status).to eq(200)
expect(omniauth_user.reload.identities.first.extern_uid).to eq('654321')
end
it 'should update user with new identity' do
put api("/users/#{user.id}", admin), provider: 'github', extern_uid: '67890'
expect(response.status).to eq(200)
expect(user.reload.identities.first.extern_uid).to eq('67890')
expect(user.reload.identities.first.provider).to eq('github')
end
it "should update admin status" do it "should update admin status" do
put api("/users/#{user.id}", admin), { admin: true } put api("/users/#{user.id}", admin), { admin: true }
expect(response.status).to eq(200) expect(response.status).to eq(200)