Skip to content

Commit

Permalink
Merge pull request #44 from toptier/refactor/single-resource
Browse files Browse the repository at this point in the history
Use user single resources instead of /users/me
  • Loading branch information
Maicol Bentancor authored Jul 14, 2017
2 parents c836f1e + 3d66914 commit 5b007d6
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 58 deletions.
76 changes: 52 additions & 24 deletions apiary.apib
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ API BASE is an internal Rootstrap project created to facilitate and standardize
}


## Current user's profile [/api/v1/user/{id}]
## Current user's profile [/api/v1/user/]

### Get current user profile [GET]

Expand Down Expand Up @@ -83,8 +83,8 @@ API BASE is an internal Rootstrap project created to facilitate and standardize
{
"email": "test@test.com",
"username": "test",
"first_name": "Juanito",
"last_name": "La Cruz"
"first_name": "John",
"last_name": "Doe"
}
}

Expand All @@ -105,8 +105,8 @@ API BASE is an internal Rootstrap project created to facilitate and standardize
{
"username": "test",
"password": "password",
"first_name": "Juanito",
"last_name": "La Cruz"
"first_name": "John",
"last_name": "Doe"
}
}

Expand All @@ -126,11 +126,51 @@ API BASE is an internal Rootstrap project created to facilitate and standardize
{
"email": "test@test.com",
"username": "test",
"first_name": "Juanito",
"last_name": "La Cruz"
"first_name": "John",
"last_name": "Doe"
}
}

## Get other user's profile [/api/v1/users/{id}]

### Get user [GET]

+ Request (application/json)
+ Parameters
+ id (integer, required)

+ Headers

access-token: sO2bm_Bpdyoo8r78jZ-fqg
client: QADgNCWRJj0LyRruqzYbBg
uid: test@test.com


+ Response 401


+ Response 200 (application/json)
+ Headers

access-token: sO2bm_Bpdyoo8r78jZ-fqg
client: QADgNCWRJj0LyRruqzYbBg
expiry: 1489009792
uid: test@test.com


+ Body

{
"user":
{
"email": "test@test.com",
"username": "test",
"first_name": "John",
"last_name": "Doe"
}
}



## Login [/api/v1/users/sign_in]

Expand Down Expand Up @@ -164,7 +204,7 @@ API BASE is an internal Rootstrap project created to facilitate and standardize
+ Body

