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 Apr 18, 2017
1 parent 896db43 commit 324a1db
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 27 deletions.
58 changes: 49 additions & 9 deletions apiary.apib
Original file line number Diff line number Diff line change
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 All @@ -50,7 +50,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de
}


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

### Get current user profile [GET]

Expand All @@ -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,46 @@ API BASE is an internal TopTier project created to facilitate and standardize de


+ Body


{
"user":
{
"email": "test@test.com",
"username": "test",
"first_name": "Juanito",
"last_name": "La Cruz"
}
}

## 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":
{
Expand All @@ -130,8 +169,9 @@ 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 +200,7 @@ API BASE is an internal TopTier project created to facilitate and standardize de


+ Body

{
"data":
{
Expand All @@ -177,8 +217,8 @@ API BASE is an internal TopTier project created to facilitate and standardize de
}


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

### Login with Facebook [POST]


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

{
{
"success": true,
"data":
{
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 }

shared_context 'fail to login with facebook' do
it 'does not returns a successful response' 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/users/me', 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
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
16 changes: 9 additions & 7 deletions spec/requests/api/v1/users/update_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
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 +30,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 324a1db

Please sign in to comment.