-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- api to update name and email - api to change password - api to set profile pic - fixes update_attribute! deprecation warning - introducing active storage
- Loading branch information
1 parent
7f96c39
commit 1abaee0
Showing
15 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Api::V1::ProfilesController < Api::BaseController | ||
before_action :fetch_user | ||
|
||
def show | ||
render json: @user | ||
end | ||
|
||
def update | ||
@user.update!(profile_params) | ||
render json: @user | ||
end | ||
|
||
private | ||
|
||
def fetch_user | ||
@user = current_user | ||
end | ||
|
||
def profile_params | ||
params.require(:profile).permit(:email, :name, :password, :password_confirmation, :avatar) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
db/migrate/20191209195420_create_active_storage_tables.active_storage.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# This migration comes from active_storage (originally 20170806125915) | ||
class CreateActiveStorageTables < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :active_storage_blobs do |t| | ||
t.string :key, null: false | ||
t.string :filename, null: false | ||
t.string :content_type | ||
t.text :metadata | ||
t.bigint :byte_size, null: false | ||
t.string :checksum, null: false | ||
t.datetime :created_at, null: false | ||
|
||
t.index [ :key ], unique: true | ||
end | ||
|
||
create_table :active_storage_attachments do |t| | ||
t.string :name, null: false | ||
t.references :record, null: false, polymorphic: true, index: false | ||
t.references :blob, null: false | ||
|
||
t.datetime :created_at, null: false | ||
|
||
t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class RemoveImageFromUser < ActiveRecord::Migration[6.0] | ||
def change | ||
remove_column :users, :image, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'Profile API', type: :request do | ||
let(:account) { create(:account) } | ||
|
||
describe 'GET /api/v1/profile' do | ||
context 'when unauthenticated user' do | ||
it 'returns unauthorized' do | ||
get '/api/v1/profile' | ||
|
||
expect(response).to have_http_status(:unauthorized) | ||
end | ||
end | ||
|
||
context 'when it authenticated user' do | ||
let(:agent) { create(:user, account: account, role: :agent) } | ||
|
||
it 'returns current user information' do | ||
get '/api/v1/profile', | ||
headers: agent.create_new_auth_token, | ||
as: :json | ||
|
||
expect(response).to have_http_status(:success) | ||
json_response = JSON.parse(response.body) | ||
expect(json_response['id']).to eq(agent.id) | ||
expect(json_response['email']).to eq(agent.email) | ||
end | ||
end | ||
end | ||
|
||
describe 'PUT /api/v1/profile' do | ||
context 'when unauthenticated user' do | ||
it 'returns unauthorized' do | ||
put '/api/v1/profile' | ||
|
||
expect(response).to have_http_status(:unauthorized) | ||
end | ||
end | ||
|
||
context 'when it authenticated user' do | ||
let(:agent) { create(:user, account: account, role: :agent) } | ||
|
||
it 'updates the name & email' do | ||
put '/api/v1/profile', | ||
params: { profile: { name: 'test', 'email': '[email protected]' } }, | ||
headers: agent.create_new_auth_token, | ||
as: :json | ||
|
||
expect(response).to have_http_status(:success) | ||
json_response = JSON.parse(response.body) | ||
agent.reload | ||
expect(json_response['id']).to eq(agent.id) | ||
expect(json_response['email']).to eq(agent.email) | ||
expect(agent.email).to eq('[email protected]') | ||
end | ||
|
||
it 'updates the password' do | ||
put '/api/v1/profile', | ||
params: { profile: { password: 'test123', password_confirmation: 'test123' } }, | ||
headers: agent.create_new_auth_token, | ||
as: :json | ||
|
||
expect(response).to have_http_status(:success) | ||
end | ||
|
||
it 'updates avatar' do | ||
# no avatar before upload | ||
expect(agent.avatar.attached?).to eq(false) | ||
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') | ||
put '/api/v1/profile', | ||
params: { profile: { name: 'test', 'email': '[email protected]', avatar: file } }, | ||
headers: agent.create_new_auth_token | ||
|
||
expect(response).to have_http_status(:success) | ||
agent.reload | ||
expect(agent.avatar.attached?).to eq(true) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters