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
|
2021-09-02 12:59:45 +00:00
|
|
|
let(:agent) { create(:user, account: account, custom_attributes: { test: 'test' }, role: :agent) }
|
2019-12-10 04:59:35 +00:00
|
|
|
|
|
|
|
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)
|
2021-09-02 12:59:45 +00:00
|
|
|
expect(json_response['custom_attributes']['test']).to eq('test')
|
2022-02-02 14:07:33 +00:00
|
|
|
expect(json_response['message_signature']).to be_nil
|
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
|
2021-06-02 12:22:24 +00:00
|
|
|
let(:agent) { create(:user, password: 'Test123!', account: account, role: :agent) }
|
2019-12-10 04:59:35 +00:00
|
|
|
|
2021-12-16 14:02:49 +00:00
|
|
|
it 'updates the name' do
|
2019-12-10 04:59:35 +00:00
|
|
|
put '/api/v1/profile',
|
2021-12-16 14:02:49 +00:00
|
|
|
params: { profile: { name: 'test' } },
|
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)
|
2021-12-16 14:02:49 +00:00
|
|
|
expect(json_response['name']).to eq(agent.name)
|
|
|
|
expect(agent.name).to eq('test')
|
2019-12-10 04:59:35 +00:00
|
|
|
end
|
|
|
|
|
2022-02-02 14:07:33 +00:00
|
|
|
it 'updates the message_signature' do
|
|
|
|
put '/api/v1/profile',
|
2022-02-15 06:40:53 +00:00
|
|
|
params: { profile: { name: 'test', message_signature: 'Thanks\nMy Signature' } },
|
2022-02-02 14:07:33 +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['name']).to eq(agent.name)
|
|
|
|
expect(agent.name).to eq('test')
|
|
|
|
expect(json_response['message_signature']).to eq('Thanks\nMy Signature')
|
|
|
|
end
|
|
|
|
|
2021-06-02 12:22:24 +00:00
|
|
|
it 'updates the password when current password is provided' do
|
2019-12-10 04:59:35 +00:00
|
|
|
put '/api/v1/profile',
|
2021-06-07 14:34:31 +00:00
|
|
|
params: { profile: { current_password: 'Test123!', password: 'Test1234!', password_confirmation: 'Test1234!' } },
|
2019-12-10 04:59:35 +00:00
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(agent.reload.valid_password?('Test1234!')).to be true
|
2021-06-02 12:22:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'throws error when current password provided is invalid' do
|
|
|
|
put '/api/v1/profile',
|
|
|
|
params: { profile: { current_password: 'Test', password: 'test123', password_confirmation: 'test123' } },
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
2019-12-10 04:59:35 +00:00
|
|
|
end
|
|
|
|
|
2022-02-02 22:21:17 +00:00
|
|
|
it 'validate name' do
|
|
|
|
user_name = 'test' * 999
|
|
|
|
put '/api/v1/profile',
|
|
|
|
params: { profile: { name: user_name } },
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response['message']).to eq('Name is too long (maximum is 255 characters)')
|
|
|
|
end
|
|
|
|
|
2019-12-10 04:59:35 +00:00
|
|
|
it 'updates avatar' do
|
|
|
|
# no avatar before upload
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(agent.avatar.attached?).to be(false)
|
2019-12-10 04:59:35 +00:00
|
|
|
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
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(agent.avatar.attached?).to be(true)
|
2019-12-10 04:59:35 +00:00
|
|
|
end
|
2020-07-04 06:12:47 +00:00
|
|
|
|
2021-01-10 13:55:33 +00:00
|
|
|
it 'updates the ui settings' do
|
|
|
|
put '/api/v1/profile',
|
|
|
|
params: { profile: { ui_settings: { is_contact_sidebar_open: false } } },
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(json_response['ui_settings']['is_contact_sidebar_open']).to be(false)
|
2021-01-10 13:55:33 +00:00
|
|
|
end
|
2019-12-10 04:59:35 +00:00
|
|
|
end
|
2021-12-16 14:02:49 +00:00
|
|
|
|
|
|
|
context 'when an authenticated user updates email' do
|
|
|
|
let(:agent) { create(:user, password: 'Test123!', account: account, role: :agent) }
|
|
|
|
|
|
|
|
it 'populates the unconfirmed email' do
|
|
|
|
new_email = Faker::Internet.email
|
|
|
|
put '/api/v1/profile',
|
|
|
|
params: { profile: { email: new_email } },
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
agent.reload
|
|
|
|
|
|
|
|
expect(agent.unconfirmed_email).to eq(new_email)
|
|
|
|
end
|
|
|
|
end
|
2019-12-10 04:59:35 +00:00
|
|
|
end
|
2021-10-07 07:51:46 +00:00
|
|
|
|
2021-11-26 19:26:07 +00:00
|
|
|
describe 'DELETE /api/v1/profile/avatar' do
|
|
|
|
let(:agent) { create(:user, password: 'Test123!', account: account, role: :agent) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
delete '/api/v1/profile/avatar'
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
before do
|
|
|
|
agent.avatar.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes the agent avatar' do
|
|
|
|
delete '/api/v1/profile/avatar',
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-07 07:51:46 +00:00
|
|
|
describe 'POST /api/v1/profile/availability' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post '/api/v1/profile/availability'
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, password: 'Test123!', account: account, role: :agent) }
|
|
|
|
|
|
|
|
it 'updates the availability status' do
|
|
|
|
post '/api/v1/profile/availability',
|
|
|
|
params: { profile: { availability: 'busy', account_id: account.id } },
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(::OnlineStatusTracker.get_status(account.id, agent.id)).to eq('busy')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-10-06 00:31:12 +00:00
|
|
|
|
2022-12-16 19:59:27 +00:00
|
|
|
describe 'POST /api/v1/profile/auto_offline' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post '/api/v1/profile/auto_offline'
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, password: 'Test123!', account: account, role: :agent) }
|
|
|
|
|
|
|
|
it 'updates the auto offline status' do
|
|
|
|
post '/api/v1/profile/auto_offline',
|
|
|
|
params: { profile: { auto_offline: false, account_id: account.id } },
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response['accounts'].first['auto_offline']).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-06 00:31:12 +00:00
|
|
|
describe 'PUT /api/v1/profile/set_active_account' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
put '/api/v1/profile/set_active_account'
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, password: 'Test123!', account: account, role: :agent) }
|
|
|
|
|
|
|
|
it 'updates the last active account id' do
|
|
|
|
put '/api/v1/profile/set_active_account',
|
|
|
|
params: { profile: { account_id: account.id } },
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-10 04:59:35 +00:00
|
|
|
end
|