Add user helper for creating lock/unlock/unblock buttons

pull/240/head
Michael Frister 11 years ago committed by Michael Frister
parent dfced0e238
commit cb042cf704
  1. 24
      app/helpers/users_helper.rb
  2. 11
      config/locales/en.yml
  3. 63
      spec/helpers/users_helper_spec.rb

@ -24,6 +24,7 @@ module UsersHelper
I18n.t(('status_' + status_name).to_sym)
end
# Format user status, including brute force prevention status
def full_user_status(user)
user_status = ''
unless [User::STATUSES[:active], User::STATUSES[:builtin]].include?(user.status)
@ -49,6 +50,29 @@ module UsersHelper
end
end
# Create buttons to lock/unlock a user and reset failed logins
def change_user_status_buttons(user)
status = user.status_name
blocked = user.failed_too_many_recent_login_attempts?
result = ''.html_safe
if status == :active
if blocked
result << submit_tag(I18n.t(:reset_failed_logins, :scope => :user),
:name => 'unlock')
end
result << submit_tag(I18n.t(:lock, :scope => :user), :name => 'lock')
else
if status == :locked
title = blocked ? :unlock_and_reset_failed_logins : :unlock
result << submit_tag(I18n.t(title, :scope => :user), :name => 'unlock')
elsif status == :registered
title = blocked ? :activate_and_reset_failed_logins : :activate
result << submit_tag(I18n.t(title, :scope => :user), :name => 'activate')
end
end
result
end
# Options for the new membership projects combo-box
def options_for_membership_project_select(user, projects)
options = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---")

@ -1502,6 +1502,17 @@ en:
total_progress: "Total progress"
user:
lock: "Lock"
reset_failed_logins: "Reset failed logins"
unlock: "Unlock"
unlock_and_reset_failed_logins: "Unlock and reset failed logins"
activate: "Activate"
activate_and_reset_failed_logins: "Activate and reset failed logins"
status_user_and_brute_force: "%{user} and %{brute_force}"
brute_force_status:
blocked: "blocked (too many login failures)"
blocked_num_failed_logins: "blocked (%{count} failed login attempts)"
deleted: "Deleted user"
version_status_closed: "closed"

@ -14,16 +14,16 @@ require 'spec_helper'
describe UsersHelper do
include UsersHelper
describe 'full_user_status' do
def build_user(status, blocked)
user = FactoryGirl.build(:user)
user.stub!(:status).and_return(User::STATUSES[status])
user.stub!(:failed_too_many_recent_login_attempts?).and_return(blocked)
user.stub!(:failed_login_count).and_return(3)
user
end
def build_user(status, blocked)
user = FactoryGirl.build(:user)
user.stub!(:status).and_return(User::STATUSES[status])
user.stub!(:failed_too_many_recent_login_attempts?).and_return(blocked)
user.stub!(:failed_login_count).and_return(3)
user
end
TEST_CASES = {
describe 'full_user_status' do
test_cases = {
[:active, false] => 'active',
[:active, true] => 'blocked (3 failed login attempts)',
[:locked, false] => 'locked',
@ -32,7 +32,7 @@ describe UsersHelper do
[:registered, true] => 'registered and blocked (3 failed login attempts)'
}
TEST_CASES.each do |(status, blocked), expectation|
test_cases.each do |(status, blocked), expectation|
describe "with status #{status} and blocked #{blocked}" do
before do
user = build_user(status, blocked)
@ -45,4 +45,47 @@ describe UsersHelper do
end
end
end
describe 'change_user_status_buttons' do
test_cases = {
[:active, false] => :lock,
[:locked, false] => :unlock,
[:locked, true] => :unlock_and_reset_failed_logins,
[:registered, false] => :activate,
[:registered, true] => :activate_and_reset_failed_logins
}
test_cases.each do |(status, blocked), expectation_symbol|
describe "with status #{status} and blocked #{blocked}" do
expectation = I18n.t(expectation_symbol, :scope => :user)
before do
user = build_user(status, blocked)
@buttons = change_user_status_buttons(user)
end
it "should contain '#{expectation}'" do
@buttons.should include(expectation)
end
it "should contain a single button" do
@buttons.scan('<input').count.should == 1
end
end
end
describe "with status active and blocked True" do
before do
user = build_user(:active, true)
@buttons = change_user_status_buttons(user)
end
it "should return inputs (buttons)" do
@buttons.scan('<input').count.should == 2
end
it "should contain 'Lock' and 'Reset Failed logins'" do
@buttons.should include(I18n.t(:lock, :scope => :user))
@buttons.should include(I18n.t(:reset_failed_logins, :scope => :user))
end
end
end
end

Loading…
Cancel
Save