Rename nickname to username in the code. Use new hub routes. (Old routes still exist as aliases for old Discourse instances.)

This commit is contained in:
Neil Lalonde
2014-03-12 12:39:27 -04:00
parent dc1d6decf5
commit 9ca516e58d
11 changed files with 104 additions and 104 deletions

View File

@ -153,7 +153,7 @@ class UsersController < ApplicationController
success: false, success: false,
message: I18n.t("login.something_already_taken") message: I18n.t("login.something_already_taken")
} }
rescue DiscourseHub::NicknameUnavailable => e rescue DiscourseHub::UsernameUnavailable => e
render json: e.response_message render json: e.response_message
rescue RestClient::Forbidden rescue RestClient::Forbidden
render json: { errors: [I18n.t("discourse_hub.access_token_problem")] } render json: { errors: [I18n.t("discourse_hub.access_token_problem")] }

View File

@ -12,8 +12,8 @@ InviteRedeemer = Struct.new(:invite) do
def self.create_user_from_invite(invite) def self.create_user_from_invite(invite)
username = UserNameSuggester.suggest(invite.email) username = UserNameSuggester.suggest(invite.email)
DiscourseHub.nickname_operation do DiscourseHub.username_operation do
match, available, suggestion = DiscourseHub.nickname_match?(username, invite.email) match, available, suggestion = DiscourseHub.username_match?(username, invite.email)
username = suggestion unless match || available username = suggestion unless match || available
end end
@ -28,7 +28,7 @@ InviteRedeemer = Struct.new(:invite) do
end end
user.save! user.save!
DiscourseHub.nickname_operation { DiscourseHub.register_nickname(username, invite.email) } DiscourseHub.username_operation { DiscourseHub.register_username(username, invite.email) }
user user
end end

View File

@ -166,7 +166,7 @@ class User < ActiveRecord::Base
self.username = new_username self.username = new_username
if current_username.downcase != new_username.downcase && valid? if current_username.downcase != new_username.downcase && valid?
DiscourseHub.nickname_operation { DiscourseHub.change_nickname(current_username, new_username) } DiscourseHub.username_operation { DiscourseHub.change_username(current_username, new_username) }
end end
save save

View File

@ -12,7 +12,7 @@ class UserActivator
end end
def start def start
register_nickname register_username
end end
def finish def finish
@ -35,9 +35,9 @@ class UserActivator
end end
end end
def register_nickname def register_username
if user.valid? && @settings.call_discourse_hub? if user.valid? && @settings.call_discourse_hub?
@hub.register_nickname(user.username, user.email) @hub.register_username(user.username, user.email)
end end
end end
end end

View File

@ -56,7 +56,7 @@ class UserDestroyer
end end
StaffActionLogger.new(@actor == user ? Discourse.system_user : @actor).log_user_deletion(user, opts.slice(:context)) StaffActionLogger.new(@actor == user ? Discourse.system_user : @actor).log_user_deletion(user, opts.slice(:context))
DiscourseHub.unregister_nickname(user.username) if SiteSetting.call_discourse_hub? DiscourseHub.unregister_username(user.username) if SiteSetting.call_discourse_hub?
MessageBus.publish "/file-change", ["refresh"], user_ids: [user.id] MessageBus.publish "/file-change", ["refresh"], user_ids: [user.id]
end end
end end

View File

