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,
message: I18n.t("login.something_already_taken")
}
rescue DiscourseHub::NicknameUnavailable => e
rescue DiscourseHub::UsernameUnavailable => e
render json: e.response_message
rescue RestClient::Forbidden
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)
username = UserNameSuggester.suggest(invite.email)
DiscourseHub.nickname_operation do
match, available, suggestion = DiscourseHub.nickname_match?(username, invite.email)
DiscourseHub.username_operation do
match, available, suggestion = DiscourseHub.username_match?(username, invite.email)
username = suggestion unless match || available
end
@ -28,7 +28,7 @@ InviteRedeemer = Struct.new(:invite) do
end
user.save!
DiscourseHub.nickname_operation { DiscourseHub.register_nickname(username, invite.email) }
DiscourseHub.username_operation { DiscourseHub.register_username(username, invite.email) }
user
end

View File

@ -166,7 +166,7 @@ class User < ActiveRecord::Base
self.username = new_username
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
save

View File

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

View File

@ -56,7 +56,7 @@ class UserDestroyer
end
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]
end
end

View File

@ -11,7 +11,7 @@ class UsernameCheckerService
check_username_with_hub_server(username, email)
end
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)
{suggestion: username_from_hub}
else
@ -34,13 +34,13 @@ class UsernameCheckerService
# Nickname and email do not match what's registered on the discourse hub.
{ available: false, global_match: false, suggestion: suggestion_from_discourse_hub }
else
# The nickname is available locally, but is registered on the discourse hub.
# We need an email to see if the nickname belongs to this person.
# The username is available locally, but is registered on the discourse hub.
# 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.
{ available: false }
end
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) }
else
# Not available anywhere.
@ -63,12 +63,12 @@ class UsernameCheckerService
def available_globally_and_suggestion_from_hub(username, email)
if email.present?
global_match, available, suggestion =
DiscourseHub.nickname_match?(username, email)
DiscourseHub.username_match?(username, email)
{ available_globally: available || global_match,
suggestion_from_discourse_hub: suggestion,
global_match: global_match }
else
args = DiscourseHub.nickname_available?(username)
args = DiscourseHub.username_available?(username)
{ available_globally: args[0],
suggestion_from_discourse_hub: args[1],
global_match: false }

View File

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

View File

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

View File

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

View File

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