From 735439a5d461891ddb7daae3f20fcf8e43ef53e2 Mon Sep 17 00:00:00 2001 From: Maicol Bentancor Date: Fri, 16 Jun 2017 18:21:37 -0300 Subject: [PATCH] Added 500 handler & make facebook webmock more flexible --- apiary.apib | 16 ++++++++-------- app/controllers/api/v1/api_controller.rb | 6 ++++++ spec/requests/api/v1/sessions/facebook_spec.rb | 15 ++++++++++----- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/apiary.apib b/apiary.apib index 06d20ec5..8a536005 100644 --- a/apiary.apib +++ b/apiary.apib @@ -3,7 +3,7 @@ HOST: http://rails5-api-base.herokuapp.com # API BASE -API BASE is an internal TopTier project created to facilitate and standardize developers work. +API BASE is an internal Rootstrap project created to facilitate and standardize developers work. ## Users Collection [/api/v1/users] @@ -36,7 +36,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de + Body - + { "id": 102, "email": "test@test.com", @@ -75,7 +75,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de + Body - + { "user": { @@ -118,7 +118,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de + Body - + { "user": { @@ -131,7 +131,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de ## Login [/api/v1/users/sign_in] - + ### Login [POST] + Request (application/json) @@ -160,7 +160,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de + Body - + { "data": { @@ -178,7 +178,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de ## Login with Facebook [/api/v1/users/facebook] - + ### Login with Facebook [POST] @@ -239,7 +239,7 @@ https://github.com/lynndylanhurley/devise_token_auth/wiki/Reset-Password-Flow + Response 200 (application/json) + Body - { + { "success": true, "data": { diff --git a/app/controllers/api/v1/api_controller.rb b/app/controllers/api/v1/api_controller.rb index 7f88956c..34deca1e 100644 --- a/app/controllers/api/v1/api_controller.rb +++ b/app/controllers/api/v1/api_controller.rb @@ -11,6 +11,7 @@ class ApiController < ApplicationController layout false respond_to :json + rescue_from Exception, with: :render_error rescue_from ActiveRecord::RecordNotFound, with: :render_not_found rescue_from ActiveRecord::RecordInvalid, with: :render_record_invalid rescue_from ActionController::RoutingError, with: :render_not_found @@ -21,6 +22,11 @@ def status render json: { online: true } end + def render_error(_exception) + logger.error(exception) # Report to your error managment tool here + render json: { error: 'An error ocurred' }, status: 500 unless performed? + end + def render_not_found(exception) logger.info(exception) # for logging render json: { error: "Couldn't find the record" }, status: :not_found diff --git a/spec/requests/api/v1/sessions/facebook_spec.rb b/spec/requests/api/v1/sessions/facebook_spec.rb index f0d8328e..0ded5b25 100644 --- a/spec/requests/api/v1/sessions/facebook_spec.rb +++ b/spec/requests/api/v1/sessions/facebook_spec.rb @@ -1,8 +1,9 @@ require 'rails_helper' describe 'POST api/v1/users/facebook', type: :request do - let(:user) { create(:user) } - let(:facebook_path) { facebook_api_v1_users_path } + let(:user) { create(:user) } + let(:facebook_path) { facebook_api_v1_users_path } + let(:facebook_api_path) { 'https://graph.facebook.com/me' } let(:facebook_response) do { first_name: 'Test', @@ -30,7 +31,8 @@ } end before do - stub_request(:get, 'https://graph.facebook.com/me?access_token=123456&fields=email,first_name,last_name') + stub_request(:get, facebook_api_path) + .with(query: hash_including(access_token: '123456', fields: 'email,first_name,last_name')) .to_return(status: 200, body: facebook_response.to_json) end @@ -77,7 +79,9 @@ end before do facebook_response[:email] = '' - stub_request(:get, 'https://graph.facebook.com/me?access_token=without_email&fields=email,first_name,last_name') + fields = 'email,first_name,last_name' + stub_request(:get, facebook_api_path) + .with(query: hash_including(access_token: 'without_email', fields: fields)) .to_return(status: 200, body: facebook_response.to_json) end @@ -121,7 +125,8 @@ code: 190 } } - stub_request(:get, 'https://graph.facebook.com/me?access_token=invalid&fields=email,first_name,last_name') + stub_request(:get, facebook_api_path) + .with(query: hash_including(access_token: 'invalid', fields: 'email,first_name,last_name')) .to_return(status: 400, body: facebook_response.to_json) end