@ -11,7 +11,7 @@ class UsernameCheckerService
check_username_with_hub_server(username, email) check_username_with_hub_server(username, email)
end end
elsif email and SiteSetting.call_discourse_hub? elsif email and SiteSetting.call_discourse_hub?
username_from_hub = DiscourseHub.nickname_for_email(email) username_from_hub = DiscourseHub.username_for_email(email)
if username_from_hub && User.username_available?(username_from_hub) if username_from_hub && User.username_available?(username_from_hub)
{suggestion: username_from_hub} {suggestion: username_from_hub}
else else
@ -34,13 +34,13 @@ class UsernameCheckerService
# Nickname and email do not match what's registered on the discourse hub. # Nickname and email do not match what's registered on the discourse hub.
{ available: false, global_match: false, suggestion: suggestion_from_discourse_hub } { available: false, global_match: false, suggestion: suggestion_from_discourse_hub }
else else
# The nickname is available locally, but is registered on the discourse hub. # The username is available locally, but is registered on the discourse hub.
# We need an email to see if the nickname belongs to this person. # We need an email to see if the username belongs to this person.
# Don't give a suggestion until we get the email and try to match it with on the discourse hub. # Don't give a suggestion until we get the email and try to match it with on the discourse hub.
{ available: false } { available: false }
end end
elsif available_globally && !available_locally elsif available_globally && !available_locally
# Already registered on this site with the matching nickname and email address. Why are you signing up again? # Already registered on this site with the matching username and email address. Why are you signing up again?
{ available: false, suggestion: UserNameSuggester.suggest(username) } { available: false, suggestion: UserNameSuggester.suggest(username) }
else else
# Not available anywhere. # Not available anywhere.
@ -63,12 +63,12 @@ class UsernameCheckerService
def available_globally_and_suggestion_from_hub(username, email) def available_globally_and_suggestion_from_hub(username, email)
if email.present? if email.present?
global_match, available, suggestion = global_match, available, suggestion =
DiscourseHub.nickname_match?(username, email) DiscourseHub.username_match?(username, email)
{ available_globally: available || global_match, { available_globally: available || global_match,
suggestion_from_discourse_hub: suggestion, suggestion_from_discourse_hub: suggestion,
global_match: global_match } global_match: global_match }
else else
args = DiscourseHub.nickname_available?(username) args = DiscourseHub.username_available?(username)
{ available_globally: args[0], { available_globally: args[0],
suggestion_from_discourse_hub: args[1], suggestion_from_discourse_hub: args[1],
global_match: false } global_match: false }

View File

@ -3,9 +3,9 @@ require_dependency 'version'
module DiscourseHub module DiscourseHub
class NicknameUnavailable < RuntimeError class UsernameUnavailable < RuntimeError
def initialize(nickname) def initialize(username)
@nickname = nickname @username = username
end end
def response_message def response_message
@ -14,7 +14,7 @@ module DiscourseHub
message: I18n.t( message: I18n.t(
"login.errors", "login.errors",
errors:I18n.t( errors:I18n.t(
"login.not_available", suggestion: UserNameSuggester.suggest(@nickname) "login.not_available", suggestion: UserNameSuggester.suggest(@username)
) )
) )
} }
@ -22,41 +22,41 @@ module DiscourseHub
end end
def self.nickname_available?(nickname) def self.username_available?(username)
json = get('/users/nickname_available', {nickname: nickname}) json = get('/users/username_available', {username: username})
[json['available'], json['suggestion']] [json['available'], json['suggestion']]
end end
def self.nickname_match?(nickname, email) def self.username_match?(username, email)
json = get('/users/nickname_match', {nickname: nickname, email: email}) json = get('/users/username_match', {username: username, email: email})
[json['match'], json['available'] || false, json['suggestion']] [json['match'], json['available'] || false, json['suggestion']]
end end
def self.nickname_for_email(email) def self.username_for_email(email)
json = get('/users/nickname_match', {email: email}) json = get('/users/username_match', {email: email})
json['suggestion'] json['suggestion']
end end
def self.register_nickname(nickname, email) def self.register_username(username, email)
json = post('/users', {nickname: nickname, email: email}) json = post('/users', {username: username, email: email})
if json.has_key?('success') if json.has_key?('success')
true true
else else
raise NicknameUnavailable.new(nickname) # TODO: report ALL the errors raise UsernameUnavailable.new(username) # TODO: report ALL the errors
end end
end end
def self.unregister_nickname(nickname) def self.unregister_username(username)
json = delete('/memberships/' + nickname) json = delete('/memberships/' + username)
json.has_key?('success') json.has_key?('success')
end end
def self.change_nickname(current_nickname, new_nickname) def self.change_username(current_username, new_username)
json = put("/users/#{current_nickname}/nickname", {nickname: new_nickname}) json = put("/users/#{current_username}/username", {username: new_username})
if json.has_key?('success') if json.has_key?('success')
true true
else else
raise NicknameUnavailable.new(new_nickname) # TODO: report ALL the errors raise UsernameUnavailable.new(new_username) # TODO: report ALL the errors
end end
end end
@ -118,11 +118,11 @@ module DiscourseHub
[:json, 'application/vnd.discoursehub.v1'] [:json, 'application/vnd.discoursehub.v1']
end end
def self.nickname_operation def self.username_operation
if SiteSetting.call_discourse_hub? if SiteSetting.call_discourse_hub?
begin begin
yield yield
rescue DiscourseHub::NicknameUnavailable rescue DiscourseHub::UsernameUnavailable
false false
rescue => e rescue => e
Rails.logger.error e.message + "\n" + e.backtrace.join("\n") Rails.logger.error e.message + "\n" + e.backtrace.join("\n")

