Commit 5187e888aefd23255e4eec996b9ea44c24f64ee8
Committed by
Rodrigo Souto
1 parent
131516d4
Exists in
master
and in
29 other branches
Avoid infinite loop on username suggestion
Showing
2 changed files
with
22 additions
and
2 deletions
Show diff stats
app/helpers/account_helper.rb
... | ... | @@ -15,12 +15,17 @@ module AccountHelper |
15 | 15 | |
16 | 16 | def suggestion_based_on_username(requested_username='') |
17 | 17 | return "" if requested_username.empty? |
18 | + | |
19 | + requested_username = requested_username.downcase.tr("^#{Profile::IDENTIFIER_FORMAT}", '') | |
18 | 20 | usernames = [] |
21 | + tries = 0 | |
19 | 22 | 3.times do |
20 | 23 | begin |
21 | 24 | valid_name = requested_username + rand(1000).to_s |
22 | - end while (usernames.include?(valid_name) || !Person.is_available?(valid_name, environment)) | |
23 | - usernames << valid_name | |
25 | + tries += 1 | |
26 | + invalid = usernames.include?(valid_name) || !Person.is_available?(valid_name, environment) | |
27 | + end while tries <= 10 && invalid | |
28 | + usernames << valid_name unless invalid | |
24 | 29 | end |
25 | 30 | usernames |
26 | 31 | end | ... | ... |
test/unit/account_helper_test.rb
... | ... | @@ -18,4 +18,19 @@ class AccountHelperTest < ActiveSupport::TestCase |
18 | 18 | end |
19 | 19 | end |
20 | 20 | |
21 | + should 'remove chars which are not allowed' do | |
22 | + stubs(:environment).returns(Environment.default) | |
23 | + suggestions = suggestion_based_on_username('z/%&#e') | |
24 | + suggestions.each do |suggestion| | |
25 | + assert_no_match /.*%&#.*/, suggestion | |
26 | + end | |
27 | + end | |
28 | + | |
29 | + should 'return empty suggestions if do not find any identifier available' do | |
30 | + stubs(:environment).returns(Environment.default) | |
31 | + Person.stubs(:is_available?).returns(false) | |
32 | + suggestions = suggestion_based_on_username('z/%&#e') | |
33 | + assert_equal [], suggestions | |
34 | + end | |
35 | + | |
21 | 36 | end | ... | ... |