Skip to content

Commit

Permalink
Use user single resources instead of /users/me
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicolBen committed Jun 20, 2017
1 parent 4a06417 commit d1289a6
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 26 deletions.
56 changes: 48 additions & 8 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 @@ -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 Down
9 changes: 8 additions & 1 deletion app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ module Api
module V1
class UsersController < Api::V1::ApiController
def show
@user = User.find(params[:id])
end

def profile
@user = current_user
render :show
end

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

Expand Down
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
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
9 changes: 5 additions & 4 deletions spec/requests/api/v1/users/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

describe 'GET api/v1/users/me', 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
15 changes: 8 additions & 7 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) }
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
8 changes: 6 additions & 2 deletions spec/routing/user_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
describe Api::V1::UsersController, type: :routing do
describe 'routing' do
it 'routes to #update' do
expect(put: '/api/v1/users/me').to route_to('api/v1/users#update', format: :json, id: 'me')
expect(put: '/api/v1/user').to route_to('api/v1/users#update', format: :json)
end

it 'routes to #show' do
expect(get: '/api/v1/users/me').to route_to('api/v1/users#show', format: :json, id: 'me')
expect(get: '/api/v1/users/1').to route_to('api/v1/users#show', format: :json, id: '1')
end

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

0 comments on commit d1289a6

Please sign in to comment.