From 3a4b2366c9444514caf42bd68f1f3fe9dca36650 Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Wed, 4 Aug 2021 15:10:38 -0500 Subject: [PATCH] Add api/users/current --- app/controllers/api/users_controller.rb | 4 +- app/views/api/users/_user.json.jbuilder | 7 ++++ app/views/api/users/current.json.jbuilder | 5 +++ config/routes.rb | 10 +++-- spec/requests/api/users_controller_spec.rb | 40 +++++++++++++++++++ .../api/users/current.json.jbuilder_spec.rb | 21 ++++++++++ 6 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 app/views/api/users/_user.json.jbuilder create mode 100644 app/views/api/users/current.json.jbuilder create mode 100644 spec/requests/api/users_controller_spec.rb create mode 100644 spec/views/api/users/current.json.jbuilder_spec.rb diff --git a/app/controllers/api/users_controller.rb b/app/controllers/api/users_controller.rb index ee7c66c2..d6745bed 100644 --- a/app/controllers/api/users_controller.rb +++ b/app/controllers/api/users_controller.rb @@ -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 diff --git a/app/views/api/users/_user.json.jbuilder b/app/views/api/users/_user.json.jbuilder new file mode 100644 index 00000000..c1296e1a --- /dev/null +++ b/app/views/api/users/_user.json.jbuilder @@ -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' diff --git a/app/views/api/users/current.json.jbuilder b/app/views/api/users/current.json.jbuilder new file mode 100644 index 00000000..fa311219 --- /dev/null +++ b/app/views/api/users/current.json.jbuilder @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 8a46be5d..bd2e2a15 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -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]) diff --git a/spec/requests/api/users_controller_spec.rb b/spec/requests/api/users_controller_spec.rb new file mode 100644 index 00000000..031a544c --- /dev/null +++ b/spec/requests/api/users_controller_spec.rb @@ -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 diff --git a/spec/views/api/users/current.json.jbuilder_spec.rb b/spec/views/api/users/current.json.jbuilder_spec.rb new file mode 100644 index 00000000..7d28d57a --- /dev/null +++ b/spec/views/api/users/current.json.jbuilder_spec.rb @@ -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