Skip to content

Commit

Permalink
Added 500 handler & make facebook webmock more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicolBen committed Jun 16, 2017
1 parent 2eebd38 commit b4b187d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
16 changes: 8 additions & 8 deletions apiary.apib
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -36,7 +36,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de


+ Body

{
"id": 102,
"email": "test@test.com",
Expand Down Expand Up @@ -75,7 +75,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de


+ Body

{
"user":
{
Expand Down Expand Up @@ -118,7 +118,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de


+ Body

{
"user":
{
Expand All @@ -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)
Expand Down Expand Up @@ -160,7 +160,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de


+ Body

{
"data":
{
Expand All @@ -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]


Expand Down Expand Up @@ -239,7 +239,7 @@ https://github.com/lynndylanhurley/devise_token_auth/wiki/Reset-Password-Flow
+ Response 200 (application/json)
+ Body

{
{
"success": true,
"data":
{
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/api/v1/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,6 +22,11 @@ def status
render json: { online: true }
end

def render_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
Expand Down
14 changes: 9 additions & 5 deletions spec/requests/api/v1/sessions/facebook_spec.rb
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -77,7 +79,8 @@
end
before do
facebook_response[:email] = ''
stub_request(:get, 'https://graph.facebook.com/me?access_token=without_email&fields=email,first_name,last_name')
stub_request(:get, facebook_api_path)
.with(query: hash_including(access_token: 'without_email', fields: 'email,first_name,last_name'))
.to_return(status: 200, body: facebook_response.to_json)
end

Expand Down Expand Up @@ -121,7 +124,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

Expand Down

0 comments on commit b4b187d

Please sign in to comment.