Improve registrar area linked user switch

- Introduce profile
- Move linked users to profile
- Use PUT

#599
This commit is contained in:
Artur Beljajev 2017-10-09 11:03:43 +03:00
parent bd78c9d5c8
commit e2ebe0aa84
16 changed files with 248 additions and 73 deletions

View file

@ -0,0 +1,18 @@
require 'rails_helper'
RSpec.feature 'Registrar area linked users', settings: false do
given!(:current_user) { create(:api_user_with_unlimited_balance, id: 1, identity_code: 'test') }
given!(:linked_user) { create(:api_user_with_unlimited_balance, id: 2, identity_code: 'test',
username: 'new-user-name') }
background do
Setting.registrar_ip_whitelist_enabled = false
sign_in_to_registrar_area(user: current_user)
end
scenario 'switches current user to a linked one' do
visit registrar_profile_path
click_link_or_button 'switch-current-user-2-btn'
expect(page).to have_text('You are now signed in as a user "new-user-name"')
end
end

View file

@ -0,0 +1,14 @@
require 'rails_helper'
RSpec.feature 'Registrar area profile', settings: false do
background do
Setting.registrar_ip_whitelist_enabled = false
sign_in_to_registrar_area(user: create(:api_user_with_unlimited_balance))
end
scenario 'shows profile' do
visit registrar_root_path
click_on 'registrar-profile-btn'
expect(page).to have_text(t('registrar.profile.show.header'))
end
end

View file

@ -1,16 +0,0 @@
require 'rails_helper'
RSpec.feature 'Registrar area user switch', settings: false do
given!(:current_user) { create(:api_user, id: 1, identity_code: 'test') }
given!(:new_user) { create(:api_user, id: 2, identity_code: 'test', username: 'new-user-name') }
background do
sign_in_to_registrar_area(user: current_user)
end
scenario 'successful user switch' do
visit registrar_root_path
click_link_or_button 'switch-current-user-2-btn'
expect(page).to have_text('You are now signed in as a user "new-user-name"')
end
end

View file

@ -93,4 +93,36 @@ RSpec.describe ApiUser do
expect(api_user.linked_users).to be_empty
end
end
describe '#linked_with?', db: false do
it 'returns true if identity codes match' do
api_user = described_class.new(identity_code: 'test')
another_api_user = described_class.new(identity_code: 'test')
expect(api_user.linked_with?(another_api_user)).to be true
end
it 'returns false if identity codes do not match' do
api_user = described_class.new(identity_code: 'test')
another_api_user = described_class.new(identity_code: 'another-test')
expect(api_user.linked_with?(another_api_user)).to be false
end
end
describe '#login', db: false do
it 'is alias to #username' do
user = described_class.new(username: 'test-username')
expect(user.login).to eq('test-username')
end
end
describe '#registrar_name', db: false do
it 'delegates to registrar' do
registrar = Registrar.new(name: 'test name')
user = described_class.new(registrar: registrar)
expect(user.registrar_name).to eq('test name')
end
end
end

View file

@ -0,0 +1,16 @@
require 'rails_helper'
RSpec.describe UserPresenter do
let(:presenter) { described_class.new(user: user, view: view) }
describe '#login_with_role' do
let(:user) { instance_double(ApiUser,
login: 'login',
roles: %w[role],
registrar_name: 'registrar') }
it 'returns username with role and registrar' do
expect(presenter.login_with_role).to eq('login (role) - registrar')
end
end
end

View file

@ -1,49 +0,0 @@
require 'rails_helper'
RSpec.describe 'Registrar current user', db: false do
describe 'GET /registrar/current_user/switch/2' do
context 'when user is authenticated', db: true do
let!(:current_user) { create(:api_user, id: 1, identity_code: 'test') }
let!(:new_user) { create(:api_user, id: 2, identity_code: 'test') }
before do
sign_in_to_registrar_area(user: current_user)
end
context 'when ip is allowed' do
let(:restricted_ip) { instance_double(Authorization::RestrictedIP,
can_access_registrar_area?: true) }
before do
allow(Authorization::RestrictedIP).to receive(:new).and_return(restricted_ip)
end
specify do
get '/registrar/current_user/switch/2', nil, { HTTP_REFERER: 'http://previous.url' }
expect(response).to redirect_to('http://previous.url')
end
end
context 'when ip is not allowed' do
let(:restricted_ip) { instance_double(Authorization::RestrictedIP,
can_access_registrar_area?: false) }
before do
allow(Authorization::RestrictedIP).to receive(:new).and_return(restricted_ip)
end
specify do
get '/registrar/current_user/switch/2'
expect(response).to redirect_to(registrar_login_url)
end
end
end
context 'when user is not authenticated' do
specify do
get '/registrar/current_user/switch/2'
expect(response).to redirect_to(registrar_login_url)
end
end
end
end

View file

@ -0,0 +1,77 @@
require 'rails_helper'
RSpec.describe 'Registrar area linked users', db: false do
describe 'user switch' do
context 'when user is authenticated', db: true do
let!(:current_user) { create(:api_user, id: 1, identity_code: 'code') }
before do
sign_in_to_registrar_area(user: current_user)
end
context 'when ip is allowed' do
let(:restricted_ip) { instance_double(Authorization::RestrictedIP,
can_access_registrar_area?: true) }
before do
allow(Authorization::RestrictedIP).to receive(:new).and_return(restricted_ip)
end
context 'when new user is linked' do
let!(:new_user) { create(:api_user, id: 2, identity_code: 'code') }
it 'signs in as a new user' do
put '/registrar/current_user/switch/2', nil, { HTTP_REFERER: registrar_contacts_url }
follow_redirect!
expect(controller.current_user.id).to eq(2)
end
it 'redirects back' do
put '/registrar/current_user/switch/2', nil, { HTTP_REFERER: 'http://previous.url' }
expect(response).to redirect_to('http://previous.url')
end
end
context 'when new user is unlinked' do
let!(:new_user) { create(:api_user, id: 2, identity_code: 'another-code') }
it 'throws exception' do
expect do
put '/registrar/current_user/switch/2', nil, { HTTP_REFERER: registrar_contacts_path }
end.to raise_error('Cannot switch to unlinked user')
end
it 'does not sign in as a new user' do
suppress StandardError do
put '/registrar/current_user/switch/2', nil, { HTTP_REFERER: registrar_contacts_path }
end
follow_redirect!
expect(controller.current_user.id).to eq(1)
end
end
end
context 'when ip is not allowed' do
let(:restricted_ip) { instance_double(Authorization::RestrictedIP,
can_access_registrar_area?: false) }
before do
allow(Authorization::RestrictedIP).to receive(:new).and_return(restricted_ip)
end
specify do
put '/registrar/current_user/switch/2'
expect(response).to redirect_to(registrar_login_url)
end
end
end
context 'when user is not authenticated' do
specify do
put '/registrar/current_user/switch/2'
expect(response).to redirect_to(registrar_login_url)
end
end
end
end