{
"data":
"user":
{
"id": 102,
"email": "test@test.com",
Expand All @@ -179,7 +219,7 @@ API BASE is an internal Rootstrap project created to facilitate and standardize
}


## Login with Facebook [/api/v1/users/facebook]
## Login with Facebook [/api/v1/user/facebook]

### Login with Facebook [POST]

Expand All @@ -195,7 +235,7 @@ API BASE is an internal Rootstrap project created to facilitate and standardize
+ Body

{
"data":
"user":
{
"id": 366,
"email": "test@facebook.com",
Expand Down Expand Up @@ -243,19 +283,7 @@ https://github.com/lynndylanhurley/devise_token_auth/wiki/Reset-Password-Flow

{
"success": true,
"data":
{
"id": 781,
"provider": "email",
"email": "wilma_farrell@reinger.info",
"uid": "wilma_farrell@reinger.info",
"first_name": "",
"last_name": "",
"username": "39kellen_bahringer",
"created_at": "2017-03-01T18:58:42.223Z",
"updated_at": "2017-03-01T18:58:42.439Z"
},
"message": "An email has been sent to 'wilma_farrell@reinger.info' containing instructions for resetting your password."
"message": "An email has been sent to 'example@mail.com' containing instructions for resetting your password."
}

### Reset passowrd [PUT]
Expand Down Expand Up @@ -285,7 +313,7 @@ https://github.com/lynndylanhurley/devise_token_auth/wiki/Reset-Password-Flow
+ Body

{
"data":
"user":
{
"id": 366,
"email": "test@facebook.com",
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def sign_up_params
end

def render_create_success
render json: resource_data
render json: { user: resource_data }
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/api/v1/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def custom_sign_in
response.headers.merge!(new_auth_header)
render_create_success
end

def render_create_success
render json: { user: resource_data }
end
end
end
end
16 changes: 15 additions & 1 deletion app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
module Api
module V1
class UsersController < Api::V1::ApiController
helper_method :user

def show
end

def profile
render :show
end

def update
current_user.update!(user_params)
user.update!(user_params)
render :show
end

Expand All @@ -16,6 +22,14 @@ def update
def user_params
params.require(:user).permit(:username, :first_name, :last_name, :email)
end

def user
@user ||= user_id.present? ? User.find(user_id) : current_user
end

def user_id
params[:id]
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/api/v1/users/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
json.user do
json.partial! 'info', user: current_user
json.partial! 'info', user: user
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
namespace :v1, defaults: { format: :json } do
devise_scope :user do
get :status, to: 'api#status'
resources :users, only: [:show, :update] do
resources :users, only: [:show]
resource :user, only: [:update] do
get :profile
controller :sessions do
post :facebook, on: :collection
end
Expand Down
5 changes: 5 additions & 0 deletions spec/requests/api/v1/passwords/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
expect(response).to have_http_status(:success)
end

it 'returns the user email' do
post user_password_path, params: params, as: :json
expect(json[:message]).to match(/#{user.email}/)
end

it 'sends an email' do
expect { post user_password_path, params: params, as: :json }
.to change { ActionMailer::Base.deliveries.count }.by(1)
Expand Down
17 changes: 7 additions & 10 deletions spec/requests/api/v1/sessions/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@
end

it 'returns the user' do
response_expected = {
id: user.id,
email: user.email,
username: user.username,
uid: user.email,
provider: 'email',
first_name: user.first_name,
last_name: user.last_name
}.with_indifferent_access
expect(json[:data]).to eq(response_expected)
expect(json[:user][:id]).to eq(user.id)
expect(json[:user][:email]).to eq(user.email)
expect(json[:user][:username]).to eq(user.username)
expect(json[:user][:uid]).to eq(user.uid)
expect(json[:user][:provider]).to eq('email')
expect(json[:user][:first_name]).to eq(user.first_name)
expect(json[:user][:last_name]).to eq(user.last_name)
end

it 'returns a valid client and access token' do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v1/sessions/facebook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe 'POST api/v1/users/facebook', type: :request do
let(:user) { create(:user) }
let(:facebook_path) { facebook_api_v1_users_path }
let(:facebook_path) { facebook_api_v1_user_path }
let(:facebook_api_path) { 'https://graph.facebook.com/me' }
let(:facebook_response) do
{
Expand Down
17 changes: 14 additions & 3 deletions spec/requests/api/v1/users/create_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rails_helper'

describe 'POST api/v1/users/', type: :request do
let!(:user) { create(:user) }
let(:user) { User.last }
let(:failed_response) { 422 }

describe 'POST create' do
Expand Down Expand Up @@ -32,10 +32,21 @@
end

it 'creates the user' do
expect do
post user_registration_path, params: params, as: :json
end.to change(User, :count).by(1)
end

it 'returns the user' do
post user_registration_path, params: params, as: :json

new_user = User.find_by_email(email)
expect(new_user).to_not be_nil
expect(json[:user][:id]).to eq(user.id)
expect(json[:user][:email]).to eq(user.email)
expect(json[:user][:username]).to eq(user.username)
expect(json[:user][:uid]).to eq(user.uid)
expect(json[:user][:provider]).to eq('email')
expect(json[:user][:first_name]).to eq(user.first_name)
expect(json[:user][:last_name]).to eq(user.last_name)
end

context 'when the email is not correct' do
Expand Down
17 changes: 17 additions & 0 deletions spec/requests/api/v1/users/profile_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'rails_helper'

describe 'GET api/v1/user/profile', type: :request do
let(:user) { create(:user) }

it 'returns success' do
get profile_api_v1_user_path, headers: auth_headers, as: :json
expect(response).to have_http_status(:success)
end

it 'returns user\'s data' do
get profile_api_v1_user_path, headers: auth_headers, as: :json

expect(json[:user][:id]).to eq user.id
expect(json[:user][:first_name]).to eq user.first_name
end
end
11 changes: 6 additions & 5 deletions spec/requests/api/v1/users/show_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
require 'rails_helper'

describe 'GET api/v1/users/me', type: :request do
describe 'GET api/v1/users/:id', type: :request do
let(:user) { create(:user) }
let(:another_user) { create :user }

it 'returns success' do
get api_v1_user_path(id: 'me'), headers: auth_headers, as: :json
get api_v1_user_path(id: another_user.id), headers: auth_headers, as: :json
expect(response).to have_http_status(:success)
end

it 'returns user\'s data' do
get api_v1_user_path(id: 'me'), headers: auth_headers, as: :json
get api_v1_user_path(id: another_user.id), headers: auth_headers, as: :json

expect(json[:user][:id]).to eq user.id
expect(json[:user][:first_name]).to eq user.first_name
expect(json[:user][:id]).to eq another_user.id
expect(json[:user][:first_name]).to eq another_user.first_name
end
end
17 changes: 9 additions & 8 deletions spec/requests/api/v1/users/update_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
require 'rails_helper'

describe 'PUT api/v1/users/me', type: :request do
let(:user) { create(:user) }
describe 'PUT api/v1/user/', type: :request do
let(:user) { create(:user) }
let(:api_v1_user_path) { '/api/v1/user' }

context 'with valid params' do
let(:params) { { user: { username: 'new username' } } }

it 'returns success' do
put api_v1_user_path(id: 'me'), params: params, headers: auth_headers, as: :json
put api_v1_user_path, params: params, headers: auth_headers, as: :json
expect(response).to have_http_status(:success)
end

it 'updates the user' do
put api_v1_user_path(id: 'me'), params: params, headers: auth_headers, as: :json
put api_v1_user_path, params: params, headers: auth_headers, as: :json
expect(user.reload.username).to eq(params[:user][:username])
end

it 'returns the user' do
put api_v1_user_path(id: 'me'), params: params, headers: auth_headers, as: :json
put api_v1_user_path, params: params, headers: auth_headers, as: :json

expect(json[:user][:id]).to eq user.id
expect(json[:user][:first_name]).to eq user.first_name
Expand All @@ -28,17 +29,17 @@
let(:params) { { user: { email: 'notanemail' } } }

it 'does not return success' do
put api_v1_user_path(id: 'me'), params: params, headers: auth_headers, as: :json
put api_v1_user_path, params: params, headers: auth_headers, as: :json
expect(response).to_not have_http_status(:success)
end

it 'does not update the user' do
put api_v1_user_path(id: 'me'), params: params, headers: auth_headers, as: :json
put api_v1_user_path, params: params, headers: auth_headers, as: :json
expect(user.reload.email).to_not eq(params[:email])
end

it 'returns the error' do
put api_v1_user_path(id: 'me'), params: params, headers: auth_headers, as: :json
put api_v1_user_path, params: params, headers: auth_headers, as: :json
expect(json[:errors][:email]).to include('is not an email')
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/routing/sessions_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
end

it 'routes to #facebook' do
expect(post: '/api/v1/users/facebook').to route_to('api/v1/sessions#facebook', format: :json)
expect(post: '/api/v1/user/facebook').to route_to('api/v1/sessions#facebook', format: :json)
end

it 'routes to #destroy' do
Expand Down
Loading

0 comments on commit 5b007d6

Please sign in to comment.