View File

@ -2,15 +2,15 @@ require 'spec_helper'
require_dependency 'discourse_hub' require_dependency 'discourse_hub'
describe DiscourseHub do describe DiscourseHub do
describe '#nickname_available?' do describe '#username_available?' do
it 'should return true when nickname is available and no suggestion' do it 'should return true when username is available and no suggestion' do
RestClient.stubs(:get).returns( {success: 'OK', available: true}.to_json ) RestClient.stubs(:get).returns( {success: 'OK', available: true}.to_json )
DiscourseHub.nickname_available?('MacGyver').should == [true, nil] DiscourseHub.username_available?('MacGyver').should == [true, nil]
end end
it 'should return false and a suggestion when nickname is not available' do it 'should return false and a suggestion when username is not available' do
RestClient.stubs(:get).returns( {success: 'OK', available: false, suggestion: 'MacGyver1'}.to_json ) RestClient.stubs(:get).returns( {success: 'OK', available: false, suggestion: 'MacGyver1'}.to_json )
available, suggestion = DiscourseHub.nickname_available?('MacGyver') available, suggestion = DiscourseHub.username_available?('MacGyver')
available.should be_false available.should be_false
suggestion.should_not be_nil suggestion.should_not be_nil
end end
@ -18,52 +18,52 @@ describe DiscourseHub do
# How to handle connect errors? timeout? 401? 403? 429? # How to handle connect errors? timeout? 401? 403? 429?
end end
describe '#nickname_match?' do describe '#username_match?' do
it 'should return true when it is a match and no suggestion' do it 'should return true when it is a match and no suggestion' do
RestClient.stubs(:get).returns( {success: 'OK', match: true, available: false}.to_json ) RestClient.stubs(:get).returns( {success: 'OK', match: true, available: false}.to_json )
DiscourseHub.nickname_match?('MacGyver', 'macg@example.com').should == [true, false, nil] DiscourseHub.username_match?('MacGyver', 'macg@example.com').should == [true, false, nil]
end end
it 'should return false and a suggestion when it is not a match and the nickname is not available' do it 'should return false and a suggestion when it is not a match and the username is not available' do
RestClient.stubs(:get).returns( {success: 'OK', match: false, available: false, suggestion: 'MacGyver1'}.to_json ) RestClient.stubs(:get).returns( {success: 'OK', match: false, available: false, suggestion: 'MacGyver1'}.to_json )
match, available, suggestion = DiscourseHub.nickname_match?('MacGyver', 'macg@example.com') match, available, suggestion = DiscourseHub.username_match?('MacGyver', 'macg@example.com')
match.should be_false match.should be_false
available.should be_false available.should be_false
suggestion.should_not be_nil suggestion.should_not be_nil
end end
it 'should return false and no suggestion when it is not a match and the nickname is available' do it 'should return false and no suggestion when it is not a match and the username is available' do
RestClient.stubs(:get).returns( {success: 'OK', match: false, available: true}.to_json ) RestClient.stubs(:get).returns( {success: 'OK', match: false, available: true}.to_json )
match, available, suggestion = DiscourseHub.nickname_match?('MacGyver', 'macg@example.com') match, available, suggestion = DiscourseHub.username_match?('MacGyver', 'macg@example.com')
match.should be_false match.should be_false
available.should be_true available.should be_true
suggestion.should be_nil suggestion.should be_nil
end end
end end
describe '#register_nickname' do describe '#register_username' do
it 'should return true when registration succeeds' do it 'should return true when registration succeeds' do
RestClient.stubs(:post).returns( {success: 'OK'}.to_json ) RestClient.stubs(:post).returns( {success: 'OK'}.to_json )
DiscourseHub.register_nickname('MacGyver', 'macg@example.com').should be_true DiscourseHub.register_username('MacGyver', 'macg@example.com').should be_true
end end
it 'should return raise an exception when registration fails' do it 'should return raise an exception when registration fails' do
RestClient.stubs(:post).returns( {failed: -200}.to_json ) RestClient.stubs(:post).returns( {failed: -200}.to_json )
expect { expect {
DiscourseHub.register_nickname('MacGyver', 'macg@example.com') DiscourseHub.register_username('MacGyver', 'macg@example.com')
}.to raise_error(DiscourseHub::NicknameUnavailable) }.to raise_error(DiscourseHub::UsernameUnavailable)
end end
end end
describe '#unregister_nickname' do describe '#unregister_username' do
it 'should return true when unregister succeeds' do it 'should return true when unregister succeeds' do
RestClient.stubs(:delete).returns( {success: 'OK'}.to_json ) RestClient.stubs(:delete).returns( {success: 'OK'}.to_json )
DiscourseHub.unregister_nickname('byebye').should be_true DiscourseHub.unregister_username('byebye').should be_true
end end
it 'should return false when unregister fails' do it 'should return false when unregister fails' do
RestClient.stubs(:delete).returns( {failed: -20}.to_json ) RestClient.stubs(:delete).returns( {failed: -20}.to_json )
DiscourseHub.unregister_nickname('byebye').should be_false DiscourseHub.unregister_username('byebye').should be_false
end end
end end
@ -75,31 +75,31 @@ describe DiscourseHub do
end end
end end
describe '#change_nickname' do describe '#change_username' do
it 'should return true when nickname is changed successfully' do it 'should return true when username is changed successfully' do
RestClient.stubs(:put).returns( {success: 'OK'}.to_json ) RestClient.stubs(:put).returns( {success: 'OK'}.to_json )
DiscourseHub.change_nickname('MacGyver', 'MacG').should be_true DiscourseHub.change_username('MacGyver', 'MacG').should be_true
end end
it 'should return raise NicknameUnavailable when nickname is not available' do it 'should return raise UsernameUnavailable when username is not available' do
RestClient.stubs(:put).returns( {failed: -200}.to_json ) RestClient.stubs(:put).returns( {failed: -200}.to_json )
expect { expect {
DiscourseHub.change_nickname('MacGyver', 'MacG') DiscourseHub.change_username('MacGyver', 'MacG')
}.to raise_error(DiscourseHub::NicknameUnavailable) }.to raise_error(DiscourseHub::UsernameUnavailable)
end end
# it 'should return raise NicknameUnavailable when nickname does not belong to this forum' do # it 'should return raise UsernameUnavailable when username does not belong to this forum' do
# RestClient.stubs(:put).returns( {failed: -13}.to_json ) # RestClient.stubs(:put).returns( {failed: -13}.to_json )
# expect { # expect {
# DiscourseHub.change_nickname('MacGyver', 'MacG') # DiscourseHub.change_username('MacGyver', 'MacG')
# }.to raise_error(DiscourseHub::ActionForbidden) # }.to raise_error(DiscourseHub::ActionForbidden)
# end # end
# it 'should return raise NicknameUnavailable when nickname does not belong to this forum' do # it 'should return raise UsernameUnavailable when username does not belong to this forum' do
# RestClient.stubs(:put).returns( {failed: -13}.to_json ) # RestClient.stubs(:put).returns( {failed: -13}.to_json )
# expect { # expect {
# DiscourseHub.change_nickname('MacGyver', 'MacG') # DiscourseHub.change_username('MacGyver', 'MacG')
# }.to raise_error(DiscourseHub::ActionForbidden) # }.to raise_error(DiscourseHub::ActionForbidden)
# end # end
end end

