From 05702e4a65b0a4d79d836a4919eff54ab174e9d1 Mon Sep 17 00:00:00 2001 From: alcampelo Date: Wed, 19 Feb 2014 11:01:10 -0500 Subject: [PATCH] Suggest available username --- app/controllers/public/account_controller.rb | 8 ++++++-- app/helpers/account_helper.rb | 13 +++++++++++++ app/views/account/_identifier_status.rhtml | 15 ++++++++++++++- app/views/account/_signup_form.rhtml | 15 ++++++--------- features/signup.feature | 27 +++++++++++++++++++++++---- public/javascripts/signup_form.js | 23 +++++++++++++++++++++++ test/functional/account_controller_test.rb | 21 +++++++++++++++------ 7 files changed, 100 insertions(+), 22 deletions(-) create mode 100644 public/javascripts/signup_form.js diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index 4a64230..47d48a2 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -250,15 +250,19 @@ class AccountController < ApplicationController end end - def check_url + def check_valid_name @identifier = params[:identifier] valid = Person.is_available?(@identifier, environment) if valid @status = _('This login name is available') @status_class = 'validated' - else + elsif !@identifier.empty? + @suggested_usernames = suggestion_based_on_username(@identifier) @status = _('This login name is unavailable') @status_class = 'invalid' + else + @status_class = 'invalid' + @status = _('This field can\'t be blank') end render :partial => 'identifier_status' end diff --git a/app/helpers/account_helper.rb b/app/helpers/account_helper.rb index 61d3e8a..9153903 100644 --- a/app/helpers/account_helper.rb +++ b/app/helpers/account_helper.rb @@ -12,4 +12,17 @@ module AccountHelper _('Checking if e-mail address is already taken...') end end + + def suggestion_based_on_username(requested_username) + return "" if requested_username.empty? + usernames = [] + 3.times do + begin + valid_name = requested_username + rand(1000).to_s + end while (usernames.include?(valid_name) && Person.is_available?(valid_name, environment)) + usernames += [valid_name] + end + usernames + end + end diff --git a/app/views/account/_identifier_status.rhtml b/app/views/account/_identifier_status.rhtml index 863651d..3ac2594 100644 --- a/app/views/account/_identifier_status.rhtml +++ b/app/views/account/_identifier_status.rhtml @@ -1,7 +1,20 @@
+ <% if @suggested_usernames %> + <% @status += '
' + _('Available: ') if not @suggested_usernames.empty? %> + <% @suggested_usernames.each do |username| %> + <% @status += "" + username + ' ' %> + <%end%> + <% end %> + +

<%= @status %>

-
+ \ No newline at end of file diff --git a/app/views/account/_signup_form.rhtml b/app/views/account/_signup_form.rhtml index 9a91453..aa9261b 100644 --- a/app/views/account/_signup_form.rhtml +++ b/app/views/account/_signup_form.rhtml @@ -40,15 +40,7 @@
- <%= observe_field 'user_login', - :url => { :action => 'check_url' }, - :with => 'identifier', - :update => 'url-check', - :loading => "jQuery('#user_login').removeClass('#{validation_classes}').addClass('checking'); - jQuery('#url-check').html('

#{checking_message(:url)}

');", - :complete => "jQuery('#user_login').removeClass('checking')" - %> - + <%= javascript_include_tag "signup_form" %>
<%= required f.password_field(:password, :id => 'user_pw') %> <%= content_tag(:small,_('Choose a password that you can remember easily. It must have at least 4 characters.'), :id => 'password-balloon') %> @@ -182,4 +174,9 @@ jQuery(function($) { else $(this).addClass('validated'); }); }); + +function fill_username(element){ + jQuery('#url-check').html('

<%= _('This login name is available') %>

') + jQuery('#user_login').val(element).addClass('validated').removeClass('invalid') +} diff --git a/features/signup.feature b/features/signup.feature index a58f417..912ec16 100644 --- a/features/signup.feature +++ b/features/signup.feature @@ -29,13 +29,32 @@ Feature: signup And I press "Log in" Then I should be logged in as "josesilva" + @selenium + Scenario: show error message if username is already used + Given I am on the homepage + When I follow "Login" + And I follow "New user" + And I fill in the following within ".no-boxes": + | e-Mail | josesilva@example.com | + | Username | josesilva | + | Password | secret | + | Password confirmation | secret | + | Full name | José da Silva | + And wait for the captcha signup time + And I press "Create my account" + Then I should receive an e-mail on josesilva@example.com + And I go to signup page + And I fill in "Username" with "josesilva" + And I fill in "e-Mail" with "josesilva1" + Then I should see "This login name is unavailable" + Scenario: be redirected if user goes to signup page and is logged Given the following users | login | name | - | joaosilva | Joao Silva | - Given I am logged in as "joaosilva" - And I go to signup page - Then I should be on joaosilva's control panel + | joaosilva | joao silva | + Given i am logged in as "joaosilva" + And i go to signup page + Then i should be on joaosilva's control panel @selenium Scenario: user cannot register without a name diff --git a/public/javascripts/signup_form.js b/public/javascripts/signup_form.js new file mode 100644 index 0000000..a08abfa --- /dev/null +++ b/public/javascripts/signup_form.js @@ -0,0 +1,23 @@ +function verifyLoginLoad() { + jQuery('#user_login').removeClass('available unavailable valid validated invalid checking').addClass('checking'); + jQuery('#url-check').html('

Checking availability of login name...

'); +} + +function verifyLoginAjax(value) { + verifyLoginLoad(); + + jQuery.get( + "/account/check_valid_name", + {'identifier': encodeURIComponent(value)}, + function(request){ + jQuery('#user_login').removeClass('checking'); + jQuery("#url-check").html(request); + } + ); +} + +jQuery(document).ready(function(){ + jQuery("#user_login").blur(function(){ + verifyLoginAjax(this.value); + }); +}); \ No newline at end of file diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 65a7864..e927dea 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -8,7 +8,6 @@ class AccountControllerTest < ActionController::TestCase # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead # Then, you can remove it from this and the units test. include AuthenticatedTestHelper - all_fixtures def teardown @@ -17,8 +16,8 @@ class AccountControllerTest < ActionController::TestCase def setup @controller = AccountController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new disable_signup_bot_check end @@ -36,6 +35,16 @@ class AccountControllerTest < ActionController::TestCase assert_response :redirect end + should 'return empty string if suggest_based_on_username argument is empty' do + result = @controller.suggestion_based_on_username("") + assert_equal(result.empty?,true) + end + + should 'return a list with three possible usernames suggestion' do + result = @controller.suggestion_based_on_username("teste") + assert_equal(result.uniq.size,3) + end + should 'display notice message if the login fail' do @controller.stubs(:logged_in?).returns(false) post :login, :user => {:login => 'quire', :password => 'quire'} @@ -646,18 +655,18 @@ class AccountControllerTest < ActionController::TestCase assert_redirected_to :controller => 'home', :action => 'index' end - should 'check_url is available on environment' do + should 'check_valid_name is available on environment' do env = fast_create(Environment, :name => 'Environment test') @controller.expects(:environment).returns(env).at_least_once profile = create_user('mylogin').person - get :check_url, :identifier => 'mylogin' + get :check_valid_name, :identifier => 'mylogin' assert_equal 'validated', assigns(:status_class) end should 'check if url is not available on environment' do @controller.expects(:environment).returns(Environment.default).at_least_once profile = create_user('mylogin').person - get :check_url, :identifier => 'mylogin' + get :check_valid_name, :identifier => 'mylogin' assert_equal 'invalid', assigns(:status_class) end -- libgit2 0.21.2