Improve registrar area sign-in/out specs

#599
This commit is contained in:
Artur Beljajev 2017-10-10 06:02:22 +03:00
parent 26eb47ae09
commit 6e597f39e8
9 changed files with 178 additions and 124 deletions

View file

@ -0,0 +1,12 @@
require 'rails_helper'
RSpec.feature 'Registrar area IP restriction', settings: false do
background do
Setting.registrar_ip_whitelist_enabled = true
end
scenario 'notifies the user if his IP is not allowed' do
visit registrar_root_path
expect(page).to have_text('Access denied from IP 127.0.0.1')
end
end

View file

@ -1,42 +0,0 @@
require 'rails_helper'
RSpec.feature 'Registrar area ip restriction', settings: false do
context 'when enabled' do
background do
Setting.registrar_ip_whitelist_enabled = true
end
context 'when ip is allowed' do
given!(:white_ip) { create(:white_ip,
ipv4: '127.0.0.1',
interfaces: [WhiteIp::REGISTRAR]) }
it 'does not show error message' do
visit registrar_login_path
expect(page).to_not have_text(error_message)
end
end
context 'when ip is not allowed' do
it 'shows error message' do
visit registrar_login_path
expect(page).to have_text(error_message)
end
end
end
context 'when disabled' do
background do
Setting.registrar_ip_whitelist_enabled = false
end
it 'does not show error message' do
visit registrar_login_path
expect(page).to_not have_text(error_message)
end
end
def error_message
t('registrar.authorization.ip_not_allowed', ip: '127.0.0.1')
end
end

View file

@ -0,0 +1,43 @@
require 'rails_helper'
RSpec.feature 'Registrar area password sign-in' do
background do
Setting.registrar_ip_whitelist_enabled = false
end
scenario 'signs in the user with valid credentials' do
create(:api_user_with_unlimited_balance,
active: true,
login: 'test',
password: 'testtest')
visit registrar_login_path
sign_in_with 'test', 'testtest'
expect(page).to have_text(t('registrar.base.current_user.sign_out'))
end
scenario 'notifies the user with invalid credentials' do
create(:api_user, login: 'test', password: 'testtest')
visit registrar_login_path
sign_in_with 'test', 'invalid'
expect(page).to have_text('No such user')
end
scenario 'notifies the user with inactive account' do
create(:api_user, active: false, login: 'test', password: 'testtest')
visit registrar_login_path
sign_in_with 'test', 'testtest'
expect(page).to have_text('User is not active')
end
def sign_in_with(username, password)
fill_in 'depp_user_tag', with: username
fill_in 'depp_user_password', with: password
click_button 'Login'
end
end

View file

@ -1,7 +1,7 @@
require 'rails_helper'
RSpec.describe Authorization::RestrictedIP do
describe '#enabled?', db: true, settings: false do
describe '::enabled?', db: true, settings: false do
context 'when "registrar_ip_whitelist_enabled" is true' do
before do
Setting.registrar_ip_whitelist_enabled = true

View file

@ -0,0 +1,90 @@
require 'rails_helper'
RSpec.describe 'Registrar area IP restriction', settings: false do
describe 'authenticated area' do
before do
sign_in_to_registrar_area
end
context 'when IP restriction is enabled' do
before do
Setting.registrar_ip_whitelist_enabled = true
end
context 'when ip is allowed' do
let!(:white_ip) { create(:white_ip,
ipv4: '127.0.0.1',
registrar: controller.current_user.registrar,
interfaces: [WhiteIp::REGISTRAR]) }
specify do
get registrar_root_url
follow_redirect!
expect(response).to be_success
end
end
context 'when ip is not allowed' do
it 'signs the user out' do
get registrar_root_url
follow_redirect!
expect(controller.current_user).to be_nil
end
it 'redirects to login url' do
get registrar_root_url
expect(response).to redirect_to(registrar_login_url)
end
end
end
context 'when IP restriction is disabled' do
before do
Setting.registrar_ip_whitelist_enabled = false
end
specify do
get registrar_root_url
follow_redirect!
expect(response).to be_success
end
end
end
describe 'unauthenticated area' do
context 'when IP restriction is enabled' do
before do
Setting.registrar_ip_whitelist_enabled = true
end
context 'when ip is allowed' do
let!(:white_ip) { create(:white_ip,
ipv4: '127.0.0.1',
interfaces: [WhiteIp::REGISTRAR]) }
specify do
get registrar_login_path
expect(response).to be_success
end
end
context 'when ip is not allowed' do
specify do
get registrar_login_path
expect(response).to be_forbidden
end
end
end
context 'when IP restriction is disabled' do
before do
Setting.registrar_ip_whitelist_enabled = false
end
specify do
get registrar_login_path
expect(response).to be_success
end
end
end
end

View file

@ -1,67 +0,0 @@
require 'rails_helper'
RSpec.describe 'Registrar session management', db: false do
describe 'GET /registrar/login' do
context 'when ip is allowed' do
let(:restricted_ip) { instance_double(Authorization::RestrictedIP,
can_access_registrar_area_sign_in_page?: true) }
before do
allow(Authorization::RestrictedIP).to receive(:new).and_return(restricted_ip)
end
specify do
get registrar_login_path
expect(response).to be_success
end
end
context 'when ip is not allowed' do
let(:restricted_ip) { instance_double(Authorization::RestrictedIP,
can_access_registrar_area_sign_in_page?: false) }
before do
allow(Authorization::RestrictedIP).to receive(:new).and_return(restricted_ip)
end
specify do
get registrar_login_path
expect(response).to be_forbidden
end
end
end
describe 'POST /registrar/sessions' do
context 'when ip is allowed' do
let(:restricted_ip) { instance_double(Authorization::RestrictedIP,
can_access_registrar_area_sign_in_page?: true) }
before do
allow(Authorization::RestrictedIP).to receive(:new).and_return(restricted_ip)
end
specify do
make_request
expect(response).to be_success
end
end
context 'when ip is not allowed' do
let(:restricted_ip) { instance_double(Authorization::RestrictedIP,
can_access_registrar_area_sign_in_page?: false) }
before do
allow(Authorization::RestrictedIP).to receive(:new).and_return(restricted_ip)
end
specify do
make_request
expect(response).to be_forbidden
end
end
def make_request
post registrar_sessions_path, depp_user: { tag: 'test', password: 'test' }
end
end
end

View file

@ -0,0 +1,20 @@
require 'rails_helper'
RSpec.describe 'Registrar area password sign-in', settings: false do
let!(:user) { create(:api_user, active: true, login: 'test', password: 'testtest') }
before do
Setting.registrar_ip_whitelist_enabled = false
end
it 'signs the user in' do
post registrar_sessions_path, depp_user: { tag: 'test', password: 'testtest' }
follow_redirect!
expect(controller.current_user).to eq(user)
end
it 'redirects to root url' do
post registrar_sessions_path, depp_user: { tag: 'test', password: 'testtest' }
expect(response).to redirect_to(registrar_root_url)
end
end

View file

@ -1,7 +1,6 @@
require 'rails_helper'
RSpec.describe 'Registrar area sign-out', settings: false do
describe 'sign-out' do
before do
Setting.registrar_ip_whitelist_enabled = false
sign_in_to_registrar_area
@ -18,4 +17,3 @@ RSpec.describe 'Registrar area sign-out', settings: false do
expect(response).to redirect_to(registrar_login_url)
end
end
end