Add api/users/current

This commit is contained in:
Eric Schultz 2021-08-04 15:10:38 -05:00 committed by Eric Schultz
parent e4111c7d94
commit 3a4b2366c9
6 changed files with 83 additions and 4 deletions

View file

@ -7,7 +7,9 @@ class Api::UsersController < Api::ApiController
before_action :authenticate_user_with_json!
# Returns the current user as JSON
# If not logged in, causes a 401 error
def current
render locals: { user: current_user }
@user = current_user
end
end

View 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'

View 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

View file

@ -11,9 +11,13 @@ Rails.application.routes.draw do
get '/button_debug/embedded/:id' => 'button_debug#embedded'
get '/button_debug/button/:id' => 'button_debug#button'
end
namespace(:api) do
resources(:nonprofits)
defaults format: :json do
namespace(:api) do
resources(:nonprofits)
resources(:users, only: []) do
get(:current, on: :collection)
end
end
end
resources(:emails, only: [:create])

View 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

View 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