2019-12-10 04:59:35 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Profile API', type: :request do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
|
|
|
|
describe 'GET /api/v1/profile' do
|
2019-12-24 07:57:25 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
2019-12-10 04:59:35 +00:00
|
|
|
it 'returns unauthorized' do
|
|
|
|
get '/api/v1/profile'
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
context 'when it is an authenticated user' do
|
2019-12-10 04:59:35 +00:00
|
|
|
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)
|
2020-05-31 17:40:20 +00:00
|
|
|
expect(json_response['access_token']).to eq(agent.access_token.token)
|
2019-12-10 04:59:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT /api/v1/profile' do
|
2019-12-24 07:57:25 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
2019-12-10 04:59:35 +00:00
|
|
|
it 'returns unauthorized' do
|
|
|
|
put '/api/v1/profile'
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-24 07:57:25 +00:00
|
|
|
context 'when it is an authenticated user' do
|
2019-12-10 04:59:35 +00:00
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
it 'updates the name & email' do
|
2019-12-24 07:57:25 +00:00
|
|
|
new_email = Faker::Internet.email
|
2019-12-10 04:59:35 +00:00
|
|
|
put '/api/v1/profile',
|
2019-12-24 07:57:25 +00:00
|
|
|
params: { profile: { name: 'test', 'email': new_email } },
|
2019-12-10 04:59:35 +00:00
|
|
|
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)
|
2019-12-24 07:57:25 +00:00
|
|
|
expect(agent.email).to eq(new_email)
|
2019-12-10 04:59:35 +00:00
|
|
|
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',
|
2019-12-24 07:57:25 +00:00
|
|
|
params: { profile: { avatar: file } },
|
2019-12-10 04:59:35 +00:00
|
|
|
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
|