Add api/users/current
This commit is contained in:
parent
e4111c7d94
commit
3a4b2366c9
6 changed files with 83 additions and 4 deletions
|
@ -7,7 +7,9 @@ class Api::UsersController < Api::ApiController
|
||||||
|
|
||||||
before_action :authenticate_user_with_json!
|
before_action :authenticate_user_with_json!
|
||||||
|
|
||||||
|
# Returns the current user as JSON
|
||||||
|
# If not logged in, causes a 401 error
|
||||||
def current
|
def current
|
||||||
render locals: { user: current_user }
|
@user = current_user
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
7
app/views/api/users/_user.json.jbuilder
Normal file
7
app/views/api/users/_user.json.jbuilder
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
||||||
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
||||||
|
json.extract! user, :id
|
||||||
|
|
||||||
|
json.object 'user'
|
5
app/views/api/users/current.json.jbuilder
Normal file
5
app/views/api/users/current.json.jbuilder
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
||||||
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
||||||
|
json.partial! '/api/users/user', user: @user
|
|
@ -11,9 +11,13 @@ Rails.application.routes.draw do
|
||||||
get '/button_debug/embedded/:id' => 'button_debug#embedded'
|
get '/button_debug/embedded/:id' => 'button_debug#embedded'
|
||||||
get '/button_debug/button/:id' => 'button_debug#button'
|
get '/button_debug/button/:id' => 'button_debug#button'
|
||||||
end
|
end
|
||||||
|
defaults format: :json do
|
||||||
namespace(:api) do
|
namespace(:api) do
|
||||||
resources(:nonprofits)
|
resources(:nonprofits)
|
||||||
|
resources(:users, only: []) do
|
||||||
|
get(:current, on: :collection)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
resources(:emails, only: [:create])
|
resources(:emails, only: [:create])
|
||||||
|
|
40
spec/requests/api/users_controller_spec.rb
Normal file
40
spec/requests/api/users_controller_spec.rb
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
||||||
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe Api::UsersController, type: :request do
|
||||||
|
describe 'GET /api/users/current' do
|
||||||
|
context 'when not logged in' do
|
||||||
|
before { get '/api/users/current' }
|
||||||
|
|
||||||
|
it {
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
}
|
||||||
|
|
||||||
|
it {
|
||||||
|
expect(response).to have_attributes(content_type: starting_with('application/json'))
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when logged in' do
|
||||||
|
before do
|
||||||
|
sign_in create(:user)
|
||||||
|
get '/api/users/current'
|
||||||
|
end
|
||||||
|
|
||||||
|
it {
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
}
|
||||||
|
|
||||||
|
it {
|
||||||
|
expect(response).to have_attributes(content_type: starting_with('application/json'))
|
||||||
|
}
|
||||||
|
|
||||||
|
it {
|
||||||
|
expect(JSON.parse(response.body)['id']).to be_a Numeric
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
21
spec/views/api/users/current.json.jbuilder_spec.rb
Normal file
21
spec/views/api/users/current.json.jbuilder_spec.rb
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
||||||
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe '/api/users/current.json.jbuilder', type: :view do
|
||||||
|
subject(:json) do
|
||||||
|
assign(:user, create(:user))
|
||||||
|
render
|
||||||
|
JSON.parse(rendered)
|
||||||
|
end
|
||||||
|
|
||||||
|
it {
|
||||||
|
is_expected.to include('id' => kind_of(Numeric))
|
||||||
|
}
|
||||||
|
|
||||||
|
it {
|
||||||
|
is_expected.to include('object' => 'user')
|
||||||
|
}
|
||||||
|
end
|
Loading…
Reference in a new issue