View File

@ -283,7 +283,7 @@ describe UsersController do
before do before do
@user = Fabricate.build(:user) @user = Fabricate.build(:user)
@user.password = "strongpassword" @user.password = "strongpassword"
DiscourseHub.stubs(:register_nickname).returns([true, nil]) DiscourseHub.stubs(:register_username).returns([true, nil])
end end
def post_user def post_user
@ -471,10 +471,10 @@ describe UsersController do
include_examples 'failed signup' include_examples 'failed signup'
end end
context 'when nickname is unavailable in DiscourseHub' do context 'when username is unavailable in DiscourseHub' do
before do before do
SiteSetting.stubs(:call_discourse_hub?).returns(true) SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.stubs(:register_nickname).raises(DiscourseHub::NicknameUnavailable.new(@user.name)) DiscourseHub.stubs(:register_username).raises(DiscourseHub::UsernameUnavailable.new(@user.name))
end end
let(:create_params) {{ let(:create_params) {{
name: @user.name, name: @user.name,
@ -489,7 +489,7 @@ describe UsersController do
context 'when an Exception is raised' do context 'when an Exception is raised' do
[ ActiveRecord::StatementInvalid, [ ActiveRecord::StatementInvalid,
DiscourseHub::NicknameUnavailable, DiscourseHub::UsernameUnavailable,
RestClient::Forbidden ].each do |exception| RestClient::Forbidden ].each do |exception|
before { User.any_instance.stubs(:save).raises(exception) } before { User.any_instance.stubs(:save).raises(exception) }
@ -539,7 +539,7 @@ describe UsersController do
context '.check_username' do context '.check_username' do
before do before do
DiscourseHub.stubs(:nickname_available?).returns([true, nil]) DiscourseHub.stubs(:username_available?).returns([true, nil])
end end
it 'raises an error without any parameters' do it 'raises an error without any parameters' do
@ -573,8 +573,8 @@ describe UsersController do
context 'when call_discourse_hub is disabled' do context 'when call_discourse_hub is disabled' do
before do before do
SiteSetting.stubs(:call_discourse_hub?).returns(false) SiteSetting.stubs(:call_discourse_hub?).returns(false)
DiscourseHub.expects(:nickname_available?).never DiscourseHub.expects(:username_available?).never
DiscourseHub.expects(:nickname_match?).never DiscourseHub.expects(:username_match?).never
end end
it 'returns nothing when given an email param but no username' do it 'returns nothing when given an email param but no username' do
@ -641,11 +641,11 @@ describe UsersController do
context 'available locally and globally' do context 'available locally and globally' do
before do before do
DiscourseHub.stubs(:nickname_available?).returns([true, nil]) DiscourseHub.stubs(:username_available?).returns([true, nil])
DiscourseHub.stubs(:nickname_match?).returns([false, true, nil]) # match = false, available = true, suggestion = nil DiscourseHub.stubs(:username_match?).returns([false, true, nil]) # match = false, available = true, suggestion = nil
end end
shared_examples 'check_username when nickname is available everywhere' do shared_examples 'check_username when username is available everywhere' do
it 'should return success' do it 'should return success' do
response.should be_success response.should be_success
end end
@ -663,14 +663,14 @@ describe UsersController do
before do before do
xhr :get, :check_username, username: 'BruceWayne' xhr :get, :check_username, username: 'BruceWayne'
end end
include_examples 'check_username when nickname is available everywhere' include_examples 'check_username when username is available everywhere'
end end
context 'both username and email is given' do context 'both username and email is given' do
before do before do
xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@gmail.com' xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@gmail.com'
end end
include_examples 'check_username when nickname is available everywhere' include_examples 'check_username when username is available everywhere'
end end
context 'only email is given' do context 'only email is given' do
@ -682,7 +682,7 @@ describe UsersController do
end end
end end
shared_examples 'when email is needed to check nickname match' do shared_examples 'when email is needed to check username match' do
it 'should return success' do it 'should return success' do
response.should be_success response.should be_success
end end
@ -698,26 +698,26 @@ describe UsersController do
context 'available locally but not globally' do context 'available locally but not globally' do
before do before do
DiscourseHub.stubs(:nickname_available?).returns([false, 'suggestion']) DiscourseHub.stubs(:username_available?).returns([false, 'suggestion'])
end end
context 'email param is not given' do context 'email param is not given' do
before do before do
xhr :get, :check_username, username: 'BruceWayne' xhr :get, :check_username, username: 'BruceWayne'
end end
include_examples 'when email is needed to check nickname match' include_examples 'when email is needed to check username match'
end end
context 'email param is an empty string' do context 'email param is an empty string' do
before do before do
xhr :get, :check_username, username: 'BruceWayne', email: '' xhr :get, :check_username, username: 'BruceWayne', email: ''
end end
include_examples 'when email is needed to check nickname match' include_examples 'when email is needed to check username match'
end end
context 'email matches global nickname' do context 'email matches global username' do
before do before do
DiscourseHub.stubs(:nickname_match?).returns([true, false, nil]) DiscourseHub.stubs(:username_match?).returns([true, false, nil])
xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@example.com' xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@example.com'
end end
include_examples 'when username is available everywhere' include_examples 'when username is available everywhere'
@ -727,9 +727,9 @@ describe UsersController do
end end
end end
context 'email does not match global nickname' do context 'email does not match global username' do
before do before do
DiscourseHub.stubs(:nickname_match?).returns([false, false, 'suggestion']) DiscourseHub.stubs(:username_match?).returns([false, false, 'suggestion'])
xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@example.com' xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@example.com'
end end
include_examples 'when username is unavailable locally' include_examples 'when username is unavailable locally'
@ -744,7 +744,7 @@ describe UsersController do
let!(:user) { Fabricate(:user) } let!(:user) { Fabricate(:user) }
before do before do
DiscourseHub.stubs(:nickname_available?).returns([false, 'suggestion']) DiscourseHub.stubs(:username_available?).returns([false, 'suggestion'])
xhr :get, :check_username, username: user.username xhr :get, :check_username, username: user.username
end end
@ -755,7 +755,7 @@ describe UsersController do
let!(:user) { Fabricate(:user) } let!(:user) { Fabricate(:user) }
before do before do
DiscourseHub.stubs(:nickname_available?).returns([true, nil]) DiscourseHub.stubs(:username_available?).returns([true, nil])
xhr :get, :check_username, username: user.username xhr :get, :check_username, username: user.username
end end
@ -766,8 +766,8 @@ describe UsersController do
context 'when discourse_org_access_key is wrong' do context 'when discourse_org_access_key is wrong' do
before do before do
SiteSetting.stubs(:call_discourse_hub?).returns(true) SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.stubs(:nickname_available?).raises(RestClient::Forbidden) DiscourseHub.stubs(:username_available?).raises(RestClient::Forbidden)
DiscourseHub.stubs(:nickname_match?).raises(RestClient::Forbidden) DiscourseHub.stubs(:username_match?).raises(RestClient::Forbidden)
end end
it 'should return an error message' do it 'should return an error message' do

View File

@ -53,13 +53,13 @@ describe UserDestroyer do
it 'should unregister the nickname as the discourse hub if hub integration is enabled' do it 'should unregister the nickname as the discourse hub if hub integration is enabled' do
SiteSetting.stubs(:call_discourse_hub?).returns(true) SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.expects(:unregister_nickname).with(@user.username) DiscourseHub.expects(:unregister_username).with(@user.username)
destroy destroy
end end
it 'should not try to unregister the nickname as the discourse hub if hub integration is disabled' do it 'should not try to unregister the nickname as the discourse hub if hub integration is disabled' do
SiteSetting.stubs(:call_discourse_hub?).returns(false) SiteSetting.stubs(:call_discourse_hub?).returns(false)
DiscourseHub.expects(:unregister_nickname).never DiscourseHub.expects(:unregister_username).never
destroy destroy
end end
end end
@ -108,7 +108,7 @@ describe UserDestroyer do
end end
it 'should not unregister the user at the discourse hub' do it 'should not unregister the user at the discourse hub' do
DiscourseHub.expects(:unregister_nickname).never DiscourseHub.expects(:unregister_username).never
destroy rescue nil destroy rescue nil
end end
end end
@ -192,7 +192,7 @@ describe UserDestroyer do
end end
it 'should not unregister the user at the discourse hub' do it 'should not unregister the user at the discourse hub' do
DiscourseHub.expects(:unregister_nickname).never DiscourseHub.expects(:unregister_username).never
destroy rescue nil destroy rescue nil
end end
end end

View File

@ -38,30 +38,30 @@ describe UsernameCheckerService do
context 'username and email is given' do context 'username and email is given' do
it 'username is available locally but not globally' do it 'username is available locally but not globally' do
DiscourseHub.expects(:nickname_available?).never DiscourseHub.expects(:username_available?).never
DiscourseHub.expects(:nickname_match?).returns([false, false, 'porkchop']) DiscourseHub.expects(:username_match?).returns([false, false, 'porkchop'])
result = @service.check_username('vincent', @email) result = @service.check_username('vincent', @email)
expected = { available: false, global_match: false, suggestion: 'porkchop' } expected = { available: false, global_match: false, suggestion: 'porkchop' }
expect(result).to eq(expected) expect(result).to eq(expected)
end end
it 'username is available both locally and globally' do it 'username is available both locally and globally' do
DiscourseHub.expects(:nickname_available?).never DiscourseHub.expects(:username_available?).never
DiscourseHub.stubs(:nickname_match?).returns([true, true, nil]) DiscourseHub.stubs(:username_match?).returns([true, true, nil])
result = @service.check_username('vincent', @email) result = @service.check_username('vincent', @email)
expected = { available: true, global_match: true } expected = { available: true, global_match: true }
expect(result).to eq(expected) expect(result).to eq(expected)
end end
it 'username is available locally but not globally' do it 'username is available locally but not globally' do
DiscourseHub.stubs(:nickname_match?).returns([false, false, 'suggestion']) DiscourseHub.stubs(:username_match?).returns([false, false, 'suggestion'])
result = @service.check_username('vincent', @email) result = @service.check_username('vincent', @email)
expected = { available: false, global_match: false, suggestion: 'suggestion' } expected = { available: false, global_match: false, suggestion: 'suggestion' }
expect(result).to eq(expected) expect(result).to eq(expected)
end end
it 'username is available globally but not locally' do it 'username is available globally but not locally' do
DiscourseHub.stubs(:nickname_match?).returns([false, true, nil]) DiscourseHub.stubs(:username_match?).returns([false, true, nil])
User.stubs(:username_available?).returns(false) User.stubs(:username_available?).returns(false)
UserNameSuggester.stubs(:suggest).returns('einar-j') UserNameSuggester.stubs(:suggest).returns('einar-j')
expected = { available: false, suggestion: 'einar-j' } expected = { available: false, suggestion: 'einar-j' }
@ -70,7 +70,7 @@ describe UsernameCheckerService do
end end
it 'username not available anywhere' do it 'username not available anywhere' do
DiscourseHub.stubs(:nickname_match?).returns([false, false, 'suggestion']) DiscourseHub.stubs(:username_match?).returns([false, false, 'suggestion'])
expected = { available: false, suggestion: 'suggestion', global_match: false } expected = { available: false, suggestion: 'suggestion', global_match: false }
@nil_email = nil @nil_email = nil
result = @service.check_username('vincent', @email) result = @service.check_username('vincent', @email)
@ -80,26 +80,26 @@ describe UsernameCheckerService do
shared_examples "only email is given" do shared_examples "only email is given" do
it "should call the correct api" do it "should call the correct api" do
DiscourseHub.expects(:nickname_available?).never DiscourseHub.expects(:username_available?).never
DiscourseHub.expects(:nickname_match?).never DiscourseHub.expects(:username_match?).never
DiscourseHub.stubs(:nickname_for_email).returns(nil) DiscourseHub.stubs(:username_for_email).returns(nil)
result result
end end
it 'no match on email' do it 'no match on email' do
DiscourseHub.stubs(:nickname_for_email).returns(nil) DiscourseHub.stubs(:username_for_email).returns(nil)
result.should == {suggestion: nil} result.should == {suggestion: nil}
end end
it 'match found for email' do it 'match found for email' do
DiscourseHub.stubs(:nickname_for_email).returns('vincent') DiscourseHub.stubs(:username_for_email).returns('vincent')
result.should == {suggestion: 'vincent'} result.should == {suggestion: 'vincent'}
end end
it 'match found for email, but username is taken' do it 'match found for email, but username is taken' do
# This case can happen when you've already signed up on the site, # This case can happen when you've already signed up on the site,
# or enforce_global_nicknames used to be disabled. # or enforce_global_nicknames used to be disabled.
DiscourseHub.stubs(:nickname_for_email).returns('taken') DiscourseHub.stubs(:username_for_email).returns('taken')
User.stubs(:username_available?).with('taken').returns(false) User.stubs(:username_available?).with('taken').returns(false)
result.should == {suggestion: nil} result.should == {suggestion: nil}
end end