From 32cdbc06f6e8e3b9783aac75f7bfc73668fc9059 Mon Sep 17 00:00:00 2001 From: Peter Kos Date: Sun, 20 Sep 2020 23:08:32 -0400 Subject: [PATCH 1/2] Create /user.json get route + test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Restricted to .json format as no page needs to exist for this Bypass devise controller because devise doesn’t allow `show` method for some reason --- app/controllers/users_controller.rb | 15 +++++++++++++++ config/routes.rb | 3 +++ test/controllers/users_controller_test.rb | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 app/controllers/users_controller.rb create mode 100644 test/controllers/users_controller_test.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 000000000..3f0cb87c8 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,15 @@ +class UsersController < ApplicationController + + before_action :logged_in + respond_to :json + + def logged_in + authenticate_user! + end + + def show + respond_to do |format| + format.json { render json: current_user } + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 9bf7f5c52..ae9b07283 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,6 +24,9 @@ mount Blazer::Engine, at: "blazer" end + # devise doesnt parse GET /user + resource :user, only: :show, constraints: lambda { |req| req.format == :json } + resource :questionnaires, path: "apply" do get :schools, on: :collection end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 000000000..2ba78c0ca --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +class UsersControllerTest < ActionController::TestCase + include ActiveJob::TestHelper + setup do + @user = create(:user) + end + + should "allow access to user#get" do + sign_in @user + get :show, params: { format: :json } + assert_response :success + end + + should "don't allow user#show if not signed in" do + get :show, params: { format: :json } + assert_response(:unauthorized) + end +end From 083ae0883d3e444a27e4bf95d6123147cebac0ad Mon Sep 17 00:00:00 2001 From: Peter Kos Date: Sun, 20 Sep 2020 23:12:22 -0400 Subject: [PATCH 2/2] bark bark --- app/controllers/users_controller.rb | 1 - config/routes.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3f0cb87c8..097a31280 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,4 @@ class UsersController < ApplicationController - before_action :logged_in respond_to :json diff --git a/config/routes.rb b/config/routes.rb index ae9b07283..d9c156a6c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,7 +25,7 @@ end # devise doesnt parse GET /user - resource :user, only: :show, constraints: lambda { |req| req.format == :json } + resource :user, only: :show, constraints: ->(req) { req.format == :json } resource :questionnaires, path: "apply" do get :schools, on: :collection